From 1bf0515cef1c7e8cad8d0534994537f1030b23ea Mon Sep 17 00:00:00 2001 From: Will Norris Date: Fri, 20 Feb 2015 23:51:07 -0800 Subject: [PATCH] be a little more precise in copying headers it doesn't matter too much right now, given the headers that are being copied, but this now makes sure that all header values get copied over if multiple are present. --- imageproxy.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/imageproxy.go b/imageproxy.go index 17a8395..0a70f2b 100644 --- a/imageproxy.go +++ b/imageproxy.go @@ -109,21 +109,28 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - w.Header().Add("Last-Modified", resp.Header.Get("Last-Modified")) - w.Header().Add("Expires", resp.Header.Get("Expires")) - w.Header().Add("Etag", resp.Header.Get("Etag")) + copyHeader(w, resp, "Last-Modified") + copyHeader(w, resp, "Expires") + copyHeader(w, resp, "Etag") if is304 := check304(r, resp); is304 { w.WriteHeader(http.StatusNotModified) return } - w.Header().Add("Content-Length", resp.Header.Get("Content-Length")) - w.Header().Add("Content-Type", resp.Header.Get("Content-Type")) + copyHeader(w, resp, "Content-Length") + copyHeader(w, resp, "Content-Type") defer resp.Body.Close() io.Copy(w, resp.Body) } +func copyHeader(w http.ResponseWriter, r *http.Response, header string) { + key := http.CanonicalHeaderKey(header) + if value, ok := r.Header[key]; ok { + w.Header()[key] = value + } +} + // allowed returns whether the specified URL is on the whitelist of remote hosts. func (p *Proxy) allowed(u *url.URL) bool { if len(p.Whitelist) == 0 {