2018-05-14 16:26:38 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import "net/http"
|
|
|
|
|
|
|
|
type statusResponseWriter struct {
|
2019-04-10 21:08:22 +00:00
|
|
|
statusCode int
|
|
|
|
responseBytes int
|
2018-05-14 16:26:38 +00:00
|
|
|
http.ResponseWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStatusResponseWriter(w http.ResponseWriter) *statusResponseWriter {
|
|
|
|
return &statusResponseWriter{
|
|
|
|
ResponseWriter: w,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 21:08:22 +00:00
|
|
|
func (w *statusResponseWriter) Write(b []byte) (int, error) {
|
|
|
|
n, err := w.ResponseWriter.Write(b)
|
|
|
|
w.responseBytes += n
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:04:53 +00:00
|
|
|
// WriteHeader writes the header and captures the status code.
|
2018-05-14 16:26:38 +00:00
|
|
|
func (w *statusResponseWriter) WriteHeader(statusCode int) {
|
|
|
|
w.statusCode = statusCode
|
|
|
|
w.ResponseWriter.WriteHeader(statusCode)
|
|
|
|
}
|
|
|
|
|
2018-07-30 22:16:37 +00:00
|
|
|
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
|
|
|
|
}
|
2019-04-10 21:08:22 +00:00
|
|
|
|
2018-05-14 16:26:38 +00:00
|
|
|
func (w *statusResponseWriter) statusCodeClass() string {
|
|
|
|
class := "XXX"
|
2018-07-30 22:16:37 +00:00
|
|
|
switch w.code() / 100 {
|
2018-05-14 16:26:38 +00:00
|
|
|
case 1:
|
|
|
|
class = "1XX"
|
2018-07-30 22:16:37 +00:00
|
|
|
case 2:
|
2018-05-14 16:26:38 +00:00
|
|
|
class = "2XX"
|
|
|
|
case 3:
|
|
|
|
class = "3XX"
|
|
|
|
case 4:
|
|
|
|
class = "4XX"
|
|
|
|
case 5:
|
|
|
|
class = "5XX"
|
|
|
|
}
|
|
|
|
return class
|
|
|
|
}
|