禁用HTTP服务器响应的分块编码

时间:2013-01-14 08:39:02

标签: http encoding go

我正在使用net / http包在GO中编写一个小型实验性http服务器,我需要所有回复才能进行“身份”传输编码。但是,GO中的http服务器始终使用“chunked”传输返回响应。 有没有办法在GO中禁用HTTP服务器上的分块编码?

1 个答案:

答案 0 :(得分:2)

我不清楚使用“转移编码:身份”回复是否在规范下有效(我想也许你应该把它留下来),但是......

检查代码here,我在WriteHeader(code int)函数中看到了这一点(它有点奇怪,但是这个函数实际上将所有标题刷新到套接字):

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 {

我认为上面第一行中的“hasCL”指的是可用的内容长度。如果可用,它将完全删除“Transfer-Encoding”标头,否则,如果版本为1.1或更高版本,则将“Transfer-Encoding”设置为chunked。因为这是在将它写入套接字之前完成的,所以我认为目前没有任何方法可以更改它。