-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathresponse.go
More file actions
56 lines (47 loc) · 1.24 KB
/
response.go
File metadata and controls
56 lines (47 loc) · 1.24 KB
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
package rivet
import (
"net/http"
)
// Response 包装一个 http.ResponseWriter 对象并扩展了方法.
type Response struct {
w http.ResponseWriter
status int
size int
}
// NewResponse 返回 *Response.
func NewResponse(rw http.ResponseWriter) *Response {
return &Response{rw, 0, 0}
}
// Flush 实现 http.Flusher 接口方法.
// 如果原 http.ResponseWriter 实现了 http.Flusher 接口, 那么原 Flush() 方法会被调用.
func (r *Response) Flush() {
if flusher, ok := r.w.(http.Flusher); ok {
flusher.Flush()
}
}
// WriteHeader 向相应发送状态码 s.
func (r *Response) WriteHeader(s int) {
r.status = s
r.w.WriteHeader(s)
}
// Write 向相应写入 b, 返回本次写入的字节和发生的错误.
func (r *Response) Write(b []byte) (int, error) {
if !r.Written() {
r.WriteHeader(http.StatusOK)
}
size, err := r.w.Write(b)
r.size += size
return size, err
}
// Status 返回通过 WriteHeader 设置的值.
func (r *Response) Status() int {
return r.status
}
// Size 返回通过 Write 的总字节数.
func (r *Response) Size() int {
return r.size
}
// Written 返回 true 表示通过 Write 或 WriteHeader 写入过, 否则返回 false.
func (r *Response) Written() bool {
return r.status != 0 || r.size != 0
}