Add OAuth2 Client ID Metadata Document support
MCP connectors such as ChatGPT and Claude register via HTTPS client_id URLs instead of pre-provisioned GIDs. Fetch and cache their metadata documents, upsert clients on first use, and advertise CIMD in OIDC discovery when allowed URLs are configured. Signed-off-by: Bryan Frimin <bryan@probo.com>
This commit is contained in:
404
pkg/cachecontrol/cachecontrol.go
Normal file
404
pkg/cachecontrol/cachecontrol.go
Normal file
@@ -0,0 +1,404 @@
|
||||
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
// Package cachecontrol parses HTTP Cache-Control header values as defined in
|
||||
// RFC 9111 Section 5.2.
|
||||
//
|
||||
// The API and parsing approach are adapted from github.com/lestrrat-go/httpcc
|
||||
// (MIT license, https://github.com/lestrrat-go/httpcc).
|
||||
package cachecontrol
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxAge = "max-age"
|
||||
MaxStale = "max-stale"
|
||||
MinFresh = "min-fresh"
|
||||
NoCache = "no-cache"
|
||||
NoStore = "no-store"
|
||||
NoTransform = "no-transform"
|
||||
OnlyIfCached = "only-if-cached"
|
||||
MustRevalidate = "must-revalidate"
|
||||
Public = "public"
|
||||
Private = "private"
|
||||
ProxyRevalidate = "proxy-revalidate"
|
||||
SMaxAge = "s-maxage"
|
||||
)
|
||||
|
||||
type (
|
||||
TokenPair struct {
|
||||
Name string
|
||||
Value string
|
||||
}
|
||||
|
||||
TokenValuePolicy int
|
||||
|
||||
directiveValidator interface {
|
||||
Validate(name string) TokenValuePolicy
|
||||
}
|
||||
|
||||
directiveValidatorFn func(string) TokenValuePolicy
|
||||
)
|
||||
|
||||
const (
|
||||
NoArgument TokenValuePolicy = iota
|
||||
TokenOnly
|
||||
QuotedStringOnly
|
||||
AnyTokenValue
|
||||
)
|
||||
|
||||
func (fn directiveValidatorFn) Validate(name string) TokenValuePolicy {
|
||||
return fn(name)
|
||||
}
|
||||
|
||||
func responseDirectiveValidator(name string) TokenValuePolicy {
|
||||
switch name {
|
||||
case MustRevalidate, NoStore, NoTransform, Public, ProxyRevalidate:
|
||||
return NoArgument
|
||||
case NoCache, Private:
|
||||
return QuotedStringOnly
|
||||
case MaxAge, SMaxAge:
|
||||
return TokenOnly
|
||||
default:
|
||||
return AnyTokenValue
|
||||
}
|
||||
}
|
||||
|
||||
func requestDirectiveValidator(name string) TokenValuePolicy {
|
||||
switch name {
|
||||
case MaxAge, MaxStale, MinFresh:
|
||||
return TokenOnly
|
||||
case NoCache, NoStore, NoTransform, OnlyIfCached:
|
||||
return NoArgument
|
||||
default:
|
||||
return AnyTokenValue
|
||||
}
|
||||
}
|
||||
|
||||
// ParseRequestDirective parses a single Cache-Control directive from a request.
|
||||
func ParseRequestDirective(raw string) (*TokenPair, error) {
|
||||
return parseDirective(raw, directiveValidatorFn(requestDirectiveValidator))
|
||||
}
|
||||
|
||||
// ParseResponseDirective parses a single Cache-Control directive from a response.
|
||||
func ParseResponseDirective(raw string) (*TokenPair, error) {
|
||||
return parseDirective(raw, directiveValidatorFn(responseDirectiveValidator))
|
||||
}
|
||||
|
||||
// ParseRequestDirectives parses Cache-Control directives from a request header.
|
||||
func ParseRequestDirectives(header string) ([]*TokenPair, error) {
|
||||
return parseDirectives(header, ParseRequestDirective)
|
||||
}
|
||||
|
||||
// ParseResponseDirectives parses Cache-Control directives from a response header.
|
||||
func ParseResponseDirectives(header string) ([]*TokenPair, error) {
|
||||
return parseDirectives(header, ParseResponseDirective)
|
||||
}
|
||||
|
||||
// ParseRequest parses the Cache-Control header value of an HTTP request.
|
||||
func ParseRequest(header string) (*RequestDirective, error) {
|
||||
tokens, err := ParseRequestDirectives(header)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse request cache-control: %w", err)
|
||||
}
|
||||
|
||||
dir := &RequestDirective{
|
||||
extensions: make(map[string]string),
|
||||
}
|
||||
|
||||
for _, token := range tokens {
|
||||
name := strings.ToLower(token.Name)
|
||||
|
||||
switch name {
|
||||
case MaxAge:
|
||||
seconds, err := parseDeltaSeconds(token.Value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse max-age: %w", err)
|
||||
}
|
||||
|
||||
dir.maxAge = &seconds
|
||||
case MaxStale:
|
||||
seconds, err := parseDeltaSeconds(token.Value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse max-stale: %w", err)
|
||||
}
|
||||
|
||||
dir.maxStale = &seconds
|
||||
case MinFresh:
|
||||
seconds, err := parseDeltaSeconds(token.Value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse min-fresh: %w", err)
|
||||
}
|
||||
|
||||
dir.minFresh = &seconds
|
||||
case NoCache:
|
||||
dir.noCache = true
|
||||
case NoStore:
|
||||
dir.noStore = true
|
||||
case NoTransform:
|
||||
dir.noTransform = true
|
||||
case OnlyIfCached:
|
||||
dir.onlyIfCached = true
|
||||
default:
|
||||
dir.extensions[token.Name] = token.Value
|
||||
}
|
||||
}
|
||||
|
||||
return dir, nil
|
||||
}
|
||||
|
||||
// ParseResponse parses the Cache-Control header value of an HTTP response.
|
||||
// When multiple max-age directives are present, the minimum value is kept
|
||||
// per RFC 7234 Section 4.2.3.
|
||||
func ParseResponse(header string) (*ResponseDirective, error) {
|
||||
tokens, err := ParseResponseDirectives(header)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse response cache-control: %w", err)
|
||||
}
|
||||
|
||||
dir := &ResponseDirective{
|
||||
extensions: make(map[string]string),
|
||||
}
|
||||
|
||||
for _, token := range tokens {
|
||||
name := strings.ToLower(token.Name)
|
||||
|
||||
switch name {
|
||||
case MaxAge:
|
||||
seconds, err := parseDeltaSeconds(token.Value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse max-age: %w", err)
|
||||
}
|
||||
|
||||
setMinimumUint64(&dir.maxAge, seconds)
|
||||
case MustRevalidate:
|
||||
dir.mustRevalidate = true
|
||||
case NoCache:
|
||||
dir.noCache = appendFields(dir.noCache, token.Value)
|
||||
case NoStore:
|
||||
dir.noStore = true
|
||||
case NoTransform:
|
||||
dir.noTransform = true
|
||||
case Public:
|
||||
dir.public = true
|
||||
case Private:
|
||||
dir.private = appendFields(dir.private, token.Value)
|
||||
case ProxyRevalidate:
|
||||
dir.proxyRevalidate = true
|
||||
case SMaxAge:
|
||||
seconds, err := parseDeltaSeconds(token.Value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse s-maxage: %w", err)
|
||||
}
|
||||
|
||||
setMinimumUint64(&dir.sMaxAge, seconds)
|
||||
default:
|
||||
dir.extensions[token.Name] = token.Value
|
||||
}
|
||||
}
|
||||
|
||||
return dir, nil
|
||||
}
|
||||
|
||||
func parseDirective(raw string, validator directiveValidator) (*TokenPair, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
|
||||
idx := strings.IndexByte(raw, '=')
|
||||
if idx == -1 {
|
||||
return &TokenPair{Name: raw}, nil
|
||||
}
|
||||
|
||||
pair := &TokenPair{
|
||||
Name: strings.TrimSpace(raw[:idx]),
|
||||
}
|
||||
|
||||
if len(raw) <= idx {
|
||||
return pair, nil
|
||||
}
|
||||
|
||||
value := strings.TrimSpace(raw[idx+1:])
|
||||
|
||||
switch validator.Validate(strings.ToLower(pair.Name)) {
|
||||
case TokenOnly:
|
||||
if value != "" && value[0] == '"' {
|
||||
return nil, fmt.Errorf("invalid value for %s: quoted string not allowed", pair.Name)
|
||||
}
|
||||
case QuotedStringOnly:
|
||||
if value == "" {
|
||||
break
|
||||
}
|
||||
|
||||
if value[0] != '"' {
|
||||
return nil, fmt.Errorf("invalid value for %s: bare token not allowed", pair.Name)
|
||||
}
|
||||
|
||||
unquoted, err := strconv.Unquote(value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid value for %s: malformed quoted string", pair.Name)
|
||||
}
|
||||
|
||||
value = unquoted
|
||||
case AnyTokenValue:
|
||||
if value != "" && value[0] == '"' {
|
||||
unquoted, err := strconv.Unquote(value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid value for %s: malformed quoted string", pair.Name)
|
||||
}
|
||||
|
||||
value = unquoted
|
||||
}
|
||||
case NoArgument:
|
||||
if value != "" {
|
||||
return nil, fmt.Errorf("received argument to directive %s", pair.Name)
|
||||
}
|
||||
}
|
||||
|
||||
pair.Value = value
|
||||
|
||||
return pair, nil
|
||||
}
|
||||
|
||||
func parseDirectives(header string, parse func(string) (*TokenPair, error)) ([]*TokenPair, error) {
|
||||
scanner := bufio.NewScanner(strings.NewReader(header))
|
||||
scanner.Split(scanCommaSeparatedWords)
|
||||
|
||||
var tokens []*TokenPair
|
||||
|
||||
for scanner.Scan() {
|
||||
token, err := parse(scanner.Text())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse directive #%d: %w", len(tokens)+1, err)
|
||||
}
|
||||
|
||||
tokens = append(tokens, token)
|
||||
}
|
||||
|
||||
return tokens, nil
|
||||
}
|
||||
|
||||
func appendFields(fields []string, raw string) []string {
|
||||
scanner := bufio.NewScanner(strings.NewReader(raw))
|
||||
scanner.Split(scanCommaSeparatedWords)
|
||||
|
||||
for scanner.Scan() {
|
||||
fields = append(fields, scanner.Text())
|
||||
}
|
||||
|
||||
return fields
|
||||
}
|
||||
|
||||
func setMinimumUint64(target **uint64, value uint64) {
|
||||
if *target == nil || value < **target {
|
||||
v := value
|
||||
*target = &v
|
||||
}
|
||||
}
|
||||
|
||||
func parseDeltaSeconds(raw string) (uint64, error) {
|
||||
if raw == "" {
|
||||
return 0, fmt.Errorf("empty delta-seconds")
|
||||
}
|
||||
|
||||
for _, r := range raw {
|
||||
if r < '0' || r > '9' {
|
||||
return 0, fmt.Errorf("invalid delta-seconds %q", raw)
|
||||
}
|
||||
}
|
||||
|
||||
seconds, err := strconv.ParseUint(raw, 10, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("invalid delta-seconds %q: %w", raw, err)
|
||||
}
|
||||
|
||||
return seconds, nil
|
||||
}
|
||||
|
||||
func secondsToDuration(seconds uint64) time.Duration {
|
||||
const maxSeconds = uint64(math.MaxInt64 / int64(time.Second))
|
||||
if seconds > maxSeconds {
|
||||
return time.Duration(math.MaxInt64)
|
||||
}
|
||||
|
||||
return time.Duration(seconds) * time.Second
|
||||
}
|
||||
|
||||
func isSpace(r rune) bool {
|
||||
if r <= '\u00FF' {
|
||||
switch r {
|
||||
case ' ', '\t', '\n', '\v', '\f', '\r':
|
||||
return true
|
||||
case '\u0085', '\u00A0':
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
if '\u2000' <= r && r <= '\u200a' {
|
||||
return true
|
||||
}
|
||||
|
||||
switch r {
|
||||
case '\u1680', '\u2028', '\u2029', '\u202f', '\u205f', '\u3000':
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func scanCommaSeparatedWords(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
||||
start := 0
|
||||
|
||||
for width := 0; start < len(data); start += width {
|
||||
var r rune
|
||||
r, width = utf8.DecodeRune(data[start:])
|
||||
if !isSpace(r) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
var ws int
|
||||
inQuotes := false
|
||||
|
||||
for width, i := 0, start; i < len(data); i += width {
|
||||
var r rune
|
||||
r, width = utf8.DecodeRune(data[i:])
|
||||
|
||||
switch {
|
||||
case r == '"':
|
||||
inQuotes = !inQuotes
|
||||
ws = 0
|
||||
case isSpace(r) && !inQuotes:
|
||||
ws++
|
||||
case r == ',' && !inQuotes:
|
||||
return i + width, data[start : i-ws], nil
|
||||
default:
|
||||
ws = 0
|
||||
}
|
||||
}
|
||||
|
||||
if atEOF && len(data) > start {
|
||||
return len(data), data[start : len(data)-ws], nil
|
||||
}
|
||||
|
||||
return start, nil, nil
|
||||
}
|
||||
410
pkg/cachecontrol/cachecontrol_test.go
Normal file
410
pkg/cachecontrol/cachecontrol_test.go
Normal file
@@ -0,0 +1,410 @@
|
||||
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package cachecontrol_test
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.probo.inc/probo/pkg/cachecontrol"
|
||||
)
|
||||
|
||||
func TestParseRequestDirective(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
source string
|
||||
wantErr bool
|
||||
want *cachecontrol.TokenPair
|
||||
}{
|
||||
{
|
||||
name: "no-store flag",
|
||||
source: "no-store",
|
||||
want: &cachecontrol.TokenPair{Name: "no-store"},
|
||||
},
|
||||
{
|
||||
name: "max-age token",
|
||||
source: "max-age=4649",
|
||||
want: &cachecontrol.TokenPair{Name: "max-age", Value: "4649"},
|
||||
},
|
||||
{
|
||||
name: "max-age quoted rejected",
|
||||
source: `max-age="4649"`,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "no-store with argument rejected",
|
||||
source: `no-store="foo"`,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.name,
|
||||
func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got, err := cachecontrol.ParseRequestDirective(tt.source)
|
||||
if tt.wantErr {
|
||||
require.Error(t, err)
|
||||
return
|
||||
}
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.want, got)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseResponseDirective(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
source string
|
||||
wantErr bool
|
||||
want *cachecontrol.TokenPair
|
||||
}{
|
||||
{
|
||||
name: "s-maxage token",
|
||||
source: "s-maxage=4649",
|
||||
want: &cachecontrol.TokenPair{Name: "s-maxage", Value: "4649"},
|
||||
},
|
||||
{
|
||||
name: "no-store flag",
|
||||
source: "no-store",
|
||||
want: &cachecontrol.TokenPair{Name: "no-store"},
|
||||
},
|
||||
{
|
||||
name: "extension with quoted value",
|
||||
source: `community="UCI"`,
|
||||
want: &cachecontrol.TokenPair{Name: "community", Value: "UCI"},
|
||||
},
|
||||
{
|
||||
name: "max-age quoted rejected",
|
||||
source: `max-age="4649"`,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.name,
|
||||
func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got, err := cachecontrol.ParseResponseDirective(tt.source)
|
||||
if tt.wantErr {
|
||||
require.Error(t, err)
|
||||
return
|
||||
}
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.want, got)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRequestDirectives(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tokens, err := cachecontrol.ParseRequestDirectives(` max-age=4649 , no-store `)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, tokens, 2)
|
||||
assert.Equal(t, &cachecontrol.TokenPair{Name: "max-age", Value: "4649"}, tokens[0])
|
||||
assert.Equal(t, &cachecontrol.TokenPair{Name: "no-store"}, tokens[1])
|
||||
}
|
||||
|
||||
func TestParseResponseDirectives(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tokens, err := cachecontrol.ParseResponseDirectives(`max-age=4649, no-store, community="UCI"`)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, tokens, 3)
|
||||
assert.Equal(t, &cachecontrol.TokenPair{Name: "max-age", Value: "4649"}, tokens[0])
|
||||
assert.Equal(t, &cachecontrol.TokenPair{Name: "no-store"}, tokens[1])
|
||||
assert.Equal(t, &cachecontrol.TokenPair{Name: "community", Value: "UCI"}, tokens[2])
|
||||
}
|
||||
|
||||
func TestParseRequest(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run(
|
||||
"max-age and no-store",
|
||||
func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir, err := cachecontrol.ParseRequest("max-age=4649, no-store")
|
||||
require.NoError(t, err)
|
||||
|
||||
seconds, ok := dir.MaxAge()
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, uint64(4649), seconds)
|
||||
assert.True(t, dir.NoStore())
|
||||
},
|
||||
)
|
||||
|
||||
t.Run(
|
||||
"invalid max-age rejected",
|
||||
func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err := cachecontrol.ParseRequest(`max-age="4649"`)
|
||||
require.Error(t, err)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func TestParseResponse(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run(
|
||||
"response directives and extension",
|
||||
func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir, err := cachecontrol.ParseResponse(`max-age=4649, no-store, community="UCI"`)
|
||||
require.NoError(t, err)
|
||||
|
||||
seconds, ok := dir.MaxAge()
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, uint64(4649), seconds)
|
||||
assert.True(t, dir.NoStore())
|
||||
assert.Equal(t, map[string]string{"community": "UCI"}, dir.Extensions())
|
||||
},
|
||||
)
|
||||
|
||||
t.Run(
|
||||
"multiple max-age uses minimum",
|
||||
func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir, err := cachecontrol.ParseResponse("max-age=3600, max-age=60")
|
||||
require.NoError(t, err)
|
||||
|
||||
seconds, ok := dir.MaxAge()
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, uint64(60), seconds)
|
||||
},
|
||||
)
|
||||
|
||||
t.Run(
|
||||
"s-maxage and flags",
|
||||
func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir, err := cachecontrol.ParseResponse("public, max-age=604800, s-maxage=86400, must-revalidate")
|
||||
require.NoError(t, err)
|
||||
|
||||
maxAge, ok := dir.MaxAge()
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, uint64(604800), maxAge)
|
||||
|
||||
sMaxAge, ok := dir.SMaxAge()
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, uint64(86400), sMaxAge)
|
||||
|
||||
assert.True(t, dir.Public())
|
||||
assert.True(t, dir.MustRevalidate())
|
||||
},
|
||||
)
|
||||
|
||||
t.Run(
|
||||
"invalid max-age rejected",
|
||||
func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err := cachecontrol.ParseResponse(`max-age="4649"`)
|
||||
require.Error(t, err)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func TestResponseMaxAgeDuration(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
header string
|
||||
wantAge time.Duration
|
||||
wantOK bool
|
||||
}{
|
||||
{
|
||||
name: "empty header",
|
||||
header: "",
|
||||
wantOK: false,
|
||||
},
|
||||
{
|
||||
name: "whitespace only",
|
||||
header: " ",
|
||||
wantOK: false,
|
||||
},
|
||||
{
|
||||
name: "no max-age directive",
|
||||
header: "public, private, no-cache",
|
||||
wantOK: false,
|
||||
},
|
||||
{
|
||||
name: "single max-age",
|
||||
header: "max-age=120",
|
||||
wantAge: 120 * time.Second,
|
||||
wantOK: true,
|
||||
},
|
||||
{
|
||||
name: "max-age with other directives",
|
||||
header: "public, max-age=120, private",
|
||||
wantAge: 120 * time.Second,
|
||||
wantOK: true,
|
||||
},
|
||||
{
|
||||
name: "max-age zero",
|
||||
header: "max-age=0",
|
||||
wantAge: 0,
|
||||
wantOK: true,
|
||||
},
|
||||
{
|
||||
name: "case insensitive directive name",
|
||||
header: "Max-Age=90",
|
||||
wantAge: 90 * time.Second,
|
||||
wantOK: true,
|
||||
},
|
||||
{
|
||||
name: "whitespace around comma separators",
|
||||
header: "public , max-age=120 , private",
|
||||
wantAge: 120 * time.Second,
|
||||
wantOK: true,
|
||||
},
|
||||
{
|
||||
name: "whitespace around equals sign",
|
||||
header: "max-age = 120",
|
||||
wantAge: 120 * time.Second,
|
||||
wantOK: true,
|
||||
},
|
||||
{
|
||||
name: "multiple max-age uses minimum",
|
||||
header: "max-age=3600, max-age=60",
|
||||
wantAge: 60 * time.Second,
|
||||
wantOK: true,
|
||||
},
|
||||
{
|
||||
name: "invalid max-age makes header invalid",
|
||||
header: "max-age=bad, max-age=30",
|
||||
wantOK: false,
|
||||
},
|
||||
{
|
||||
name: "all max-age values invalid",
|
||||
header: "max-age=, max-age=abc",
|
||||
wantOK: false,
|
||||
},
|
||||
{
|
||||
name: "negative max-age rejected",
|
||||
header: "max-age=-1",
|
||||
wantOK: false,
|
||||
},
|
||||
{
|
||||
name: "decimal max-age rejected",
|
||||
header: "max-age=1.5",
|
||||
wantOK: false,
|
||||
},
|
||||
{
|
||||
name: "quoted max-age rejected",
|
||||
header: `max-age="120"`,
|
||||
wantOK: false,
|
||||
},
|
||||
{
|
||||
name: "leading zeros preserved",
|
||||
header: "max-age=0060",
|
||||
wantAge: 60 * time.Second,
|
||||
wantOK: true,
|
||||
},
|
||||
{
|
||||
name: "s-maxage ignored by MaxAge helper",
|
||||
header: "s-maxage=3600",
|
||||
wantOK: false,
|
||||
},
|
||||
{
|
||||
name: "s-maxage and max-age both present",
|
||||
header: "s-maxage=3600, max-age=120",
|
||||
wantAge: 120 * time.Second,
|
||||
wantOK: true,
|
||||
},
|
||||
{
|
||||
name: "directive name must match exactly",
|
||||
header: "foo-max-age=120",
|
||||
wantOK: false,
|
||||
},
|
||||
{
|
||||
name: "comma inside quoted extension value",
|
||||
header: `foo="bar,baz", max-age=120`,
|
||||
wantAge: 120 * time.Second,
|
||||
wantOK: true,
|
||||
},
|
||||
{
|
||||
name: "real world nginx style",
|
||||
header: "max-age=31536000, public, immutable",
|
||||
wantAge: 365 * 24 * time.Hour,
|
||||
wantOK: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.name,
|
||||
func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir, err := cachecontrol.ParseResponse(tt.header)
|
||||
if !tt.wantOK {
|
||||
if err == nil {
|
||||
_, gotOK := dir.MaxAgeDuration()
|
||||
assert.False(t, gotOK)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
require.NoError(t, err)
|
||||
gotAge, gotOK := dir.MaxAgeDuration()
|
||||
assert.True(t, gotOK)
|
||||
assert.Equal(t, tt.wantAge, gotAge)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponseMaxAgeDuration_Overflow(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir, err := cachecontrol.ParseResponse("max-age=9223372036854775807")
|
||||
require.NoError(t, err)
|
||||
|
||||
age, ok := dir.MaxAgeDuration()
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, time.Duration(math.MaxInt64), age)
|
||||
}
|
||||
|
||||
func TestParseResponseDirectives_NoSpaceAfterComma(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tokens, err := cachecontrol.ParseResponseDirectives("max-age=120,no-store")
|
||||
require.NoError(t, err)
|
||||
require.Len(t, tokens, 2)
|
||||
}
|
||||
152
pkg/cachecontrol/directives.go
Normal file
152
pkg/cachecontrol/directives.go
Normal file
@@ -0,0 +1,152 @@
|
||||
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package cachecontrol
|
||||
|
||||
import "time"
|
||||
|
||||
type (
|
||||
RequestDirective struct {
|
||||
maxAge *uint64
|
||||
maxStale *uint64
|
||||
minFresh *uint64
|
||||
noCache bool
|
||||
noStore bool
|
||||
noTransform bool
|
||||
onlyIfCached bool
|
||||
extensions map[string]string
|
||||
}
|
||||
|
||||
ResponseDirective struct {
|
||||
maxAge *uint64
|
||||
mustRevalidate bool
|
||||
noCache []string
|
||||
noStore bool
|
||||
noTransform bool
|
||||
public bool
|
||||
private []string
|
||||
proxyRevalidate bool
|
||||
sMaxAge *uint64
|
||||
extensions map[string]string
|
||||
}
|
||||
)
|
||||
|
||||
func (d *RequestDirective) MaxAge() (uint64, bool) {
|
||||
if v := d.maxAge; v != nil {
|
||||
return *v, true
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (d *RequestDirective) MaxStale() (uint64, bool) {
|
||||
if v := d.maxStale; v != nil {
|
||||
return *v, true
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (d *RequestDirective) MinFresh() (uint64, bool) {
|
||||
if v := d.minFresh; v != nil {
|
||||
return *v, true
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (d *RequestDirective) NoCache() bool {
|
||||
return d.noCache
|
||||
}
|
||||
|
||||
func (d *RequestDirective) NoStore() bool {
|
||||
return d.noStore
|
||||
}
|
||||
|
||||
func (d *RequestDirective) NoTransform() bool {
|
||||
return d.noTransform
|
||||
}
|
||||
|
||||
func (d *RequestDirective) OnlyIfCached() bool {
|
||||
return d.onlyIfCached
|
||||
}
|
||||
|
||||
func (d *RequestDirective) Extensions() map[string]string {
|
||||
return d.extensions
|
||||
}
|
||||
|
||||
func (d *RequestDirective) Extension(name string) string {
|
||||
return d.extensions[name]
|
||||
}
|
||||
|
||||
func (d *ResponseDirective) MaxAge() (uint64, bool) {
|
||||
if v := d.maxAge; v != nil {
|
||||
return *v, true
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (d *ResponseDirective) MaxAgeDuration() (time.Duration, bool) {
|
||||
seconds, ok := d.MaxAge()
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return secondsToDuration(seconds), true
|
||||
}
|
||||
|
||||
func (d *ResponseDirective) MustRevalidate() bool {
|
||||
return d.mustRevalidate
|
||||
}
|
||||
|
||||
func (d *ResponseDirective) NoCache() []string {
|
||||
return d.noCache
|
||||
}
|
||||
|
||||
func (d *ResponseDirective) NoStore() bool {
|
||||
return d.noStore
|
||||
}
|
||||
|
||||
func (d *ResponseDirective) NoTransform() bool {
|
||||
return d.noTransform
|
||||
}
|
||||
|
||||
func (d *ResponseDirective) Public() bool {
|
||||
return d.public
|
||||
}
|
||||
|
||||
func (d *ResponseDirective) Private() []string {
|
||||
return d.private
|
||||
}
|
||||
|
||||
func (d *ResponseDirective) ProxyRevalidate() bool {
|
||||
return d.proxyRevalidate
|
||||
}
|
||||
|
||||
func (d *ResponseDirective) SMaxAge() (uint64, bool) {
|
||||
if v := d.sMaxAge; v != nil {
|
||||
return *v, true
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (d *ResponseDirective) Extensions() map[string]string {
|
||||
return d.extensions
|
||||
}
|
||||
|
||||
func (d *ResponseDirective) Extension(name string) string {
|
||||
return d.extensions[name]
|
||||
}
|
||||
Reference in New Issue
Block a user