禁用HTTP服务器响应的分块编码 [英] Disable chunked encoding for HTTP server responses

查看:64
本文介绍了禁用HTTP服务器响应的分块编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用net/http包在GO中编写一个小型的实验性HTTP服务器,我需要所有答复都具有身份"传输编码.但是,GO中的http服务器始终使用块式"传输返回响应.有什么办法可以禁用GO中HTTP服务器上的分块编码?

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?

推荐答案

我尚不清楚根据规范,"Transfer-Encoding:identity"是否有效(我想也许您应该将其忽略),但是...

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...

此处中检查代码,我在WriteHeader(code int)函数(有点奇怪,但是此函数实际上将所有标头刷新到套接字):

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 {

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

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.

这篇关于禁用HTTP服务器响应的分块编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆