Skip to content

gzhttp: buffered Write does not commit the implicit HTTP status #1317

Closed

Description

@hakarrr

On current master 7213df05def493d5fb921e472b3f84d92db98aba, gzhttp lets a later WriteHeader replace the implicit 200 selected by a buffered Write. This makes the final status depend on the response size and negotiated encoding.

package main

import (
    "fmt"
    "net/http"
    "net/http/httptest"
    "strings"

    "github.com/klauspost/compress/gzhttp"
)

func main() {
    for _, encoding := range []string{"identity", "gzip", "zstd"} {
        for _, size := range []int{0, 3, 2048} {
            handler := gzhttp.GzipHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
                _, _ = w.Write([]byte(strings.Repeat("x", size)))
                w.WriteHeader(http.StatusInternalServerError)
            }))
            req := httptest.NewRequest(http.MethodGet, "/", nil)
            req.Header.Set("Accept-Encoding", encoding)
            rec := httptest.NewRecorder()
            handler.ServeHTTP(rec, req)
            fmt.Println(encoding, size, rec.Code)
        }
    }
}

With Linux/amd64 and Go 1.26.6:

identity 0 200
identity 3 200
identity 2048 200
gzip 0 500
gzip 3 500
gzip 2048 200
zstd 0 500
zstd 3 500
zstd 2048 200

The net/http ResponseWriter contract says Write implicitly calls WriteHeader(200) if a final status has not been sent. Later final statuses should be ignored, including when the wrapper has not yet forwarded its buffer. Zero-length Write has the same status effect. Unwrapped net/http returns 200 in these cases.

GzipResponseWriter.code currently means both "no status selected" and "status already forwarded": forwarding clears it. A separate final-status-selected flag appears to address this without forcing the buffer out early. The same state can prevent informational headers from being forwarded after a final status has been selected, while preserving early hints before it.

I have a focused local fix with 30 table cases covering implicit/explicit status selection, empty/small/compressed bodies, identity/gzip/zstd, body round trips, and informational headers. On the unchanged source, 16 cases fail and 14 controls pass. With the fix, the gzhttp suite, race run, noasm/nounsafe package runs, and repository-wide short suite pass. Repository-wide vet and go fix -diff also pass. Full non-short repository tests did not finish within the local run limit; Windows/macOS and the full version matrix were not run.

Would a PR along these lines be welcome, or is there existing work or a preferred approach?

Activity

  1. klauspost commented on Nov 13, 2026

    @klauspost
    Owner

    You are very welcome to send a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions