From 3c7ff7534432c3d7ada85f62625967589b7ef74b Mon Sep 17 00:00:00 2001 From: Medya Gh Date: Fri, 27 Dec 2019 23:14:02 -0800 Subject: [PATCH] Add option to override expire headers --- README.md | 7 +++++++ cmd/imageproxy/main.go | 12 +++++++++++- imageproxy.go | 10 +++++++++- imageproxy_test.go | 2 +- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0d242b3..a3f5118 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,13 @@ can see the go versions that are tested against in imageproxy URLs are of the form `http://localhost/{options}/{remote_url}`. +## Override request expires ## +To manually override the remote image expiration : + +``` +imageproxy -override-cache-control="public" -override-expires="Sat, 28 Dec 2199 04:09:32 GMT" +``` + ### Options ### Options are available for cropping, resizing, rotation, flipping, and digital diff --git a/cmd/imageproxy/main.go b/cmd/imageproxy/main.go index a6e63d6..a1d4a2d 100644 --- a/cmd/imageproxy/main.go +++ b/cmd/imageproxy/main.go @@ -47,6 +47,8 @@ var verbose = flag.Bool("verbose", false, "print verbose logging messages") var _ = flag.Bool("version", false, "Deprecated: this flag does nothing") var contentTypes = flag.String("contentTypes", "image/*", "comma separated list of allowed content types") var userAgent = flag.String("userAgent", "willnorris/imageproxy", "specify the user-agent used by imageproxy when fetching images from origin website") +var overrideCacheControl = flag.String("override-cache-control", "", "manually override the remote website Cache-Control header. for more info see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control") +var overrideExpires = flag.String("override-expires", "", "manually override the remote website expires header in form of Sat, 28 Dec 2019 04:09:32 GMT") func init() { flag.Var(&cache, "cache", "location to cache images (see https://github.com/willnorris/imageproxy#cache)") @@ -57,7 +59,15 @@ func main() { envy.Parse("IMAGEPROXY") flag.Parse() - p := imageproxy.NewProxy(nil, cache.Cache) + overrideOpts := map[string]string{} + if *overrideCacheControl != "" { + overrideOpts["Cache-Control"] = *overrideCacheControl + } + if *overrideExpires != "" { + overrideOpts["Expires"] = *overrideExpires + } + + p := imageproxy.NewProxy(nil, cache.Cache, overrideOpts) if *allowHosts != "" { p.AllowHosts = strings.Split(*allowHosts, ",") } diff --git a/imageproxy.go b/imageproxy.go index 8a37a56..e7f3949 100644 --- a/imageproxy.go +++ b/imageproxy.go @@ -85,12 +85,16 @@ type Proxy struct { // The User-Agent used by imageproxy when requesting origin image UserAgent string + + // Request header override key values. provided by user. (for example Expires) + HeaderOverrides map[string]string } // NewProxy constructs a new proxy. The provided http RoundTripper will be // used to fetch remote URLs. If nil is provided, http.DefaultTransport will // be used. -func NewProxy(transport http.RoundTripper, cache Cache) *Proxy { +// overrideHeader are key/values to override the request headers, such as expires +func NewProxy(transport http.RoundTripper, cache Cache, overrideHeader map[string]string) *Proxy { if transport == nil { transport, _ = aia.NewTransport() } @@ -217,6 +221,10 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) { copyHeader(w.Header(), resp.Header, "Cache-Control", "Last-Modified", "Expires", "Etag", "Link") + for k, v := range p.HeaderOverrides { + p.logf("overriding request header %s:%s", k, v) + resp.Header.Set(k, v) + } if should304(r, resp) { w.WriteHeader(http.StatusNotModified) return diff --git a/imageproxy_test.go b/imageproxy_test.go index c2d4b82..888ecd0 100644 --- a/imageproxy_test.go +++ b/imageproxy_test.go @@ -166,7 +166,7 @@ func TestAllowed(t *testing.T) { } for _, tt := range tests { - p := NewProxy(nil, nil) + p := NewProxy(nil, nil, map[string]string{}) p.AllowHosts = tt.allowHosts p.DenyHosts = tt.denyHosts p.SignatureKeys = tt.keys