mirror of
https://github.com/willnorris/imageproxy.git
synced 2026-04-25 12:56:23 +02:00
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.
40 lines
834 B
Go
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, ", ")
|
|
}
|