对我来说,尚不清楚在规范下以“ Transfer-Encoding:identity”作为响应是否有效(我想也许您应该将其保留),但是...
在这里检查代码,我在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”设置为分块。因为这是在将其写入套接字之前完成的,所以我认为当前没有任何方法可以对其进行更改。