From c8ac9a0f58dcb35b0c1dd8d8ba3d508014b291dc Mon Sep 17 00:00:00 2001 From: Vincent LE GOFF Date: Wed, 29 May 2019 19:44:14 +0200 Subject: http: add rfc7230 handling (denoland/deno_std#451) Original: https://github.com/denoland/deno_std/commit/1db594d5b0fd377ee6c749041b7265101f92eef1 --- http/server.ts | 12 ++++++++---- http/server_test.ts | 6 ++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/http/server.ts b/http/server.ts index bdf48fca3..5ea52a0b8 100644 --- a/http/server.ts +++ b/http/server.ts @@ -215,6 +215,14 @@ function fixLength(req: ServerRequest): void { if (req.method === "HEAD" && c && c !== "0") { throw Error("http: method cannot contain a Content-Length"); } + if (c && req.headers.has("transfer-encoding")) { + // A sender MUST NOT send a Content-Length header field in any message + // that contains a Transfer-Encoding header field. + // rfc: https://tools.ietf.org/html/rfc7230#section-3.3.2 + throw new Error( + "http: Transfer-Encoding and Content-Length cannot be send together" + ); + } } } @@ -288,10 +296,6 @@ export async function readRequest( [req.protoMinor, req.protoMajor] = parseHTTPVersion(req.proto); req.headers = headers; fixLength(req); - // TODO(zekth) : add parsing of headers eg: - // rfc: https://tools.ietf.org/html/rfc7230#section-3.3.2 - // A sender MUST NOT send a Content-Length header field in any message - // that contains a Transfer-Encoding header field. return req; } diff --git a/http/server_test.ts b/http/server_test.ts index 32f12cc40..e24ab0be9 100644 --- a/http/server_test.ts +++ b/http/server_test.ts @@ -385,6 +385,12 @@ test(async function testReadRequestError(): Promise { 10: { in: "HEAD / HTTP/1.1\r\nContent-Length:0\r\nContent-Length: 0\r\n\r\n", headers: [{ key: "Content-Length", value: "0" }] + }, + 11: { + in: + "POST / HTTP/1.1\r\nContent-Length:0\r\ntransfer-encoding: chunked\r\n\r\n", + headers: [], + err: "http: Transfer-Encoding and Content-Length cannot be send together" } }; for (const p in testCases) { -- cgit v1.2.3