diff --git a/README.adoc b/README.adoc index e0cef5b..42d35ad 100644 --- a/README.adoc +++ b/README.adoc @@ -302,6 +302,7 @@ Flags: --ssl-key=STRING ssl private key (key.pem) path --ssl-cert=STRING ssl cert (cert.pem) path --max-size=INT-64 Max allowed response size (KB) + --max-size-redirect URL to redirect to when max-size is exceeded --timeout=4s Upstream request timeout --max-redirects=3 Maximum number of redirects to follow --metrics Enable Prometheus compatible metrics endpoint diff --git a/cmd/go-camo/main.go b/cmd/go-camo/main.go index 2e61063..1ad5858 100644 --- a/cmd/go-camo/main.go +++ b/cmd/go-camo/main.go @@ -81,6 +81,7 @@ type CLI struct { MaxSize int64 `name:"max-size" help:"Max allowed response size (KB)"` ReqTimeout time.Duration `name:"timeout" default:"4s" help:"Upstream request timeout"` MaxRedirects int `name:"max-redirects" default:"3" help:"Maximum number of redirects to follow"` + MaxSizeRedirect string `long:"max-size-redirect" description:"URL to redirect when max-size is exceeded"` Metrics bool `name:"metrics" help:"Enable Prometheus compatible metrics endpoint"` NoDebugVars bool `name:"no-debug-vars" help:"Disable the /debug/vars/ metrics endpoint. This option has no effects when the metrics are not enabled."` NoLogTS bool `name:"no-log-ts" help:"Do not add a timestamp to logging"` @@ -227,6 +228,7 @@ func (cli *CLI) Run() { config.MaxSize = cli.MaxSize * 1024 config.RequestTimeout = cli.ReqTimeout config.MaxRedirects = cli.MaxRedirects + config.MaxSizeRedirect = cli.MaxSizeRedirect config.ServerName = ServerName // configure metrics collection in camo diff --git a/pkg/camo/proxy.go b/pkg/camo/proxy.go index 070929e..45fddf3 100644 --- a/pkg/camo/proxy.go +++ b/pkg/camo/proxy.go @@ -36,6 +36,8 @@ type Config struct { HMACKey []byte // MaxSize is the maximum valid image size response (in bytes). MaxSize int64 + // MaxSizeRedirect is the URL to redirect when MaxSize is exceeded. + MaxSizeRedirect string // MaxRedirects is the maximum number of redirects to follow. MaxRedirects int // Request timeout is a timeout for fetching upstream data. @@ -246,7 +248,11 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, req *http.Request) { if mlog.HasDebug() { mlog.Debugx("content length exceeded", mlog.A("url", sURL)) } - http.Error(w, "Content length exceeded", http.StatusNotFound) + if p.config.MaxSizeRedirect != "" { + http.Redirect(w, req, p.config.MaxSizeRedirect, http.StatusFound) + } else { + http.Error(w, "Content length exceeded", http.StatusNotFound) + } return }