diff --git a/cmd/imageproxy/main.go b/cmd/imageproxy/main.go index 396ca12..c17ddf0 100644 --- a/cmd/imageproxy/main.go +++ b/cmd/imageproxy/main.go @@ -51,6 +51,7 @@ var signatureKey = flag.String("signatureKey", "", "HMAC key used in calculating var scaleUp = flag.Bool("scaleUp", false, "allow images to scale beyond their original dimensions") var timeout = flag.Duration("timeout", 0, "time limit for requests served by this proxy") var version = flag.Bool("version", false, "print version information") +var userAgent = flag.String("userAgent", "willnorris/imageproxy", "specify the user-agent used by imageproxy when fetching images from origin website") func main() { flag.Parse() @@ -94,6 +95,7 @@ func main() { p.Timeout = *timeout p.ScaleUp = *scaleUp + p.UserAgent = *userAgent server := &http.Server{ Addr: *addr, diff --git a/imageproxy.go b/imageproxy.go index 4fb65b5..373e823 100644 --- a/imageproxy.go +++ b/imageproxy.go @@ -64,6 +64,9 @@ type Proxy struct { // If a call runs for longer than its time limit, a 504 Gateway Timeout // response is returned. A Timeout of zero means no timeout. Timeout time.Duration + + // The User-Agent used by imageproxy when requesting origin image + UserAgent string } // NewProxy constructs a new proxy. The provided http RoundTripper will be @@ -130,7 +133,11 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) { return } - resp, err := p.Client.Get(req.String()) + actualReq, _ := http.NewRequest("GET", req.String(), nil) + if p.UserAgent != "" { + actualReq.Header.Set("User-Agent", p.UserAgent) + } + resp, err := p.Client.Do(actualReq) if err != nil { msg := fmt.Sprintf("error fetching remote image: %v", err) glog.Error(msg) @@ -291,9 +298,8 @@ func (t *TransformingTransport) RoundTrip(req *http.Request) (*http.Response, er return t.Transport.RoundTrip(req) } - u := *req.URL - u.Fragment = "" - resp, err := t.CachingClient.Get(u.String()) + req.URL.Fragment = "" + resp, err := t.CachingClient.Do(req) if err != nil { return nil, err }