forked from devfeel/dotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
165 lines (139 loc) · 3.58 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package dotweb
import (
"bufio"
"compress/gzip"
"errors"
"io"
"net"
"net/http"
)
type (
Response struct {
writer http.ResponseWriter
Status int
Size int64
body []byte
committed bool
header http.Header
isEnd bool
}
gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
)
func NewResponse(w http.ResponseWriter) (r *Response) {
return &Response{writer: w,
header: w.Header()}
}
func (r *Response) Header() http.Header {
return r.header
}
func (r *Response) QueryHeader(key string) string {
return r.Header().Get(key)
}
func (r *Response) Redirect(code int, targetUrl string) error {
r.Header().Set(HeaderCacheControl, "no-cache")
r.Header().Set(HeaderLocation, targetUrl)
return r.WriteHeader(code)
}
func (r *Response) Writer() http.ResponseWriter {
return r.writer
}
func (r *Response) SetWriter(w http.ResponseWriter) *Response {
r.writer = w
return r
}
//HttpCode return http code format int
func (r *Response) HttpCode() int {
return r.Status
}
func (r *Response) Body() []byte {
return r.body
}
func (r *Response) BodyString() string {
return string(r.body)
}
func (r *Response) SetHeader(key, val string) {
r.Header().Set(key, val)
}
func (r *Response) SetContentType(contenttype string) {
r.SetHeader(HeaderContentType, contenttype)
}
func (r *Response) SetStatusCode(code int) error {
return r.WriteHeader(code)
}
// 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) error {
if r.committed {
return errors.New("response already set status")
}
r.Status = code
r.writer.WriteHeader(code)
r.committed = true
return nil
}
// Write writes the data to the connection as part of an HTTP reply.
func (r *Response) Write(code int, b []byte) (n int, err error) {
if !r.committed {
r.WriteHeader(code)
}
n, err = r.writer.Write(b)
r.Size += int64(n)
r.body = append(r.body, b[0:]...)
return
}
//stop current response
func (r *Response) End() {
r.isEnd = true
}
// 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() {
r.writer.(http.Flusher).Flush()
}
// Hijack implements the http.Hijacker interface to allow an HTTP handler to
// take over the connection.
// See https://golang.org/pkg/net/http/#Hijacker
func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return r.writer.(http.Hijacker).Hijack()
}
//reset response attr
func (r *Response) reset(w http.ResponseWriter) {
r.writer = w
r.header = w.Header()
r.Status = http.StatusOK
r.Size = 0
r.committed = false
}
//reset response attr
func (r *Response) release() {
r.writer = nil
r.header = nil
r.Status = http.StatusOK
r.Size = 0
r.committed = false
}
/*gzipResponseWriter*/
func (w *gzipResponseWriter) WriteHeader(code int) {
if code == http.StatusNoContent { // Issue #489
w.ResponseWriter.Header().Del(HeaderContentEncoding)
}
w.ResponseWriter.WriteHeader(code)
}
func (w *gzipResponseWriter) Write(b []byte) (int, error) {
if w.Header().Get(HeaderContentType) == "" {
w.Header().Set(HeaderContentType, http.DetectContentType(b))
}
return w.Writer.Write(b)
}
func (w *gzipResponseWriter) Flush() {
w.Writer.(*gzip.Writer).Flush()
}
func (w *gzipResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return w.ResponseWriter.(http.Hijacker).Hijack()
}