From ac871141101927ab0bbda336837e82a025da49f3 Mon Sep 17 00:00:00 2001 From: adityajai119 Date: Thu, 12 Nov 2026 18:10:34 +0530 Subject: [PATCH] fix(response): send 1xx informational status codes immediately responseWriter.WriteHeader stored every status code as the final one. A 1xx informational code such as 103 Early Hints was therefore never sent unless the header was flushed, and flushing it marked the response as written: the final status the handler set afterwards was ignored (net/http then sent 200) and Status() reported 103. Pass 1xx codes other than 101 Switching Protocols straight to the underlying ResponseWriter, as net/http does, without recording them as the final status. Fixes #3289 --- response_writer.go | 4 ++++ response_writer_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/response_writer.go b/response_writer.go index 8e2d8b30..979f5fd2 100644 --- a/response_writer.go +++ b/response_writer.go @@ -70,6 +70,10 @@ func (w *responseWriter) WriteHeader(code int) { debugPrint("[WARNING] Headers were already written. Wanted to override status code %d with %d", w.status, code) return } + if code >= http.StatusContinue && code < http.StatusOK && code != http.StatusSwitchingProtocols { + w.ResponseWriter.WriteHeader(code) + return + } w.status = code } } diff --git a/response_writer_test.go b/response_writer_test.go index 99548431..190798ae 100644 --- a/response_writer_test.go +++ b/response_writer_test.go @@ -6,9 +6,13 @@ package gin import ( "bufio" + "io" "net" "net/http" "net/http/httptest" + "net/http/httptrace" + "net/textproto" + "strconv" "testing" "github.com/stretchr/testify/assert" @@ -295,6 +299,41 @@ func TestResponseWriterFlush(t *testing.T) { assert.Equal(t, http.StatusInternalServerError, resp.StatusCode) } +func TestResponseWriterWriteHeaderInformational(t *testing.T) { + testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + writer := &responseWriter{} + writer.reset(w) + + writer.Header().Set("Link", "; rel=preload; as=style") + writer.WriteHeader(http.StatusEarlyHints) + writer.WriteHeader(http.StatusCreated) + _, _ = writer.WriteString(strconv.Itoa(writer.Status())) + })) + defer testServer.Close() + + var informational []int + var link string + trace := &httptrace.ClientTrace{ + Got1xxResponse: func(code int, header textproto.MIMEHeader) error { + informational = append(informational, code) + link = header.Get("Link") + return nil + }, + } + req, err := http.NewRequestWithContext(httptrace.WithClientTrace(t.Context(), trace), http.MethodGet, testServer.URL, nil) + require.NoError(t, err) + resp, err := http.DefaultClient.Do(req) + require.NoError(t, err) + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + require.NoError(t, err) + + assert.Equal(t, []int{http.StatusEarlyHints}, informational) + assert.Equal(t, "; rel=preload; as=style", link) + assert.Equal(t, http.StatusCreated, resp.StatusCode) + assert.Equal(t, "201", string(body)) +} + func TestResponseWriterStatusCode(t *testing.T) { testWriter := httptest.NewRecorder() writer := &responseWriter{}