mirror of
https://github.com/willnorris/imageproxy.git
synced 2025-02-03 23:10:17 -05:00
Allow specifying a custom User-Agent when requesting original image
Related to Issue/PR #83
This commit is contained in:
parent
50f6f640b2
commit
4cba1889fa
2 changed files with 12 additions and 4 deletions
|
@ -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 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 timeout = flag.Duration("timeout", 0, "time limit for requests served by this proxy")
|
||||||
var version = flag.Bool("version", false, "print version information")
|
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() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
@ -94,6 +95,7 @@ func main() {
|
||||||
|
|
||||||
p.Timeout = *timeout
|
p.Timeout = *timeout
|
||||||
p.ScaleUp = *scaleUp
|
p.ScaleUp = *scaleUp
|
||||||
|
p.UserAgent = *userAgent
|
||||||
|
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
Addr: *addr,
|
Addr: *addr,
|
||||||
|
|
|
@ -64,6 +64,9 @@ type Proxy struct {
|
||||||
// If a call runs for longer than its time limit, a 504 Gateway Timeout
|
// If a call runs for longer than its time limit, a 504 Gateway Timeout
|
||||||
// response is returned. A Timeout of zero means no timeout.
|
// response is returned. A Timeout of zero means no timeout.
|
||||||
Timeout time.Duration
|
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
|
// 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
|
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)
|
||||||
glog.Error(msg)
|
glog.Error(msg)
|
||||||
|
@ -291,9 +298,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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue