-
Notifications
You must be signed in to change notification settings - Fork 623
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
Consume body on res.respond if user does not call req.body #49
Conversation
net/http_test.ts
Outdated
}); | ||
// Body should have been secretly consumed by here | ||
const body = dec.decode(await req.body()); | ||
assertEqual(body, ""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we allow to consume body multiple times?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean that you expect req.body()
to return the content even after respond, or do you mean that invoking multiple times of req.body()
would all yield empty string here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO error should be thrown if one tries to consume body after respond
- it'll be less surprising for users and would allow to spot mistakes in code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Updated.
@kevinkassimo I'm a little concerned about this patch. I'm imaging an HTTP/1.0 connection (i.e. without keep-alive) in which the requester is uploading a file. Imagine the server gets the request, processes it and decides that it will not accept the upload, it wants to send back an html page with status 401 (Unauthorized). The server should simply stop reading from the socket - send the response - and then close the connection. If the server is forced to read a potentially very large upload every time it wants to reply, then this could be a DoS attack vector. |
Since our server is a close port of Go's, I think it would be good if we researched how Go handles this situation. Does anyone have time to dive into https://github.com/golang/go/tree/master/src/net/http and pull up some links? |
I already read through some of it for #22, so I can handle that |
I went digging through Golang's codebase. Here some sythesis with links.
// after establishing connection
w, err := c.readRequest(ctx) // 1.
...
if err != nil {
...
}
// Expect 100 Continue support
...
serverHandler{c.server}.ServeHTTP(w, w.req) // 2.
...
w.finishRequest() // 3.
if !w.shouldReuseConnection() { // 4.
if w.requestBodyLimitHit || w.closedRequestBodyEarly() {
c.closeWriteAndWait()
}
return
}
...
I have to admit that this logic is very complicated and I'm not experienced with Golang so please could someone double-check it? |
Thank you! closedRequestBodyEarly and closeWriteAndWait is the smart idea I think we need here. |
@bartlomieju I'll also look into details tonight. Thanks for diving in! |
Actually, I'm planning to make HTTP more Go-ish in the next few days. It would probably be nice to just directly replicating the structure instead of developing some unreliable design ourselves... |
Also, let's complete #76 first. Closing this. I'll work on a best effort Go port after completing reorg |
@kevinkassimo @bartlomieju any updates on this? |
@ry Quite busy these days and probably won't involve very much in the coming month. Hope someone else could take on the task. |
I'll look into it this week |
Fix #40
Consume body on
req.respond()
if the user fails to callreq.body()