-
Notifications
You must be signed in to change notification settings - Fork 56
/
log.go
146 lines (119 loc) · 3.95 KB
/
log.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
package serverHandler
import (
"mjpclab.dev/ghfs/src/serverLog"
"mjpclab.dev/ghfs/src/util"
"net/http"
"net/url"
"strconv"
)
type loggableResponseWriter struct {
http.ResponseWriter
request *http.Request
logger *serverLog.Logger
}
func (w loggableResponseWriter) WriteHeader(statusCode int) {
w.ResponseWriter.WriteHeader(statusCode)
go logRequest(w.logger, w.request, statusCode)
}
func logRequest(logger *serverLog.Logger, r *http.Request, statusCode int) {
code := strconv.Itoa(statusCode)
var unescapedUri []byte
unescapedLen := 0
unescapedStr, err := url.PathUnescape(r.RequestURI)
if err == nil && unescapedStr != r.RequestURI {
unescapedUri = util.EscapeControllingRune(unescapedStr)
if len(unescapedUri) > 0 {
unescapedLen = len(unescapedUri) + 5 // " <=> "
}
}
uri := util.EscapeControllingRune(r.RequestURI)
buf := serverLog.NewBuffer(3 + len(r.RemoteAddr) + len(code) + len(r.Method) + unescapedLen + len(uri))
buf = append(buf, []byte(r.RemoteAddr)...) // ~ 9-47 bytes, mainly 21 bytes
buf = append(buf, ' ') // 1 byte
buf = append(buf, []byte(code)...) // 3 bytes
buf = append(buf, ' ') // 1 byte
buf = append(buf, []byte(r.Method)...) // ~ 3-4 bytes
buf = append(buf, ' ') // 1 byte
if unescapedLen > 0 {
buf = append(buf, unescapedUri...)
buf = append(buf, ' ', '<', '=', '>', ' ') // 5 bytes
}
buf = append(buf, uri...)
logger.LogAccess(buf)
}
func (h *aliasHandler) logMutate(username, action, detail string, r *http.Request) {
if !h.logger.CanLogAccess() {
return
}
buf := serverLog.NewBuffer(6 + len(r.RemoteAddr) + len(username) + len(action) + len(detail))
buf = append(buf, []byte(r.RemoteAddr)...) // ~ 9-47 bytes, mainly 21 bytes
if len(username) > 0 {
buf = append(buf, ' ', '(') // 2 bytes
buf = append(buf, []byte(username)...)
buf = append(buf, ')') // 1 byte
}
buf = append(buf, ' ') // 1 byte
buf = append(buf, []byte(action)...) // ~ 5-6 bytes
buf = append(buf, ':', ' ') // 2 bytes
buf = append(buf, []byte(detail)...)
go h.logger.LogAccess(buf)
}
func (h *aliasHandler) logUpload(username, filename, fsPath string, r *http.Request) {
if !h.logger.CanLogAccess() {
return
}
buf := serverLog.NewBuffer(16 + len(r.RemoteAddr) + len(username) + len(filename) + len(fsPath))
buf = append(buf, []byte(r.RemoteAddr)...) // ~ 9-47 bytes, mainly 21 bytes
if len(username) > 0 {
buf = append(buf, ' ', '(') // 2 bytes
buf = append(buf, []byte(username)...)
buf = append(buf, ')') // 1 byte
}
buf = append(buf, []byte(" upload: ")...) // 9 bytes
buf = append(buf, []byte(filename)...)
buf = append(buf, []byte(" -> ")...) // 4 bytes
buf = append(buf, []byte(fsPath)...)
go h.logger.LogAccess(buf)
}
func (h *aliasHandler) logArchive(filename, relPath string, r *http.Request) {
if !h.logger.CanLogAccess() {
return
}
buf := serverLog.NewBuffer(19 + len(r.RemoteAddr) + len(filename) + len(relPath))
buf = append(buf, []byte(r.RemoteAddr)...) // ~ 9-47 bytes, mainly 21 bytes
buf = append(buf, []byte(" archive file: ")...) // 15 bytes
buf = append(buf, []byte(filename)...)
buf = append(buf, []byte(" <- ")...) // 4 bytes
buf = append(buf, []byte(relPath)...)
go h.logger.LogAccess(buf)
}
func (h *aliasHandler) logErrors(errs []error) (hasError bool) {
if len(errs) == 0 {
return false
}
if h.logger.CanLogError() {
go func(errs []error) {
for i := range errs {
errBytes := util.EscapeControllingRune(errs[i].Error())
buf := serverLog.NewBuffer(len(errBytes))
buf = append(buf, errBytes...)
h.logger.LogError(buf)
}
}(errs)
}
return true
}
func (h *aliasHandler) logError(err error) (hasError bool) {
if err == nil {
return false
}
if h.logger.CanLogError() {
go func(err error) {
errBytes := util.EscapeControllingRune(err.Error())
buf := serverLog.NewBuffer(len(errBytes))
buf = append(buf, errBytes...)
h.logger.LogError(buf)
}(err)
}
return true
}