-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathresponse.go
128 lines (113 loc) · 3.42 KB
/
response.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package baa
import (
"bufio"
"errors"
"io"
"net"
"net/http"
)
// Response implement ResponseWriter
type Response struct {
wroteHeader bool // reply header has been (logically) written
written int64 // number of bytes written in body
status int // status code passed to WriteHeader
resp http.ResponseWriter
writer io.Writer
baa *Baa
}
// NewResponse ...
func NewResponse(w http.ResponseWriter, b *Baa) *Response {
r := new(Response)
r.resp = w
r.writer = w
r.baa = b
return r
}
// Header returns the header map that will be sent by
// WriteHeader. Changing the header after a call to
// WriteHeader (or Write) has no effect unless the modified
// headers were declared as trailers by setting the
// "Trailer" header before the call to WriteHeader (see example).
// To suppress implicit response headers, set their value to nil.
func (r *Response) Header() http.Header {
return r.resp.Header()
}
// Write writes the data to the connection as part of an HTTP reply.
// If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK)
// before writing the data. If the Header does not contain a
// Content-Type line, Write adds a Content-Type set to the result of passing
// the initial 512 bytes of written data to DetectContentType.
func (r *Response) Write(b []byte) (int, error) {
if !r.wroteHeader {
r.WriteHeader(http.StatusOK)
}
n, err := r.writer.Write(b)
r.written += int64(n)
return n, err
}
// WriteHeader sends an HTTP response header with status code.
// If WriteHeader is not called explicitly, the first call to Write
// will trigger an implicit WriteHeader(http.StatusOK).
// Thus explicit calls to WriteHeader are mainly used to
// send error codes.
func (r *Response) WriteHeader(code int) {
if r.wroteHeader {
r.baa.Logger().Println("http: multiple response.WriteHeader calls")
return
}
r.wroteHeader = true
r.status = code
r.resp.WriteHeader(code)
}
// Flush implements the http.Flusher interface to allow an HTTP handler to flush
// buffered data to the client.
// See [http.Flusher](https://golang.org/pkg/net/http/#Flusher)
func (r *Response) Flush() {
if v, ok := r.resp.(http.Flusher); ok {
v.Flush()
}
}
// Pusher is the interface implemented by ResponseWriters that support
// HTTP/2 server push. For more background, see
// https://tools.ietf.org/html/rfc7540#section-8.2.
func (r *Response) Pusher() (http.Pusher, bool) {
v, ok := r.resp.(http.Pusher)
return v, ok
}
// Hijack implements the http.Hijacker interface to allow an HTTP handler to
// take over the connection.
// See [http.Hijacker](https://golang.org/pkg/net/http/#Hijacker)
func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if v, ok := r.resp.(http.Hijacker); ok {
return v.Hijack()
}
return nil, nil, errors.New("http.response denot implements the http.Hijacker")
}
// reset reuse response
func (r *Response) reset(w http.ResponseWriter) {
r.resp = w
r.writer = w
r.wroteHeader = false
r.written = 0
r.status = http.StatusOK
}
// Status returns status code
func (r *Response) Status() int {
return r.status
}
// Size returns body size
func (r *Response) Size() int64 {
return r.written
}
// Wrote returns if writes something
func (r *Response) Wrote() bool {
return r.wroteHeader
}
// GetWriter returns response io writer
func (r *Response) GetWriter() io.Writer {
return r.writer
}
// SetWriter set response io writer
func (r *Response) SetWriter(w io.Writer) {
r.writer = w
}