Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable chunked encoding for HTTP server responses

Tags:

http

encoding

go

I'm writing a small experimental http server in GO using the net/http package, and I need all my replies to have 'identity' transfer encoding. However the http server in GO always returns responses using the 'chunked' transfer. Is there any way to disable chunked encoding on HTTP server in GO?

like image 793
Dany Avatar asked Nov 04 '22 06:11

Dany


1 Answers

It's not clear to me whether responding with "Transfer-Encoding: identity" is valid under the spec (I think maybe you should just leave it out), but...

Inspecting the code here, I see this inside the WriteHeader(code int) function (it's a little bit strange, but this function actually flushes all the headers to the socket):

367     } else if hasCL {
368         w.contentLength = contentLength
369         w.header.Del("Transfer-Encoding")
370     } else if w.req.ProtoAtLeast(1, 1) {
371         // HTTP/1.1 or greater: use chunked transfer encoding
372         // to avoid closing the connection at EOF.
373         // TODO: this blows away any custom or stacked Transfer-Encoding they
374         // might have set.  Deal with that as need arises once we have a valid
375         // use case.
376         w.chunking = true
377         w.header.Set("Transfer-Encoding", "chunked")
378     } else {

I believe "hasCL" in the first line above refers to having a content length available. If it is available, it removes the "Transfer-Encoding" header altogether, otherwise, if the version is 1.1 or greater, it sets the "Transfer-Encoding" to chunked. Because this is done right before writing it to the socket, I don't think there's currently going to be any way for you to change it.

like image 149
laslowh Avatar answered Nov 09 '22 02:11

laslowh