-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlog.go
269 lines (233 loc) · 5.69 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
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
/* Copyright (C) 2015 by Alexandru Cojocaru */
/* This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
// Package logparse parses a log entry in the most common formats.
package logparse // import "xojoc.pw/logparse"
import (
"bufio"
"fmt"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"xojoc.pw/useragent"
)
const timeLayout = "02/Jan/2006:15:04:05 -0700"
// An Entry represents a log entry.
type Entry struct {
// The IP of the client which made the request (nil if unknown).
Host net.IP
// The username of the logged in user making the request (empty if anonymous).
User string
// The time the request was made (zero value if unknown, check with IsZero).
Time time.Time
// The HTTP request line from the client (nil if unknown).
Request *http.Request
// The HTTP status code returned to the client (-1 if unknown).
Status int
// The size in bytes of the data sent to the client (0 if no data sent).
Bytes int
// The URL of the host the client comes from (nil if unknown).
Referer *url.URL
// The user agent of the client (nil if unknown).
UserAgent *useragent.UserAgent
}
// Formats the Entry e in the combined log format.
func (e *Entry) String() string {
s := ""
if e.Host == nil {
s += "-"
} else {
s += e.Host.String()
}
s += " "
s += "- "
if e.User == "" {
s += "-"
} else {
s += e.User
}
s += " "
if e.Time.IsZero() {
s += "-"
} else {
s += "[" + e.Time.Format(timeLayout) + "]"
}
s += " "
if e.Request == nil {
s += "-"
} else {
s += fmt.Sprintf(`"%s %s %s"`, e.Request.Method, e.Request.URL.RequestURI(), e.Request.Proto)
}
s += " "
if e.Status < 0 {
s += "-"
} else {
s += strconv.Itoa(e.Status)
}
s += " "
s += strconv.Itoa(e.Bytes)
s += " "
if e.Referer == nil {
s += "-"
} else {
s += `"` + e.Referer.String() + `"`
}
s += " "
if e.UserAgent == nil {
s += "-"
} else {
s += `"` + e.UserAgent.Original + `"`
}
return s
}
func nextField(l *lex, sep string) (string, error) {
f, ok := l.span(sep)
if !ok {
return "", fmt.Errorf("%q: cannot find separator %q", l.s[l.p:], sep)
}
return f, nil
}
func expect(l *lex, sep rune) error {
if !l.match(string(sep)) {
r := l.next()
if r == eof {
return fmt.Errorf("expected %q but got EOF", sep)
} else {
return fmt.Errorf("%d: expected %q but got %q", l.ColumnNumber()-1, sep, r)
}
}
return nil
}
// FIXME: error checks are too noisy
func common(l *lex) (*Entry, error) {
e := &Entry{}
ip, err := nextField(l, " ")
if err != nil {
return nil, err
}
e.Host = net.ParseIP(ip)
if e.Host == nil {
return nil, fmt.Errorf("cannot parse IP %q", ip)
}
_, err = nextField(l, " ")
if err != nil {
return nil, err
}
e.User, err = nextField(l, " ")
if err != nil {
return nil, err
}
err = expect(l, '[')
if err != nil {
return nil, err
}
t, err := nextField(l, "] ")
if err != nil {
return nil, err
}
e.Time, err = time.Parse(timeLayout, t)
if err != nil {
return nil, err
}
err = expect(l, '"')
if err != nil {
return nil, err
}
r, err := nextField(l, `" `)
if err != nil {
return nil, err
}
e.Request, err = http.ReadRequest(bufio.NewReader(strings.NewReader(r + "\r\n\r\n")))
if err != nil {
return nil, err
}
s, err := nextField(l, " ")
if err != nil {
return nil, err
}
e.Status, err = strconv.Atoi(s)
if err != nil {
return nil, err
}
b, err := nextField(l, " ")
if err != nil {
b = l.s[l.p:]
// return nil, err
}
e.Bytes, err = strconv.Atoi(b)
if err != nil {
return nil, err
}
return e, nil
}
// Common parses a log line containing a log entry in the common log format.
//
// An entry in the common log format has the form:
// Host - User Time Request Status Bytes
// where:
// Host is the ip of the client which made the request.
// - this field never is used.
// User is the name of the logged in user doing the request.
// Time is the date/time/zone the request was made.
// Request is the HTTP request line from the client.
// Status is the status code returned to the client.
// Bytes is the size in bytes of the data sent to the client.
func Common(line string) (*Entry, error) {
l := newLex(line)
e, err := common(l)
return e, err
}
// Combined parses a log line containing a log entry in the combined log format.
//
// An entry in the combined log format has the form:
// Host - User Time Request Status Bytes Referer UserAgent
// basicaly it's the same as the common log format with the added fields:
// Referer the URL of the host the client comes from
// UserAgent the user agent of the client
func Combined(line string) (*Entry, error) {
l := newLex(line)
e, err := common(l)
if err != nil {
return nil, err
}
err = expect(l, '"')
if err != nil {
return nil, err
}
ref, err := nextField(l, `" `)
if err != nil {
return nil, err
}
if ref != `-` {
e.Referer, err = url.ParseRequestURI(ref)
if err != nil {
return nil, err
}
}
err = expect(l, '"')
if err != nil {
return nil, err
}
uas, err := nextField(l, `"`)
if err != nil {
return nil, err
}
e.UserAgent = useragent.Parse(uas)
return e, nil
}
type ExtendedDirective struct {
}
func (x *ExtendedDirective) Extended(line string) *Entry {
return nil
}