From 2b84ef01ed6b88461206b0769ae31a1a6e34daf3 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Tue, 24 Feb 2015 09:34:49 -0800 Subject: [PATCH] log requests and whether response is from cache also make sure request body is always closed fixes #17 --- imageproxy.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/imageproxy.go b/imageproxy.go index 0a70f2b..6baf532 100644 --- a/imageproxy.go +++ b/imageproxy.go @@ -94,6 +94,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { if req.Options != emptyOptions { u += "#" + req.Options.String() } + resp, err := p.Client.Get(u) if err != nil { msg := fmt.Sprintf("error fetching remote image: %v", err) @@ -101,6 +102,10 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { http.Error(w, msg, http.StatusInternalServerError) return } + defer resp.Body.Close() + + cached := resp.Header.Get(httpcache.XFromCache) + glog.Infof("request: %v (served from cache: %v)", *req, cached == "1") if resp.StatusCode != http.StatusOK { msg := fmt.Sprintf("remote URL %q returned status: %v", req.URL, resp.Status) @@ -120,7 +125,6 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { copyHeader(w, resp, "Content-Length") copyHeader(w, resp, "Content-Type") - defer resp.Body.Close() io.Copy(w, resp.Body) }