Browse Source

Sending User-Agent with source image request

DarthSim 6 years ago
parent
commit
ea10cbe059
3 changed files with 14 additions and 1 deletions
  1. 5 0
      config.go
  2. 1 0
      docs/configuration.md
  3. 8 1
      download.go

+ 5 - 0
config.go

@@ -140,6 +140,8 @@ type config struct {
 
 	AllowOrigin string
 
+	UserAgent string
+
 	IgnoreSslVerification bool
 
 	LocalFileSystemRoot string
@@ -176,6 +178,7 @@ var conf = config{
 	AllowInsecure:         false,
 	Quality:               80,
 	GZipCompression:       5,
+	UserAgent:             fmt.Sprintf("imgproxy/%s", version),
 	ETagEnabled:           false,
 	S3Enabled:             false,
 	WatermarkOpacity:      1,
@@ -227,6 +230,8 @@ func init() {
 
 	strEnvConfig(&conf.AllowOrigin, "IMGPROXY_ALLOW_ORIGIN")
 
+	strEnvConfig(&conf.UserAgent, "IMGPROXY_USER_AGENT")
+
 	boolEnvConfig(&conf.IgnoreSslVerification, "IMGPROXY_IGNORE_SSL_VERIFICATION")
 
 	strEnvConfig(&conf.LocalFileSystemRoot, "IMGPROXY_LOCAL_FILESYSTEM_ROOT")

+ 1 - 0
docs/configuration.md

@@ -30,6 +30,7 @@ $ echo $(xxd -g 2 -l 64 -p /dev/random | tr -d '\n')
 * `IMGPROXY_CONCURRENCY`: the maximum number of image requests to be processed simultaneously. Default: number of CPU cores times two;
 * `IMGPROXY_MAX_CLIENTS`: the maximum number of simultaneous active connections. Default: `IMGPROXY_CONCURRENCY * 10`;
 * `IMGPROXY_TTL`: duration (in seconds) sent in `Expires` and `Cache-Control: max-age` HTTP headers. Default: `3600` (1 hour);
+* `IMGPROXY_USER_AGENT`: User-Agent header that will be sent with source image request. Default: `imgproxy/%current_version`;
 * `IMGPROXY_USE_ETAG`: when `true`, enables using [ETag](https://en.wikipedia.org/wiki/HTTP_ETag) HTTP header for HTTP cache control. Default: false;
 
 ### Security

+ 8 - 1
download.go

@@ -149,7 +149,14 @@ func downloadImage(ctx context.Context) (context.Context, context.CancelFunc, er
 		defer startPrometheusDuration(prometheusDownloadDuration)()
 	}
 
-	res, err := downloadClient.Get(url)
+	req, err := http.NewRequest("GET", url, nil)
+	if err != nil {
+		return ctx, func() {}, err
+	}
+
+	req.Header.Set("User-Agent", conf.UserAgent)
+
+	res, err := downloadClient.Do(req)
 	if err != nil {
 		return ctx, func() {}, err
 	}