0
Fork 0
mirror of https://github.com/willnorris/imageproxy.git synced 2025-01-27 23:04:32 -05:00

Allow specifying a custom User-Agent when requesting original image

Related to Issue/PR #83
This commit is contained in:
Hugues Alary 2017-07-31 18:28:48 -07:00
parent 50f6f640b2
commit 4cba1889fa
2 changed files with 12 additions and 4 deletions

View file

@ -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,

View file

@ -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
}