Configurable path for sticky cookies

This commit is contained in:
IIpragmaII 2024-11-06 16:04:04 +01:00 committed by GitHub
parent 552bd8f180
commit ec00c4aa42
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 530 additions and 26 deletions

View file

@ -26,6 +26,7 @@ type stickyCookie struct {
httpOnly bool
sameSite string
maxAge int
path string
}
func convertSameSite(sameSite string) http.SameSite {
@ -79,6 +80,10 @@ func New(sticky *dynamic.Sticky, wantHealthCheck bool) *Balancer {
httpOnly: sticky.Cookie.HTTPOnly,
sameSite: sticky.Cookie.SameSite,
maxAge: sticky.Cookie.MaxAge,
path: "/",
}
if sticky.Cookie.Path != nil {
balancer.stickyCookie.path = *sticky.Cookie.Path
}
}
@ -236,7 +241,7 @@ func (b *Balancer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
cookie := &http.Cookie{
Name: b.stickyCookie.name,
Value: hash(server.name),
Path: "/",
Path: b.stickyCookie.path,
HttpOnly: b.stickyCookie.httpOnly,
Secure: b.stickyCookie.secure,
SameSite: convertSameSite(b.stickyCookie.sameSite),

View file

@ -226,6 +226,7 @@ func TestSticky(t *testing.T) {
HTTPOnly: true,
SameSite: "none",
MaxAge: 42,
Path: func(v string) *string { return &v }("/foo"),
},
}, false)
@ -263,6 +264,7 @@ func TestSticky(t *testing.T) {
assert.True(t, recorder.cookies["test"].Secure)
assert.Equal(t, http.SameSiteNoneMode, recorder.cookies["test"].SameSite)
assert.Equal(t, 42, recorder.cookies["test"].MaxAge)
assert.Equal(t, "/foo", recorder.cookies["test"].Path)
}
func TestSticky_FallBack(t *testing.T) {