redline
← arena

Counted twice, once

SeniorGonet/httpMined
MINED FROM REAL HISTORYgo-chi/chiMIT

The code below is the file exactly as it stood in go-chi/chi. The defect was introduced on 2016-11-22, reviewed, merged, and fixed on 2026-05-16 in commit 4ef87eaf — after 3462 days in the tree.

The commit was written by a human contributor to go-chi/chi, not by an agent. The ticket and the description on the next two panels are ours — a reconstruction of the case the original change made, so you meet it the way its reviewer did. The code, the defect and the dates are untouched.

MW-140the ticket

Implement io.ReaderFrom on the response wrapper

Wrapping `http.ResponseWriter` in middleware costs us `sendfile`. The standard library's writer implements `io.ReaderFrom`, and `io.Copy` uses it to hand a file straight to the kernel — but our wrapper does not implement it, so `io.Copy` falls back to a userspace buffer loop and every static file response gets slower.

Add `ReadFrom` to the fancy writer and forward to the underlying `ReaderFrom`. It has to keep working when a `Tee` writer is set, and `BytesWritten()` must stay accurate either way — the access log and the bytes-served metric both read it.

the case the change madereconstructed by us

Added `ReadFrom` with two paths.

When a tee is configured we cannot delegate to the underlying `ReaderFrom`, because the kernel copy would bypass the tee entirely and the mirrored stream would be silently empty. So that branch runs `io.Copy` against the `basicWriter`, which fans out to both destinations.

With no tee we take the fast path: assert `io.ReaderFrom` on the wrapped writer, ensure the header has been written, and hand the reader over. `sendfile` is back.

Both branches add the copied byte count to `basicWriter.bytes` before returning, so `BytesWritten()` is accurate on both. Symmetrical, and easy to keep that way.

Our reconstruction of the argument the real change made, not a quotation of it. Fluent, specific, and not evidence of anything — which is the point.

middleware/wrap_writer.go65 lines
1func (b *basicWriter) Write(buf []byte) (n int, err error) {
2 b.maybeWriteHeader()
3 if !b.discard {
4 n, err = b.ResponseWriter.Write(buf)
5 if b.tee != nil {
6 _, err2 := b.tee.Write(buf[:n])
7 // Prefer errors generated by the proxied writer.
8 if err == nil {
9 err = err2
10 }
11 }
12 } else if b.tee != nil {
13 n, err = b.tee.Write(buf)
14 } else {
15 n, err = io.Discard.Write(buf)
16 }
17 b.bytes += n
18 return n, err
19}
20
21func (b *basicWriter) maybeWriteHeader() {
22 if !b.wroteHeader {
23 b.WriteHeader(http.StatusOK)
24 }
25}
26
27func (b *basicWriter) BytesWritten() int {
28 return b.bytes
29}
30
31func (b *basicWriter) Tee(w io.Writer) {
32 b.tee = w
33}
34
35// httpFancyWriter is a HTTP writer that additionally satisfies http.Flusher,
36// http.Hijacker, and io.ReaderFrom. It exists for the common case of wrapping
37// the http.ResponseWriter that package http gives you, in order to make the
38// proxied object support the full method set of the proxied object.
39type httpFancyWriter struct {
40 basicWriter
41}
42
43func (f *httpFancyWriter) Flush() {
44 f.wroteHeader = true
45 fl := f.basicWriter.ResponseWriter.(http.Flusher)
46 fl.Flush()
47}
48
49func (f *httpFancyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
50 hj := f.basicWriter.ResponseWriter.(http.Hijacker)
51 return hj.Hijack()
52}
53
54func (f *httpFancyWriter) ReadFrom(r io.Reader) (int64, error) {
55 if f.basicWriter.tee != nil {
56 n, err := io.Copy(&f.basicWriter, r)
57 f.basicWriter.bytes += int(n)
58 return n, err
59 }
60 rf := f.basicWriter.ResponseWriter.(io.ReaderFrom)
61 f.basicWriter.maybeWriteHeader()
62 n, err := rf.ReadFrom(r)
63 f.basicWriter.bytes += int(n)
64 return n, err
65}