mirror of
https://github.com/willnorris/imageproxy.git
synced 2026-05-03 16:52:40 +02:00
refactor error handling logic
this is mostly a noop for now, but will make it easier to migrate to using httputil.ReverseProxy.
This commit is contained in:
parent
1dbc725ae3
commit
5f3c970a20
2 changed files with 31 additions and 19 deletions
|
|
@ -115,9 +115,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) {
|
||||
req, err := NewRequest(r, p.DefaultBaseURL)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("invalid request URL: %v", err)
|
||||
glog.Error(msg)
|
||||
http.Error(w, msg, http.StatusBadRequest)
|
||||
p.writeError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -125,16 +123,13 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) {
|
|||
req.Options.ScaleUp = p.ScaleUp
|
||||
|
||||
if err := p.allowed(req); err != nil {
|
||||
glog.Error(err)
|
||||
http.Error(w, err.Error(), http.StatusForbidden)
|
||||
p.writeError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := p.Client.Get(req.String())
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("error fetching remote image: %v", err)
|
||||
glog.Error(msg)
|
||||
http.Error(w, msg, http.StatusInternalServerError)
|
||||
p.writeError(w, err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
|
@ -154,6 +149,20 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) {
|
|||
io.Copy(w, resp.Body)
|
||||
}
|
||||
|
||||
// writerError writes err to the http response.
|
||||
func (p *Proxy) writeError(w http.ResponseWriter, err error) {
|
||||
type statusCoder interface {
|
||||
StatusCode() int
|
||||
}
|
||||
|
||||
glog.Error(err)
|
||||
code := http.StatusBadGateway
|
||||
if err, ok := err.(statusCoder); ok {
|
||||
code = err.StatusCode()
|
||||
}
|
||||
http.Error(w, err.Error(), code)
|
||||
}
|
||||
|
||||
// copyHeader copies header values from src to dst, adding to any existing
|
||||
// values with the same header name. If keys is not empty, only those header
|
||||
// keys will be copied.
|
||||
|
|
@ -176,7 +185,7 @@ func copyHeader(dst, src http.Header, keys ...string) {
|
|||
// allowed.
|
||||
func (p *Proxy) allowed(r *Request) error {
|
||||
if len(p.Referrers) > 0 && !validReferrer(p.Referrers, r.Original) {
|
||||
return fmt.Errorf("request does not contain an allowed referrer: %v", r)
|
||||
return permissionError(fmt.Sprintf("request does not contain an allowed referrer: %v", r))
|
||||
}
|
||||
|
||||
if len(p.Whitelist) == 0 && len(p.SignatureKey) == 0 {
|
||||
|
|
@ -191,7 +200,7 @@ func (p *Proxy) allowed(r *Request) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("request does not contain an allowed host or valid signature: %v", r)
|
||||
return permissionError(fmt.Sprintf("request does not contain an allowed host or valid signature: %v", r))
|
||||
}
|
||||
|
||||
// validHost returns whether the host in u matches one of hosts.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue