/
request.go
394 lines (340 loc) · 9.44 KB
/
request.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package azugo
import (
"bytes"
"net"
"strings"
"time"
"azugo.io/azugo/internal/utils"
"azugo.io/azugo/user"
"azugo.io/core"
"azugo.io/core/paginator"
"github.com/oklog/ulid/v2"
"github.com/valyala/bytebufferpool"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
"golang.org/x/exp/maps"
)
var (
protocolHTTP = []byte("http")
protocolHTTPS = []byte("https")
protocolSeparator = []byte("://")
headerXForwardedProto = []byte("X-Forwarded-Proto")
headerXForwardedHost = []byte("X-Forwarded-Host")
contentTypeFormURLEncoded = []byte("application/x-www-form-urlencoded")
contentTypeMultipartFormData = []byte("multipart/form-data")
nilArgsValuer formKeyValuer = &nilArgs{}
nilRequestID = ulid.ULID{}
)
type Context struct {
noCopy noCopy //nolint:unused,structcheck
// Base fastHTTP request context
context *fasthttp.RequestCtx
method string // HTTP method
path string // HTTP path with the modifications by the configuration -> string copy from pathBuffer
routerPath string // HTTP path as registered in the router
requestID ulid.ULID // Request ID
// Core data
app *App
mux *mux
user User
// Logger
loggerCore *zap.Logger
loggerFields map[string]zap.Field
logger *zap.Logger
// Header access methods
Header HeaderCtx
// Query access methods
Query QueryCtx
// Body access methods
Body BodyCtx
// Form access methods
Form FormCtx
// Route parameters access methods
Params ParamsCtx
}
func (a *App) acquireCtx(m *mux, path string, c *fasthttp.RequestCtx) *Context {
v := a.ctxPool.Get()
var ctx *Context
if v == nil {
ctx = new(Context)
ctx.app = a
ctx.loggerFields = make(map[string]zap.Field, 10)
ctx.Header.app = a
ctx.Header.ctx = ctx
ctx.Query.app = a
ctx.Query.ctx = ctx
ctx.Body.app = a
ctx.Body.ctx = ctx
ctx.Form.app = a
ctx.Form.ctx = ctx
ctx.Form.form = nilArgsValuer
ctx.Params.app = a
ctx.Params.ctx = ctx
} else {
ctx = v.(*Context)
}
// Set method
if c != nil {
ctx.method = utils.B2S(c.Request.Header.Method())
if u := c.Request.URI(); u != nil {
ctx.path = utils.B2S(u.Path())
}
}
ctx.routerPath = path
if ctx.method == fasthttp.MethodPost || ctx.method == fasthttp.MethodPut || ctx.method == fasthttp.MethodPatch {
if bytes.Equal(c.Request.Header.ContentType(), contentTypeFormURLEncoded) {
ctx.Form.form = &postArgs{
args: c.Request.PostArgs(),
}
} else if bytes.HasPrefix(c.Request.Header.ContentType(), contentTypeMultipartFormData) {
if form, err := c.Request.MultipartForm(); err == nil {
ctx.Form.form = &multiPartArgs{
args: form,
}
}
}
}
// Ignore error
ctx.requestID, _ = ulid.New(ulid.Timestamp(time.Now().UTC()), a.entropy)
// Attach base fastHTTP request context
ctx.context = c
// Attach mux to request context
ctx.mux = m
// Set default user as anonymous
ctx.user = user.Anonymous{}
// Attach logger to request context
_ = ctx.ReplaceLogger(a.Log())
if c != nil {
ctx.initLoggerFields()
}
return ctx
}
func (a *App) releaseCtx(ctx *Context) {
ctx.reset()
a.ctxPool.Put(ctx)
}
// RequestHandler must process incoming requests.
//
// RequestHandler must call ctx.TimeoutError() before returning
// if it keeps references to ctx and/or its' members after the return.
// Consider wrapping RequestHandler into TimeoutHandler if response time
// must be limited.
type RequestHandler func(ctx *Context)
// Handler is an adapter to process incoming requests using object method.
type Handler interface {
Handler(*Context)
}
// Handle allows to use object method that implements Handler interface to
// handle incoming requests.
func Handle(h Handler) RequestHandler {
return h.Handler
}
func (ctx *Context) reset() {
ctx.Form.form.Reset(ctx)
ctx.Form.form = nilArgsValuer
ctx.user = nil
ctx.context = nil
ctx.mux = nil
maps.Clear(ctx.loggerFields)
ctx.loggerCore = nil
ctx.logger = nil
ctx.requestID = nilRequestID
}
// App returns the application.
func (ctx *Context) App() *App {
return ctx.app
}
// Env returns the application environment.
func (ctx *Context) Env() core.Environment {
return ctx.app.Env()
}
// RouterOptions returns the router options.
func (ctx *Context) RouterOptions() *RouterOptions {
return ctx.mux.RouterOptions
}
// Context returns *fasthttp.RequestCtx that carries a deadline
// a cancellation signal, and other values across API boundaries.
func (ctx *Context) Context() *fasthttp.RequestCtx {
return ctx.context
}
// Request return the *fasthttp.Request object
// This allows you to use all fasthttp request methods
// https://godoc.org/github.com/valyala/fasthttp#Request
func (ctx *Context) Request() *fasthttp.Request {
return &ctx.context.Request
}
// ID returns the unique request identifier.
func (ctx *Context) ID() string {
return ctx.requestID.String()
}
// IP returns the client's network IP address.
func (ctx *Context) IP() net.IP {
t, ok := ctx.context.RemoteAddr().(*net.TCPAddr)
if !ok || t.IP == nil {
return nil
}
return t.IP
}
// Method returns the request method.
func (ctx *Context) Method() string {
return ctx.method
}
// IsTLS returns true if the underlying connection is TLS.
//
// If the request comes from trusted proxy it will use X-Forwarded-Proto header.
func (ctx *Context) IsTLS() bool {
if ctx.IsTrustedProxy() {
if bytes.Equal(ctx.Request().Header.PeekBytes(headerXForwardedProto), protocolHTTPS) {
return true
} else if bytes.Equal(ctx.Request().Header.PeekBytes(headerXForwardedProto), protocolHTTP) {
return false
}
}
return ctx.context.IsTLS()
}
// Host returns requested host.
//
// If the request comes from trusted proxy it will use X-Forwarded-Host header.
func (ctx *Context) Host() string {
// Check if custom host is set
if host := ctx.mux.Host(); len(host) > 0 {
return host
}
// Use proxy set header
if ctx.IsTrustedProxy() {
if host := ctx.context.Request.Header.PeekBytes(headerXForwardedHost); len(host) > 0 {
return utils.B2S(host)
}
}
// Detect from request
return utils.B2S(ctx.context.Host())
}
// BasePath returns the base path.
func (ctx *Context) BasePath() string {
return ctx.mux.BasePath()
}
// BaseURL returns the base URL for the request.
func (ctx *Context) BaseURL() string {
url := bytebufferpool.Get()
defer bytebufferpool.Put(url)
if ctx.IsTLS() {
_, _ = url.Write(protocolHTTPS)
} else {
_, _ = url.Write(protocolHTTP)
}
_, _ = url.Write(protocolSeparator)
_, _ = url.WriteString(ctx.Host())
_, _ = url.WriteString(ctx.BasePath())
return url.String()
}
// RouterPath returns the registered router path.
func (ctx *Context) RouterPath() string {
return ctx.routerPath
}
// Path returns the path part of the request URL.
func (ctx *Context) Path() string {
return ctx.path
}
// UserAgent returns the client's User-Agent, if sent in the request.
func (ctx *Context) UserAgent() string {
return utils.B2S(ctx.context.Request.Header.UserAgent())
}
// SetUserValue stores the given value (arbitrary object)
// under the given key in context.
//
// The value stored in contex may be obtained by UserValue.
//
// This functionality may be useful for passing arbitrary values between
// functions involved in request processing.
//
// All the values are removed from context after returning from the top
// RequestHandler. Additionally, Close method is called on each value
// implementing io.Closer before removing the value from context.
func (ctx *Context) SetUserValue(name string, value any) {
ctx.context.SetUserValue(name, value)
}
// UserValue returns the value stored via SetUserValue under the given key.
func (ctx *Context) UserValue(name string) any {
return ctx.context.UserValue(name)
}
// Returns Paginator with page size from query parameters.
func (ctx *Context) Paging() *paginator.Paginator {
page, err := ctx.Query.Int(paginator.QueryParameterPage)
if err != nil || page <= 0 {
page = 1
}
pageSize, err := ctx.Query.Int(paginator.QueryParameterPerPage)
if err != nil || pageSize <= 0 {
pageSize = paginator.DefaultPageSize
}
return paginator.New(page*pageSize, pageSize, page)
}
// Accepts checks if provided content type is acceptable for client.
func (ctx *Context) Accepts(contentType string) bool {
h := ctx.Header.Get(HeaderAccept)
if len(h) == 0 {
return true
}
ctGroup, _, _ := strings.Cut(contentType, "/")
ctGroup = ctGroup + "/*"
var part string
var pos int
for len(h) > 0 && pos != -1 {
pos = strings.IndexByte(h, ',')
if pos != -1 {
part = strings.Trim(h[:pos], " ")
} else {
part = strings.Trim(h, " ")
}
// Ignore priority
if f := strings.IndexByte(part, ';'); f != -1 {
part = strings.TrimRight(part[:f], " ")
}
if part == "*/*" {
return true
}
if part == contentType {
return true
}
if part == ctGroup {
return true
}
if pos != -1 {
h = h[pos+1:]
}
}
return false
}
// AcceptsExplicit checks if provided content type is explicitly acceptable for client.
func (ctx *Context) AcceptsExplicit(contentType string) bool {
h := ctx.Header.Get(HeaderAccept)
if len(h) == 0 {
return false
}
ctGroup, _, _ := strings.Cut(contentType, "/")
ctGroup = ctGroup + "/*"
var part string
var pos int
for len(h) > 0 && pos != -1 {
pos = strings.IndexByte(h, ',')
if pos != -1 {
part = strings.Trim(h[:pos], " ")
} else {
part = strings.Trim(h, " ")
}
// Ignore priority
if f := strings.IndexByte(part, ';'); f != -1 {
part = strings.TrimRight(part[:f], " ")
}
if part == contentType {
return true
}
if part == ctGroup {
return true
}
if pos != -1 {
h = h[pos+1:]
}
}
return false
}