Explorar el Código

Add IMGPROXY_PATH_PREFIX config

DarthSim hace 5 años
padre
commit
70e26cdd97
Se han modificado 6 ficheros con 19 adiciones y 5 borrados
  1. 1 0
      CHANGELOG.md
  2. 4 0
      config.go
  3. 1 0
      docs/configuration.md
  4. 8 2
      processing_options.go
  5. 4 2
      router.go
  6. 1 1
      server.go

+ 1 - 0
CHANGELOG.md

@@ -3,6 +3,7 @@
 ## [Unreleased]
 ## Addded
 - Video thumbnails.
+- `IMGPROXY_PATH_PREFIX` config.
 
 ## Changed
 - Quantizr updated to 0.2.0 in Docker image.

+ 4 - 0
config.go

@@ -167,6 +167,8 @@ type config struct {
 
 	SoReuseport bool
 
+	PathPrefix string
+
 	MaxSrcDimension    int
 	MaxSrcResolution   int
 	MaxSrcFileSize     int
@@ -295,6 +297,8 @@ func configure() error {
 
 	boolEnvConfig(&conf.SoReuseport, "IMGPROXY_SO_REUSEPORT")
 
+	strEnvConfig(&conf.PathPrefix, "IMGPROXY_PATH_PREFIX")
+
 	intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION")
 	megaIntEnvConfig(&conf.MaxSrcResolution, "IMGPROXY_MAX_SRC_RESOLUTION")
 	intEnvConfig(&conf.MaxSrcFileSize, "IMGPROXY_MAX_SRC_FILE_SIZE")

+ 1 - 0
docs/configuration.md

@@ -37,6 +37,7 @@ echo $(xxd -g 2 -l 64 -p /dev/random | tr -d '\n')
 * `IMGPROXY_TTL`: duration (in seconds) sent in `Expires` and `Cache-Control: max-age` HTTP headers. Default: `3600` (1 hour);
 * `IMGPROXY_CACHE_CONTROL_PASSTHROUGH`: when `true` and source image response contains `Expires` or `Cache-Control` headers, reuse those headers. Default: false;
 * `IMGPROXY_SO_REUSEPORT`: when `true`, enables `SO_REUSEPORT` socket option (currently on linux and darwin only);
+* `IMGPROXY_PATH_PREFIX`: URL path prefix. Example: when set to `/abc/def`, imgproxy URL will be `/abc/def/%signature/%processing_options/%source_url`. Default: blank.
 * `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;
 * `IMGPROXY_CUSTOM_REQUEST_HEADERS`: <img class="pro-badge" src="assets/pro.svg" alt="pro" /> list of custom headers that imgproxy will send while requesting the source image, divided by `\;` (can be redefined by `IMGPROXY_CUSTOM_HEADERS_SEPARATOR`). Example: `X-MyHeader1=Lorem\;X-MyHeader2=Ipsum`;

+ 8 - 2
processing_options.go

@@ -996,10 +996,17 @@ func parsePathBasic(parts []string, headers *processingHeaders) (string, *proces
 }
 
 func parsePath(ctx context.Context, r *http.Request) (context.Context, error) {
+	var err error
+
 	path := r.URL.RawPath
 	if len(path) == 0 {
 		path = r.URL.Path
 	}
+
+	if len(conf.PathPrefix) > 0 {
+		path = strings.TrimPrefix(path, conf.PathPrefix)
+	}
+
 	parts := strings.Split(strings.TrimPrefix(path, "/"), "/")
 
 	if len(parts) < 2 {
@@ -1007,7 +1014,7 @@ func parsePath(ctx context.Context, r *http.Request) (context.Context, error) {
 	}
 
 	if !conf.AllowInsecure {
-		if err := validatePath(parts[0], strings.TrimPrefix(path, fmt.Sprintf("/%s", parts[0]))); err != nil {
+		if err = validatePath(parts[0], strings.TrimPrefix(path, fmt.Sprintf("/%s", parts[0]))); err != nil {
 			return ctx, newError(403, err.Error(), msgForbidden)
 		}
 	}
@@ -1021,7 +1028,6 @@ func parsePath(ctx context.Context, r *http.Request) (context.Context, error) {
 
 	var imageURL string
 	var po *processingOptions
-	var err error
 
 	if conf.OnlyPresets {
 		imageURL, po, err = parsePathPresets(parts[1:], headers)

+ 4 - 2
router.go

@@ -27,6 +27,7 @@ type route struct {
 }
 
 type router struct {
+	prefix       string
 	Routes       []*route
 	PanicHandler panicHandler
 }
@@ -43,8 +44,9 @@ func (r *route) IsMatch(req *http.Request) bool {
 	return strings.HasPrefix(req.URL.Path, r.Prefix)
 }
 
-func newRouter() *router {
+func newRouter(prefix string) *router {
 	return &router{
+		prefix: prefix,
 		Routes: make([]*route, 0),
 	}
 }
@@ -52,7 +54,7 @@ func newRouter() *router {
 func (r *router) Add(method, prefix string, handler routeHandler, exact bool) {
 	r.Routes = append(
 		r.Routes,
-		&route{Method: method, Prefix: prefix, Handler: handler, Exact: exact},
+		&route{Method: method, Prefix: r.prefix + prefix, Handler: handler, Exact: exact},
 	)
 }
 

+ 1 - 1
server.go

@@ -17,7 +17,7 @@ var (
 )
 
 func buildRouter() *router {
-	r := newRouter()
+	r := newRouter(conf.PathPrefix)
 
 	r.PanicHandler = handlePanic