allow customizing response headers passed to client

A nil slice of headers will use the previous set of default response
headers to maintain existing behavior. The same list of headers is
repeated as the default flag value in `cmd/imageproxy` as documentation
for users to know what values they are overriding (and might want to
still include).

Fixes #387
This commit is contained in:
Will Norris 2025-06-30 18:11:15 -04:00
parent 554bfc5d8e
commit 7f3639886b
2 changed files with 17 additions and 1 deletions

View file

@ -39,6 +39,7 @@ var includeReferer = flag.Bool("includeReferer", false, "include referer header
var followRedirects = flag.Bool("followRedirects", true, "follow redirects") var followRedirects = flag.Bool("followRedirects", true, "follow redirects")
var baseURL = flag.String("baseURL", "", "default base URL for relative remote URLs") var baseURL = flag.String("baseURL", "", "default base URL for relative remote URLs")
var passRequestHeaders = flag.String("passRequestHeaders", "", "comma separatetd list of request headers to pass to remote server") var passRequestHeaders = flag.String("passRequestHeaders", "", "comma separatetd list of request headers to pass to remote server")
var passResponseHeaders = flag.String("passResponseHeaders", "Cache-Control,Last-Modified,Expires,Etag,Link", "comma separated list of response headers to pass from remote server")
var cache tieredCache var cache tieredCache
var signatureKeys signatureKeyList var signatureKeys signatureKeyList
var scaleUp = flag.Bool("scaleUp", false, "allow images to scale beyond their original dimensions") var scaleUp = flag.Bool("scaleUp", false, "allow images to scale beyond their original dimensions")
@ -75,6 +76,12 @@ func main() {
if *passRequestHeaders != "" { if *passRequestHeaders != "" {
p.PassRequestHeaders = strings.Split(*passRequestHeaders, ",") p.PassRequestHeaders = strings.Split(*passRequestHeaders, ",")
} }
if *passResponseHeaders != "" {
p.PassResponseHeaders = strings.Split(*passResponseHeaders, ",")
} else {
// set to a non-nil empty slice to pass no headers.
p.PassResponseHeaders = []string{}
}
p.SignatureKeys = signatureKeys p.SignatureKeys = signatureKeys
if *baseURL != "" { if *baseURL != "" {
var err error var err error

View file

@ -94,6 +94,10 @@ type Proxy struct {
// requests to the proxied server. // requests to the proxied server.
PassRequestHeaders []string PassRequestHeaders []string
// PassResponseHeaders identifies HTTP headers to pass from server responses to the proxy client.
// If nil, a default set of headers is passed: Cache-Control, Last-Modified, Expires, Etag, Link.
PassResponseHeaders []string
// MinimumCacheDuration is the minimum duration to cache remote images. // MinimumCacheDuration is the minimum duration to cache remote images.
// This will override cache duration from the remote server. // This will override cache duration from the remote server.
MinimumCacheDuration time.Duration MinimumCacheDuration time.Duration
@ -312,7 +316,12 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) {
metricServedFromCache.Inc() metricServedFromCache.Inc()
} }
copyHeader(w.Header(), resp.Header, "Cache-Control", "Last-Modified", "Expires", "Etag", "Link") if p.PassResponseHeaders == nil {
// pass default set of response headers
copyHeader(w.Header(), resp.Header, "Cache-Control", "Last-Modified", "Expires", "Etag", "Link")
} else {
copyHeader(w.Header(), resp.Header, p.PassResponseHeaders...)
}
if should304(r, resp) { if should304(r, resp) {
w.WriteHeader(http.StatusNotModified) w.WriteHeader(http.StatusNotModified)