-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Update h2c upgrading to handle request bodies #127
Conversation
This PR (HEAD: 5944212) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/net/+/381915 to see it. Tip: You can toggle comments from me using the |
Message from Gopher Robot: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be During May-July and Nov-Jan the Go project is in a code freeze, during which Please don’t reply on this GitHub thread. Visit golang.org/cl/381915. |
http2/h2c/h2c.go
Outdated
@@ -249,6 +250,37 @@ func convertH1ReqToH2(r *http.Request) (*bytes.Buffer, []http2.Setting, error) { | |||
} | |||
} | |||
|
|||
if r.Body != nil { |
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.
r.Body != nil && r.Body != http.NoBody
This PR (HEAD: 14bfb06) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/net/+/381915 to see it. Tip: You can toggle comments from me using the |
Message from Ian Lance Taylor: Patch Set 2: (6 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/381915. |
This PR (HEAD: 43a20da) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/net/+/381915 to see it. Tip: You can toggle comments from me using the |
This PR (HEAD: 7ecf925) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/net/+/381915 to see it. Tip: You can toggle comments from me using the |
All comments should be addressed now. |
If a request that triggered an upgrade from HTTP/1.1 -> HTTP/2 contained a body, it would not be replayed by the server as a HTTP/2 data frame. This would result in hangs as the client would get no data back, as the request body was never actually handled. This code corrects this, and sends HTTP/2 DATA frames with the request body. As an example: Client: ``` $ curl -v --http2 -d 'POST BODY' http://localhost:5555 * Trying 127.0.0.1... * TCP_NODELAY set * Connected to localhost (127.0.0.1) port 5555 (#0) > POST / HTTP/1.1 > Host: localhost:5555 > User-Agent: curl/7.64.1 > Accept: */* > Connection: Upgrade, HTTP2-Settings > Upgrade: h2c > HTTP2-Settings: AAMAAABkAARAAAAAAAIAAAAA > Content-Length: 9 > Content-Type: application/x-www-form-urlencoded > * upload completely sent off: 9 out of 9 bytes < HTTP/1.1 101 Switching Protocols < Connection: Upgrade < Upgrade: h2c * Received 101 * Using HTTP2, server supports multi-use * Connection state changed (HTTP/2 confirmed) * Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0 * Connection state changed (MAX_CONCURRENT_STREAMS == 250)! < HTTP/2 200 < content-length: 0 < date: Sat, 29 Jan 2022 06:51:05 GMT < * Connection #0 to host localhost left intact * Closing connection 0 ``` Echo server: ``` $ ./bin/h2test Listening [0.0.0.0:5555]... Request: {Method:POST URL:/ Proto:HTTP/2.0 ProtoMajor:2 ProtoMinor:0 Header:map[Accept:[*/*] Content-Length:[9] Content-Type:[application/x-www-form-urlencoded] User-Agent:[curl/7.64.1]] Body:0xc000098120 GetBody:<nil> ContentLength:9 TransferEncoding:[] Close:false Host:localhost:5555 Form:map[] PostForm:map[] MultipartForm:<nil> Trailer:map[] RemoteAddr:127.0.0.1:54540 RequestURI:/ TLS:<nil> Cancel:<nil> Response:<nil> ctx:0xc0000a0000} Received body: POST BODY ``` Fixes #38064
This PR (HEAD: 189b99a) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/net/+/381915 to see it. Tip: You can toggle comments from me using the |
I think I've made a meal of this with force pushes. Let me open a new pull request. |
This fixes at least part of golang/go#38064
If a request that triggered an upgrade from HTTP/1.1 -> HTTP/2 contained a body, it would not be replayed by the server as a HTTP/2 data frame.
This would result in hangs as the client would get no data back, as the request body was never actually handled.
This code corrects this, and sends HTTP/2 DATA frames with the request body.
As an example:
Client:
Echo server: