5

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?

3
  • 1
    You need to add a Content-Length header ;) Commented Jan 14, 2013 at 8:50
  • 1
    One thread in golang-nuts touches ths topic: groups.google.com/d/topic/golang-nuts/3sYwQlUOv-o/discussion Commented Jan 14, 2013 at 8:54
  • 1
    Thanks @Tom! There was a typo in the Content-Length header string. This fixed everything :) Commented Jan 14, 2013 at 9:06

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Adding Content-Length does the trick. (I got a typo in that header). Thanks!
But what if you don't know the content length?
@Timmmm then nobody should talk to your client / proxy because it is poorly designed to follow a problematic part of HTTP1.1 which HTTP2 removed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.