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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 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,45 @@ 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
export function parseHTTPVersion(vers: string): [number, number, boolean] {
const Big = 1000000; // arbitrary upper bound
const digitReg = /^\d+$/; // test if string is only digit
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];
}

let majorStr = vers.substring(vers.indexOf("/") + 1, dot);
major = parseInt(majorStr);
if (!digitReg.test(majorStr) || isNaN(major) || major < 0 || major > Big) {
return [0, 0, false];
}

let minorStr = vers.substring(dot + 1);
minor = parseInt(minorStr);
if (!digitReg.test(minorStr) || 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 +270,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
43 changes: 42 additions & 1 deletion http/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
Response,
ServerRequest,
writeResponse,
readRequest
readRequest,
parseHTTPVersion
} from "./server.ts";
import { BufReader, BufWriter } from "../io/bufio.ts";
import { StringReader } from "../io/readers.ts";
Expand Down Expand Up @@ -364,6 +365,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 All @@ -386,4 +402,29 @@ test(async function testReadRequestError(): Promise<void> {
}
}
});

// Ported from https://github.com/golang/go/blob/f5c43b9/src/net/http/request_test.go#L535-L565
test({
name: "[http] parseHttpVersion",
fn(): void {
const testCases = [
{ in: "HTTP/0.9", want: [0, 9, true] },
{ in: "HTTP/1.0", want: [1, 0, true] },
{ in: "HTTP/1.1", want: [1, 1, true] },
{ in: "HTTP/3.14", want: [3, 14, true] },
{ in: "HTTP", want: [0, 0, false] },
{ in: "HTTP/one.one", want: [0, 0, false] },
{ in: "HTTP/1.1/", want: [0, 0, false] },
{ in: "HTTP/-1.0", want: [0, 0, false] },
{ in: "HTTP/0.-1", want: [0, 0, false] },
{ in: "HTTP/", want: [0, 0, false] },
{ in: "HTTP/1,0", want: [0, 0, false] }
];
for (const t of testCases) {
const r = parseHTTPVersion(t.in);
assertEquals(r, t.want, t.in);
}
}
});

runIfMain(import.meta);