gitea源码

context_cookie.go 1.2KB

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