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] Handle aborted http Requests #450

Closed
wants to merge 2 commits into from
Closed
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
14 changes: 9 additions & 5 deletions http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,15 @@ export class Server implements AsyncIterable<ServerRequest> {
if (bufStateErr === "EOF") {
// The connection was gracefully closed.
} else if (bufStateErr instanceof Error) {
// An error was thrown while parsing request headers.
await writeResponse(req.w, {
status: 400,
body: new TextEncoder().encode(`${bufStateErr.message}\r\n\r\n`)
});
// in case of wrong first line HTTP header or
// aborted request we don't have to respond
if (req) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still run the risk of responding to the previous request.
This could be fixed by adding req = undefined to the start of the while loop above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH i don't know if this case could happen. As i haven't found a way to test it. I trust you on this.

// An error was thrown while parsing request headers.
await writeResponse(req.w, {
status: 400,
body: new TextEncoder().encode(`${bufStateErr.message}\r\n\r\n`)
});
}
} else if (this.closing) {
// There are more requests incoming but the server is closing.
// TODO(ry): send a back a HTTP 503 Service Unavailable status.
Expand Down
5 changes: 5 additions & 0 deletions http/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ 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: "ToTAlly \\ Non REAdable HTTTTTTp pRotOcOlz",
err: "EOF",
headers: []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn't demonstrate the error. When I run it without your other change, it passes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mm you're right. We may work on a way to mock the request flow in the test runner. I'll try to find a way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you looked at these tests? https://github.com/golang/go/blob/master/src/net/http/requestwrite_test.go

They're quite nice. I think we need to add ResponseWriter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case it will not be revelant because we will not write the response. But yes it would be a good addition sure. Adding this in another PR?

}
};
for (const p in testCases) {
Expand Down