-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatusrecorder.go
40 lines (32 loc) · 962 Bytes
/
statusrecorder.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
package zaphttp
import (
"net/http"
)
type statusRecorder struct {
writer http.ResponseWriter
writeHeaderCalled bool
StatusCode int
ContentType string
}
var _ http.ResponseWriter = &statusRecorder{}
func (s *statusRecorder) Header() http.Header {
return s.writer.Header()
}
func (s *statusRecorder) Write(data []byte) (int, error) {
if !s.writeHeaderCalled {
// Replicate behaviour from http.ResponseWriter.
// When Write() is called before WriteHeader(), a 200 OK is returned.
s.WriteHeader(http.StatusOK)
}
return s.writer.Write(data)
}
func (s *statusRecorder) WriteHeader(statusCode int) {
s.writeHeaderCalled = true
s.StatusCode = statusCode
s.ContentType = s.writer.Header().Get("Content-Type")
s.writer.WriteHeader(statusCode)
}
// Unwrap implements the http.unWrapper interface (not exported). This is used for the http.ResponseController.
func (s *statusRecorder) Unwrap() http.ResponseWriter {
return s.writer
}