mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-07 12:48:16 +08:00
Merge 3317f91a1b222bf00faf059f1bca53996c47549f into d3ffc9985281dcf4d3bef604cce4e662b1a327a6
This commit is contained in:
commit
922b671476
@ -1328,10 +1328,16 @@ func (c *Context) SSEvent(name string, message any) {
|
||||
func (c *Context) Stream(step func(w io.Writer) bool) bool {
|
||||
w := c.Writer
|
||||
clientGone := w.CloseNotify()
|
||||
var requestGone <-chan struct{}
|
||||
if c.Request != nil {
|
||||
requestGone = c.Request.Context().Done()
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case <-clientGone:
|
||||
return true
|
||||
case <-requestGone:
|
||||
return true
|
||||
default:
|
||||
keepOpen := step(w)
|
||||
w.Flush()
|
||||
|
||||
@ -3078,6 +3078,25 @@ func TestContextStreamWithClientGone(t *testing.T) {
|
||||
assert.Equal(t, "test", w.Body.String())
|
||||
}
|
||||
|
||||
func TestContextStreamWithRequestContextDone(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
reqCtx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/", nil).WithContext(reqCtx)
|
||||
|
||||
disconnected := c.Stream(func(writer io.Writer) bool {
|
||||
_, err := writer.Write([]byte("test"))
|
||||
require.NoError(t, err)
|
||||
|
||||
return true
|
||||
})
|
||||
|
||||
assert.True(t, disconnected)
|
||||
assert.Empty(t, w.Body.String())
|
||||
}
|
||||
|
||||
func TestContextResetInHandler(t *testing.T) {
|
||||
w := CreateTestResponseRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
@ -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