forked from coffeehc/httpx
/
reply.go
140 lines (122 loc) · 2.87 KB
/
reply.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
// reply
package web
import (
"io"
"net/http"
"github.com/coffeehc/logger"
)
type Reply struct {
statusCode int
data interface{}
headers map[string]string
cookies []http.Cookie
redirect bool
transport Transport
contentType string
openStream bool
w http.ResponseWriter
isWebSocket bool
}
func newReply(w http.ResponseWriter) *Reply {
return &Reply{statusCode: 200, transport: StringTransport{}, headers: make(map[string]string, 0), cookies: make([]http.Cookie, 0), w: w, isWebSocket: false}
}
func (this *Reply) startWebSocket() {
this.isWebSocket = true
}
func (this *Reply) OpenStream() *Stream {
this.SetContentType("text/plain")
this.writeHeader()
this.openStream = true
return &Stream{w: this.w}
}
func (this *Reply) GetStatusCode() int {
return this.statusCode
}
func (this *Reply) GetContentType() string {
return this.contentType
}
func (this *Reply) SetContentType(contentType string) *Reply {
this.contentType = contentType
return this
}
func (this *Reply) SetCode(statusCode int) *Reply {
this.statusCode = statusCode
return this
}
func (this *Reply) SetCookie(cookie http.Cookie) *Reply {
this.cookies = append(this.cookies, cookie)
return this
}
func (this *Reply) SetHeader(key, value string) *Reply {
this.headers[key] = value
return this
}
func (this *Reply) GetHeader(key string) string {
return this.headers[key]
}
func (this *Reply) DelHeader(key string) {
delete(this.headers, key)
}
func (this *Reply) Redirect(code int, url string) *Reply {
this.redirect = true
this.headers["Location"] = url
this.statusCode = code
return this
}
func (this *Reply) With(data interface{}) *Reply {
this.data = data
return this
}
func (this *Reply) As(transport Transport) *Reply {
if transport != nil {
this.transport = transport
}
return this
}
func (this *Reply) write() {
if this.isWebSocket {
return
}
this.writeHeader()
if this.redirect {
return
}
if this.data != nil {
if reader, ok := this.data.(io.Reader); ok {
_, err := io.Copy(this.w, reader)
if err != nil {
logger.Error("数据输出出现错误:%s", err)
return
}
if limitReader, ok := this.data.(*io.LimitedReader); ok {
reader = limitReader.R
if closer, ok := reader.(io.Closer); ok {
closer.Close()
}
}
if closer, ok := this.data.(io.Closer); ok {
closer.Close()
}
} else {
err := this.transport.Out(this.w, this.data)
if err != nil {
logger.Error("数据序列化失败:%s", err)
}
}
}
}
func (this *Reply) writeHeader() {
header := this.w.Header()
for key, value := range this.headers {
header.Set(key, value)
}
for _, cookie := range this.cookies {
header.Set("Set-Cookie", cookie.String())
}
if this.contentType != "" {
header.Set("Content-Type", this.contentType)
} else {
header.Set("Content-Type", this.transport.ContentType())
}
this.w.WriteHeader(this.statusCode)
}