mirror of
https://github.com/willnorris/imageproxy.git
synced 2024-12-16 21:56:43 -05:00
allow overriding the Logger used by Proxy
This commit is contained in:
parent
e1558d5626
commit
d4246a08fd
2 changed files with 73 additions and 6 deletions
|
@ -61,6 +61,9 @@ type Proxy struct {
|
||||||
// absolute.
|
// absolute.
|
||||||
DefaultBaseURL *url.URL
|
DefaultBaseURL *url.URL
|
||||||
|
|
||||||
|
// The Logger used by the image proxy
|
||||||
|
Logger *log.Logger
|
||||||
|
|
||||||
// SignatureKey is the HMAC key used to verify signed requests.
|
// SignatureKey is the HMAC key used to verify signed requests.
|
||||||
SignatureKey []byte
|
SignatureKey []byte
|
||||||
|
|
||||||
|
@ -105,7 +108,7 @@ func NewProxy(transport http.RoundTripper, cache Cache) *Proxy {
|
||||||
CachingClient: client,
|
CachingClient: client,
|
||||||
log: func(format string, v ...interface{}) {
|
log: func(format string, v ...interface{}) {
|
||||||
if proxy.Verbose {
|
if proxy.Verbose {
|
||||||
log.Printf(format, v...)
|
proxy.logf(format, v...)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -141,13 +144,13 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) {
|
||||||
req, err := NewRequest(r, p.DefaultBaseURL)
|
req, err := NewRequest(r, p.DefaultBaseURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg := fmt.Sprintf("invalid request URL: %v", err)
|
msg := fmt.Sprintf("invalid request URL: %v", err)
|
||||||
log.Print(msg)
|
p.log(msg)
|
||||||
http.Error(w, msg, http.StatusBadRequest)
|
http.Error(w, msg, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.allowed(req); err != nil {
|
if err := p.allowed(req); err != nil {
|
||||||
log.Printf("%s: %v", err, req)
|
p.logf("%s: %v", err, req)
|
||||||
http.Error(w, msgNotAllowed, http.StatusForbidden)
|
http.Error(w, msgNotAllowed, http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -166,7 +169,7 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
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)
|
p.log(msg)
|
||||||
http.Error(w, msg, http.StatusInternalServerError)
|
http.Error(w, msg, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -175,7 +178,7 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
cached := resp.Header.Get(httpcache.XFromCache)
|
cached := resp.Header.Get(httpcache.XFromCache)
|
||||||
if p.Verbose {
|
if p.Verbose {
|
||||||
log.Printf("request: %+v (served from cache: %t)", *actualReq, cached == "1")
|
p.logf("request: %+v (served from cache: %t)", *actualReq, cached == "1")
|
||||||
}
|
}
|
||||||
|
|
||||||
copyHeader(w.Header(), resp.Header, "Cache-Control", "Last-Modified", "Expires", "Etag", "Link")
|
copyHeader(w.Header(), resp.Header, "Cache-Control", "Last-Modified", "Expires", "Etag", "Link")
|
||||||
|
@ -193,7 +196,7 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) {
|
||||||
contentType = peekContentType(b)
|
contentType = peekContentType(b)
|
||||||
}
|
}
|
||||||
if resp.ContentLength != 0 && !contentTypeMatches(p.ContentTypes, contentType) {
|
if resp.ContentLength != 0 && !contentTypeMatches(p.ContentTypes, contentType) {
|
||||||
log.Printf("content-type not allowed: %q", contentType)
|
p.logf("content-type not allowed: %q", contentType)
|
||||||
http.Error(w, msgNotAllowed, http.StatusForbidden)
|
http.Error(w, msgNotAllowed, http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -368,6 +371,22 @@ func should304(req *http.Request, resp *http.Response) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Proxy) log(v ...interface{}) {
|
||||||
|
if p.Logger != nil {
|
||||||
|
p.Logger.Print(v...)
|
||||||
|
} else {
|
||||||
|
log.Print(v...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Proxy) logf(format string, v ...interface{}) {
|
||||||
|
if p.Logger != nil {
|
||||||
|
p.Logger.Printf(format, v...)
|
||||||
|
} else {
|
||||||
|
log.Printf(format, v...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TransformingTransport is an implementation of http.RoundTripper that
|
// TransformingTransport is an implementation of http.RoundTripper that
|
||||||
// optionally transforms images using the options specified in the request URL
|
// optionally transforms images using the options specified in the request URL
|
||||||
// fragment.
|
// fragment.
|
||||||
|
|
|
@ -21,9 +21,11 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"image/png"
|
"image/png"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -412,6 +414,52 @@ func TestProxy_ServeHTTP_is304(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestProxy_log(t *testing.T) {
|
||||||
|
var b strings.Builder
|
||||||
|
|
||||||
|
p := &Proxy{
|
||||||
|
Logger: log.New(&b, "", 0),
|
||||||
|
}
|
||||||
|
p.log("Test")
|
||||||
|
|
||||||
|
if got, want := b.String(), "Test\n"; got != want {
|
||||||
|
t.Errorf("log wrote %s, want %s", got, want)
|
||||||
|
}
|
||||||
|
|
||||||
|
b.Reset()
|
||||||
|
p.logf("Test %v", 123)
|
||||||
|
|
||||||
|
if got, want := b.String(), "Test 123\n"; got != want {
|
||||||
|
t.Errorf("logf wrote %s, want %s", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProxy_log_default(t *testing.T) {
|
||||||
|
var b strings.Builder
|
||||||
|
|
||||||
|
defer func(flags int) {
|
||||||
|
log.SetOutput(os.Stderr)
|
||||||
|
log.SetFlags(flags)
|
||||||
|
}(log.Flags())
|
||||||
|
|
||||||
|
log.SetOutput(&b)
|
||||||
|
log.SetFlags(0)
|
||||||
|
|
||||||
|
p := &Proxy{}
|
||||||
|
p.log("Test")
|
||||||
|
|
||||||
|
if got, want := b.String(), "Test\n"; got != want {
|
||||||
|
t.Errorf("log wrote %s, want %s", got, want)
|
||||||
|
}
|
||||||
|
|
||||||
|
b.Reset()
|
||||||
|
p.logf("Test %v", 123)
|
||||||
|
|
||||||
|
if got, want := b.String(), "Test 123\n"; got != want {
|
||||||
|
t.Errorf("logf wrote %s, want %s", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTransformingTransport(t *testing.T) {
|
func TestTransformingTransport(t *testing.T) {
|
||||||
client := new(http.Client)
|
client := new(http.Client)
|
||||||
tr := &TransformingTransport{
|
tr := &TransformingTransport{
|
||||||
|
|
Loading…
Reference in a new issue