首页 > 解决方案 > 从 Go 服务器发送分块的 HTTPS 响应

问题描述

以下示例非常适用于HTTP从 Go 服务器发送分块的 HTTP 响应

添加 TLS 后,我看到响应不再分块:

func main() {
  http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    flusher, ok := w.(http.Flusher)
    if !ok {
      panic("expected http.ResponseWriter to be an http.Flusher")
    }
    w.Header().Set("X-Content-Type-Options", "nosniff")
    for i := 1; i <= 10; i++ {
      fmt.Fprintf(w, "Chunk #%d\n", i)
      flusher.Flush() // Trigger "chunked" encoding and send a chunk...
      time.Sleep(500 * time.Millisecond)
    }
  })

  log.Print("Listening on localhost:8080")
  log.Fatal(http.ListenAndServeTLS(":8080", "<CERT_FILE>", "<KEY_FILE>", nil))
}

任何想法为什么会这样?

标签: sslgochunked-encoding

解决方案


log.Infof("Protocol Version: %s", request.Proto)

确认其使用 HTTP/2.0

谢谢 Vorsprung


推荐阅读