Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[http] add ParseHTTPVersion #452

Merged
merged 3 commits into from
May 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Next Next commit
add ParseHTTPVersion
  • Loading branch information
zekth committed May 24, 2019
commit cb5f028ebfe4f1b8dba5989e0e163155c0fb4c22
40 changes: 40 additions & 0 deletions http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ export class ServerRequest {
url: string;
method: string;
proto: string;
protoMinor: number;
protoMajor: number;
headers: Headers;
r: BufReader;
w: BufWriter;
Expand Down Expand Up @@ -215,6 +217,37 @@ function fixLength(req: ServerRequest): void {
}
}

// ParseHTTPVersion parses a HTTP version string.
// "HTTP/1.0" returns (1, 0, true).
// Ported from https://github.com/golang/go/blob/f5c43b9/src/net/http/request.go#L766-L792
function ParseHTTPVersion(vers: string): [number, number, boolean] {
const Big = 1000000; // arbitrary upper bound
let major: number;
let minor: number;
switch (vers) {
case "HTTP/1.1":
return [1, 1, true];
case "HTTP/1.0":
return [1, 0, true];
}
if (!vers.startsWith("HTTP/")) {
return [0, 0, false];
}
const dot = vers.indexOf(".");
if (dot < 0) {
return [0, 0, false];
}
major = parseInt(vers[dot - 1]);
if (isNaN(major) || major < 0 || major > Big) {
return [0, 0, false];
}
minor = parseInt(vers[dot + 1]);
if (isNaN(minor) || minor < 0 || minor > Big) {
return [0, 0, false];
}
return [major, minor, true];
}

export async function readRequest(
bufr: BufReader
): Promise<[ServerRequest, BufState]> {
Expand All @@ -229,6 +262,13 @@ export async function readRequest(
return [null, err];
}
[req.method, req.url, req.proto] = firstLine.split(" ", 3);

let ok: boolean;
[req.protoMinor, req.protoMajor, ok] = ParseHTTPVersion(req.proto);
if (!ok) {
throw Error(`malformed HTTP version ${req.proto}`);
}

[req.headers, err] = await tp.readMIMEHeader();
fixLength(req);
// TODO(zekth) : add parsing of headers eg:
Expand Down
15 changes: 15 additions & 0 deletions http/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,21 @@ test(async function testReadRequestError(): Promise<void> {
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: "GET / HTTP/1.A\r\nContent-Length:0\r\n\r\n",
headers: [{ key: "Content-Length", value: "0" }],
err: `malformed HTTP version HTTP/1.A`
},
12: {
in: "GET / HTTP/A.1\r\nContent-Length:0\r\n\r\n",
headers: [],
err: `malformed HTTP version HTTP/A.1`
},
13: {
in: "GET / HTTP/1.0\r\nContent-Length:4\r\n\r\n",
headers: [{ key: "Content-Length", value: "4" }],
err: null
zekth marked this conversation as resolved.
Show resolved Hide resolved
}
};
for (const p in testCases) {
Expand Down