-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
266 lines (225 loc) · 5.43 KB
/
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package cotton
import (
"encoding/json"
"net"
"net/http"
"net/url"
"strings"
"sync"
)
var (
htmlContentType = "text/html; charset=utf-8"
jsonContentType = "application/json; charset=utf-8"
)
const (
defaultMultipartMemory = 32 << 20 // 32 MB
)
var ctxPool sync.Pool
func init() {
ctxPool.New = func() interface{} {
return &Context{}
}
}
// Context context for request
type Context struct {
Request *http.Request
Response responseWriter
// statusCode int
handlers []HandlerFunc
index int8
indexAbort int8
paramCache map[string]string
queryCache url.Values
postFormCache url.Values
values map[string]interface{}
router *Router
}
func newContext(w http.ResponseWriter, r *http.Request, router *Router) *Context {
// use sync.Pool
ctx := ctxPool.Get().(*Context)
ctx.reset()
ctx.Request = r
ctx.Response = &resWriter{
ResponseWriter: w,
statusCode: http.StatusOK,
}
ctx.router = router
return ctx
}
func (ctx *Context) reset() {
ctx.Request = nil
ctx.Response = nil
ctx.handlers = ctx.handlers[0:0]
ctx.index = -1
ctx.indexAbort = -1
ctx.paramCache = nil
ctx.queryCache = nil
ctx.postFormCache = nil
ctx.values = nil
ctx.router = nil
}
func (ctx *Context) destroy() {
ctx.reset()
ctxPool.Put(ctx)
}
func (ctx *Context) initQueryCache() {
if nil == ctx.queryCache {
if nil != ctx.Request {
ctx.queryCache = ctx.Request.URL.Query()
} else {
ctx.queryCache = url.Values{}
}
}
}
// Next fn
func (ctx *Context) Next() {
if nil != ctx.handlers {
ctx.index++
num := int8(len(ctx.handlers))
for ctx.index < num {
// NOTICE: there ctx will escape
ctx.handlers[ctx.index](ctx)
ctx.index++
}
}
}
// Abort fn
func (ctx *Context) Abort() {
ctx.index = int8(len(ctx.handlers) + 1)
}
// NotFound for 404
func (ctx *Context) NotFound() {
// ctx.StatusCode(http.StatusNotFound)
ctx.Response.WriteHeader(http.StatusNotFound)
// http.NotFound(ctx.Response, ctx.Request)
ctx.Response.Write([]byte("404 page not found"))
ctx.Abort()
}
// Set set value
func (ctx *Context) Set(key string, val interface{}) {
if ctx.values == nil {
ctx.values = make(map[string]interface{})
}
ctx.values[key] = val
}
// Get get value
func (ctx *Context) Get(key string) (interface{}, bool) {
v, ok := ctx.values[key]
return v, ok
}
// Cookie get cookie
func (ctx *Context) Cookie(key string) (string, error) {
c, e := ctx.Request.Cookie(key)
if e == nil {
return c.Value, nil
}
return "", e
}
// GetQuery for Request.URL.Query().Get
func (ctx *Context) GetQuery(key string) string {
return ctx.GetDefaultQuery(key, "")
}
// GetDefaultQuery get default query
func (ctx *Context) GetDefaultQuery(key, defaultVal string) string {
ctx.initQueryCache()
if v, ok := ctx.queryCache[key]; ok {
return v[0]
}
return defaultVal
}
// GetQueryArray get query array
// url?list[]=1&list[]=2
// GetQueryArray("list[]") => ["1", "2"]
func (ctx *Context) GetQueryArray(key string) (list []string) {
ctx.initQueryCache()
if v, ok := ctx.queryCache[key]; ok {
return v
}
return
}
func getValue(m map[string][]string, key string) (dicts map[string]string, exists bool) {
dicts = make(map[string]string)
for k, v := range m {
if i := strings.IndexByte(k, '['); i > 0 && k[:i] == key {
if j := strings.IndexByte(k, ']'); j > 2 {
dicts[k[i+1:j]] = v[0]
exists = true
}
}
}
return
}
// GetQueryMap get query map
func (ctx *Context) GetQueryMap(key string) (dicts map[string]string, exists bool) {
ctx.initQueryCache()
return getValue(ctx.queryCache, key)
}
// GetAllQuery get all query value
func (ctx Context) GetAllQuery() url.Values {
ctx.initQueryCache()
return ctx.queryCache
}
// Param returns the value of the URL param.
// router.GET("/user/:id", func(c *gin.Context) {
// // a GET request to /user/john
// id := c.Param("id") // id == "john"
// })
func (ctx *Context) Param(key string) string {
if ctx.paramCache != nil {
val, ok := ctx.paramCache[key]
if ok {
return val
}
}
return ""
}
// response with string
func (ctx *Context) String(code int, content string) {
ctx.Response.WriteHeader(code)
ctx.Response.Write([]byte(content))
}
// JSON response with json
func (ctx *Context) JSON(code int, val M) {
b, e := json.Marshal(val)
if e != nil {
panic(e)
}
ctx.Response.Header().Add("Content-Type", jsonContentType)
ctx.Response.WriteHeader(code)
ctx.Response.Write(b)
}
// HTML response with html
func (ctx *Context) HTML(code int, html string) {
ctx.Response.Header().Add("Content-Type", htmlContentType)
ctx.Response.WriteHeader(code)
ctx.Response.Write([]byte(html))
}
// GetRequestHeader get request header, short for ctx.Request.Header.Get
func (ctx *Context) GetRequestHeader(key string) string {
if nil != ctx.Request {
return ctx.Request.Header.Get(key)
}
return ""
}
// ClientIP get client ip
func (ctx *Context) ClientIP() string {
clientIP := ctx.GetRequestHeader("X-Forwarded-For")
clientIP = strings.TrimSpace(strings.Split(clientIP, ",")[0])
if clientIP == "" {
clientIP = strings.TrimSpace(ctx.GetRequestHeader("X-Real-Ip"))
}
if clientIP != "" {
return clientIP
}
if addr := ctx.GetRequestHeader("X-Appengine-Remote-Addr"); addr != "" {
return addr
}
if ip, _, err := net.SplitHostPort(strings.TrimSpace(ctx.Request.RemoteAddr)); err == nil {
return ip
}
return "no-ip"
}
// Redirect short for http.Redirect
func (ctx *Context) Redirect(code int, location string) {
http.Redirect(ctx.Response, ctx.Request, location, code)
}