@@ -24,34 +24,14 @@ import (
2424)
2525
2626// ResponseWriterWrapper wraps an underlying ResponseWriter and
27- // promotes its Pusher/Flusher/Hijacker methods as well. To use
28- // this type, embed a pointer to it within your own struct type
29- // that implements the http.ResponseWriter interface, then call
30- // methods on the embedded value. You can make sure your type
31- // wraps correctly by asserting that it implements the
32- // HTTPInterfaces interface.
27+ // promotes its Pusher method as well. To use this type, embed
28+ // a pointer to it within your own struct type that implements
29+ // the http.ResponseWriter interface, then call methods on the
30+ // embedded value.
3331type ResponseWriterWrapper struct {
3432 http.ResponseWriter
3533}
3634
37- // Hijack implements http.Hijacker. It simply calls the underlying
38- // ResponseWriter's Hijack method if there is one, or returns
39- // ErrNotImplemented otherwise.
40- func (rww * ResponseWriterWrapper ) Hijack () (net.Conn , * bufio.ReadWriter , error ) {
41- if hj , ok := rww .ResponseWriter .(http.Hijacker ); ok {
42- return hj .Hijack ()
43- }
44- return nil , nil , ErrNotImplemented
45- }
46-
47- // Flush implements http.Flusher. It simply calls the underlying
48- // ResponseWriter's Flush method if there is one.
49- func (rww * ResponseWriterWrapper ) Flush () {
50- if f , ok := rww .ResponseWriter .(http.Flusher ); ok {
51- f .Flush ()
52- }
53- }
54-
5535// Push implements http.Pusher. It simply calls the underlying
5636// ResponseWriter's Push method if there is one, or returns
5737// ErrNotImplemented otherwise.
@@ -62,29 +42,18 @@ func (rww *ResponseWriterWrapper) Push(target string, opts *http.PushOptions) er
6242 return ErrNotImplemented
6343}
6444
65- // ReadFrom implements io.ReaderFrom. It simply calls the underlying
66- // ResponseWriter's ReadFrom method if there is one, otherwise it defaults
67- // to io.Copy.
45+ // ReadFrom implements io.ReaderFrom. It simply calls io.Copy,
46+ // which uses io.ReaderFrom if available.
6847func (rww * ResponseWriterWrapper ) ReadFrom (r io.Reader ) (n int64 , err error ) {
69- if rf , ok := rww .ResponseWriter .(io.ReaderFrom ); ok {
70- return rf .ReadFrom (r )
71- }
7248 return io .Copy (rww .ResponseWriter , r )
7349}
7450
75- // Unwrap returns the underlying ResponseWriter.
51+ // Unwrap returns the underlying ResponseWriter, necessary for
52+ // http.ResponseController to work correctly.
7653func (rww * ResponseWriterWrapper ) Unwrap () http.ResponseWriter {
7754 return rww .ResponseWriter
7855}
7956
80- // HTTPInterfaces mix all the interfaces that middleware ResponseWriters need to support.
81- type HTTPInterfaces interface {
82- http.ResponseWriter
83- http.Pusher
84- http.Flusher
85- http.Hijacker
86- }
87-
8857// ErrNotImplemented is returned when an underlying
8958// ResponseWriter does not implement the required method.
9059var ErrNotImplemented = fmt .Errorf ("method not implemented" )
@@ -262,7 +231,8 @@ func (rr *responseRecorder) WriteResponse() error {
262231}
263232
264233func (rr * responseRecorder ) Hijack () (net.Conn , * bufio.ReadWriter , error ) {
265- conn , brw , err := rr .ResponseWriterWrapper .Hijack ()
234+ //nolint:bodyclose
235+ conn , brw , err := http .NewResponseController (rr .ResponseWriterWrapper ).Hijack ()
266236 if err != nil {
267237 return nil , nil , err
268238 }
@@ -294,7 +264,7 @@ func (hc *hijackedConn) ReadFrom(r io.Reader) (int64, error) {
294264// responses instead of writing them to the client. See
295265// docs for NewResponseRecorder for proper usage.
296266type ResponseRecorder interface {
297- HTTPInterfaces
267+ http. ResponseWriter
298268 Status () int
299269 Buffer () * bytes.Buffer
300270 Buffered () bool
@@ -309,12 +279,13 @@ type ShouldBufferFunc func(status int, header http.Header) bool
309279
310280// Interface guards
311281var (
312- _ HTTPInterfaces = (* ResponseWriterWrapper )(nil )
313- _ ResponseRecorder = (* responseRecorder )(nil )
282+ _ http. ResponseWriter = (* ResponseWriterWrapper )(nil )
283+ _ ResponseRecorder = (* responseRecorder )(nil )
314284
315285 // Implementing ReaderFrom can be such a significant
316286 // optimization that it should probably be required!
317287 // see PR #5022 (25%-50% speedup)
318288 _ io.ReaderFrom = (* ResponseWriterWrapper )(nil )
319289 _ io.ReaderFrom = (* responseRecorder )(nil )
290+ _ io.ReaderFrom = (* hijackedConn )(nil )
320291)
0 commit comments