mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-19 16:57:22 +08:00
fix(response): preserve writer state when hijack fails
This commit is contained in:
parent
0f09c3a9b4
commit
412f843070
@ -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.
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user