response_writer: keep Written() false when Hijack is unsupported

Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
This commit is contained in:
Sai Asish Y 2026-05-28 01:09:22 -07:00
parent 1182339849
commit adbdcceff3
2 changed files with 7 additions and 5 deletions

View File

@ -114,13 +114,13 @@ func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if w.size > 0 { if w.size > 0 {
return nil, nil, errHijackAlreadyWritten return nil, nil, errHijackAlreadyWritten
} }
if w.size < 0 {
w.size = 0
}
hijacker, ok := w.ResponseWriter.(http.Hijacker) hijacker, ok := w.ResponseWriter.(http.Hijacker)
if !ok { if !ok {
return nil, nil, http.ErrNotSupported return nil, nil, http.ErrNotSupported
} }
if w.size < 0 {
w.size = 0
}
return hijacker.Hijack() return hijacker.Hijack()
} }

View File

@ -114,12 +114,14 @@ func TestResponseWriterHijack(t *testing.T) {
w := ResponseWriter(writer) w := ResponseWriter(writer)
// httptest.ResponseRecorder doesn't implement http.Hijacker; return // httptest.ResponseRecorder doesn't implement http.Hijacker; return
// http.ErrNotSupported instead of panicking (#4638). // http.ErrNotSupported instead of panicking (#4638). On unsupported the
// writer state stays untouched so the handler can still emit a normal
// HTTP response as a fallback.
conn, buf, err := w.Hijack() conn, buf, err := w.Hijack()
assert.Nil(t, conn) assert.Nil(t, conn)
assert.Nil(t, buf) assert.Nil(t, buf)
require.ErrorIs(t, err, http.ErrNotSupported) require.ErrorIs(t, err, http.ErrNotSupported)
assert.True(t, w.Written()) assert.False(t, w.Written())
// CloseNotify on a non-CloseNotifier returns nil instead of panicking. // CloseNotify on a non-CloseNotifier returns nil instead of panicking.
assert.Nil(t, w.CloseNotify()) assert.Nil(t, w.CloseNotify())