send 200 status for requests with empty http.Handlers

fixes #286
This commit is contained in:
Lucas Clemente
2016-08-16 12:28:47 +02:00
parent 88e1e50efe
commit a4dcf5efd7
4 changed files with 32 additions and 0 deletions

View File

@@ -68,3 +68,9 @@ func (w *responseWriter) Write(p []byte) (int, error) {
}
return w.dataStream.Write(p)
}
func (w *responseWriter) finish() {
if !w.headerWritten {
w.WriteHeader(200)
}
}

View File

@@ -78,4 +78,15 @@ var _ = Describe("Response Writer", func() {
0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72,
}))
})
It("writes a 200 in finish if nothing was called", func() {
w.finish()
Expect(headerStream.Bytes()).To(Equal([]byte{0x0, 0x0, 0x1, 0x1, 0x4, 0x0, 0x0, 0x0, 0x5, 0x88})) // 0x88 is 200
})
It("doesn't do anything in finish if data was written before", func() {
w.WriteHeader(200)
w.finish()
Expect(headerStream.Bytes()).To(Equal([]byte{0x0, 0x0, 0x1, 0x1, 0x4, 0x0, 0x0, 0x0, 0x5, 0x88})) // 0x88 is 200
})
})

View File

@@ -167,6 +167,7 @@ func (s *Server) handleRequest(session streamCreator, headerStream utils.Stream,
handler = http.DefaultServeMux
}
handler.ServeHTTP(responseWriter, req)
responseWriter.finish()
if responseWriter.dataStream != nil {
responseWriter.dataStream.Close()
}

View File

@@ -84,6 +84,20 @@ var _ = Describe("H2 server", func() {
Expect(dataStream.remoteClosed).To(BeTrue())
})
It("returns 200 with an empty handler", func() {
s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
headerStream.Write([]byte{
0x0, 0x0, 0x11, 0x1, 0x5, 0x0, 0x0, 0x0, 0x5,
// Taken from https://http2.github.io/http2-spec/compression.html#request.examples.with.huffman.coding
0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff,
})
err := s.handleRequest(session, headerStream, &sync.Mutex{}, hpackDecoder, h2framer)
Expect(err).NotTo(HaveOccurred())
Eventually(func() []byte {
return headerStream.Buffer.Bytes()
}).Should(Equal([]byte{0x0, 0x0, 0x1, 0x1, 0x4, 0x0, 0x0, 0x0, 0x5, 0x88})) // 0x88 is 200
})
It("does not close the dataStream when end of stream is not set", func() {
var handlerCalled bool
s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {