@marcel91200 / caddy Mirror · read-only / commits / fab6375a
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 10:45 UTC

reverseproxy: add Max-Age option to sticky cookie (#6398)

* reverseproxy: add Max-Age option to sticky cookie * Update selectionpolicies.go Co-authored-by: Francis Lavoie <lavofr@gmail.com> * Update selectionpolicies.go Co-authored-by: Francis Lavoie <lavofr@gmail.com> --------- Co-authored-by: Francis Lavoie <lavofr@gmail.com>

Jason Yuan committed Jun 15, 2024 at 09:50 UTC fab6375a8bebd952abc80e63fa31b648ae1ebc0b
1 file changed +25
modules/caddyhttp/reverseproxy/selectionpolicies.go
+25
@@ -26,6 +26,7 @@ import (
26 "strconv"
27 "strings"
28 "sync/atomic"
29 + "time"
30
31 "github.com/cespare/xxhash/v2"
32
@@ -613,6 +614,8 @@ type CookieHashSelection struct {
614 Name string `json:"name,omitempty"`
615 // Secret to hash (Hmac256) chosen upstream in cookie
616 Secret string `json:"secret,omitempty"`
617 + // The cookie's Max-Age before it expires. Default is no expiry.
618 + MaxAge caddy.Duration `json:"max_age,omitempty"`
619
620 // The fallback policy to use if the cookie is not present. Defaults to `random`.
621 FallbackRaw json.RawMessage `json:"fallback,omitempty" caddy:"namespace=http.reverse_proxy.selection_policies inline_key=policy"`
@@ -671,6 +674,9 @@ func (s CookieHashSelection) Select(pool UpstreamPool, req *http.Request, w http
674 cookie.Secure = true
675 cookie.SameSite = http.SameSiteNoneMode
676 }
677 + if s.MaxAge > 0 {
678 + cookie.MaxAge = int(time.Duration(s.MaxAge).Seconds())
679 + }
680 http.SetCookie(w, cookie)
681 return upstream
682 }
@@ -699,6 +705,7 @@ func (s CookieHashSelection) Select(pool UpstreamPool, req *http.Request, w http
705 //
706 // lb_policy cookie [<name> [<secret>]] {
707 // fallback <policy>
708 +// max_age <duration>
709 // }
710 //
711 // By default name is `lb`
@@ -728,6 +735,24 @@ func (s *CookieHashSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
735 return err
736 }
737 s.FallbackRaw = mod
738 + case "max_age":
739 + if !d.NextArg() {
740 + return d.ArgErr()
741 + }
742 + if s.MaxAge != 0 {
743 + return d.Err("cookie max_age already specified")
744 + }
745 + maxAge, err := caddy.ParseDuration(d.Val())
746 + if err != nil {
747 + return d.Errf("invalid duration: %s", d.Val())
748 + }
749 + if maxAge <= 0 {
750 + return d.Errf("invalid duration: %s, max_age should be non-zero and positive", d.Val())
751 + }
752 + if d.NextArg() {
753 + return d.ArgErr()
754 + }
755 + s.MaxAge = caddy.Duration(maxAge)
756 default:
757 return d.Errf("unrecognized option '%s'", d.Val())
758 }