This repository has been archived by the owner on Mar 29, 2021. It is now read-only.
forked from jmhodges/howsmyssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat_writer.go
88 lines (78 loc) · 1.66 KB
/
stat_writer.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
package main
import (
"bufio"
"errors"
"expvar"
"net"
"net/http"
)
var (
_ http.Hijacker = &statWriter{}
errNotAHijacker = errors.New("statWriter: given ResponseWriter was not a Hijacker")
)
type statusStats struct {
status1xx *expvar.Int
status2xx *expvar.Int
status3xx *expvar.Int
status4xx *expvar.Int
status5xx *expvar.Int
}
func newStatusStats(outer *expvar.Map) *statusStats {
m := new(expvar.Map).Init()
outer.Set("statuses", m)
status1xx := &expvar.Int{}
status2xx := &expvar.Int{}
status3xx := &expvar.Int{}
status4xx := &expvar.Int{}
status5xx := &expvar.Int{}
m.Set("1xx", status1xx)
m.Set("2xx", status2xx)
m.Set("3xx", status3xx)
m.Set("4xx", status4xx)
m.Set("5xx", status5xx)
return &statusStats{
status1xx,
status2xx,
status3xx,
status4xx,
status5xx,
}
}
type statWriter struct {
w http.ResponseWriter
stats *statusStats
writtenTo bool
}
func (sw *statWriter) WriteHeader(statusCode int) {
switch {
case statusCode > 499:
sw.stats.status5xx.Add(1)
case statusCode > 399:
sw.stats.status4xx.Add(1)
case statusCode > 299:
sw.stats.status3xx.Add(1)
case statusCode > 199:
sw.stats.status2xx.Add(1)
default:
sw.stats.status1xx.Add(1)
}
sw.writtenTo = true
sw.w.WriteHeader(statusCode)
}
func (sw *statWriter) Write(bs []byte) (int, error) {
if !sw.writtenTo {
sw.stats.status2xx.Add(1)
sw.writtenTo = true
}
return sw.w.Write(bs)
}
func (sw *statWriter) Header() http.Header {
return sw.w.Header()
}
func (sw *statWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hj, ok := sw.w.(http.Hijacker)
if !ok {
return nil, nil, errNotAHijacker
}
return hj.Hijack()
}