dtnpf35197 2013-01-14 08:39
浏览 208
已采纳

禁用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?

  • 写回答

1条回答 默认 最新

  • douci4026 2013-01-14 21:50
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?