code.gitea.io/gitea@v1.22.3/services/context/context_cookie.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package context
     5  
     6  import (
     7  	"net/http"
     8  	"strings"
     9  
    10  	"code.gitea.io/gitea/modules/setting"
    11  	"code.gitea.io/gitea/modules/web/middleware"
    12  )
    13  
    14  const CookieNameFlash = "gitea_flash"
    15  
    16  func removeSessionCookieHeader(w http.ResponseWriter) {
    17  	cookies := w.Header()["Set-Cookie"]
    18  	w.Header().Del("Set-Cookie")
    19  	for _, cookie := range cookies {
    20  		if strings.HasPrefix(cookie, setting.SessionConfig.CookieName+"=") {
    21  			continue
    22  		}
    23  		w.Header().Add("Set-Cookie", cookie)
    24  	}
    25  }
    26  
    27  // SetSiteCookie convenience function to set most cookies consistently
    28  // CSRF and a few others are the exception here
    29  func (ctx *Context) SetSiteCookie(name, value string, maxAge int) {
    30  	middleware.SetSiteCookie(ctx.Resp, name, value, maxAge)
    31  }
    32  
    33  // DeleteSiteCookie convenience function to delete most cookies consistently
    34  // CSRF and a few others are the exception here
    35  func (ctx *Context) DeleteSiteCookie(name string) {
    36  	middleware.SetSiteCookie(ctx.Resp, name, "", -1)
    37  }
    38  
    39  // GetSiteCookie returns given cookie value from request header.
    40  func (ctx *Context) GetSiteCookie(name string) string {
    41  	return middleware.GetSiteCookie(ctx.Req, name)
    42  }