Skip to content

Commit

Permalink
http: add rfc7230 handling (denoland/std#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
zekth authored and piscisaureus committed May 29, 2019
1 parent b95f79d commit c8ac9a0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 8 additions & 4 deletions http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}
}
}

Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions http/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,12 @@ test(async function testReadRequestError(): Promise<void> {
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) {
Expand Down

0 comments on commit c8ac9a0

Please sign in to comment.