Merge pull request #74 from infinite-iroha/break/v1-feat-add-samesite
Some checks are pending
Go Test / test (push) Waiting to run

feat(cookie): add SameSite support to SetCookie method
This commit is contained in:
WJQSERVER 2026-03-30 01:50:43 +08:00 committed by GitHub
commit 8dc7d8c136
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1160,17 +1160,25 @@ func (c *Context) SetSameSite(samesite http.SameSite) {
} }
// SetCookie 设置一个 HTTP cookie // SetCookie 设置一个 HTTP cookie
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool) { // sameSite 参数是可选的,如果不提供则使用通过 SetSameSite 设置的值
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool, sameSite ...http.SameSite) {
if path == "" { if path == "" {
path = "/" path = "/"
} }
site := c.sameSite
if len(sameSite) > 0 {
if len(sameSite) > 1 {
c.Warnf("SetCookie: only the first SameSite value will be used, got %d values", len(sameSite))
}
site = sameSite[0]
}
http.SetCookie(c.Writer, &http.Cookie{ http.SetCookie(c.Writer, &http.Cookie{
Name: name, Name: name,
Value: url.QueryEscape(value), Value: url.QueryEscape(value),
MaxAge: maxAge, MaxAge: maxAge,
Path: path, Path: path,
Domain: domain, Domain: domain,
SameSite: c.sameSite, SameSite: site,
Secure: secure, Secure: secure,
HttpOnly: httpOnly, HttpOnly: httpOnly,
}) })