-
Notifications
You must be signed in to change notification settings - Fork 57
/
response_writer.go
50 lines (40 loc) · 994 Bytes
/
response_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
package typhon
import (
"bufio"
"net"
"net/http"
)
type ResponseWriter interface {
http.ResponseWriter
// WriteJSON writes the given data as JSON to the Response. The passed value must (perhaps obviously) be
// serialisable to JSON.
WriteJSON(interface{})
// WriteError writes the given error to the Response.
WriteError(err error)
}
type responseWriterWrapper struct {
r *Response
}
func (rw responseWriterWrapper) Header() http.Header {
return rw.r.Header
}
func (rw responseWriterWrapper) Write(b []byte) (int, error) {
return rw.r.Write(b)
}
func (rw responseWriterWrapper) WriteHeader(status int) {
rw.r.StatusCode = status
}
func (rw responseWriterWrapper) WriteJSON(v interface{}) {
rw.r.Encode(v)
}
func (rw responseWriterWrapper) WriteError(err error) {
rw.r.Error = err
}
type hijackerRw struct {
responseWriterWrapper
http.Hijacker
}
func (rw hijackerRw) Hijack() (net.Conn, *bufio.ReadWriter, error) {
rw.r.hijacked = true
return rw.Hijacker.Hijack()
}