/
context.go
908 lines (790 loc) · 22.3 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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
package gow
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"math"
"mime/multipart"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
"time"
)
const (
ContentJSON = "application/json; charset=utf-8"
ContentHTML = "text/html; charset=utf-8"
ContentJavaScript = "application/javascript; charset=utf-8"
ContentXML = "application/xml; charset=utf-8"
ContentXML2 = "text/xml; charset=utf-8"
ContentPlain = "text/plain; charset=utf-8"
ContentPOSTForm = "application/x-www-form-urlencoded"
ContentMultipartPOSTForm = "multipart/form-data"
ContentPROTOBUF = "application/x-protobuf"
ContentMSGPACK = "application/x-msgpack"
ContentMSGPACK2 = "application/msgpack"
ContentYAML = "application/x-yaml; charset=utf-8"
ContentDownload = "application/octet-stream; charset=utf-8"
)
// Context gow context
type Context struct {
writermem responseWriter
Request *http.Request
Writer ResponseWriter
Params Params
handlers HandlersChain
index int8
fullPath string
engine *Engine
params *Params
// This mutex protect Keys map
mu sync.RWMutex
// Keys is a key/value pair exclusively for the context of each request.
Keys map[string]interface{}
Data map[interface{}]interface{}
// Errors is a list of errors attached to all the handlers/middlewares who used this context.
Errors errorMsgs
// Accepted defines a list of manually accepted formats for content negotiation.
Accepted []string
// queryCache use url.ParseQuery cached the param query result from c.Request.URL.Query()
queryCache url.Values
// formCache use url.ParseQuery cached PostForm contains the parsed form data from POST, PATCH,
// or PUT body parameters.
formCache url.Values
// SameSite allows a server to define a cookie attribute making it impossible for
// the browser to send this cookie along with cross-site requests.
sameSite http.SameSite
//Pager
Pager *Pager
}
const (
abortIndex int8 = math.MaxInt8 / 2
)
func (c *Context) reset() {
c.Writer = &c.writermem
c.Params = c.Params[0:0]
c.handlers = nil
c.index = -1
c.fullPath = ""
c.Keys = nil
c.Errors = c.Errors[0:0]
c.Accepted = nil
c.queryCache = nil
c.formCache = nil
c.Data = make(map[interface{}]interface{}, 0)
c.Pager = nil
*c.params = (*c.params)[0:0]
}
// Handler returns the main handler.
func (c *Context) Handler() HandlerFunc {
return c.handlers.Last()
}
// HandlerName last handler name
func (c *Context) HandlerName() string {
return nameOfFunction(c.handlers.Last())
}
// FullPath returns a matched route full path. For not found routes
// returns an empty string.
// router.GET("/user/{id}", func(c *gin.Context) {
// c.FullPath() == "/user/:id" // true
// })
func (c *Context) FullPath() string {
return c.fullPath
}
// Next should be used only inside middleware.
// It executes the pending handlers in the chain inside the calling handler.
// See example in GitHub.
func (c *Context) Next() {
c.index++
for c.index < int8(len(c.handlers)) {
c.handlers[c.index](c)
c.index++
}
}
// IsProd return bool
// 是否运行在生产环境下
func (c *Context) IsProd() bool {
return c.engine.RunMode == ProdMode
}
// GetRunMode return app run mode string
// return dev or prod
func (c *Context) GetRunMode() string {
return c.engine.RunMode
}
// IsAborted returns true if the current context was aborted.
func (c *Context) IsAborted() bool {
return c.index >= abortIndex
}
// Abort abort handler
func (c *Context) Abort() {
c.index = abortIndex
}
// StopRun
func (c *Context) StopRun() {
panic(stopRun)
}
func (c *Context) Error(err error) *Error {
if err == nil {
panic("err is nil")
}
parsedError, ok := err.(*Error)
if !ok {
parsedError = &Error{
Err: err,
Type: ErrorTypePrivate,
}
}
c.Errors = append(c.Errors, parsedError)
return parsedError
}
/************************************/
/************ INPUT DATA ************/
/************************************/
// IsWeChat return is WeChat user-agent
func (c *Context) IsWeChat() bool {
return strings.Contains(strings.ToLower(c.UserAgent()), strings.ToLower("MicroMessenger"))
}
// IsAjax return is ajax request
func (c *Context) IsAjax() bool {
return c.GetHeader("X-Requested-With") == "XMLHttpRequest"
}
// IsWebsocket return is websocket request
func (c *Context) IsWebsocket() bool {
if strings.Contains(strings.ToLower(c.GetHeader("Connection")), "upgrade") &&
strings.EqualFold(c.GetHeader("Upgrade"), "websocket") {
return true
}
return false
}
// Referer return request referer
func (c *Context) Referer() string {
return c.Request.Referer()
}
// Host return request host string
func (c *Context) Host() string {
return c.Request.Host
}
// Param returns the value of the URL param.
// It is a shortcut for c.Params.ByName(key)
// router.GET("/user/{id}, func(c *gin.Context) {
// // a GET request to /user/john
// id := c.Param("id") // id == "john"
// })
func (c *Context) Param(key string) string {
return c.Params.ByName(key)
}
// ParamInt return the value of the URL param
func (c *Context) ParamInt(key string) (int, error) {
v := c.Param(key)
return strconv.Atoi(v)
}
// ParamInt64 return the value of the URL param
func (c *Context) ParamInt64(key string) (int64, error) {
v := c.Param(key)
return strconv.ParseInt(v, 10, 64)
}
// UserAgent get useragent
func (c *Context) UserAgent() string {
return c.GetHeader("User-Agent")
}
// Query return query string
func (c *Context) Query(key string) string {
return c.Request.URL.Query().Get(key)
}
// Form return request.FormValue key
func (c *Context) Form(key string) string {
return c.Request.FormValue(key)
}
// input
func (c *Context) input() url.Values {
if c.Request.Form == nil {
c.Request.ParseForm()
}
return c.Request.Form
}
// formValue formValue
func (c *Context) formValue(key string) string {
if v := c.Form(key); v != "" {
return v
}
if c.Request.Form == nil {
c.Request.ParseForm()
}
return c.Request.Form.Get(key)
}
// GetString 按key返回字串值,可以设置default值
func (c *Context) GetString(key string, def ...string) string {
if v := c.formValue(key); v != "" {
return v
}
if len(def) > 0 {
return def[0]
}
return ""
}
// GetStrings GetStrings
func (c *Context) GetStrings(key string, def ...[]string) []string {
var defaultDef []string
if len(def) > 0 {
defaultDef = def[0]
}
if v := c.input(); v == nil {
return defaultDef
} else if kv := v[key]; len(kv) > 0 {
return kv
}
return defaultDef
}
// GetInt return int
func (c *Context) GetInt(key string, def ...int) (int, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
return strconv.Atoi(v)
}
// GetInt8 GetInt8
// -128~127
func (c *Context) GetInt8(key string, def ...int8) (int8, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
i64, err := strconv.ParseInt(v, 10, 8)
return int8(i64), err
}
//GetUint8 GetUint8
// 0~255
func (c *Context) GetUint8(key string, def ...uint8) (uint8, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
i64, err := strconv.ParseUint(v, 10, 8)
return uint8(i64), err
}
// GetInt16 GetInt16
// -32768~32767
func (c *Context) GetInt16(key string, def ...int16) (int16, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
i64, err := strconv.ParseInt(v, 10, 16)
return int16(i64), err
}
// GetUint16 GetUint16
// 0~65535
func (c *Context) GetUint16(key string, def ...uint16) (uint16, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
i64, err := strconv.ParseUint(v, 10, 16)
return uint16(i64), err
}
//GetInt32 GetInt32
// -2147483648~2147483647
func (c *Context) GetInt32(key string, def ...int32) (int32, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
i64, err := strconv.ParseInt(v, 10, 32)
return int32(i64), err
}
// GetUint32 GetUint32
// 0~4294967295
func (c *Context) GetUint32(key string, def ...uint32) (uint32, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
i64, err := strconv.ParseUint(v, 10, 32)
return uint32(i64), err
}
// GetInt64 GetInt64
// -9223372036854775808~9223372036854775807
func (c *Context) GetInt64(key string, def ...int64) (int64, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
return strconv.ParseInt(v, 10, 64)
}
// GetUint64 GetUint64
// 0~18446744073709551615
func (c *Context) GetUint64(key string, def ...uint64) (uint64, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
i64, err := strconv.ParseUint(v, 10, 64)
return uint64(i64), err
}
// GetFloat64 GetFloat64
func (c *Context) GetFloat64(key string, def ...float64) (float64, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
return strconv.ParseFloat(v, 64)
}
//GetBool GetBool
func (c *Context) GetBool(key string, def ...bool) (bool, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
return strconv.ParseBool(v)
}
/************************************/
/******** UPLOAD********/
/************************************/
// GetFile get single file from request
func (c *Context) GetFile(key string) (multipart.File, *multipart.FileHeader, error) {
if c.Request.MultipartForm == nil {
if err := c.Request.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil {
return nil, nil, err
}
}
return c.Request.FormFile(key)
}
// GetFiles get files from request
func (c *Context) GetFiles(key string) ([]*multipart.FileHeader, error) {
if files, ok := c.Request.MultipartForm.File[key]; ok {
return files, nil
}
return nil, http.ErrMissingFile
}
// SaveToFile saves uploaded file to new path.
// upload the file and save it on the server
// c.SaveToFile("file","./upload/1.jpg")
func (c *Context) SaveToFile(fromFile, toFile string) error {
file, _, err := c.Request.FormFile(fromFile)
if err != nil {
return err
}
defer file.Close()
f, err := os.OpenFile(toFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer f.Close()
io.Copy(f, file)
return nil
}
/************************************/
/******** METADATA MANAGEMENT********/
/************************************/
// SetKey is used to store a new key/value pair exclusively for this context.
// It also lazy initializes c.Keys if it was not used previously.
func (c *Context) SetKey(key string, value interface{}) {
c.mu.Lock()
if c.Keys == nil {
c.Keys = make(map[string]interface{})
}
c.Keys[key] = value
c.mu.Unlock()
}
// GetKey returns the value for the given key, ie: (value, true).
// If the value does not exists it returns (nil, false)
func (c *Context) GetKey(key string) (value interface{}, exists bool) {
c.mu.RLock()
value, exists = c.Keys[key]
c.mu.RUnlock()
return
}
// MustGet returns the value for the given key if it exists, otherwise it panics.
func (c *Context) MustGet(key string) interface{} {
if value, exists := c.GetKey(key); exists {
return value
}
panic("Key \"" + key + "\" does not exist")
}
// KeyString returns the value associated with the key as a string.
func (c *Context) KeyString(key string) (s string) {
if val, ok := c.GetKey(key); ok && val != nil {
s, _ = val.(string)
}
return
}
// KeyBool returns the value associated with the key as a boolean.
func (c *Context) KeyBool(key string) (b bool) {
if val, ok := c.GetKey(key); ok && val != nil {
b, _ = val.(bool)
}
return
}
// KeyInt returns the value associated with the key as an integer.
func (c *Context) KeyInt(key string) (i int) {
if val, ok := c.GetKey(key); ok && val != nil {
i, _ = val.(int)
}
return
}
// KeyInt64 returns the value associated with the key as an integer.
func (c *Context) KeyInt64(key string) (i64 int64) {
if val, ok := c.GetKey(key); ok && val != nil {
i64, _ = val.(int64)
}
return
}
// KeyUint returns the value associated with the key as an unsigned integer.
func (c *Context) KeyUint(key string) (ui uint) {
if val, ok := c.GetKey(key); ok && val != nil {
ui, _ = val.(uint)
}
return
}
// KeyUint64 returns the value associated with the key as an unsigned integer.
func (c *Context) KeyUint64(key string) (ui64 uint64) {
if val, ok := c.GetKey(key); ok && val != nil {
ui64, _ = val.(uint64)
}
return
}
// KeyFloat64 returns the value associated with the key as a float64.
func (c *Context) KeyFloat64(key string) (f64 float64) {
if val, ok := c.GetKey(key); ok && val != nil {
f64, _ = val.(float64)
}
return
}
// KeyTime returns the value associated with the key as time.
func (c *Context) KeyTime(key string) (t time.Time) {
if val, ok := c.GetKey(key); ok && val != nil {
t, _ = val.(time.Time)
}
return
}
// KeyDuration returns the value associated with the key as a duration.
func (c *Context) KeyDuration(key string) (d time.Duration) {
if val, ok := c.GetKey(key); ok && val != nil {
d, _ = val.(time.Duration)
}
return
}
// KeyStringSlice returns the value associated with the key as a slice of strings.
func (c *Context) KeyStringSlice(key string) (ss []string) {
if val, ok := c.GetKey(key); ok && val != nil {
ss, _ = val.([]string)
}
return
}
// KeyStringMap returns the value associated with the key as a map of interfaces.
func (c *Context) KeyStringMap(key string) (sm map[string]interface{}) {
if val, ok := c.GetKey(key); ok && val != nil {
sm, _ = val.(map[string]interface{})
}
return
}
// KeyStringMapString returns the value associated with the key as a map of strings.
func (c *Context) KeyStringMapString(key string) (sms map[string]string) {
if val, ok := c.GetKey(key); ok && val != nil {
sms, _ = val.(map[string]string)
}
return
}
// KeyStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
func (c *Context) KeyStringMapStringSlice(key string) (smss map[string][]string) {
if val, ok := c.GetKey(key); ok && val != nil {
smss, _ = val.(map[string][]string)
}
return
}
/************************************/
/******** RESPONSE RENDERING ********/
/************************************/
// Header Header
func (c *Context) Header(key, value string) {
if value == "" {
c.Writer.Header().Del(key)
return
}
c.Writer.Header().Set(key, value)
}
// GetHeader returns value from request headers.
func (c *Context) GetHeader(key string) string {
return c.Request.Header.Get(key)
}
// SetSameSite with cookie
func (c *Context) SetSameSite(samesite http.SameSite) {
c.sameSite = samesite
}
// SetCookie adds a Set-Cookie header to the ResponseWriter's headers.
// The provided cookie must have a valid Name. Invalid cookies may be
// silently dropped.
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool) {
if path == "" {
path = "/"
}
http.SetCookie(c.Writer, &http.Cookie{
Name: name,
Value: url.QueryEscape(value),
MaxAge: maxAge,
Path: path,
Domain: domain,
SameSite: c.sameSite,
Secure: secure,
HttpOnly: httpOnly,
})
}
// GetCookie returns the named cookie provided in the request or
// ErrNoCookie if not found. And return the named cookie is unescaped.
// If multiple cookies match the given name, only one cookie will
// be returned.
func (c *Context) GetCookie(name string) (string, error) {
cookie, err := c.Request.Cookie(name)
if err != nil {
return "", err
}
val, _ := url.QueryUnescape(cookie.Value)
return val, nil
}
// Redirect http redirect
// like : 301 302 ...
func (c *Context) Redirect(code int, url string) {
c.Writer.WriteHeader(code)
http.Redirect(c.Writer, c.Request, url, code)
}
// Body request body
func (c *Context) Body() []byte {
if c.Request.Body == nil {
return []byte{}
}
var body []byte
body, _ = ioutil.ReadAll(c.Request.Body)
c.Request.Body.Close()
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
return body
}
// Render render html
func (c *Context) Render(statusCode int, name string, data interface{}) {
c.Writer.WriteHeader(statusCode)
if !bodyAllowedForStatus(statusCode) {
c.engine.Render.WriteContentType(c.Writer)
c.Writer.WriteHeader(statusCode)
return
}
if err := c.engine.Render.Render(c.Writer, name, data); err != nil {
debugPrint("html render error:%v", err)
}
}
// ServerHTML render html page
func (c *Context) ServerHTML(statusCode int, name string, data ...interface{}) {
//未设置 AutoRender时,不渲染模板
if !c.engine.AutoRender {
c.ServerString(404, string(default404Body))
return
}
var v interface{}
if len(data) > 0 {
v = data[0]
} else {
v = c.Data
}
c.Render(statusCode, name, v)
}
// HTML render html page
// When inputting data, use the value of data, otherwise use c.Data
func (c *Context) HTML(name string, data ...interface{}) {
if len(data) > 0 {
v := data[0]
c.ServerHTML(http.StatusOK, name, v)
return
}
c.ServerHTML(http.StatusOK, name)
}
// ServerHTMLString write html string into response body
func (c *Context) ServerHTMLString(code int,msg string){
if code < 0 {
code = http.StatusOK
}
c.Writer.Header().Set("Content-Type", ContentHTML)
c.Status(code)
c.Writer.Write([]byte(msg))
}
// ServerString write string into the response body
func (c *Context) ServerString(code int, msg string) {
if code < 0 {
code = http.StatusOK
}
c.Writer.Header().Set("Content-Type", ContentPlain)
c.Status(code)
c.Writer.Write([]byte(msg))
}
// String write string into the response
func (c *Context) String(msg string) {
c.ServerString(http.StatusOK, msg)
}
// ServerYAML serializes the given struct as YAML into the response body.
func (c *Context) ServerYAML(code int, data interface{}) {
if code < 0 {
code = http.StatusOK
}
c.Header("Content-Type", ContentYAML)
c.Status(code)
bytes, err := yaml.Marshal(data)
if err != nil {
c.Header("Content-Type", "")
c.ServerString(http.StatusServiceUnavailable, err.Error())
}
c.Writer.Write(bytes)
}
// YAML serializes the given struct as YAML into the response body
func (c *Context) YAML(data interface{}) {
c.ServerYAML(http.StatusOK, data)
}
// ServerJSON serializes the given struct as JSON into the response body.
func (c *Context) ServerJSON(code int, data interface{}) {
if code < 0 {
code = http.StatusOK
}
c.Header("Content-Type", ContentJSON)
c.Status(code)
encoder := json.NewEncoder(c.Writer)
if c.engine.RunMode == DevMode {
encoder.SetIndent("", " ")
}
if err := encoder.Encode(data); err != nil {
c.Header("Content-Type", "")
c.ServerString(http.StatusServiceUnavailable, err.Error())
}
}
// JSON serializes the given struct as JSON into the response body
func (c *Context) JSON(data interface{}) {
c.ServerJSON(http.StatusOK, data)
}
// ServerJSONP write data by jsonp format
func (c *Context) ServerJSONP(code int, callback string, data interface{}) {
if code < 0 {
code = http.StatusOK
}
c.Header("Content-Type", ContentJavaScript)
c.Status(code)
bytes, err := json.Marshal(data)
if err != nil {
c.Header("Content-Type", "")
c.ServerString(http.StatusServiceUnavailable, err.Error())
}
c.Writer.Write([]byte(callback + "("))
c.Writer.Write(bytes)
c.Writer.Write([]byte(");"))
}
// JSONP write date by jsonp format
func (c *Context) JSONP(callback string, data interface{}) {
c.ServerJSONP(http.StatusOK, callback, data)
}
// ServerXML write data by xml format
func (c *Context) ServerXML(code int, data interface{}) {
if code < 0 {
code = http.StatusOK
}
c.Header("Content-Type", ContentXML)
c.Status(code)
encoder := xml.NewEncoder(c.Writer)
if err := encoder.Encode(data); err != nil {
c.Header("Content-Type", "")
c.ServerString(http.StatusServiceUnavailable, err.Error())
}
}
// XML response xml doc
func (c *Context) XML(data interface{}) {
c.ServerXML(http.StatusOK, data)
}
// GetIP return k8s Cluster ip
// if ip=="" return "10.10.10.2"
func (c *Context) GetIP() (ip string) {
//header传递传递的IP
ip = c.GetHeader("ip")
if ip == "" {
ip = c.GetHeader("X-Original-Forwarded-For")
}
if ip == "" {
ip = c.GetHeader("Remote-Host")
}
if ip == "" {
ip = c.GetHeader("X-Real-IP")
}
if ip == "" {
ip = c.ClientIP()
}
if ip == "" {
ip = "10.10.10.2"
}
ips := strings.Split(ip, ",")
if len(ips) > 0 {
ip = ips[0]
}
return strings.TrimSpace(ip)
}
// ClientIP get client ip address
func (c *Context) ClientIP() (ip string) {
addr := c.Request.RemoteAddr
str := strings.Split(addr, ":")
if len(str) > 1 {
ip = str[0]
}
return
}
// Status sets the HTTP response code.
func (c *Context) Status(code int) {
c.Writer.WriteHeader(code)
}
// AbortWithStatus calls `Abort()` and writes the headers with the specified status code.
// For example, a failed attempt to authenticate a request could use: context.AbortWithStatus(401).
func (c *Context) AbortWithStatus(code int) {
c.Status(code)
c.Writer.WriteHeaderNow()
c.Abort()
}
// AbortWithError `AbortWithStatus()` and `Error()` internally.
// This method stops the chain, writes the status code and pushes the specified error to `c.Errors`.
// See Context.Error() for more details.
func (c *Context) AbortWithError(code int, err error) *Error {
c.AbortWithStatus(code)
return c.Error(err)
}
// File writes the specified file into the body stream in a efficient way.
func (c *Context) File(filepath string) {
http.ServeFile(c.Writer, c.Request, filepath)
}
// FileFromFS writes the specified file from http.FileSytem into the body stream in an efficient way.
func (c *Context) FileFromFS(filepath string, fs http.FileSystem) {
defer func(old string) {
c.Request.URL.Path = old
}(c.Request.URL.Path)
c.Request.URL.Path = filepath
http.FileServer(fs).ServeHTTP(c.Writer, c.Request)
}
// FileAttachment writes the specified file into the body stream in an efficient way
// On the client side, the file will typically be downloaded with the given filename
func (c *Context) FileAttachment(filepath, filename string) {
c.Header("content-disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
http.ServeFile(c.Writer, c.Request, filepath)
}
// Download download data
func (c *Context) Download(data []byte) {
c.Header("Content-Type", ContentDownload)
c.Writer.WriteHeader(http.StatusOK)
c.Writer.Write(data)
}
//============private method=============
func bodyAllowedForStatus(status int) bool {
switch {
case status >= 100 && status <= 199:
return false
case status == http.StatusNoContent:
return false
case status == http.StatusNotModified:
return false
}
return true
}