/
request_logger.go
197 lines (175 loc) · 5.13 KB
/
request_logger.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// This file is part of graze/golang-service
//
// Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
//
// license: https://github.com/graze/golang-service/blob/master/LICENSE
// link: https://github.com/graze/golang-service
package handlers
import (
"bufio"
"fmt"
"net"
"net/http"
"net/url"
"strings"
"time"
)
// LogServeHTTP creates a LoggingResponseWriter from `w` if applicable and calls `caller` with the request status, size,
// time and duration
func LogServeHTTP(w http.ResponseWriter, req *http.Request, handler http.Handler, caller func(w LoggingResponseWriter, req *http.Request, url url.URL, ts time.Time, dur time.Duration, status, size int)) {
t := time.Now().UTC()
logger := MakeLogger(w)
url := *req.URL
handler.ServeHTTP(logger, req)
dur := time.Now().UTC().Sub(t)
caller(logger, req, url, t, dur, logger.Status(), logger.Size())
}
// MakeLogger creates a LoggingResponseWriter from a http.ResponseWriter
//
// The loggingResponsWriter adds status field and the size of the response to the LoggingResponseWriter
func MakeLogger(w http.ResponseWriter) LoggingResponseWriter {
if logResponse, ok := w.(LoggingResponseWriter); ok {
return logResponse
}
var logger LoggingResponseWriter = &responseLogger{w: w}
if _, ok := w.(http.Hijacker); ok {
logger = &hijackLogger{responseLogger{w: w}}
}
h, ok1 := logger.(http.Hijacker)
c, ok2 := w.(http.CloseNotifier)
if ok1 && ok2 {
return hijackCloseNotifier{logger, h, c}
}
if ok2 {
return &closeNotifyWriter{logger, c}
}
return logger
}
// LoggingResponseWriter wraps a `http.ResponseWriter` `http.Flusher` and stores a logging context and status/size info
type LoggingResponseWriter interface {
http.ResponseWriter
http.Flusher
Status() int
Size() int
}
// responseLogger is wrapper of http.ResponseWriter that keeps track of its HTTP
// status code and body size
type responseLogger struct {
w http.ResponseWriter
status int
size int
}
func (l *responseLogger) Header() http.Header {
return l.w.Header()
}
func (l *responseLogger) Write(b []byte) (int, error) {
if l.status == 0 {
// The status will be StatusOK if WriteHeader has not been called yet
l.status = http.StatusOK
}
size, err := l.w.Write(b)
l.size += size
return size, err
}
func (l *responseLogger) WriteHeader(s int) {
l.w.WriteHeader(s)
l.status = s
}
func (l *responseLogger) Status() int {
return l.status
}
func (l *responseLogger) Size() int {
return l.size
}
func (l *responseLogger) Flush() {
f, ok := l.w.(http.Flusher)
if ok {
f.Flush()
}
}
type hijackLogger struct {
responseLogger
}
func (l *hijackLogger) Hijack() (net.Conn, *bufio.ReadWriter, error) {
h := l.responseLogger.w.(http.Hijacker)
conn, rw, err := h.Hijack()
if err == nil && l.responseLogger.status == 0 {
// The status will be StatusSwitchingProtocols if there was no error and
// WriteHeader has not been called yet
l.responseLogger.status = http.StatusSwitchingProtocols
}
return conn, rw, err
}
type closeNotifyWriter struct {
LoggingResponseWriter
http.CloseNotifier
}
type hijackCloseNotifier struct {
LoggingResponseWriter
http.Hijacker
http.CloseNotifier
}
// parseURI takes a request and url and returns a RFC7540 compliant uri
func parseURI(req *http.Request, url url.URL) (uri string) {
uri = req.RequestURI
// Requests using the CONNECT method over HTTP/2.0 must use
// the authority field (aka r.Host) to identify the target.
// Refer: https://httpwg.github.io/specs/rfc7540.html#CONNECT
if req.ProtoMajor == 2 && req.Method == "CONNECT" {
uri = req.Host
}
if uri == "" {
uri = url.RequestURI()
}
return
}
// uriPath extracts the path from a request uri
func uriPath(req *http.Request, url url.URL) (uri string) {
uri = url.EscapedPath()
// Requests using the CONNECT method over HTTP/2.0 must use
// the authority field (aka r.Host) to identify the target.
// Refer: https://httpwg.github.io/specs/rfc7540.html#CONNECT
if req.ProtoMajor == 2 && req.Method == "CONNECT" {
uri = req.Host
}
if uri == "" {
parsed, err := url.Parse(req.RequestURI)
if err != nil {
uri = "unknown"
} else {
uri = parsed.EscapedPath()
}
}
if uri == "" {
uri = "/"
}
return
}
// getUserIP takes a request and extracts the users ip, using `X-Forwarded-For` and `X-Real-Ip` headers
// then the `req.RemoteAddr`
func getUserIP(req *http.Request) (net.IP, error) {
for _, h := range []string{"X-Forwarded-For", "X-Real-Ip"} {
if req.Header.Get(h) != "" {
for _, ip := range strings.Split(req.Header.Get(h), ",") {
// header can contain spaces too, strip those out.
userIP := net.ParseIP(strings.Replace(ip, " ", "", -1))
if userIP == nil {
return nil, fmt.Errorf("getUserIP: %q is not a valid IP", ip)
}
return userIP, nil
}
}
}
ip, _, err := net.SplitHostPort(req.RemoteAddr)
if err != nil {
return nil, fmt.Errorf("getUserIP: %q is not a valid IP:Port", req.RemoteAddr)
}
userIP := net.ParseIP(ip)
if userIP == nil {
return nil, fmt.Errorf("getUserIP: %q is not a valid IP:port", req.RemoteAddr)
}
return userIP, nil
}