Home > Backend Development > Golang > If golang http ResponseWriter does not exceed 2kb, why is the content length automatically added?

If golang http ResponseWriter does not exceed 2kb, why is the content length automatically added?

WBOY
Release: 2024-02-09 08:28:02
forward
602 people have browsed it

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

php editor Xinyi will answer a question about golang for you: Why is the content length automatically added when processing an http request if the content of the ResponseWriter does not exceed 2kb? In fact, this is because in the HTTP protocol, the content length is a required field, which is used to tell the client the length of data to receive in order to correctly parse the response. Even if the content length is small, the server still needs to provide this field to ensure a complete communication process. In this way, the client can correctly receive and parse the content, ensuring the integrity and accuracy of requests and responses.

Question content

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

If len(content) <= 2048, content-length will be automatically added to the response. And if it exceeds 2048, there is no content-length, and transfer-encoding: chunked will be added.

I can't find where to determine 2048.

I'm asking for help finding the source code where 2048 is determined.

Solution

Let's take a look at the documentation for this function in the http.responsewriter interface, for reference only Clarity:

[i] If the total size of all written data is below a few kb and there are no flush calls, the content-length header is automatically added.

First, we can see that the number may not be exactly 2048 (2 kb), but is within the "few kb" range we would expect. Secondly, we can see that this behavior is related to the flush method, which is documented in the flusher interface:

flush sends all buffered data to the client.

The flusher interface is implemented by responsewriter and allows http handlers to flush buffered data to the client.

The default http/1.x and http/2 responsewriter implementations support flushers, but responsewriter wrappers may not. Handlers should always test this functionality at runtime.

As it says, your responsewriter may support data buffering and flushing. This means that when you write data to the response writer, it is not immediately transferred over the connection. Instead, it is written to the buffer first. Each time the buffer becomes too full for more writes, the entire buffer will be transferred when the servehttp method returns. This ensures that even if you do a lot of tiny writes, the data is transferred efficiently and all the data is eventually transferred. You can also choose to proactively clear the buffer at any time using the flush method. http headers must be sent before the body data, but they do not need to be sent before the buffer is first emptied.

Putting all this together, you'll see that if the total amount written does not exceed the buffer size, and we never call flush, then there is no need to send headers until all data is ready , at this point we know the length of the content. If the total amount written is greater than the buffer size, the headers must be sent before the content length is known, so responsewriter cannot determine it automatically.

This is at net/http/server.go. Specifically, here is the declaration of the buffer size, and 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 {
Copy after login

Source code link for 1.19.5. Please note that the source code may change with each go version.

The above is the detailed content of If golang http ResponseWriter does not exceed 2kb, why is the content length automatically added?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template