I'm writing a HTTP server in Go that will receive requests from clients with a Expect: 100-continue
header. However, it appears that the net/http server doesn't send a HTTP/1.1 100 Continue
unless the client also sends a Transfer-Encoding: Chunked
header, which some clients (for example, ffmpeg with an icecast://
destination) do not.
Here's a minimal server, that writes into a bytes.Buffer
(I've reproduced the same behaviour with a more complicated server that, for example, uses io.Copy()
to write into a file):
func main() {
http.HandleFunc("/", func(writer http.ResponseWriter, r *http.Request) {
log.Printf("Expect header: %v\n", r.Header.Get("Expect"))
log.Printf("Transfer-Encoding header: %v\n", r.Header.Get("Transfer-Encoding"))
buf := new(bytes.Buffer)
defer func() {
log.Printf("Buffer size: %d\n", buf.Len())
}()
defer r.Body.Close()
log.Println("Writing.")
io.Copy(buf, r.Body)
})
log.Fatal(http.ListenAndServe(":3948", nil))
}
And here's a transcript of two HTTP conversations (via telnet), where the server sends a 100 in one but not in the other:
PUT /telnetlol HTTP/1.1
Host: localhost
Expect: 100-continue
HTTP/1.1 200 OK
Date: Thu, 18 Mar 2021 10:59:09 GMT
Content-Length: 0
PUT /telnetlol HTTP/1.1
Host: localhost
Expect: 100-continue
Transfer-Encoding: chunked
HTTP/1.1 100 Continue
test
HTTP/1.1 200 OK
Date: Thu, 18 Mar 2021 10:59:35 GMT
Content-Length: 0
Connection: close
Is this a bug in Go, or am I misunderstanding the HTTP spec? The spec reads:
Upon receiving a request which includes an Expect request-header field with the "100-continue" expectation, an origin server MUST respond with 100 (Continue) status and continue to read from the input stream, or respond with a final status code. The origin server MUST NOT wait for the request body before sending the 100 (Continue) response.
Edit: Sending a non-zero Content-Length
header in the initial request also makes the server reply with a 100 Continue
. (Although, if I understand the spec correctly, it should still reply with a Continue irregardless.)