0
Fork 0
mirror of https://github.com/willnorris/imageproxy.git synced 2024-12-16 21:56:43 -05:00

minor cleanup of error messages

- used same string for logging and error response to ensure they stay in
  sync
- pass through any http error status codes from remote URLs
This commit is contained in:
Will Norris 2014-11-21 17:56:59 -08:00
parent 8ada90ffd2
commit 2dd79ebd20
2 changed files with 13 additions and 8 deletions

View file

@ -154,7 +154,7 @@ func NewRequest(r *http.Request) (*Request, error) {
} }
if req.URL.Scheme != "http" && req.URL.Scheme != "https" { if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
return nil, URLError{"remote URL must have http or https URL", r.URL} return nil, URLError{"remote URL must have http or https scheme", r.URL}
} }
// query string is always part of the remote URL // query string is always part of the remote URL

View file

@ -66,14 +66,16 @@ func NewProxy(transport http.RoundTripper, cache Cache) *Proxy {
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
req, err := NewRequest(r) req, err := NewRequest(r)
if err != nil { if err != nil {
glog.Errorf("invalid request URL: %v", err) msg := fmt.Sprintf("invalid request URL: %v", err)
http.Error(w, fmt.Sprintf("invalid request URL: %v", err), http.StatusBadRequest) glog.Error(msg)
http.Error(w, msg, http.StatusBadRequest)
return return
} }
if !p.allowed(req.URL) { if !p.allowed(req.URL) {
glog.Errorf("remote URL is not for an allowed host: %v", req.URL) msg := fmt.Sprintf("remote URL is not for an allowed host: %v", req.URL)
http.Error(w, fmt.Sprintf("remote URL is not for an allowed host: %v", req.URL), http.StatusBadRequest) glog.Error(msg)
http.Error(w, msg, http.StatusBadRequest)
return return
} }
@ -83,13 +85,16 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
resp, err := p.Client.Get(u) resp, err := p.Client.Get(u)
if err != nil { if err != nil {
glog.Errorf("error fetching remote image: %v", err) msg := fmt.Sprintf("error fetching remote image: %v", err)
http.Error(w, fmt.Sprintf("Error fetching remote image: %v", err), http.StatusInternalServerError) glog.Error(msg)
http.Error(w, msg, http.StatusInternalServerError)
return return
} }
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
http.Error(w, fmt.Sprintf("Remote URL %q returned status: %v", req.URL, resp.Status), http.StatusInternalServerError) msg := fmt.Sprintf("remote URL %q returned status: %v", req.URL, resp.Status)
glog.Error(msg)
http.Error(w, msg, resp.StatusCode)
return return
} }