-
Notifications
You must be signed in to change notification settings - Fork 2
/
context.go
553 lines (497 loc) · 14.6 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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package macross
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"github.com/insionng/macross/libraries/i18n"
"github.com/valyala/fasthttp"
"io"
"mime"
"os"
"path"
"path/filepath"
"time"
ktx "context"
)
type (
// SerializeFunc serializes the given data of arbitrary type into a byte array.
SerializeFunc func(data interface{}) ([]byte, error)
// Context represents the contextual data and environment while processing an incoming HTTP request.
Context struct {
*fasthttp.RequestCtx
ktx ktx.Context // standard context
Serialize SerializeFunc // the function serializing the given data of arbitrary type into a byte array.
Session Sessioner
Localer
Flash *Flash
macross *Macross
pnames []string // list of route parameter names
pvalues []string // list of parameter values corresponding to pnames
data map[string]interface{} // data items managed by Get , Set , GetStore and SetStore
index int // the index of the currently executing handler in handlers
handlers []Handler // the handlers associated with the current route
}
// Localer reprents a localization interface.
Localer interface {
Language() string
Tr(string, ...interface{}) string
}
localer struct {
i18n.Locale
}
)
const (
indexPage = "index.html"
)
// Reset sets the request and response of the context and resets all other properties.
func (c *Context) Reset(ctx *fasthttp.RequestCtx) {
c.RequestCtx = ctx
c.ktx = ktx.Background()
c.data = nil
c.index = -1
c.Serialize = Serialize
}
// Macross returns the Macross that is handling the incoming HTTP request.
func (c *Context) Macross() *Macross {
return c.macross
}
func (c *Context) Kontext() ktx.Context {
return c.ktx
}
func (c *Context) SetKontext(ktx ktx.Context) {
c.ktx = ktx
}
func (c *Context) Handler() Handler {
return c.handlers[c.index]
}
func (c *Context) SetHandler(h Handler) {
c.handlers[c.index] = h
}
func (c *Context) WrapBeforeHandler(handler Handler) Handler {
return func(self *Context) error {
if err := handler(c); err != nil {
return err
}
return self.Next()
}
}
func (c *Context) WrapAfterHandler(handler Handler) Handler {
return func(self *Context) error {
if err := self.Next(); err != nil {
return err
}
return handler(c)
}
}
// Serialize converts the given data into a byte array.
// If the data is neither a byte array nor a string, it will call fmt.Sprint to convert it into a string.
func Serialize(data interface{}) (bytes []byte, err error) {
switch data.(type) {
case []byte:
return data.([]byte), nil
case string:
return []byte(data.(string)), nil
default:
if data != nil {
return []byte(fmt.Sprint(data)), nil
}
}
return nil, nil
}
func (c *Context) Bind(i interface{}) error {
return c.macross.binder.Bind(i, c)
}
// Language returns language current locale represents.
func (l localer) Language() string {
return l.Lang
}
// Tr translates content to target language.
func (l localer) Tr(format string, args ...interface{}) string {
return l.Tr(format, args...)
}
func (c *Context) RequestBody() io.Reader {
return bytes.NewBuffer(c.Request.Body())
}
// Get returns the named data item previously registered with the context by calling Set.
// If the named data item cannot be found, nil will be returned.
func (c *Context) Get(name string) interface{} {
return c.data[name]
}
// Set stores the named data item in the context so that it can be retrieved later.
func (c *Context) Set(name string, value interface{}) {
if c.data == nil {
c.data = make(map[string]interface{})
}
c.data[name] = value
}
func (c *Context) SetStore(data map[string]interface{}) {
if c.data == nil {
c.data = make(map[string]interface{})
}
for k, v := range data {
c.data[k] = v
}
}
func (c *Context) GetStore() map[string]interface{} {
return c.data
}
// Next calls the rest of the handlers associated with the current route.
// If any of these handlers returns an error, Next will return the error and skip the following handlers.
// Next is normally used when a handler needs to do some postprocessing after the rest of the handlers
// are executed.
func (c *Context) Next() error {
c.index++
for n := len(c.handlers); c.index < n; c.index++ {
if err := c.handlers[c.index](c); err != nil {
return err
}
}
return nil
}
// Abort skips the rest of the handlers associated with the current route.
// Abort is normally used when a handler handles the request normally and wants to skip the rest of the handlers.
// If a handler wants to indicate an error condition, it should simply return the error without calling Abort.
func (c *Context) Abort() error {
c.index = len(c.handlers)
return nil
}
// Break 中断继续执行后续动作,返回指定状态及错误,不设置错误亦可.
func (c *Context) Break(status int, err ...error) error {
var e error
if len(err) > 0 {
e = err[0]
}
c.Response.Header.SetStatusCode(status)
c.macross.HandleError(c, e)
return c.Abort()
}
// URL creates a URL using the named route and the parameter values.
// The parameters should be given in the sequence of name1, value1, name2, value2, and so on.
// If a parameter in the route is not provided a value, the parameter token will remain in the resulting URL.
// Parameter values will be properly URL encoded.
// The method returns an empty string if the URL creation fails.
func (c *Context) URL(route string, pairs ...interface{}) string {
if r := c.macross.routes[route]; r != nil {
return r.URL(pairs...)
}
return ""
}
// Data writes the given data of arbitrary type to the response.
// The method calls the Serialize() method to convert the data into a byte array and then writes
// the byte array to the response.
func (c *Context) Data(data interface{}) (err error) {
var bytes []byte
if bytes, err = c.Serialize(data); err == nil {
_, err = c.Write(bytes)
c.Abort()
}
return
}
func (c *Context) JSON(i interface{}, status ...int) (err error) {
var code int
if len(status) > 0 {
code = status[0]
} else {
code = StatusOK
}
b, err := json.Marshal(i)
if err != nil {
return err
}
return c.JSONBlob(b, code)
}
func (c *Context) JSONPretty(i interface{}, indent string, status ...int) (err error) {
var code int
if len(status) > 0 {
code = status[0]
} else {
code = StatusOK
}
b, err := json.MarshalIndent(i, "", indent)
if err != nil {
return
}
return c.JSONBlob(b, code)
}
func (c *Context) JSONBlob(b []byte, status ...int) (err error) {
var code int
if len(status) > 0 {
code = status[0]
} else {
code = StatusOK
}
return c.Blob(MIMEApplicationJSONCharsetUTF8, b, code)
}
func (c *Context) JSONP(callback string, i interface{}, status ...int) (err error) {
var code int
if len(status) > 0 {
code = status[0]
} else {
code = StatusOK
}
b, err := json.Marshal(i)
if err != nil {
return err
}
return c.JSONPBlob(callback, b, code)
}
func (c *Context) JSONPBlob(callback string, b []byte, status ...int) (err error) {
var code int
if len(status) > 0 {
code = status[0]
} else {
code = StatusOK
}
c.Response.Header.Set(HeaderContentType, MIMEApplicationJavaScriptCharsetUTF8)
c.Response.Header.SetStatusCode(code)
if _, err = c.Write([]byte(callback + "(")); err != nil {
return
}
if _, err = c.Write(b); err != nil {
return
}
_, err = c.Write([]byte(");"))
c.Abort()
return
}
func (c *Context) Render(name string, status ...int) (err error) {
var code int
if len(status) > 0 {
code = status[0]
} else {
code = StatusOK
}
if c.macross.renderer == nil {
return ErrRendererNotRegistered
}
buf := new(bytes.Buffer)
if err = c.macross.renderer.Render(buf, name, c); err != nil {
return
}
c.Response.Header.Set(HeaderContentType, MIMETextHTMLCharsetUTF8)
c.Response.Header.SetStatusCode(code)
_, err = c.Write(buf.Bytes())
c.Abort()
return
}
func (c *Context) HTML(html string, status ...int) (err error) {
var code int
if len(status) > 0 {
code = status[0]
} else {
code = StatusOK
}
c.Response.Header.Set(HeaderContentType, MIMETextHTMLCharsetUTF8)
c.Response.Header.SetStatusCode(code)
_, err = c.Write([]byte(html))
c.Abort()
return
}
func (c *Context) String(s string, status ...int) (err error) {
var code int
if len(status) > 0 {
code = status[0]
} else {
code = StatusOK
}
c.Response.Header.Set(HeaderContentType, MIMETextPlainCharsetUTF8)
c.Response.Header.SetStatusCode(code)
_, err = c.Write([]byte(s))
c.Abort()
return
}
func (c *Context) XML(i interface{}, status ...int) (err error) {
var code int
if len(status) > 0 {
code = status[0]
} else {
code = StatusOK
}
b, err := xml.Marshal(i)
if err != nil {
return err
}
return c.XMLBlob(b, code)
}
func (c *Context) XMLPretty(i interface{}, indent string, status ...int) (err error) {
var code int
if len(status) > 0 {
code = status[0]
} else {
code = StatusOK
}
b, err := xml.MarshalIndent(i, "", indent)
if err != nil {
return
}
return c.XMLBlob(b, code)
}
func (c *Context) XMLBlob(b []byte, status ...int) (err error) {
var code int
if len(status) > 0 {
code = status[0]
} else {
code = StatusOK
}
c.Response.Header.Set(HeaderContentType, MIMEApplicationXMLCharsetUTF8)
c.Response.Header.SetStatusCode(code)
if _, err = c.Write([]byte(xml.Header)); err != nil {
return
}
_, err = c.Write(b)
c.Abort()
return
}
func (c *Context) Blob(contentType string, b []byte, status ...int) (err error) {
var code int
if len(status) > 0 {
code = status[0]
} else {
code = StatusOK
}
c.Response.Header.Set(HeaderContentType, contentType)
c.Response.Header.SetStatusCode(code)
_, err = c.Write(b)
c.Abort()
return
}
func (c *Context) Stream(contentType string, r io.Reader, status ...int) (err error) {
var code int
if len(status) > 0 {
code = status[0]
} else {
code = StatusOK
}
c.Response.Header.Set(HeaderContentType, contentType)
c.Response.Header.SetStatusCode(code)
_, err = io.Copy(c, r)
return
}
// ServeFile serves a view file, to send a file ( zip for example) to the client
// you should use the SendFile(serverfilename,clientfilename)
//
// You can define your own "Content-Type" header also, after this function call
// This function doesn't implement resuming (by range), use ctx.SendFile/fasthttp.ServeFileUncompressed(ctx.RequestCtx,path)/fasthttpServeFile(ctx.RequestCtx,path) instead
//
// Use it when you want to serve css/js/... files to the client, for bigger files and 'force-download' use the SendFile
func (ctx *Context) ServeFile(file string) error {
f, err := os.Open(file)
if err != nil {
return ErrNotFound
}
defer f.Close()
fi, _ := f.Stat()
if fi.IsDir() {
file = path.Join(file, indexPage)
f, err = os.Open(file)
if err != nil {
return ErrNotFound
}
fi, _ = f.Stat()
}
return ctx.ServeContent(f, fi.Name(), fi.ModTime())
}
// SendFile sends file for force-download to the client
//
// Use this instead of ServeFile to 'force-download' bigger files to the client
func (ctx *Context) SendFile(filename string, destinationName string) {
ctx.RequestCtx.SendFile(filename)
ctx.RequestCtx.Response.Header.Set(HeaderContentDisposition, "attachment;filename="+destinationName)
}
func (c *Context) Attachment(file, name string) (err error) {
return c.contentDisposition(file, name, "attachment")
}
func (c *Context) Inline(file, name string) (err error) {
return c.contentDisposition(file, name, "inline")
}
func (c *Context) contentDisposition(file, name, dispositionType string) (err error) {
c.Response.Header.Set(HeaderContentDisposition, fmt.Sprintf("%s; filename=%s", dispositionType, name))
c.ServeFile(file)
return
}
// TimeFormat is the time format to use when generating times in HTTP
// headers. It is like time.RFC1123 but hard-codes GMT as the time
// zone. The time being formatted must be in UTC for Format to
// generate the correct format.
//
// For parsing this time format, see ParseTime.
const TimeFormat = "Mon, 02 Jan 2006 15:04:05 GMT"
// RequestHeader returns the request header's value
// accepts one parameter, the key of the header (string)
// returns string
func (ctx *Context) RequestHeader(k string) string {
return string(ctx.RequestCtx.Request.Header.Peek(k))
}
// ServeContent serves content, headers are autoset
// receives three parameters, it's low-level function, instead you can use .ServeFile(string,bool)/SendFile(string,string)
//
// You can define your own "Content-Type" header also, after this function call
// Doesn't implements resuming (by range), use ctx.SendFile instead
func (ctx *Context) ServeContent(content io.ReadSeeker, filename string, modtime time.Time) error {
if t, err := time.Parse(TimeFormat, ctx.RequestHeader(HeaderIfModifiedSince)); err == nil && modtime.Before(t.Add(1*time.Second)) {
ctx.RequestCtx.Response.Header.Del(HeaderContentType)
ctx.RequestCtx.Response.Header.Del(HeaderContentLength)
ctx.RequestCtx.SetStatusCode(StatusNotModified)
return nil
}
ctx.RequestCtx.Response.Header.Set(HeaderContentType, ctx.ContentTypeByExtension(filename))
ctx.RequestCtx.Response.Header.Set(HeaderLastModified, modtime.UTC().Format(TimeFormat))
ctx.RequestCtx.SetStatusCode(StatusOK)
_, err := io.Copy(ctx.RequestCtx.Response.BodyWriter(), content)
return err
}
// ContentTypeByExtension returns the MIME type associated with the file based on
// its extension. It returns `application/octet-stream` incase MIME type is not
// found.
func (ctx *Context) ContentTypeByExtension(name string) (t string) {
ext := filepath.Ext(name)
//these should be found by the windows(registry) and unix(apache) but on windows some machines have problems on this part.
if t = mime.TypeByExtension(ext); t == "" {
// no use of map here because we will have to lock/unlock it, by hand is better, no problem:
if ext == ".json" {
t = MIMEApplicationJSON
} else if ext == ".zip" {
t = "application/zip"
} else if ext == ".3gp" {
t = "video/3gpp"
} else if ext == ".7z" {
t = "application/x-7z-compressed"
} else if ext == ".ace" {
t = "application/x-ace-compressed"
} else if ext == ".aac" {
t = "audio/x-aac"
} else if ext == ".ico" { // for any case
t = "image/x-icon"
} else if ext == ".png" {
t = "image/png"
} else {
t = MIMEOctetStream
}
}
return
}
func (c *Context) NoContent(status ...int) error {
var code int
if len(status) > 0 {
code = status[0]
} else {
code = StatusOK
}
c.Response.Header.SetStatusCode(code)
return nil
}
func (c *Context) Redirect(url string, status ...int) error {
var code int
if len(status) > 0 {
code = status[0]
} else {
code = StatusFound
}
if code < StatusMultipleChoices || code > StatusTemporaryRedirect {
return ErrInvalidRedirectCode
}
c.Response.Header.Set(HeaderLocation, url)
c.Response.Header.SetStatusCode(code)
return nil
}