Skip to content

Commit 7f36398

Browse files
committed
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
1 parent 554bfc5 commit 7f36398

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

cmd/imageproxy/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var includeReferer = flag.Bool("includeReferer", false, "include referer header
3939
var followRedirects = flag.Bool("followRedirects", true, "follow redirects")
4040
var baseURL = flag.String("baseURL", "", "default base URL for relative remote URLs")
4141
var passRequestHeaders = flag.String("passRequestHeaders", "", "comma separatetd list of request headers to pass to remote server")
42+
var passResponseHeaders = flag.String("passResponseHeaders", "Cache-Control,Last-Modified,Expires,Etag,Link", "comma separated list of response headers to pass from remote server")
4243
var cache tieredCache
4344
var signatureKeys signatureKeyList
4445
var scaleUp = flag.Bool("scaleUp", false, "allow images to scale beyond their original dimensions")
@@ -75,6 +76,12 @@ func main() {
7576
if *passRequestHeaders != "" {
7677
p.PassRequestHeaders = strings.Split(*passRequestHeaders, ",")
7778
}
79+
if *passResponseHeaders != "" {
80+
p.PassResponseHeaders = strings.Split(*passResponseHeaders, ",")
81+
} else {
82+
// set to a non-nil empty slice to pass no headers.
83+
p.PassResponseHeaders = []string{}
84+
}
7885
p.SignatureKeys = signatureKeys
7986
if *baseURL != "" {
8087
var err error

imageproxy.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ type Proxy struct {
9494
// requests to the proxied server.
9595
PassRequestHeaders []string
9696

97+
// PassResponseHeaders identifies HTTP headers to pass from server responses to the proxy client.
98+
// If nil, a default set of headers is passed: Cache-Control, Last-Modified, Expires, Etag, Link.
99+
PassResponseHeaders []string
100+
97101
// MinimumCacheDuration is the minimum duration to cache remote images.
98102
// This will override cache duration from the remote server.
99103
MinimumCacheDuration time.Duration
@@ -312,7 +316,12 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) {
312316
metricServedFromCache.Inc()
313317
}
314318

315-
copyHeader(w.Header(), resp.Header, "Cache-Control", "Last-Modified", "Expires", "Etag", "Link")
319+
if p.PassResponseHeaders == nil {
320+
// pass default set of response headers
321+
copyHeader(w.Header(), resp.Header, "Cache-Control", "Last-Modified", "Expires", "Etag", "Link")
322+
} else {
323+
copyHeader(w.Header(), resp.Header, p.PassResponseHeaders...)
324+
}
316325

317326
if should304(r, resp) {
318327
w.WriteHeader(http.StatusNotModified)

0 commit comments

Comments
 (0)