-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
318 lines (298 loc) · 10.2 KB
/
handler.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package zlog
import (
"bytes"
"fmt"
"net"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"
"time"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/rs/xid"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/zenazn/goji/web/mutil"
"zliu.org/goutil"
)
// FromRequest gets the logger in the request's context.
// This is a shortcut for log.Ctx(r.Context())
func FromRequest(r *http.Request) *zerolog.Logger {
return log.Ctx(r.Context())
}
// NewHandler injects log into requests context.
func NewHandler(log zerolog.Logger) func(caddyhttp.Handler) caddyhttp.Handler {
return func(next caddyhttp.Handler) caddyhttp.Handler {
return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
// Create a copy of the logger (including internal context slice)
// to prevent data race when using UpdateContext.
l := log.With().Logger()
r = r.WithContext(l.WithContext(r.Context()))
return next.ServeHTTP(w, r)
})
}
}
// URLHandler adds the requested URL as a field to the context's logger
// using fieldKey as field key.
func URLHandler(fieldKey string) func(next caddyhttp.Handler) caddyhttp.Handler {
return func(next caddyhttp.Handler) caddyhttp.Handler {
return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
log := zerolog.Ctx(r.Context())
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, r.URL.String())
})
return next.ServeHTTP(w, r)
})
}
}
// MethodHandler adds the request method as a field to the context's logger
// using fieldKey as field key.
func MethodHandler(fieldKey string) func(next caddyhttp.Handler) caddyhttp.Handler {
return func(next caddyhttp.Handler) caddyhttp.Handler {
return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
log := zerolog.Ctx(r.Context())
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, r.Method)
})
return next.ServeHTTP(w, r)
})
}
}
// RequestHandler adds the request method and URL as a field to the context's logger
// using fieldKey as field key.
func RequestHandler(fieldKey string) func(next caddyhttp.Handler) caddyhttp.Handler {
return func(next caddyhttp.Handler) caddyhttp.Handler {
return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
log := zerolog.Ctx(r.Context())
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, r.Method+" "+r.URL.String())
})
return next.ServeHTTP(w, r)
})
}
}
// RemoteAddrHandler adds the request's remote address as a field to the context's logger
// using fieldKey as field key.
func RemoteAddrHandler(fieldKey string) func(next caddyhttp.Handler) caddyhttp.Handler {
return func(next caddyhttp.Handler) caddyhttp.Handler {
return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
if host, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
log := zerolog.Ctx(r.Context())
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, host)
})
}
return next.ServeHTTP(w, r)
})
}
}
// UserAgentHandler adds the request's user-agent as a field to the context's logger
// using fieldKey as field key.
func UserAgentHandler(fieldKey string) func(next caddyhttp.Handler) caddyhttp.Handler {
return func(next caddyhttp.Handler) caddyhttp.Handler {
return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
if ua := r.Header.Get("User-Agent"); ua != "" {
log := zerolog.Ctx(r.Context())
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, ua)
})
}
return next.ServeHTTP(w, r)
})
}
}
// RefererHandler adds the request's referer as a field to the context's logger
// using fieldKey as field key.
func RefererHandler(fieldKey string) func(next caddyhttp.Handler) caddyhttp.Handler {
return func(next caddyhttp.Handler) caddyhttp.Handler {
return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
if ref := r.Header.Get("Referer"); ref != "" {
log := zerolog.Ctx(r.Context())
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, ref)
})
}
return next.ServeHTTP(w, r)
})
}
}
type idKey struct{}
// AccessHandler returns a handler that call f after each request.
func AccessHandler(f func(r *http.Request, status, size int, duration time.Duration)) func(next caddyhttp.Handler) caddyhttp.Handler {
return func(next caddyhttp.Handler) caddyhttp.Handler {
return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
start := time.Now()
lw := mutil.WrapWriter(w)
err := next.ServeHTTP(lw, r)
f(r, lw.Status(), lw.BytesWritten(), time.Since(start))
return err
})
}
}
// IDFromRequest returns the unique id associated to the request if any.
func IDFromRequest(r *http.Request, headerName string) (id xid.ID, err error) {
if r == nil {
return
}
id, err = xid.FromString(r.Header.Get(headerName))
return
}
// RequestIDHandler returns a handler setting a unique id to the request which can
// be gathered using IDFromRequest(req). This generated id is added as a field to the
// logger using the passed fieldKey as field name. The id is also added as a response
// header if the headerName is not empty.
//
// The generated id is a URL safe base64 encoded mongo object-id-like unique id.
// Mongo unique id generation algorithm has been selected as a trade-off between
// size and ease of use: UUID is less space efficient and snowflake requires machine
// configuration.
func RequestIDHandler(fieldKey, headerName string) func(next caddyhttp.Handler) caddyhttp.Handler {
return func(next caddyhttp.Handler) caddyhttp.Handler {
return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
id, err := IDFromRequest(r, headerName)
if err != nil {
id = xid.New()
}
if fieldKey != "" {
log := zerolog.Ctx(ctx)
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, id.String())
})
}
if headerName != "" {
r.Header.Set(headerName, id.String())
}
return next.ServeHTTP(w, r)
})
}
}
func hashPostRequest(req string) string {
parts := strings.Split(req, "\r\n\r\n")
if len(parts) != 2 {
return req
}
postData, err := url.ParseQuery(parts[1])
if err != nil {
return req
}
urlVal := url.Values{}
for k, _ := range postData {
v := postData.Get(k)
if len(v) > 1000 {
md5 := goutil.MD5(v)
urlVal.Set(fmt.Sprintf("md5-%s", k), md5)
// store the raw value in c.hashStore
if c.hashStore != nil {
line, _ := goutil.JSONMarshal(map[string]string{"field": k, "md5": md5, "content": v})
c.hashStore.WriteLine(line)
}
} else {
urlVal.Set(k, v)
}
}
ret := fmt.Sprintf("%s\r\n\r\n%s", parts[0], urlVal.Encode())
return ret
}
func DumpRequestHandler(fieldKey string) func(next caddyhttp.Handler) caddyhttp.Handler {
return func(next caddyhttp.Handler) caddyhttp.Handler {
return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
log := zerolog.Ctx(r.Context())
log.UpdateContext(func(ctx zerolog.Context) zerolog.Context {
res, err := httputil.DumpRequest(r, true)
var msg string
if err != nil {
msg = err.Error()
} else {
if c.hashStore == nil {
msg = string(res)
} else {
msg = hashPostRequest(string(res))
}
}
return ctx.Str(fieldKey, msg)
})
return next.ServeHTTP(w, r)
})
}
}
// HeaderHandler adds the request's headerName from Header as a field to the
// context's logger using headerName as field key.
func HeaderHandler(headerName string) func(next caddyhttp.Handler) caddyhttp.Handler {
return func(next caddyhttp.Handler) caddyhttp.Handler {
return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
if v := r.Header.Get(headerName); v != "" {
log := zerolog.Ctx(r.Context())
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(headerName, v)
})
}
return next.ServeHTTP(w, r)
})
}
}
func DumpResponseHandler(fieldKey string) func(next caddyhttp.Handler) caddyhttp.Handler {
return func(next caddyhttp.Handler) caddyhttp.Handler {
return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
nw := NewRespProxyWriter(w)
err := next.ServeHTTP(nw, r)
var b bytes.Buffer
nw.SourceHeader.WriteSubset(&b, nil)
log := zerolog.Ctx(r.Context())
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, ResponseLog{
Request: r,
StatusCode: nw.Code,
Body: string(nw.Body),
Header: string(b.Bytes())}.DumpResponse())
})
return err
})
}
}
func DelResponseHeaderHandler(headerName string) func(next caddyhttp.Handler) caddyhttp.Handler {
return func(next caddyhttp.Handler) caddyhttp.Handler {
return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
nw := NewRespProxyWriter(w)
nw.delHeader(headerName)
return next.ServeHTTP(nw, r)
})
}
}
func ResponseHeaderHandler(headerName, valType string) func(next caddyhttp.Handler) caddyhttp.Handler {
return func(next caddyhttp.Handler) caddyhttp.Handler {
return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
nw := NewRespProxyWriter(w)
err := next.ServeHTTP(nw, r)
if v := nw.SourceHeader.Get(headerName); v != "" {
log := zerolog.Ctx(r.Context())
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
switch valType {
case "bool":
if val, err := strconv.ParseBool(v); err == nil {
return c.Bool(headerName, val)
}
case "float":
if val, err := strconv.ParseFloat(v, 64); err == nil {
return c.Float64(headerName, val)
}
case "int":
if val, err := strconv.ParseInt(v, 10, 64); err == nil {
return c.Int64(headerName, val)
}
case "uint":
if val, err := strconv.ParseUint(v, 10, 64); err == nil {
return c.Uint64(headerName, val)
}
case "str":
return c.Str(headerName, v)
}
// if strconv convert failed, saved as str by default
return c.Str(headerName, v)
})
}
return err
})
}
}