@@ -60,7 +60,7 @@ type Context struct {
60
60
Render Render
61
61
translation.Locale
62
62
Cache cache.Cache
63
- csrf CSRFProtector
63
+ Csrf CSRFProtector
64
64
Flash * middleware.Flash
65
65
Session session.Store
66
66
@@ -478,38 +478,26 @@ func (ctx *Context) Redirect(location string, status ...int) {
478
478
http .Redirect (ctx .Resp , ctx .Req , location , code )
479
479
}
480
480
481
- // SetCookie convenience function to set most cookies consistently
481
+ // SetSiteCookie convenience function to set most cookies consistently
482
482
// CSRF and a few others are the exception here
483
- func (ctx * Context ) SetCookie (name , value string , expiry int ) {
484
- middleware .SetCookie (ctx .Resp , name , value ,
485
- expiry ,
486
- setting .AppSubURL ,
487
- setting .SessionConfig .Domain ,
488
- setting .SessionConfig .Secure ,
489
- true ,
490
- middleware .SameSite (setting .SessionConfig .SameSite ))
483
+ func (ctx * Context ) SetSiteCookie (name , value string , maxAge int ) {
484
+ middleware .SetSiteCookie (ctx .Resp , name , value , maxAge )
491
485
}
492
486
493
- // DeleteCookie convenience function to delete most cookies consistently
487
+ // DeleteSiteCookie convenience function to delete most cookies consistently
494
488
// CSRF and a few others are the exception here
495
- func (ctx * Context ) DeleteCookie (name string ) {
496
- middleware .SetCookie (ctx .Resp , name , "" ,
497
- - 1 ,
498
- setting .AppSubURL ,
499
- setting .SessionConfig .Domain ,
500
- setting .SessionConfig .Secure ,
501
- true ,
502
- middleware .SameSite (setting .SessionConfig .SameSite ))
489
+ func (ctx * Context ) DeleteSiteCookie (name string ) {
490
+ middleware .SetSiteCookie (ctx .Resp , name , "" , - 1 )
503
491
}
504
492
505
- // GetCookie returns given cookie value from request header.
506
- func (ctx * Context ) GetCookie (name string ) string {
507
- return middleware .GetCookie (ctx .Req , name )
493
+ // GetSiteCookie returns given cookie value from request header.
494
+ func (ctx * Context ) GetSiteCookie (name string ) string {
495
+ return middleware .GetSiteCookie (ctx .Req , name )
508
496
}
509
497
510
498
// GetSuperSecureCookie returns given cookie value from request header with secret string.
511
499
func (ctx * Context ) GetSuperSecureCookie (secret , name string ) (string , bool ) {
512
- val := ctx .GetCookie (name )
500
+ val := ctx .GetSiteCookie (name )
513
501
return ctx .CookieDecrypt (secret , val )
514
502
}
515
503
@@ -530,10 +518,9 @@ func (ctx *Context) CookieDecrypt(secret, val string) (string, bool) {
530
518
}
531
519
532
520
// SetSuperSecureCookie sets given cookie value to response header with secret string.
533
- func (ctx * Context ) SetSuperSecureCookie (secret , name , value string , expiry int ) {
521
+ func (ctx * Context ) SetSuperSecureCookie (secret , name , value string , maxAge int ) {
534
522
text := ctx .CookieEncrypt (secret , value )
535
-
536
- ctx .SetCookie (name , text , expiry )
523
+ ctx .SetSiteCookie (name , text , maxAge )
537
524
}
538
525
539
526
// CookieEncrypt encrypts a given value using the provided secret
@@ -549,19 +536,19 @@ func (ctx *Context) CookieEncrypt(secret, value string) string {
549
536
550
537
// GetCookieInt returns cookie result in int type.
551
538
func (ctx * Context ) GetCookieInt (name string ) int {
552
- r , _ := strconv .Atoi (ctx .GetCookie (name ))
539
+ r , _ := strconv .Atoi (ctx .GetSiteCookie (name ))
553
540
return r
554
541
}
555
542
556
543
// GetCookieInt64 returns cookie result in int64 type.
557
544
func (ctx * Context ) GetCookieInt64 (name string ) int64 {
558
- r , _ := strconv .ParseInt (ctx .GetCookie (name ), 10 , 64 )
545
+ r , _ := strconv .ParseInt (ctx .GetSiteCookie (name ), 10 , 64 )
559
546
return r
560
547
}
561
548
562
549
// GetCookieFloat64 returns cookie result in float64 type.
563
550
func (ctx * Context ) GetCookieFloat64 (name string ) float64 {
564
- v , _ := strconv .ParseFloat (ctx .GetCookie (name ), 64 )
551
+ v , _ := strconv .ParseFloat (ctx .GetSiteCookie (name ), 64 )
565
552
return v
566
553
}
567
554
@@ -659,7 +646,10 @@ func WithContext(req *http.Request, ctx *Context) *http.Request {
659
646
660
647
// GetContext retrieves install context from request
661
648
func GetContext (req * http.Request ) * Context {
662
- return req .Context ().Value (contextKey ).(* Context )
649
+ if ctx , ok := req .Context ().Value (contextKey ).(* Context ); ok {
650
+ return ctx
651
+ }
652
+ return nil
663
653
}
664
654
665
655
// GetContextUser returns context user
@@ -687,6 +677,8 @@ func getCsrfOpts() CsrfOptions {
687
677
}
688
678
}
689
679
680
+ const CookieNameFlash = "gitea_flash"
681
+
690
682
// Contexter initializes a classic context for a request.
691
683
func Contexter (ctx context.Context ) func (next http.Handler ) http.Handler {
692
684
_ , rnd := templates .HTMLRenderer (ctx )
@@ -726,54 +718,32 @@ func Contexter(ctx context.Context) func(next http.Handler) http.Handler {
726
718
ctx .Data ["Context" ] = & ctx
727
719
728
720
ctx .Req = WithContext (req , & ctx )
729
- ctx .csrf = PrepareCSRFProtector (csrfOpts , & ctx )
721
+ ctx .Csrf = PrepareCSRFProtector (csrfOpts , & ctx )
730
722
731
- // Get flash.
732
- flashCookie := ctx . GetCookie ( "macaron_flash" )
733
- vals , _ := url .ParseQuery (flashCookie )
734
- if len ( vals ) > 0 {
735
- f : = & middleware.Flash {
723
+ // Get the last flash message from cookie
724
+ lastFlashCookie := middleware . GetSiteCookie ( ctx . Req , CookieNameFlash )
725
+ if vals , _ := url .ParseQuery (lastFlashCookie ); len ( vals ) > 0 {
726
+ // store last Flash message into the template data, to render it
727
+ ctx . Data [ "Flash" ] = & middleware.Flash {
736
728
DataStore : & ctx ,
737
729
Values : vals ,
738
730
ErrorMsg : vals .Get ("error" ),
739
731
SuccessMsg : vals .Get ("success" ),
740
732
InfoMsg : vals .Get ("info" ),
741
733
WarningMsg : vals .Get ("warning" ),
742
734
}
743
- ctx .Data ["Flash" ] = f
744
735
}
745
736
746
- f := & middleware.Flash {
747
- DataStore : & ctx ,
748
- Values : url.Values {},
749
- ErrorMsg : "" ,
750
- WarningMsg : "" ,
751
- InfoMsg : "" ,
752
- SuccessMsg : "" ,
753
- }
737
+ // prepare an empty Flash message for current request
738
+ ctx .Flash = & middleware.Flash {DataStore : & ctx , Values : url.Values {}}
754
739
ctx .Resp .Before (func (resp ResponseWriter ) {
755
- if flash := f .Encode (); len (flash ) > 0 {
756
- middleware .SetCookie (resp , "macaron_flash" , flash , 0 ,
757
- setting .SessionConfig .CookiePath ,
758
- middleware .Domain (setting .SessionConfig .Domain ),
759
- middleware .HTTPOnly (true ),
760
- middleware .Secure (setting .SessionConfig .Secure ),
761
- middleware .SameSite (setting .SessionConfig .SameSite ),
762
- )
763
- return
740
+ if val := ctx .Flash .Encode (); val != "" {
741
+ middleware .SetSiteCookie (ctx .Resp , CookieNameFlash , val , 0 )
742
+ } else if lastFlashCookie != "" {
743
+ middleware .SetSiteCookie (ctx .Resp , CookieNameFlash , "" , - 1 )
764
744
}
765
-
766
- middleware .SetCookie (ctx .Resp , "macaron_flash" , "" , - 1 ,
767
- setting .SessionConfig .CookiePath ,
768
- middleware .Domain (setting .SessionConfig .Domain ),
769
- middleware .HTTPOnly (true ),
770
- middleware .Secure (setting .SessionConfig .Secure ),
771
- middleware .SameSite (setting .SessionConfig .SameSite ),
772
- )
773
745
})
774
746
775
- ctx .Flash = f
776
-
777
747
// If request sends files, parse them here otherwise the Query() can't be parsed and the CsrfToken will be invalid.
778
748
if ctx .Req .Method == "POST" && strings .Contains (ctx .Req .Header .Get ("Content-Type" ), "multipart/form-data" ) {
779
749
if err := ctx .Req .ParseMultipartForm (setting .Attachment .MaxSize << 20 ); err != nil && ! strings .Contains (err .Error (), "EOF" ) { // 32MB max size
@@ -785,7 +755,7 @@ func Contexter(ctx context.Context) func(next http.Handler) http.Handler {
785
755
httpcache .SetCacheControlInHeader (ctx .Resp .Header (), 0 , "no-transform" )
786
756
ctx .Resp .Header ().Set (`X-Frame-Options` , setting .CORSConfig .XFrameOptions )
787
757
788
- ctx .Data ["CsrfToken" ] = ctx .csrf .GetToken ()
758
+ ctx .Data ["CsrfToken" ] = ctx .Csrf .GetToken ()
789
759
ctx .Data ["CsrfTokenHtml" ] = template .HTML (`<input type="hidden" name="_csrf" value="` + ctx .Data ["CsrfToken" ].(string ) + `">` )
790
760
791
761
// FIXME: do we really always need these setting? There should be someway to have to avoid having to always set these
0 commit comments