Skip to content

Commit 1bf0515

Browse files
committed
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.
1 parent bf8d7a0 commit 1bf0515

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

imageproxy.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,28 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
109109
return
110110
}
111111

112-
w.Header().Add("Last-Modified", resp.Header.Get("Last-Modified"))
113-
w.Header().Add("Expires", resp.Header.Get("Expires"))
114-
w.Header().Add("Etag", resp.Header.Get("Etag"))
112+
copyHeader(w, resp, "Last-Modified")
113+
copyHeader(w, resp, "Expires")
114+
copyHeader(w, resp, "Etag")
115115

116116
if is304 := check304(r, resp); is304 {
117117
w.WriteHeader(http.StatusNotModified)
118118
return
119119
}
120120

121-
w.Header().Add("Content-Length", resp.Header.Get("Content-Length"))
122-
w.Header().Add("Content-Type", resp.Header.Get("Content-Type"))
121+
copyHeader(w, resp, "Content-Length")
122+
copyHeader(w, resp, "Content-Type")
123123
defer resp.Body.Close()
124124
io.Copy(w, resp.Body)
125125
}
126126

127+
func copyHeader(w http.ResponseWriter, r *http.Response, header string) {
128+
key := http.CanonicalHeaderKey(header)
129+
if value, ok := r.Header[key]; ok {
130+
w.Header()[key] = value
131+
}
132+
}
133+
127134
// allowed returns whether the specified URL is on the whitelist of remote hosts.
128135
func (p *Proxy) allowed(u *url.URL) bool {
129136
if len(p.Whitelist) == 0 {

0 commit comments

Comments
 (0)