Skip to content
Myr

fix(response): panic on Hijack/CloseNotify when wrapper unsupported (#4645)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* response_writer: don't panic on Hijack/CloseNotify when wrapper unsupported Closes #4638. http.TimeoutHandler's writer doesn't implement http.Hijacker/CloseNotifier; mirror Flush's graceful degradation. * response_writer: keep Written() false when Hijack is unsupported Signed-off-by: Sai Asish Y <say.apm35@gmail.com> ---------

authored by Sai Asish Y · committed by GitHub · parent 8d0468f · Browse files · 2 files+20 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>

Diff stats

2 files+20 added−10 removed

  • other:+20 added−10 removed

2 files+20 added−10 removed

modified:response_writer_test.go+11 added-8 removed
111 unmodified lines
112 writer := &responseWriter{}113 writer.reset(testWriter)114 w := ResponseWriter(writer)115116 assert.Panics(t, func() {117 _, _, err := w.Hijack()118 require.NoError(t, err)119 })120 assert.True(t, w.Written())116 // httptest.ResponseRecorder doesn't implement http.Hijacker; return117 // http.ErrNotSupported instead of panicking (#4638). On unsupported the118 // writer state stays untouched so the handler can still emit a normal119 // HTTP response as a fallback.120 conn, buf, err := w.Hijack()121 assert.Nil(t, conn)122 assert.Nil(t, buf)123 require.ErrorIs(t, err, http.ErrNotSupported)124 assert.False(t, w.Written())125122 assert.Panics(t, func() {123 w.CloseNotify()124 })126 // CloseNotify on a non-CloseNotifier returns nil instead of panicking.127 assert.Nil(t, w.CloseNotify())128129 w.Flush()130}131
189 unmodified lines
modified:response_writer.go+9 added-2 removed
112 unmodified lines
113 // but not after body data is written (size > 0). For compatibility with websocket libraries (e.g., github.com/coder/websocket)114 if w.size > 0 {115 return nil, nil, errHijackAlreadyWritten116 }117 hijacker, ok := w.ResponseWriter.(http.Hijacker)118 if !ok {119 return nil, nil, http.ErrNotSupported120 }121 if w.size < 0 {122 w.size = 0123 }120 return w.ResponseWriter.(http.Hijacker).Hijack()124 return hijacker.Hijack()125}126127// CloseNotify implements the http.CloseNotifier interface.128func (w *responseWriter) CloseNotify() <-chan bool {125 return w.ResponseWriter.(http.CloseNotifier).CloseNotify()129 if cn, ok := w.ResponseWriter.(http.CloseNotifier); ok {130 return cn.CloseNotify()131 }132 return nil133}134135// Flush implements the http.Flusher interface.136func (w *responseWriter) Flush() {
12 unmodified lines