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

allow custom User-Agent when fetching remote image

Closes #83
This commit is contained in:
Hugues Alary 2017-07-31 18:28:48 -07:00 committed by Will Norris
parent ab8916a938
commit 3444fd9cb4
2 changed files with 13 additions and 5 deletions

View file

@ -53,6 +53,7 @@ var timeout = flag.Duration("timeout", 0, "time limit for requests served by thi
var verbose = flag.Bool("verbose", false, "print verbose logging messages") var verbose = flag.Bool("verbose", false, "print verbose logging messages")
var version = flag.Bool("version", false, "Deprecated: this flag does nothing") var version = flag.Bool("version", false, "Deprecated: this flag does nothing")
var contentTypes = flag.String("contentTypes", "image/*", "comma separated list of allowed content types") var contentTypes = flag.String("contentTypes", "image/*", "comma separated list of allowed content types")
var userAgent = flag.String("userAgent", "willnorris/imageproxy", "specify the user-agent used by imageproxy when fetching images from origin website")
func init() { func init() {
flag.Var(&cache, "cache", "location to cache images (see https://github.com/willnorris/imageproxy#cache)") flag.Var(&cache, "cache", "location to cache images (see https://github.com/willnorris/imageproxy#cache)")
@ -98,6 +99,7 @@ func main() {
p.Timeout = *timeout p.Timeout = *timeout
p.ScaleUp = *scaleUp p.ScaleUp = *scaleUp
p.Verbose = *verbose p.Verbose = *verbose
p.UserAgent = *userAgent
server := &http.Server{ server := &http.Server{
Addr: *addr, Addr: *addr,

View file

@ -76,6 +76,9 @@ type Proxy struct {
// ContentTypes specifies a list of content types to allow. An empty // ContentTypes specifies a list of content types to allow. An empty
// list means all content types are allowed. // list means all content types are allowed.
ContentTypes []string ContentTypes []string
// The User-Agent used by imageproxy when requesting origin image
UserAgent string
} }
// NewProxy constructs a new proxy. The provided http RoundTripper will be // NewProxy constructs a new proxy. The provided http RoundTripper will be
@ -150,7 +153,11 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) {
return 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 { if err != nil {
msg := fmt.Sprintf("error fetching remote image: %v", err) msg := fmt.Sprintf("error fetching remote image: %v", err)
log.Print(msg) log.Print(msg)
@ -345,9 +352,8 @@ func (t *TransformingTransport) RoundTrip(req *http.Request) (*http.Response, er
return t.Transport.RoundTrip(req) return t.Transport.RoundTrip(req)
} }
u := *req.URL req.URL.Fragment = ""
u.Fragment = "" resp, err := t.CachingClient.Do(req)
resp, err := t.CachingClient.Get(u.String())
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -368,7 +374,7 @@ func (t *TransformingTransport) RoundTrip(req *http.Request) (*http.Response, er
img, err := Transform(b, opt) img, err := Transform(b, opt)
if err != nil { if err != nil {
log.Printf("error transforming image %s: %v", u.String(), err) log.Printf("error transforming image %s: %v", req.URL.String(), err)
img = b img = b
} }