首頁  >  文章  >  後端開發  >  如果 golang http ResponseWriter 不超過 2kb,為什麼會自動加入內容長度

如果 golang http ResponseWriter 不超過 2kb,為什麼會自動加入內容長度

WBOY
WBOY轉載
2024-02-09 08:28:02570瀏覽

如果 golang http ResponseWriter 不超过 2kb,为什么会自动添加内容长度

php小編新一將為您解答一個關於golang中的疑惑:為什麼在處理http請求時,如果ResponseWriter的內容不超過2kb,還會自動新增內容長度?實際上,這是因為在http協定中,內容長度是一個必需的字段,它用於告知客戶端要接收的資料長度,以便正確解析回應。即使內容長度較小,伺服器仍然需要提供這個字段,以確保完整的通訊過程。這樣,客戶端才能正確地接收並解析內容,確保請求和回應的完整性和準確性。

問題內容

func (handler Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    var content string
    ...
    w.Write([]byte(content))
}

如果 len(content) <= 2048,則 content-length 會自動加入到回應中。而如果超過2048,則沒有content-length,則會加上transfer-encoding: chunked

我找不到在哪裡確定 2048。

我請求協助尋找在哪裡確定 2048 的原始程式碼。

解決方法

讓我們看看http.responsewriter介面中該功能的文檔,僅供參考清晰度:

[i]如果所有寫入資料的總大小低於幾 kb 且沒有 flush 調用,則會自動新增 content-length 標頭。

首先,我們可以看到該數字可能不完全是 2048 (2 kb),但在我們預期的「幾 kb」範圍內。其次,我們可以看到此行為與 flush 方法有關,該方法記錄在 flusher介面:

flush 將所有緩衝的資料傳送到客戶端。

flusher 介面由 responsewriter 實現,允許 http 處理程序將緩衝資料刷新到客戶端。

預設的 http/1.x 和 http/2 responsewriter 實作支援 flusher,但 responsewriter 包裝器可能不支援。處理程序應始終在運行時測試此功能。

正如它所說,您的 responsewriter 可能支援資料緩衝和刷新。這表示當您將資料寫入回應寫入器時,它不會立即透過連線傳輸。相反,它首先被寫入緩衝區。每次緩衝區太滿而無法再寫入時,當 servehttp 方法傳回時,整個緩衝區將會傳送。這可以確保即使您進行大量微小寫入,資料也能有效傳輸,並且所有資料最終都能傳輸。您也可以選擇使用 flush 方法隨時主動清空緩衝區。 http 標頭必須在正文資料之前傳送,但在緩衝區第一次清空之前不需要傳送它們。

把所有這些放在一起,你會發現如果寫入的總量不超過緩衝區大小,並且我們從不調用flush,那麼在所有數據準備好之前不需要發送標頭,此時點我們知道內容的長度。如果寫入的總量大於緩衝區大小,則必須在知道內容長度之前發送標頭,因此 responsewriter 無法自動確定。

這是在 net/http/server.go。具體來說,這裡是緩衝區大小的聲明,以及實現部分緩衝寫入行為的 nchunkedwriter

// This should be >= 512 bytes for DetectContentType,
// but otherwise it's somewhat arbitrary.
const bufferBeforeChunkingSize = 2048

// chunkWriter writes to a response's conn buffer, and is the writer
// wrapped by the response.w buffered writer.
//
// chunkWriter also is responsible for finalizing the Header, including
// conditionally setting the Content-Type and setting a Content-Length
// in cases where the handler's final output is smaller than the buffer
// size. It also conditionally adds chunk headers, when in chunking mode.
//
// See the comment above (*response).Write for the entire write flow.
type chunkWriter struct {

1.19.5 的原始碼連結。請注意,原始程式碼可能會隨著每個 go 版本的變化而變化。

以上是如果 golang http ResponseWriter 不超過 2kb,為什麼會自動加入內容長度的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除