mirror of
https://github.com/gin-gonic/gin.git
synced 2026-04-29 23:23:18 +08:00
Merge 1f5552f6d28246e2ab80ac0df422d9ec964aaa6b into d3ffc9985281dcf4d3bef604cce4e662b1a327a6
This commit is contained in:
commit
13f38c4e7e
@ -17,7 +17,10 @@ const (
|
||||
defaultStatus = http.StatusOK
|
||||
)
|
||||
|
||||
var errHijackAlreadyWritten = errors.New("gin: response body already written")
|
||||
var (
|
||||
errHijackAlreadyWritten = errors.New("gin: response body already written")
|
||||
errHijackNotSupported = errors.New("gin: underlying ResponseWriter does not support hijacking")
|
||||
)
|
||||
|
||||
// ResponseWriter ...
|
||||
type ResponseWriter interface {
|
||||
@ -109,20 +112,25 @@ func (w *responseWriter) Written() bool {
|
||||
|
||||
// Hijack implements the http.Hijacker interface.
|
||||
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)
|
||||
if w.size > 0 {
|
||||
return nil, nil, errHijackAlreadyWritten
|
||||
}
|
||||
hijacker, ok := w.ResponseWriter.(http.Hijacker)
|
||||
if !ok {
|
||||
return nil, nil, errHijackNotSupported
|
||||
}
|
||||
if w.size < 0 {
|
||||
w.size = 0
|
||||
}
|
||||
return w.ResponseWriter.(http.Hijacker).Hijack()
|
||||
return hijacker.Hijack()
|
||||
}
|
||||
|
||||
// CloseNotify implements the http.CloseNotifier interface.
|
||||
func (w *responseWriter) CloseNotify() <-chan bool {
|
||||
return w.ResponseWriter.(http.CloseNotifier).CloseNotify()
|
||||
if cn, ok := w.ResponseWriter.(http.CloseNotifier); ok {
|
||||
return cn.CloseNotify()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Flush implements the http.Flusher interface.
|
||||
|
||||
@ -113,17 +113,13 @@ func TestResponseWriterHijack(t *testing.T) {
|
||||
writer.reset(testWriter)
|
||||
w := ResponseWriter(writer)
|
||||
|
||||
assert.Panics(t, func() {
|
||||
_, _, err := w.Hijack()
|
||||
require.NoError(t, err)
|
||||
})
|
||||
assert.True(t, w.Written())
|
||||
// Hijack on a non-hijacker writer returns an error without panicking.
|
||||
_, _, err := w.Hijack()
|
||||
require.Error(t, err)
|
||||
// Capability check happens before state mutation, so Written() stays false on failure.
|
||||
assert.False(t, w.Written())
|
||||
|
||||
assert.Panics(t, func() {
|
||||
w.CloseNotify()
|
||||
})
|
||||
|
||||
w.Flush()
|
||||
assert.NotPanics(t, func() { w.Flush() })
|
||||
}
|
||||
|
||||
type mockHijacker struct {
|
||||
@ -315,3 +311,51 @@ func TestPusherWithoutPusher(t *testing.T) {
|
||||
pusher := w.Pusher()
|
||||
assert.Nil(t, pusher, "Expected pusher to be nil")
|
||||
}
|
||||
|
||||
// mockNonHijackerWriter implements only http.ResponseWriter.
|
||||
// It intentionally does NOT implement http.Hijacker, http.Flusher, or http.CloseNotifier.
|
||||
type mockNonHijackerWriter struct {
|
||||
headers http.Header
|
||||
}
|
||||
|
||||
func (m *mockNonHijackerWriter) Header() http.Header {
|
||||
if m.headers == nil {
|
||||
m.headers = make(http.Header)
|
||||
}
|
||||
return m.headers
|
||||
}
|
||||
|
||||
func (m *mockNonHijackerWriter) Write(b []byte) (int, error) {
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
func (m *mockNonHijackerWriter) WriteHeader(statusCode int) {}
|
||||
|
||||
func TestResponseWriterOptionalInterfaceFallbacks(t *testing.T) {
|
||||
w := &mockNonHijackerWriter{}
|
||||
rw := &responseWriter{}
|
||||
rw.reset(w)
|
||||
|
||||
t.Run("Flush does not panic without Flusher", func(t *testing.T) {
|
||||
assert.NotPanics(t, func() { rw.Flush() })
|
||||
})
|
||||
|
||||
t.Run("CloseNotify returns nil without CloseNotifier", func(t *testing.T) {
|
||||
var ch <-chan bool
|
||||
assert.NotPanics(t, func() { ch = rw.CloseNotify() })
|
||||
assert.Nil(t, ch)
|
||||
})
|
||||
|
||||
t.Run("Hijack returns error without Hijacker", func(t *testing.T) {
|
||||
rw.reset(w) // reset state so Written() starts false
|
||||
assert.NotPanics(t, func() {
|
||||
conn, buf, err := rw.Hijack()
|
||||
assert.Nil(t, conn)
|
||||
assert.Nil(t, buf)
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "does not support hijacking")
|
||||
})
|
||||
// Capability check happens before state mutation, so Written() stays false on failure.
|
||||
assert.False(t, rw.Written())
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user