@@ -68,13 +68,15 @@ type Request struct {
68
68
* http.Request
69
69
}
70
70
71
+ // Body returns a RequestBody for the request
71
72
func (r * Request ) Body () * RequestBody {
72
73
return & RequestBody {r .Request .Body }
73
74
}
74
75
75
76
// ContextInvoker is an inject.FastInvoker wrapper of func(ctx *Context).
76
77
type ContextInvoker func (ctx * Context )
77
78
79
+ // Invoke implements inject.FastInvoker which simplifies calls of `func(ctx *Context)` function.
78
80
func (invoke ContextInvoker ) Invoke (params []interface {}) ([]reflect.Value , error ) {
79
81
invoke (params [0 ].(* Context ))
80
82
return nil , nil
@@ -97,41 +99,43 @@ type Context struct {
97
99
Data map [string ]interface {}
98
100
}
99
101
100
- func (c * Context ) handler () Handler {
101
- if c .index < len (c .handlers ) {
102
- return c .handlers [c .index ]
102
+ func (ctx * Context ) handler () Handler {
103
+ if ctx .index < len (ctx .handlers ) {
104
+ return ctx .handlers [ctx .index ]
103
105
}
104
- if c .index == len (c .handlers ) {
105
- return c .action
106
+ if ctx .index == len (ctx .handlers ) {
107
+ return ctx .action
106
108
}
107
109
panic ("invalid index for context handler" )
108
110
}
109
111
110
- func (c * Context ) Next () {
111
- c .index += 1
112
- c .run ()
112
+ // Next runs the next handler in the context chain
113
+ func (ctx * Context ) Next () {
114
+ ctx .index ++
115
+ ctx .run ()
113
116
}
114
117
115
- func (c * Context ) Written () bool {
116
- return c .Resp .Written ()
118
+ // Written returns whether the context response has been written to
119
+ func (ctx * Context ) Written () bool {
120
+ return ctx .Resp .Written ()
117
121
}
118
122
119
- func (c * Context ) run () {
120
- for c .index <= len (c .handlers ) {
121
- vals , err := c .Invoke (c .handler ())
123
+ func (ctx * Context ) run () {
124
+ for ctx .index <= len (ctx .handlers ) {
125
+ vals , err := ctx .Invoke (ctx .handler ())
122
126
if err != nil {
123
127
panic (err )
124
128
}
125
- c .index += 1
129
+ ctx .index ++
126
130
127
131
// if the handler returned something, write it to the http response
128
132
if len (vals ) > 0 {
129
- ev := c .GetVal (reflect .TypeOf (ReturnHandler (nil )))
133
+ ev := ctx .GetVal (reflect .TypeOf (ReturnHandler (nil )))
130
134
handleReturn := ev .Interface ().(ReturnHandler )
131
- handleReturn (c , vals )
135
+ handleReturn (ctx , vals )
132
136
}
133
137
134
- if c .Written () {
138
+ if ctx .Written () {
135
139
return
136
140
}
137
141
}
@@ -172,6 +176,7 @@ func (ctx *Context) HTMLSet(status int, setName, tplName string, data ...interfa
172
176
ctx .renderHTML (status , setName , tplName , data ... )
173
177
}
174
178
179
+ // Redirect sends a redirect response
175
180
func (ctx * Context ) Redirect (location string , status ... int ) {
176
181
code := http .StatusFound
177
182
if len (status ) == 1 {
@@ -181,7 +186,7 @@ func (ctx *Context) Redirect(location string, status ...int) {
181
186
http .Redirect (ctx .Resp , ctx .Req .Request , location , code )
182
187
}
183
188
184
- // Maximum amount of memory to use when parsing a multipart form.
189
+ // MaxMemory is the maximum amount of memory to use when parsing a multipart form.
185
190
// Set this to whatever value you prefer; default is 10 MB.
186
191
var MaxMemory = int64 (1024 * 1024 * 10 )
187
192
@@ -341,26 +346,34 @@ func (ctx *Context) SetCookie(name string, value string, others ...interface{})
341
346
cookie .MaxAge = int (v )
342
347
case int32 :
343
348
cookie .MaxAge = int (v )
349
+ case func (* http.Cookie ):
350
+ v (& cookie )
344
351
}
345
352
}
346
353
347
354
cookie .Path = "/"
348
355
if len (others ) > 1 {
349
356
if v , ok := others [1 ].(string ); ok && len (v ) > 0 {
350
357
cookie .Path = v
358
+ } else if v , ok := others [1 ].(func (* http.Cookie )); ok {
359
+ v (& cookie )
351
360
}
352
361
}
353
362
354
363
if len (others ) > 2 {
355
364
if v , ok := others [2 ].(string ); ok && len (v ) > 0 {
356
365
cookie .Domain = v
366
+ } else if v , ok := others [1 ].(func (* http.Cookie )); ok {
367
+ v (& cookie )
357
368
}
358
369
}
359
370
360
371
if len (others ) > 3 {
361
372
switch v := others [3 ].(type ) {
362
373
case bool :
363
374
cookie .Secure = v
375
+ case func (* http.Cookie ):
376
+ v (& cookie )
364
377
default :
365
378
if others [3 ] != nil {
366
379
cookie .Secure = true
@@ -371,13 +384,25 @@ func (ctx *Context) SetCookie(name string, value string, others ...interface{})
371
384
if len (others ) > 4 {
372
385
if v , ok := others [4 ].(bool ); ok && v {
373
386
cookie .HttpOnly = true
387
+ } else if v , ok := others [1 ].(func (* http.Cookie )); ok {
388
+ v (& cookie )
374
389
}
375
390
}
376
391
377
392
if len (others ) > 5 {
378
393
if v , ok := others [5 ].(time.Time ); ok {
379
394
cookie .Expires = v
380
395
cookie .RawExpires = v .Format (time .UnixDate )
396
+ } else if v , ok := others [1 ].(func (* http.Cookie )); ok {
397
+ v (& cookie )
398
+ }
399
+ }
400
+
401
+ if len (others ) > 6 {
402
+ for _ , other := range others [6 :] {
403
+ if v , ok := other .(func (* http.Cookie )); ok {
404
+ v (& cookie )
405
+ }
381
406
}
382
407
}
383
408
0 commit comments