cookieKit

package
v3.0.91 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteCookie

func DeleteCookie(writer http.ResponseWriter, cookie *http.Cookie)

DeleteCookie

@param cookie 可以为nil

func DeleteCookieByName

func DeleteCookieByName(req *http.Request, writer http.ResponseWriter, name string)

DeleteCookieByName

@param name 可以为"",但无意义

func GetCookie

func GetCookie(req *http.Request, name string) (*http.Cookie, error)

GetCookie

!!!: (1) 获取到的 *http.Cookie 实例,如果对其进行了修改,浏览器端并不会同步修改(除非修改后set回去). (2) 如果不存在与传参name对应的cookie,将返回 (nil, http.ErrNoCookie). (3) 如果返回error,唯一的原因:不存在与 传参name 对应的cookie.

@param req e.g. gin.Context Request @param name 可以为"",但无意义

func GetCookieValue

func GetCookieValue(req *http.Request, name string) (string, error)

GetCookieValue

@param req e.g. gin.Context Request @param name 可以为"",但无意义

func NewCookie

func NewCookie(name, value string, cookieOptions ...CookieOption) *http.Cookie

NewCookie

e.g. 跨域情况下,iframe页面(子页面)设置cookie失败的一种解决方法 满足以下条件:

(1) WithSameSite(http.SameSiteNoneMode)
(2) WithSecure(true)
(3) https协议

func SetCookie

func SetCookie(writer http.ResponseWriter, cookie *http.Cookie)

SetCookie

@param writer e.g. gin.Context Writer @param cookie 可以为nil,但这样的话调用此方法就没意义了

func SetCookieMaxAge

func SetCookieMaxAge(cookie *http.Cookie, maxAge int)

SetCookieMaxAge 修改Cookie的MaxAge属性(兼容IE浏览器)

PS: (1) 同时设置 MaxAge 和 Expires 的情况下,

(a) IE浏览器,忽略 MaxAge,使用 Expires;
(b) 其它浏览器,忽略 Expires,使用 MaxAge.

(2) IE浏览器只认 Expires,但这样有缺陷: 浏览器(客户端)和服务端存在时间差的情况下,Cookie可能会提前或延后过期.

Types

type CookieOption

type CookieOption func(*options)

func WithDomain

func WithDomain(domain string) CookieOption

func WithHttpOnly

func WithHttpOnly(httpOnly bool) CookieOption

func WithMaxAge

func WithMaxAge(maxAge int) CookieOption

func WithPath

func WithPath(path string) CookieOption

func WithSameSite

func WithSameSite(sameSite http.SameSite) CookieOption

func WithSecure

func WithSecure(secure bool) CookieOption

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL