/
http.go
139 lines (124 loc) · 3.21 KB
/
http.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
package handlers
import (
"bufio"
"github.com/felixge/httpsnoop"
"github.com/n-ask/fancylog"
"io"
"net"
"net/http"
)
type loggingHandler struct {
writer io.Writer
handler http.Handler
log fancylog.FancyHttpLog
}
// 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) Write(b []byte) (int, error) {
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) Hijack() (net.Conn, *bufio.ReadWriter, error) {
conn, rw, err := l.w.(http.Hijacker).Hijack()
if err == nil && l.status == 0 {
// The status will be StatusSwitchingProtocols if there was no error and
// WriteHeader has not been called yet
l.status = http.StatusSwitchingProtocols
}
return conn, rw, err
}
func (h loggingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logger, w := makeLogger(w)
url := *r.URL
h.handler.ServeHTTP(w, r)
if r.MultipartForm != nil {
r.MultipartForm.RemoveAll()
}
msg := map[string]any{}
if url.User != nil {
if name := url.User.Username(); name != "" {
msg["user"] = name
}
}
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
host = r.RemoteAddr
msg["host"] = host
}
msg["uri"] = r.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 r.ProtoMajor == 2 && r.Method == "CONNECT" {
msg["uri"] = r.Host
}
if msg["uri"] == "" {
msg["uri"] = url.RequestURI()
}
//msg["method"] = r.Method
msg["proto"] = r.Proto
msg["status"] = logger.Status()
msg["size"] = logger.Size()
if h.log.DebugHeaders() {
headers := map[string][]string{}
for header, v := range r.Header {
headers[header] = v
}
msg["headers"] = headers
}
switch r.Method {
case http.MethodGet:
h.log.GetMethod(msg, logger.status)
case http.MethodConnect:
h.log.ConnectMethod(msg, logger.status)
case http.MethodDelete:
h.log.DeleteMethod(msg, logger.status)
case http.MethodHead:
h.log.HeadMethod(msg, logger.status)
case http.MethodOptions:
h.log.OptionsMethod(msg, logger.status)
case http.MethodPost:
h.log.PostMethod(msg, logger.status)
case http.MethodPut:
h.log.PutMethod(msg, logger.status)
case http.MethodTrace:
h.log.TraceMethod(msg, logger.status)
default:
msg["method"] = r.Method
h.log.InfoMap(msg)
}
}
func makeLogger(w http.ResponseWriter) (*responseLogger, http.ResponseWriter) {
logger := &responseLogger{w: w, status: http.StatusOK}
return logger, httpsnoop.Wrap(w, httpsnoop.Hooks{
Write: func(httpsnoop.WriteFunc) httpsnoop.WriteFunc {
return logger.Write
},
WriteHeader: func(httpsnoop.WriteHeaderFunc) httpsnoop.WriteHeaderFunc {
return logger.WriteHeader
},
})
}
func LoggingHandler(log fancylog.FancyHttpLog, out io.Writer, h http.Handler) http.Handler {
return loggingHandler{
writer: out,
handler: h,
log: log,
}
}