Documentation
¶
Overview ¶
Package cookies provides proxy-safe cookie rewriting helpers for mounted reverse proxies.
The package focuses on a narrow but important boundary: rewriting Set-Cookie and Cookie headers so a mounted reverse proxy can safely expose multiple upstreams without browser-cookie collisions. It does not own routing, storage, or session semantics.
Example ¶
headers := http.Header{}
headers.Add("Set-Cookie", "session=abc; Path=/; HttpOnly")
headers.Set("Cookie", "gpx_demo__session=abc; theme=dark")
RewriteSetCookies(headers, RewriteOptions{
Mode: ModeIsolate,
Namespace: "demo",
DomainStrategy: "hostOnly",
PathStrategy: "prefix",
ProxyPathPrefix: "/proxy",
})
RewriteOutboundCookies(headers, RewriteOptions{
Mode: ModeIsolate,
Namespace: "demo",
})
fmt.Println(headers.Values("Set-Cookie")[0])
fmt.Println(headers.Get("Cookie"))
Output: gpx_demo__session=abc; Path=/proxy; HttpOnly session=abc
Index ¶
- Constants
- func BuildSetCookieLine(p ParsedSetCookie) string
- func DomainAttrSafe(host string) bool
- func NamespaceForURL(u *url.URL) string
- func NamespacePrefix(ns string) string
- func RewriteOutboundCookies(h Header, opts RewriteOptions)
- func RewriteSetCookies(h Header, opts RewriteOptions)
- func SanitizeHost(hostport string) string
- type Header
- type ParsedSetCookie
- type RewriteOptions
Examples ¶
Constants ¶
const ( ModeIsolate = "isolate" ModeAuto = "auto" ModeOff = "off" )
Variables ¶
This section is empty.
Functions ¶
func BuildSetCookieLine ¶
func BuildSetCookieLine(p ParsedSetCookie) string
BuildSetCookieLine serializes a parsed Set-Cookie value back to a header line.
func DomainAttrSafe ¶
DomainAttrSafe reports whether a cookie Domain attribute is safe for the supplied host.
func NamespaceForURL ¶
NamespaceForURL derives a stable namespace from scheme and host.
func NamespacePrefix ¶
NamespacePrefix returns the browser-storage prefix used for isolate mode.
func RewriteOutboundCookies ¶
func RewriteOutboundCookies(h Header, opts RewriteOptions)
RewriteOutboundCookies rewrites Cookie headers for upstream requests in isolate mode by filtering to the current namespace and unwrapping names.
func RewriteSetCookies ¶
func RewriteSetCookies(h Header, opts RewriteOptions)
RewriteSetCookies mutates Set-Cookie headers in h according to opts.
func SanitizeHost ¶
SanitizeHost strips a port and any leading dot from a host value.
Types ¶
type Header ¶
type Header interface {
Values(key string) []string
Set(key, value string)
Add(key, value string)
Del(key string)
}
Header is the minimal interface needed to rewrite cookie headers.
type ParsedSetCookie ¶
type ParsedSetCookie struct {
Name string
Value string
Attrs map[string]string
Flags map[string]bool
ExtraTokens []string
}
ParsedSetCookie is a lightweight Set-Cookie representation that preserves unrecognized tokens on rebuild.
func ParseSetCookieLine ¶
func ParseSetCookieLine(raw string) (ParsedSetCookie, bool)
ParseSetCookieLine parses a Set-Cookie line while preserving unrecognized tokens for round-trip rebuilding.