forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
54 lines (47 loc) · 1.07 KB
/
status.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
package http
import "net/http"
type statusResponseWriter struct {
statusCode int
responseBytes int
http.ResponseWriter
}
func newStatusResponseWriter(w http.ResponseWriter) *statusResponseWriter {
return &statusResponseWriter{
ResponseWriter: w,
}
}
func (w *statusResponseWriter) Write(b []byte) (int, error) {
n, err := w.ResponseWriter.Write(b)
w.responseBytes += n
return n, err
}
// WriteHeader writes the header and captures the status code.
func (w *statusResponseWriter) WriteHeader(statusCode int) {
w.statusCode = statusCode
w.ResponseWriter.WriteHeader(statusCode)
}
func (w *statusResponseWriter) code() int {
code := w.statusCode
if code == 0 {
// When statusCode is 0 then WriteHeader was never called and we can assume that
// the ResponseWriter wrote an http.StatusOK.
code = http.StatusOK
}
return code
}
func (w *statusResponseWriter) statusCodeClass() string {
class := "XXX"
switch w.code() / 100 {
case 1:
class = "1XX"
case 2:
class = "2XX"
case 3:
class = "3XX"
case 4:
class = "4XX"
case 5:
class = "5XX"
}
return class
}