


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)) }
If len(content) 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 {
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!

Golangisidealforbuildingscalablesystemsduetoitsefficiencyandconcurrency,whilePythonexcelsinquickscriptinganddataanalysisduetoitssimplicityandvastecosystem.Golang'sdesignencouragesclean,readablecodeanditsgoroutinesenableefficientconcurrentoperations,t

Golang is better than C in concurrency, while C is better than Golang in raw speed. 1) Golang achieves efficient concurrency through goroutine and channel, which is suitable for handling a large number of concurrent tasks. 2)C Through compiler optimization and standard library, it provides high performance close to hardware, suitable for applications that require extreme optimization.

Reasons for choosing Golang include: 1) high concurrency performance, 2) static type system, 3) garbage collection mechanism, 4) rich standard libraries and ecosystems, which make it an ideal choice for developing efficient and reliable software.

Golang is suitable for rapid development and concurrent scenarios, and C is suitable for scenarios where extreme performance and low-level control are required. 1) Golang improves performance through garbage collection and concurrency mechanisms, and is suitable for high-concurrency Web service development. 2) C achieves the ultimate performance through manual memory management and compiler optimization, and is suitable for embedded system development.

Golang performs better in compilation time and concurrent processing, while C has more advantages in running speed and memory management. 1.Golang has fast compilation speed and is suitable for rapid development. 2.C runs fast and is suitable for performance-critical applications. 3. Golang is simple and efficient in concurrent processing, suitable for concurrent programming. 4.C Manual memory management provides higher performance, but increases development complexity.

Golang's application in web services and system programming is mainly reflected in its simplicity, efficiency and concurrency. 1) In web services, Golang supports the creation of high-performance web applications and APIs through powerful HTTP libraries and concurrent processing capabilities. 2) In system programming, Golang uses features close to hardware and compatibility with C language to be suitable for operating system development and embedded systems.

Golang and C have their own advantages and disadvantages in performance comparison: 1. Golang is suitable for high concurrency and rapid development, but garbage collection may affect performance; 2.C provides higher performance and hardware control, but has high development complexity. When making a choice, you need to consider project requirements and team skills in a comprehensive way.

Golang is suitable for high-performance and concurrent programming scenarios, while Python is suitable for rapid development and data processing. 1.Golang emphasizes simplicity and efficiency, and is suitable for back-end services and microservices. 2. Python is known for its concise syntax and rich libraries, suitable for data science and machine learning.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
