This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathwrapped_responsewriter_go18.go
63 lines (50 loc) · 1.87 KB
/
wrapped_responsewriter_go18.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// +build go1.8
package httpwares
import (
"bufio"
"io"
"net"
"net/http"
)
// newWrappedResponseWriter handles the four different methods of upgrading a
// http.ResponseWriter to delegator.
func newWrappedResponseWriter(w http.ResponseWriter) WrappedResponseWriter {
wrapped := &wrappedResponseWriter{ResponseWriter: w}
_, isCloseNotifier := w.(http.CloseNotifier)
_, isFlusher := w.(http.Flusher)
_, isHijacker := w.(http.Hijacker)
_, isPusher := w.(http.Pusher)
_, isReaderFrom := w.(io.ReaderFrom)
// Check for the four most common combination of interfaces a
// http.ResponseWriter might implement.
if !isHijacker && isPusher && isCloseNotifier { // http2.responseWriter (http 2.0)
return &http2WrappedResponseWriter{wrapped}
} else if isCloseNotifier && isFlusher && isHijacker && isReaderFrom { // http.response (http 1.1)
return &http1WrappedResponseWriter{wrapped}
}
return wrapped
}
type http2WrappedResponseWriter struct {
*wrappedResponseWriter
}
func (w *http2WrappedResponseWriter) Flush() {
w.wrappedResponseWriter.ResponseWriter.(http.Flusher).Flush()
}
func (w *http2WrappedResponseWriter) CloseNotify() <-chan bool {
return w.wrappedResponseWriter.ResponseWriter.(http.CloseNotifier).CloseNotify()
}
func (w *http2WrappedResponseWriter) Push(target string, opts *http.PushOptions) error {
return w.wrappedResponseWriter.ResponseWriter.(http.Pusher).Push(target, opts)
}
type http1WrappedResponseWriter struct {
*wrappedResponseWriter
}
func (w *http1WrappedResponseWriter) Flush() {
w.wrappedResponseWriter.ResponseWriter.(http.Flusher).Flush()
}
func (w *http1WrappedResponseWriter) CloseNotify() <-chan bool {
return w.wrappedResponseWriter.ResponseWriter.(http.CloseNotifier).CloseNotify()
}
func (w *http1WrappedResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return w.wrappedResponseWriter.ResponseWriter.(http.Hijacker).Hijack()
}