mirror of
https://github.com/willnorris/imageproxy.git
synced 2024-12-16 21:56:43 -05:00
parent
ab8916a938
commit
3444fd9cb4
2 changed files with 13 additions and 5 deletions
|
@ -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 version = flag.Bool("version", false, "Deprecated: this flag does nothing")
|
||||
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() {
|
||||
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.ScaleUp = *scaleUp
|
||||
p.Verbose = *verbose
|
||||
p.UserAgent = *userAgent
|
||||
|
||||
server := &http.Server{
|
||||
Addr: *addr,
|
||||
|
|
|
@ -76,6 +76,9 @@ type Proxy struct {
|
|||
// ContentTypes specifies a list of content types to allow. An empty
|
||||
// list means all content types are allowed.
|
||||
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
|
||||
|
@ -150,7 +153,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)
|
||||
log.Print(msg)
|
||||
|
@ -345,9 +352,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
|
||||
}
|
||||
|
@ -368,7 +374,7 @@ func (t *TransformingTransport) RoundTrip(req *http.Request) (*http.Response, er
|
|||
|
||||
img, err := Transform(b, opt)
|
||||
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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue