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

Revert "http: delete conn parameter in readRequest (#430)" #550

Merged
merged 4 commits into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export class ServerRequest {
protoMinor!: number;
protoMajor!: number;
headers!: Headers;
conn!: Conn;
r!: BufReader;
w!: BufWriter;
done: Deferred<void> = deferred();
Expand Down Expand Up @@ -283,6 +284,7 @@ export function parseHTTPVersion(vers: string): [number, number] {
}

export async function readRequest(
conn: Conn,
bufr: BufReader
): Promise<ServerRequest | Deno.EOF> {
const tp = new TextProtoReader(bufr);
Expand All @@ -292,6 +294,7 @@ export async function readRequest(
if (headers === Deno.EOF) throw new UnexpectedEOFError();

const req = new ServerRequest();
req.conn = conn;
req.r = bufr;
[req.method, req.url, req.proto] = firstLine.split(" ", 3);
[req.protoMinor, req.protoMajor] = parseHTTPVersion(req.proto);
Expand Down Expand Up @@ -321,7 +324,7 @@ export class Server implements AsyncIterable<ServerRequest> {

while (!this.closing) {
try {
req = await readRequest(bufr);
req = await readRequest(conn, bufr);
} catch (e) {
err = e;
break;
Expand Down
34 changes: 32 additions & 2 deletions http/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ test(async function responseWrite(): Promise<void> {
const request = new ServerRequest();
request.w = bufw;

request.conn = {
localAddr: "",
remoteAddr: "",
rid: -1,
closeRead: (): void => {},
closeWrite: (): void => {},
read: async (): Promise<number | Deno.EOF> => {
return 0;
},
write: async (): Promise<number> => {
return -1;
},
close: (): void => {}
};

await request.respond(testCase.response);
assertEquals(buf.toString(), testCase.raw);
await request.done;
Expand Down Expand Up @@ -318,14 +333,29 @@ test(async function writeStringReaderResponse(): Promise<void> {
assertEquals(r.more, false);
});

const mockConn = {
localAddr: "",
remoteAddr: "",
rid: -1,
closeRead: (): void => {},
closeWrite: (): void => {},
read: async (): Promise<number | Deno.EOF> => {
return 0;
},
write: async (): Promise<number> => {
return -1;
},
close: (): void => {}
};

test(async function readRequestError(): Promise<void> {
let input = `GET / HTTP/1.1
malformedHeader
`;
const reader = new BufReader(new StringReader(input));
let err;
try {
await readRequest(reader);
await readRequest(mockConn, reader);
} catch (e) {
err = e;
}
Expand Down Expand Up @@ -404,7 +434,7 @@ test(async function testReadRequestError(): Promise<void> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let req: any;
try {
req = await readRequest(reader);
req = await readRequest(mockConn, reader);
} catch (e) {
err = e;
}
Expand Down