Sfoglia il codice sorgente

Ensure URL replacements are executed in the specified order

DarthSim 1 anno fa
parent
commit
a246eda065

+ 4 - 2
config/config.go

@@ -16,6 +16,8 @@ import (
 	"github.com/imgproxy/imgproxy/v3/version"
 )
 
+type URLReplacement = configurators.URLReplacement
+
 var (
 	Network                string
 	Bind                   string
@@ -127,7 +129,7 @@ var (
 	LastModifiedEnabled bool
 
 	BaseURL         string
-	URLReplacements map[*regexp.Regexp]string
+	URLReplacements []URLReplacement
 
 	Presets     []string
 	OnlyPresets bool
@@ -318,7 +320,7 @@ func Reset() {
 	LastModifiedEnabled = false
 
 	BaseURL = ""
-	URLReplacements = make(map[*regexp.Regexp]string)
+	URLReplacements = make([]URLReplacement, 0)
 
 	Presets = make([]string, 0)
 	OnlyPresets = false

+ 19 - 11
config/configurators/configurators.go

@@ -12,6 +12,11 @@ import (
 	"github.com/imgproxy/imgproxy/v3/imagetype"
 )
 
+type URLReplacement struct {
+	Regexp      *regexp.Regexp
+	Replacement string
+}
+
 func Int(i *int, name string) {
 	if env, err := strconv.Atoi(os.Getenv(name)); err == nil {
 		*i = env
@@ -240,21 +245,24 @@ func Patterns(s *[]*regexp.Regexp, name string) {
 	}
 }
 
-func Replacements(m *map[*regexp.Regexp]string, name string) error {
-	var sm map[string]string
-
-	if err := StringMap(&sm, name); err != nil {
-		return err
-	}
+func Replacements(s *[]URLReplacement, name string) error {
+	if env := os.Getenv(name); len(env) > 0 {
+		ss := []URLReplacement(nil)
 
-	if len(sm) > 0 {
-		mm := make(map[*regexp.Regexp]string)
+		keyvalues := strings.Split(env, ";")
 
-		for k, v := range sm {
-			mm[RegexpFromPattern(k)] = v
+		for _, keyvalue := range keyvalues {
+			parts := strings.SplitN(keyvalue, "=", 2)
+			if len(parts) != 2 {
+				return fmt.Errorf("Invalid key/value: %s", keyvalue)
+			}
+			ss = append(ss, URLReplacement{
+				Regexp:      RegexpFromPattern(parts[0]),
+				Replacement: parts[1],
+			})
 		}
 
-		*m = mm
+		*s = ss
 	}
 
 	return nil

+ 6 - 4
options/processing_options_test.go

@@ -56,8 +56,9 @@ func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithBase() {
 }
 
 func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithReplacement() {
-	config.URLReplacements = map[*regexp.Regexp]string{
-		regexp.MustCompile("^test://([^/]*)/"): "http://images.dev/${1}/dolor/",
+	config.URLReplacements = []config.URLReplacement{
+		{Regexp: regexp.MustCompile("^test://([^/]*)/"), Replacement: "test2://images.dev/${1}/dolor/"},
+		{Regexp: regexp.MustCompile("^test2://"), Replacement: "http://"},
 	}
 
 	originURL := "test://lorem/ipsum.jpg?param=value"
@@ -112,8 +113,9 @@ func (s *ProcessingOptionsTestSuite) TestParsePlainURLWithBase() {
 }
 
 func (s *ProcessingOptionsTestSuite) TestParsePlainURLWithReplacement() {
-	config.URLReplacements = map[*regexp.Regexp]string{
-		regexp.MustCompile("^test://([^/]*)/"): "http://images.dev/${1}/dolor/",
+	config.URLReplacements = []config.URLReplacement{
+		{Regexp: regexp.MustCompile("^test://([^/]*)/"), Replacement: "test2://images.dev/${1}/dolor/"},
+		{Regexp: regexp.MustCompile("^test2://"), Replacement: "http://"},
 	}
 
 	originURL := "test://lorem/ipsum.jpg"

+ 2 - 2
options/url.go

@@ -13,8 +13,8 @@ import (
 const urlTokenPlain = "plain"
 
 func preprocessURL(u string) string {
-	for re, repl := range config.URLReplacements {
-		u = re.ReplaceAllString(u, repl)
+	for _, repl := range config.URLReplacements {
+		u = repl.Regexp.ReplaceAllString(u, repl.Replacement)
 	}
 
 	if len(config.BaseURL) == 0 || strings.HasPrefix(u, config.BaseURL) {