The Life Of A Write is like this:

Handler starts. No header has been sent. The handler can either write a header, or just start writing. Writing before sending a header sends an implicitly empty 200 OK header.

If the handler didn't declare a Content-Length up front, we either go into chunking mode or, if the handler finishes running before the chunking buffer size, we compute a Content-Length and send that in the header instead.

Likewise, if the handler didn't set a Content-Type, we sniff that from the initial chunk of output.

The Writers are wired together like:

1. *response (the ResponseWriter) -> 2. (*response).w, a *bufio.Writer of bufferBeforeChunkingSize bytes 3. chunkWriter.Writer (whose writeHeader finalizes Content-Length/Type)

and which writes the chunk headers, if needed.

4. conn.buf, a bufio.Writer of default (4kB) bytes, writing to -> 5. checkConnErrorWriter{c}, which notes any non-nil error on Write

and populates c.werr with it if so. but otherwise writes to:

6. the rwc, the net.Conn.

TODO(bradfitz): short-circuit some of the buffering when the initial header contains both a Content-Type and Content-Length. Also short-circuit in (1) when the header's been sent and not in chunking mode, writing directly to (4) instead, if (2) has no buffered data. More generally, we could short-circuit from (1) to (3) even in chunking mode if the write size from (1) is over some threshold and nothing is in (2). The answer might be mostly making bufferBeforeChunkingSize smaller and having bufio's fast-paths deal with this instead.

Write is referenced in 0 repositories