/
http-context.go
210 lines (163 loc) 路 3.98 KB
/
http-context.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package types
import (
"context"
"fmt"
"net/http"
"regexp"
"strings"
"sync"
"github.com/zishang520/engine.io/errors"
"github.com/zishang520/engine.io/events"
"github.com/zishang520/engine.io/utils"
)
type HttpContext struct {
events.EventEmitter
Websocket *WebSocketConn
Cleanup Callable
request *http.Request
response http.ResponseWriter
headers *utils.ParameterBag
query *utils.ParameterBag
method string
pathInfo string
isHostValid bool
ctx context.Context
isDone bool
done chan Void
mu sync.RWMutex
statusCode int
mu_wh sync.RWMutex
ResponseHeaders *utils.ParameterBag
mu_w sync.Mutex
}
func NewHttpContext(w http.ResponseWriter, r *http.Request) *HttpContext {
c := &HttpContext{}
c.EventEmitter = events.New()
c.ctx = r.Context()
c.done = make(chan Void)
c.request = r
c.response = w
c.headers = utils.NewParameterBag(r.Header)
c.query = utils.NewParameterBag(r.URL.Query())
c.isHostValid = true
c.ResponseHeaders = utils.NewParameterBag(nil)
c.ResponseHeaders.With(c.response.Header())
go func() {
select {
case <-c.ctx.Done():
c.Flush()
c.Emit("close")
case <-c.done:
c.Emit("close")
}
}()
return c
}
func (c *HttpContext) Flush() {
c.mu.Lock()
defer c.mu.Unlock()
if !c.isDone {
close(c.done)
c.isDone = true
}
}
func (c *HttpContext) Done() <-chan Void {
return c.done
}
func (c *HttpContext) IsDone() bool {
c.mu.RLock()
defer c.mu.RUnlock()
return c.isDone
}
func (c *HttpContext) SetStatusCode(statusCode int) {
c.mu_wh.Lock()
defer c.mu_wh.Unlock()
c.statusCode = statusCode
}
func (c *HttpContext) GetStatusCode() int {
c.mu_wh.RLock()
defer c.mu_wh.RUnlock()
return c.statusCode
}
func (c *HttpContext) Write(wb []byte) (int, error) {
c.mu_w.Lock()
defer c.mu_w.Unlock()
if !c.IsDone() {
defer c.Flush()
for k, v := range c.ResponseHeaders.All() {
c.response.Header().Set(k, v[0])
}
c.response.WriteHeader(c.GetStatusCode())
return c.response.Write(wb)
}
return 0, errors.New("You cannot write data repeatedly.").Err()
}
func (c *HttpContext) Request() *http.Request {
return c.request
}
func (c *HttpContext) Response() http.ResponseWriter {
return c.response
}
func (c *HttpContext) Headers() *utils.ParameterBag {
return c.headers
}
func (c *HttpContext) Query() *utils.ParameterBag {
return c.query
}
func (c *HttpContext) Context() context.Context {
return c.ctx
}
func (c *HttpContext) GetPathInfo() string {
if c.pathInfo == "" {
c.pathInfo = c.request.URL.Path
}
return c.pathInfo
}
func (c *HttpContext) Get(key string, _default ...string) string {
v, _ := c.query.Get(key, _default...)
return v
}
func (c *HttpContext) Gets(key string, _default ...[]string) []string {
v, _ := c.query.Gets(key, _default...)
return v
}
func (c *HttpContext) Method() string {
return c.GetMethod()
}
func (c *HttpContext) GetMethod() string {
if c.method != "" {
return c.method
}
c.method = strings.ToUpper(c.request.Method)
return c.method
}
func (c *HttpContext) Path() string {
if pattern := strings.Trim(c.GetPathInfo(), "/"); pattern != "" {
return pattern
}
return "/"
}
func (c *HttpContext) GetHost() (string, error) {
host := c.request.Host
// trim and remove port number from host
// host is lowercase as per RFC 952/2181
host = regexp.MustCompile(`:\d+$`).ReplaceAllString(strings.TrimSpace(host), "")
// as the host can come from the user (HTTP_HOST and depending on the configuration, SERVER_NAME too can come from the user)
// check that it does not contain forbidden characters (see RFC 952 and RFC 2181)
if host != "" {
if host = regexp.MustCompile(`(?:^\[)?[a-zA-Z0-9-:\]_]+\.?`).ReplaceAllString(host, ""); host != "" {
if !c.isHostValid {
return "", nil
}
c.isHostValid = false
return "", errors.New(fmt.Sprintf(`Invalid Host "%s".`, host)).Err()
}
}
return host, nil
}
func (c *HttpContext) UserAgent() string {
return c.headers.Peek("User-Agent")
}
func (c *HttpContext) Secure() bool {
return c.request.TLS != nil
}