diff --git a/response_writer.go b/response_writer.go index 8e2d8b30..6e0eb63b 100644 --- a/response_writer.go +++ b/response_writer.go @@ -108,6 +108,7 @@ func (w *responseWriter) Written() bool { } // Hijack implements the http.Hijacker interface. +// On error, the writer state is left unchanged so callers can send a fallback HTTP response. func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { // Allow hijacking before any data is written (size == -1) or after headers are written (size == 0), // but not after body data is written (size > 0). For compatibility with websocket libraries (e.g., github.com/coder/websocket) @@ -118,10 +119,11 @@ func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { if !ok { return nil, nil, http.ErrNotSupported } - if w.size < 0 { + conn, rw, err := hijacker.Hijack() + if err == nil && w.size < 0 { w.size = 0 } - return hijacker.Hijack() + return conn, rw, err } // CloseNotify implements the http.CloseNotifier interface. diff --git a/response_writer_test.go b/response_writer_test.go index 99548431..59b4c4ca 100644 --- a/response_writer_test.go +++ b/response_writer_test.go @@ -170,6 +170,7 @@ func TestResponseWriterHijackAfterWrite(t *testing.T) { expectWrittenBeforeHijack bool expectHijackSuccess bool expectWrittenAfterHijack bool + expectSizeAfterHijack int expectError error }{ { @@ -178,6 +179,7 @@ func TestResponseWriterHijackAfterWrite(t *testing.T) { expectWrittenBeforeHijack: false, expectHijackSuccess: true, expectWrittenAfterHijack: true, // Hijack itself marks the writer as written + expectSizeAfterHijack: 0, expectError: nil, }, { @@ -189,6 +191,7 @@ func TestResponseWriterHijackAfterWrite(t *testing.T) { expectWrittenBeforeHijack: true, expectHijackSuccess: false, expectWrittenAfterHijack: true, + expectSizeAfterHijack: 4, expectError: errHijackAlreadyWritten, }, } @@ -216,10 +219,59 @@ func TestResponseWriterHijackAfterWrite(t *testing.T) { require.ErrorIs(t, hijackErr, tc.expectError, "unexpected error from Hijack()") assert.Equal(t, tc.expectHijackSuccess, hijacker.hijacked, "unexpected hijacker.hijacked state") assert.Equal(t, tc.expectWrittenAfterHijack, w.Written(), "unexpected w.Written() state after hijack") + assert.Equal(t, tc.expectSizeAfterHijack, w.Size(), "unexpected w.Size() state after hijack") }) } } +type failingHijacker struct { + *httptest.ResponseRecorder +} + +func (f *failingHijacker) Hijack() (net.Conn, *bufio.ReadWriter, error) { + return nil, nil, http.ErrNotSupported +} + +func TestResponseWriterHijackFailureKeepsWriterUsable(t *testing.T) { + testWriter := &failingHijacker{ + ResponseRecorder: httptest.NewRecorder(), + } + + writer := &responseWriter{} + writer.reset(testWriter) + + w := ResponseWriter(writer) + + conn, buf, err := w.Hijack() + + assert.Nil(t, conn) + assert.Nil(t, buf) + require.ErrorIs(t, err, http.ErrNotSupported) + + // Hijacking failed before any response was written. + // Keep the writer untouched so the handler can still + // emit a normal HTTP response as a fallback. + assert.False(t, w.Written()) + assert.Equal(t, noWritten, w.Size()) + + http.Error( + w, + "hijack failed", + http.StatusInternalServerError, + ) + + assert.Equal( + t, + http.StatusInternalServerError, + testWriter.Code, + ) + assert.Equal( + t, + "hijack failed\n", + testWriter.Body.String(), + ) +} + // TestResponseWriterHijackAfterWriteHeaderNow verifies that hijacking is allowed after // WriteHeaderNow but blocked after body data is written. func TestResponseWriterHijackAfterWriteHeaderNow(t *testing.T) {