| 1 | package zhttp |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "fmt" |
| 6 | "net" |
| 7 | "net/http" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | // Logger wraps a ResponseWriter and records the resulting status code |
| 12 | // and how many bytes are written |
| 13 | type Logger struct { |
| 14 | http.ResponseWriter |
| 15 | length int64 |
| 16 | status int |
| 17 | started time.Time |
| 18 | Now func() time.Time |
| 19 | } |
| 20 | |
| 21 | // Write implements ResponseWriter |
| 22 | func (l *Logger) Write(d []byte) (size int, err error) { |
| 23 | if l.status == 0 { |
| 24 | l.WriteHeader(http.StatusOK) |
| 25 | } |
| 26 | size, err = l.ResponseWriter.Write(d) |
| 27 | l.length += int64(size) |
| 28 | return |
| 29 | } |
| 30 | |
| 31 | // WriteHeader implements ResponseWriter |
| 32 | func (l *Logger) WriteHeader(status int) { |
| 33 | // suppress duplicate WriteHeader calls, but do save the status code |
| 34 | if l.status == 0 { |
| 35 | l.ResponseWriter.WriteHeader(status) |
| 36 | l.started = l.Now() |
| 37 | } |
| 38 | l.status = status |
| 39 | } |
| 40 | |
| 41 | // Flush wraps a nested Flusher |
| 42 | func (l *Logger) Flush() { |
| 43 | if flusher, ok := l.ResponseWriter.(http.Flusher); ok { |
| 44 | flusher.Flush() |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // Hijack wraps a nested Hijacker |
| 49 | func (l *Logger) Hijack() (net.Conn, *bufio.ReadWriter, error) { |
| 50 | if h, ok := l.ResponseWriter.(http.Hijacker); ok { |
| 51 | conn, rw, err := h.Hijack() |
| 52 | if err == nil && l.status == 0 { |
| 53 | l.status = http.StatusSwitchingProtocols |
| 54 | } |
| 55 | if hc, ok := conn.(halfCloser); ok { |
| 56 | // more featureful for tcp.Conn common case |
| 57 | conn = halfCloseLogger{halfCloser: hc, rl: l} |
| 58 | } else { |
| 59 | conn = hijackLogger{Conn: conn, rl: l} |
| 60 | } |
| 61 | return conn, rw, err |
| 62 | } |
| 63 | return nil, nil, fmt.Errorf("Hijacker interface not supported by type %T", l.ResponseWriter) |
| 64 | } |
| 65 | |
| 66 | // CloseNotify wraps a nested CloseNotifier |
| 67 | func (l *Logger) CloseNotify() <-chan bool { |
| 68 | //nolint // provided for backwards compatibility |
| 69 | if cn, ok := l.ResponseWriter.(http.CloseNotifier); ok { |
| 70 | return cn.CloseNotify() |
| 71 | } |
| 72 | return make(chan bool) |
| 73 | } |
| 74 | |
| 75 | // Push wraps a nested Pusher |
| 76 | func (l *Logger) Push(target string, opts *http.PushOptions) error { |
| 77 | if p, ok := l.ResponseWriter.(http.Pusher); ok { |
| 78 | return p.Push(target, opts) |
| 79 | } |
| 80 | return fmt.Errorf("Pusher interface not supported by type %T", l.ResponseWriter) |
| 81 | } |
| 82 | |
| 83 | // Length returns the number of bytes written |
| 84 | func (l *Logger) Length() int64 { |
| 85 | return l.length |
| 86 | } |
| 87 | |
| 88 | // Status returns the response status |
| 89 | func (l *Logger) Status() int { |
| 90 | if l.status == 0 { |
| 91 | return http.StatusOK |
| 92 | } |
| 93 | return l.status |
| 94 | } |
| 95 | |
| 96 | // Started returns the time at which headers were written |
| 97 | func (l *Logger) Started() time.Time { |
| 98 | if l.started.IsZero() { |
| 99 | return l.Now() |
| 100 | } |
| 101 | return l.started |
| 102 | } |
| 103 | |
| 104 | // hijackLogger wraps a hijacked connection in order to enable WriteErrorReason |
| 105 | // to set the status of the request for logging purposes |
| 106 | type hijackLogger struct { |
| 107 | net.Conn |
| 108 | rl *Logger |
| 109 | } |
| 110 | |
| 111 | // SetStatus records the final status of a hijacked connection |
| 112 | func (h hijackLogger) SetStatus(status int, length int64) { |
| 113 | h.rl.status = status |
| 114 | h.rl.length = length |
| 115 | } |
| 116 | |
| 117 | type halfCloser interface { |
| 118 | net.Conn |
| 119 | CloseRead() error |
| 120 | CloseWrite() error |
| 121 | } |
| 122 | |
| 123 | // halfCloseLogger wraps a hijacked connection in order to enable |
| 124 | // WriteErrorReason to set the status of the request for logging purposes |
| 125 | type halfCloseLogger struct { |
| 126 | halfCloser |
| 127 | rl *Logger |
| 128 | } |
| 129 | |
| 130 | // SetStatus records the final status of a hijacked connection |
| 131 | func (h halfCloseLogger) SetStatus(status int, length int64) { |
| 132 | h.rl.status = status |
| 133 | h.rl.length = length |
| 134 | } |