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.

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?