Skip to content

Commit

Permalink
Fix DoTimeout Streaming body bug
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdubbelboer committed Apr 25, 2022
1 parent 9a0b4d0 commit 7cc6f4c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,14 @@ func swapRequestBody(a, b *Request) {
a.body, b.body = b.body, a.body
a.bodyRaw, b.bodyRaw = b.bodyRaw, a.bodyRaw
a.bodyStream, b.bodyStream = b.bodyStream, a.bodyStream

// This code assumes that if a requestStream was swapped the headers are also swapped or copied.
if rs, ok := a.bodyStream.(*requestStream); ok {
rs.header = &a.Header
}
if rs, ok := b.bodyStream.(*requestStream); ok {
rs.header = &b.Header
}
}

func swapResponseBody(a, b *Response) {
Expand Down Expand Up @@ -1881,6 +1889,9 @@ func (req *Request) closeBodyStream() error {
if bsc, ok := req.bodyStream.(io.Closer); ok {
err = bsc.Close()
}
if rs, ok := req.bodyStream.(*requestStream); ok {
releaseRequestStream(rs)
}
req.bodyStream = nil
return err
}
Expand Down
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2440,6 +2440,7 @@ func (s *Server) serveConn(c net.Conn) (err error) {
if rs, ok := ctx.Request.bodyStream.(*requestStream); ok {
releaseRequestStream(rs)
}
ctx.Request.bodyStream = nil
}

s.setState(c, StateIdle)
Expand Down
1 change: 1 addition & 0 deletions streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func releaseRequestStream(rs *requestStream) {
rs.totalBytesRead = 0
rs.chunkLeft = 0
rs.reader = nil
rs.header = nil
requestStreamPool.Put(rs)
}

Expand Down

0 comments on commit 7cc6f4c

Please sign in to comment.