@@ -24,34 +24,14 @@ import (
24
24
)
25
25
26
26
// 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.
33
31
type ResponseWriterWrapper struct {
34
32
http.ResponseWriter
35
33
}
36
34
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
-
55
35
// Push implements http.Pusher. It simply calls the underlying
56
36
// ResponseWriter's Push method if there is one, or returns
57
37
// ErrNotImplemented otherwise.
@@ -62,29 +42,18 @@ func (rww *ResponseWriterWrapper) Push(target string, opts *http.PushOptions) er
62
42
return ErrNotImplemented
63
43
}
64
44
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.
68
47
func (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
- }
72
48
return io .Copy (rww .ResponseWriter , r )
73
49
}
74
50
75
- // Unwrap returns the underlying ResponseWriter.
51
+ // Unwrap returns the underlying ResponseWriter, necessary for
52
+ // http.ResponseController to work correctly.
76
53
func (rww * ResponseWriterWrapper ) Unwrap () http.ResponseWriter {
77
54
return rww .ResponseWriter
78
55
}
79
56
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
-
88
57
// ErrNotImplemented is returned when an underlying
89
58
// ResponseWriter does not implement the required method.
90
59
var ErrNotImplemented = fmt .Errorf ("method not implemented" )
@@ -262,7 +231,8 @@ func (rr *responseRecorder) WriteResponse() error {
262
231
}
263
232
264
233
func (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 ()
266
236
if err != nil {
267
237
return nil , nil , err
268
238
}
@@ -294,7 +264,7 @@ func (hc *hijackedConn) ReadFrom(r io.Reader) (int64, error) {
294
264
// responses instead of writing them to the client. See
295
265
// docs for NewResponseRecorder for proper usage.
296
266
type ResponseRecorder interface {
297
- HTTPInterfaces
267
+ http. ResponseWriter
298
268
Status () int
299
269
Buffer () * bytes.Buffer
300
270
Buffered () bool
@@ -309,12 +279,13 @@ type ShouldBufferFunc func(status int, header http.Header) bool
309
279
310
280
// Interface guards
311
281
var (
312
- _ HTTPInterfaces = (* ResponseWriterWrapper )(nil )
313
- _ ResponseRecorder = (* responseRecorder )(nil )
282
+ _ http. ResponseWriter = (* ResponseWriterWrapper )(nil )
283
+ _ ResponseRecorder = (* responseRecorder )(nil )
314
284
315
285
// Implementing ReaderFrom can be such a significant
316
286
// optimization that it should probably be required!
317
287
// see PR #5022 (25%-50% speedup)
318
288
_ io.ReaderFrom = (* ResponseWriterWrapper )(nil )
319
289
_ io.ReaderFrom = (* responseRecorder )(nil )
290
+ _ io.ReaderFrom = (* hijackedConn )(nil )
320
291
)
0 commit comments