diff --git a/http/server.ts b/http/server.ts index baccaacfb95c..0046514c1f81 100644 --- a/http/server.ts +++ b/http/server.ts @@ -274,11 +274,15 @@ export class Server implements AsyncIterable { if (bufStateErr === "EOF") { // The connection was gracefully closed. } else if (bufStateErr instanceof Error) { - // An error was thrown while parsing request headers. - await writeResponse(req.w, { - status: 400, - body: new TextEncoder().encode(`${bufStateErr.message}\r\n\r\n`) - }); + // in case of wrong first line HTTP header or + // aborted request we don't have to respond + if (req) { + // An error was thrown while parsing request headers. + await writeResponse(req.w, { + status: 400, + body: new TextEncoder().encode(`${bufStateErr.message}\r\n\r\n`) + }); + } } else if (this.closing) { // There are more requests incoming but the server is closing. // TODO(ry): send a back a HTTP 503 Service Unavailable status. diff --git a/http/server_test.ts b/http/server_test.ts index 705fea1baeb6..df5261af712b 100644 --- a/http/server_test.ts +++ b/http/server_test.ts @@ -364,6 +364,11 @@ test(async function testReadRequestError(): Promise { in: "HEAD / HTTP/1.1\r\nContent-Length:0\r\nContent-Length: 0\r\n\r\n", headers: [{ key: "Content-Length", value: "0" }], err: null + }, + 11: { + in: "ToTAlly \\ Non REAdable HTTTTTTp pRotOcOlz", + err: "EOF", + headers: [] } }; for (const p in testCases) {