@@ -68,13 +68,15 @@ type Request struct {
6868 * http.Request
6969}
7070
71+ // Body returns a RequestBody for the request
7172func (r * Request ) Body () * RequestBody {
7273 return & RequestBody {r .Request .Body }
7374}
7475
7576// ContextInvoker is an inject.FastInvoker wrapper of func(ctx *Context).
7677type ContextInvoker func (ctx * Context )
7778
79+ // Invoke implements inject.FastInvoker which simplifies calls of `func(ctx *Context)` function.
7880func (invoke ContextInvoker ) Invoke (params []interface {}) ([]reflect.Value , error ) {
7981 invoke (params [0 ].(* Context ))
8082 return nil , nil
@@ -97,41 +99,43 @@ type Context struct {
9799 Data map [string ]interface {}
98100}
99101
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 ]
103105 }
104- if c .index == len (c .handlers ) {
105- return c .action
106+ if ctx .index == len (ctx .handlers ) {
107+ return ctx .action
106108 }
107109 panic ("invalid index for context handler" )
108110}
109111
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 ()
113116}
114117
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 ()
117121}
118122
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 ())
122126 if err != nil {
123127 panic (err )
124128 }
125- c .index += 1
129+ ctx .index ++
126130
127131 // if the handler returned something, write it to the http response
128132 if len (vals ) > 0 {
129- ev := c .GetVal (reflect .TypeOf (ReturnHandler (nil )))
133+ ev := ctx .GetVal (reflect .TypeOf (ReturnHandler (nil )))
130134 handleReturn := ev .Interface ().(ReturnHandler )
131- handleReturn (c , vals )
135+ handleReturn (ctx , vals )
132136 }
133137
134- if c .Written () {
138+ if ctx .Written () {
135139 return
136140 }
137141 }
@@ -172,6 +176,7 @@ func (ctx *Context) HTMLSet(status int, setName, tplName string, data ...interfa
172176 ctx .renderHTML (status , setName , tplName , data ... )
173177}
174178
179+ // Redirect sends a redirect response
175180func (ctx * Context ) Redirect (location string , status ... int ) {
176181 code := http .StatusFound
177182 if len (status ) == 1 {
@@ -181,7 +186,7 @@ func (ctx *Context) Redirect(location string, status ...int) {
181186 http .Redirect (ctx .Resp , ctx .Req .Request , location , code )
182187}
183188
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.
185190// Set this to whatever value you prefer; default is 10 MB.
186191var MaxMemory = int64 (1024 * 1024 * 10 )
187192
@@ -341,26 +346,34 @@ func (ctx *Context) SetCookie(name string, value string, others ...interface{})
341346 cookie .MaxAge = int (v )
342347 case int32 :
343348 cookie .MaxAge = int (v )
349+ case func (* http.Cookie ):
350+ v (& cookie )
344351 }
345352 }
346353
347354 cookie .Path = "/"
348355 if len (others ) > 1 {
349356 if v , ok := others [1 ].(string ); ok && len (v ) > 0 {
350357 cookie .Path = v
358+ } else if v , ok := others [1 ].(func (* http.Cookie )); ok {
359+ v (& cookie )
351360 }
352361 }
353362
354363 if len (others ) > 2 {
355364 if v , ok := others [2 ].(string ); ok && len (v ) > 0 {
356365 cookie .Domain = v
366+ } else if v , ok := others [1 ].(func (* http.Cookie )); ok {
367+ v (& cookie )
357368 }
358369 }
359370
360371 if len (others ) > 3 {
361372 switch v := others [3 ].(type ) {
362373 case bool :
363374 cookie .Secure = v
375+ case func (* http.Cookie ):
376+ v (& cookie )
364377 default :
365378 if others [3 ] != nil {
366379 cookie .Secure = true
@@ -371,13 +384,25 @@ func (ctx *Context) SetCookie(name string, value string, others ...interface{})
371384 if len (others ) > 4 {
372385 if v , ok := others [4 ].(bool ); ok && v {
373386 cookie .HttpOnly = true
387+ } else if v , ok := others [1 ].(func (* http.Cookie )); ok {
388+ v (& cookie )
374389 }
375390 }
376391
377392 if len (others ) > 5 {
378393 if v , ok := others [5 ].(time.Time ); ok {
379394 cookie .Expires = v
380395 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+ }
381406 }
382407 }
383408
0 commit comments