commit 281c8b2b392c08ef5775b16114949004b6d0d995
parent 0cd4f37233b935b61e9f77f5f8d5ba1f745cf55b
Author: Florian Dold <dold@taler.net>
Date: Fri, 27 Nov 2026 18:12:42 +0100
HTTP client: retain the Node response status on a body read error
Issue: https://bugs.taler.net/n/12291
Diffstat:
2 files changed, 48 insertions(+), 19 deletions(-)
diff --git a/packages/taler-util/src/http-impl.node.test.ts b/packages/taler-util/src/http-impl.node.test.ts
@@ -216,3 +216,26 @@ test("TLS-required HTTP follows secure redirects without sending plaintext reque
await rm(dir, { recursive: true, force: true });
}
});
+
+test("a truncated HTTP body preserves the refusal status", async (t) => {
+ const server = createServer((req, res) => {
+ if (req.url === "/no-response") {
+ res.destroy();
+ return;
+ }
+ res.writeHead(451, {
+ "content-length": 128,
+ "content-type": "application/json",
+ });
+ res.write("{", () => res.destroy());
+ });
+ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
+ t.after(() => server.close());
+ const url = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
+ const http = createPlatformHttpLib({ enableThrottling: false });
+ const response = await http.fetch(url);
+ assert.equal(response.status, 451);
+ assert.equal(response.headers.get("content-type"), "application/json");
+ await assert.rejects(response.json());
+ await assert.rejects(http.fetch(url + "/no-response"));
+});
diff --git a/packages/taler-util/src/http-impl.node.ts b/packages/taler-util/src/http-impl.node.ts
@@ -170,22 +170,32 @@ export const rawLib: HttpRawLib = {
};
return new Promise<HttpRawResponse>((resolve, reject) => {
+ let response: Pick<HttpRawResponse, "status" | "headers"> | undefined;
+ const fail = (error: Error) => {
+ doCleanup();
+ if (response) {
+ // HTTP status is meaningful even if reading the body failed.
+ resolve({
+ ...response,
+ bytes: async () => {
+ throw error;
+ },
+ });
+ } else {
+ reject(error);
+ }
+ };
const handler = (res: IncomingMessage) => {
+ const headers = new HeadersImpl();
+ for (const [name, value] of Object.entries(res.headers)) {
+ if (value)
+ headers.set(name, Array.isArray(value) ? value.join(", ") : value);
+ }
+ response = { status: res.statusCode || 0, headers };
res.on("data", (d) => {
chunks.push(d);
});
res.on("end", () => {
- const headers: HeadersImpl = new HeadersImpl();
- for (const [k, v] of Object.entries(res.headers)) {
- if (!v) {
- continue;
- }
- if (typeof v === "string") {
- headers.set(k, v);
- } else {
- headers.set(k, v.join(", "));
- }
- }
const data = typedArrayConcat(chunks);
doCleanup();
if (SHOW_CURL_HTTP_REQUEST) {
@@ -203,8 +213,7 @@ export const rawLib: HttpRawLib = {
});
res.on("error", (e) => {
const code = "code" in e ? e.code : "unknown";
- doCleanup();
- reject(new Error(`Error in HTTP response handler: ${code}`));
+ fail(new Error(`Error in HTTP response handler: ${code}`));
});
};
@@ -222,23 +231,20 @@ export const rawLib: HttpRawLib = {
logger.info(`request to ${url} timed out`);
timeoutHandle = undefined;
req.destroy();
- doCleanup();
- reject(new RequestTimeoutError());
+ fail(new RequestTimeoutError());
}, opt.timeoutMs);
}
if (opt.cancellationToken) {
cancelCancelledHandler = opt.cancellationToken.onCancelled(() => {
req.destroy();
- doCleanup();
- reject(new RequestCancelledError());
+ fail(new RequestCancelledError());
});
}
req.on("error", (e: Error) => {
const code = "code" in e ? e.code : "unknown";
- doCleanup();
- reject(new Error(`Error in HTTP request: ${code}`));
+ fail(new Error(`Error in HTTP request: ${code}`));
});
if (opt.body) {