don’t panic if a request handler replaces the request.Body

fixes #405
This commit is contained in:
Marten Seemann
2017-01-28 22:59:14 +09:00
parent dfa0137bd6
commit 7e0cb8072b
2 changed files with 21 additions and 2 deletions

View File

@@ -162,7 +162,8 @@ func (s *Server) handleRequest(session streamCreator, headerStream utils.Stream,
_, _ = dataStream.Read([]byte{0}) // read the eof
}
req.Body = newRequestBody(dataStream)
reqBody := newRequestBody(dataStream)
req.Body = reqBody
responseWriter := newResponseWriter(headerStream, headerStreamMutex, dataStream, protocol.StreamID(h2headersFrame.StreamID))
@@ -191,7 +192,7 @@ func (s *Server) handleRequest(session streamCreator, headerStream utils.Stream,
responseWriter.WriteHeader(200)
}
if responseWriter.dataStream != nil {
if !streamEnded && !req.Body.(*requestBody).requestRead {
if !streamEnded && !reqBody.requestRead {
responseWriter.dataStream.Reset(nil)
}
responseWriter.dataStream.Close()

View File

@@ -2,6 +2,7 @@ package h2quic
import (
"bytes"
"io"
"net"
"net/http"
"os"
@@ -152,6 +153,23 @@ var _ = Describe("H2 server", func() {
Expect(handlerCalled).To(BeTrue())
})
It("resets the dataStream when the body of POST request is not read, and the request handler replaces the request.Body", func() {
var handlerCalled bool
s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Body = struct {
io.Reader
io.Closer
}{}
handlerCalled = true
})
headerStream.dataToRead.Write([]byte{0x0, 0x0, 0x20, 0x1, 0x24, 0x0, 0x0, 0x0, 0x5, 0x0, 0x0, 0x0, 0x0, 0xff, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff, 0x83, 0x84, 0x87, 0x5c, 0x1, 0x37, 0x7a, 0x85, 0xed, 0x69, 0x88, 0xb4, 0xc7})
err := s.handleRequest(session, headerStream, &sync.Mutex{}, hpackDecoder, h2framer)
Expect(err).NotTo(HaveOccurred())
Eventually(func() bool { return dataStream.reset }).Should(BeTrue())
Consistently(func() bool { return dataStream.remoteClosed }).Should(BeFalse())
Expect(handlerCalled).To(BeTrue())
})
It("closes the dataStream if the body of POST request was read", func() {
var handlerCalled bool
s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {