imageproxy/third_party/httpcache/httpcache.go
Will Norris 1ceba2538c add -forceCache flag to override no-store and private directives
The httpcache package is intended only to be used in private caches, so
it will cache responses marked `private` like normal.  However,
imageproxy is a shared cache, so these response should not be cached
under normal circumstances.  This change introduces a potentially
breaking change to start respecting the `private` cache directive in
responses.

This also adds a new `-forceCache` flag to ignore the `private` and
`no-store` directives, and cache all responses regardless.
2025-05-01 02:54:36 -07:00

40 lines
834 B
Go

package httpcache
import (
"net/http"
"sort"
"strings"
)
type CacheControl map[string]string
func ParseCacheControl(headers http.Header) CacheControl {
cc := CacheControl{}
ccHeader := headers.Get("Cache-Control")
for _, part := range strings.Split(ccHeader, ",") {
part = strings.Trim(part, " ")
if part == "" {
continue
}
if strings.ContainsRune(part, '=') {
keyval := strings.Split(part, "=")
cc[strings.ToLower(strings.Trim(keyval[0], " "))] = strings.Trim(keyval[1], ",")
} else {
cc[strings.ToLower(part)] = ""
}
}
return cc
}
func (cc CacheControl) String() string {
parts := make([]string, 0, len(cc))
for k, v := range cc {
if v == "" {
parts = append(parts, k)
} else {
parts = append(parts, k+"="+v)
}
}
sort.StringSlice(parts).Sort()
return strings.Join(parts, ", ")
}