如何避免发送 Content-Length 头部信息。

huangapple go评论80阅读模式
英文:

how to avoid sending Content-Length header

问题

对于流式传输的HTTP端点,有没有一种方法可以不发送长度?

w.Header().Set("Content-Type", "image/jpeg")
w.Header().Set("Transfer-Encoding", "chunked")
w.Header().Del("Content-Length")

这是我得到的回复。

HTTP/1.1 200 OK
Content-Length: 0
Content-Type: image/jpeg
Date: Mon, 23 Jun 2014 10:00:59 GMT
Transfer-Encoding: chunked
Transfer-Encoding: chunked

服务器还打印了一个警告。

2014/06/23 06:04:03 http: WriteHeader调用了"chunked"的Transfer-Encoding和Content-Length为0。

英文:

For a streaming http endpoint is there a way to refrain from sending the length?

  w.Header().Set("Content-Type", "image/jpeg")
  w.Header().Set("Transfer-Encoding", "chunked")
  w.Header().Del("Content-Length")

This is what I get back.

HTTP/1.1 200 OK
Content-Length: 0
Content-Type: image/jpeg
Date: Mon, 23 Jun 2014 10:00:59 GMT
Transfer-Encoding: chunked
Transfer-Encoding: chunked

The server prints a warning as well.

2014/06/23 06:04:03 http: WriteHeader called with both Transfer-Encoding of "chunked" and a Content-Length of 0

答案1

得分: 6

你不应该手动设置Transfer-Encoding。Go语言会为你自动设置,包括Content-Length

curl、Go语言的http客户端或任何标准的http客户端都会自动正确读取分块或非分块的http响应。

以下是一个分块服务器的简单示例:http://play.golang.org/p/miEV7URi8P

package main

import (
        "io"
        "log"
        "net/http"
)

// hello world, the web server
func HelloServer(w http.ResponseWriter, req *http.Request) {
        w.WriteHeader(200)
        for i := 0; i < 5; i++ {
                io.WriteString(w, "hello, world!\n")
                w.(http.Flusher).Flush()
        }
}

func main() {
        http.HandleFunc("/", HelloServer)
        err := http.ListenAndServe(":8080", nil)
        if err != nil {
                log.Fatal("ListenAndServe: ", err)
        }
}

对于image/jpeg类型的情况,你可以将分块的决定交给Go语言处理,或者手动发送图像的N个字节,然后刷新。

英文:

You should not manually set the Transfer-Encoding. Go is going to do that for you, as well as the Content-Length.

curl, the Go http client or any standard http client will automatically read correctly chunked or non-chunked http response.

Small exmaple of chunked server: http://play.golang.org/p/miEV7URi8P

package main

import (
        &quot;io&quot;
        &quot;log&quot;
        &quot;net/http&quot;
)

// hello world, the web server
func HelloServer(w http.ResponseWriter, req *http.Request) {
        w.WriteHeader(200)
        for i := 0; i &lt; 5; i++ {
                io.WriteString(w, &quot;hello, world!\n&quot;)
                w.(http.Flusher).Flush()
        }
}

func main() {
        http.HandleFunc(&quot;/&quot;, HelloServer)
        err := http.ListenAndServe(&quot;:8080&quot;, nil)
        if err != nil {
                log.Fatal(&quot;ListenAndServe: &quot;, err)
        }
}

In a case of an image/jpeg, you can either delegate the chunked decision to Go or manually send N bytes from the image then flush.

huangapple
  • 本文由 发表于 2014年6月23日 18:05:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/24363240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定