/
apache.go
136 lines (116 loc) · 3.37 KB
/
apache.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
package useful
import (
"bufio"
"errors"
"io"
"net"
"net/http"
"strings"
"time"
)
// ErrUnHijackable indicates an unhijackable connection. I.e., (one of)
// the underlying http.ResponseWriter(s) doesn't support the http.Hijacker
// interface.
var ErrUnHijackable = errors.New("A(n) underlying ResponseWriter doesn't support the http.Hijacker interface")
// These format strings correspond with the log formats described in
// https://httpd.apache.org/docs/2.2/mod/mod_log_config.html
var (
// CommonLog is "%h %l %u %t \"%r\" %>s %b"
CommonLog commonLog = "%s - - [%s] \"%s\" %d %d\n"
// CommonLogWithVHost is "%v %h %l %u %t \"%r\" %>s %b"
CommonLogWithVHost commonLogWithVHost = "- %s - - [%s] \"%s\" %d %d\n"
// NCSALog is
// "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
NCSALog ncsaLog = "%s - - [%s] \"%s\" %d %d \"%s\" \"%s\"\n"
// RefererLog is "%{Referer}i -> %U"
RefererLog refererLog = "%s -> %s\n"
// AgentLog is "%{User-agent}i"
AgentLog agentLog = "%s\n"
)
type (
commonLog string
commonLogWithVHost string
ncsaLog string
refererLog string
agentLog string
)
// ApacheLogRecord is a structure containing the necessary information
// to write a proper log in the ApacheFormatPattern.
type ApacheLogRecord struct {
http.ResponseWriter
Logger
ip string
time time.Time
method string
uri string
protocol string
status int
responseBytes int64
elapsedTime time.Duration
referer string
agent string
}
// Hijack implements the http.Hijacker interface to allow connection
// hijacking.
func (a *ApacheLogRecord) Hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error) {
hj, ok := a.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, ErrUnHijackable
}
return hj.Hijack()
}
// Log will log an entry to its io.Writer.
func (l *Log) Log(r ApacheLogRecord) {
l.Lock()
n, err := r.WriteTo(l.out)
if err != nil {
return
}
if l.size+int64(n) >= l.MaxFileSize {
l.Rotate()
}
l.size += int64(n)
l.Unlock()
}
func (r ApacheLogRecord) WriteTo(w io.Writer) (n int64, err error) {
nn, err := r.Logger.WriteLog(w, r)
return int64(nn), err
}
// Write fulfills the Write method of the http.ResponseWriter interface.
func (r *ApacheLogRecord) Write(p []byte) (int, error) {
n, err := r.ResponseWriter.Write(p)
r.responseBytes += int64(n)
return n, err
}
// WriteHeader fulfills the WriteHeader method of the http.ResponseWriter
// interface.
func (r *ApacheLogRecord) WriteHeader(status int) {
r.status = status
r.ResponseWriter.WriteHeader(status)
}
// ServeHTTP fulfills the ServeHTTP method of the http.Handler interface.
func (h *Handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
clientIP := r.RemoteAddr
if colon := strings.LastIndex(clientIP, ":"); colon != -1 {
clientIP = clientIP[:colon]
}
record := ApacheLogRecord{
ResponseWriter: rw,
Logger: h.Log,
ip: clientIP,
time: time.Time{},
method: r.Method,
uri: r.RequestURI,
protocol: r.Proto,
status: http.StatusOK,
elapsedTime: time.Duration(0),
referer: r.Referer(),
agent: r.UserAgent(),
}
startTime := time.Now()
h.handler.ServeHTTP(&record, r)
finishTime := time.Now()
record.time = finishTime.UTC()
record.elapsedTime = finishTime.Sub(startTime)
h.Log.Log(record)
}