From aa9b18a8d601aae7f694c41f842ed231c0a2b0e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Sun, 2 Apr 2023 18:24:29 -0700 Subject: [PATCH] Make sure PROXY_IMAGES option is backward compatible Bug introduced in PR #1610 Fixes #1753 --- config/config_test.go | 60 +++++++++++++++++++++++++++++++++++++++++++ config/parser.go | 10 ++++++-- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index 1725caa5..de502d7a 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1226,6 +1226,66 @@ func TestProxyMediaTypes(t *testing.T) { } } +func TestProxyMediaTypesWithDuplicatedValues(t *testing.T) { + os.Clearenv() + os.Setenv("PROXY_MEDIA_TYPES", "image,audio, image") + + parser := NewParser() + opts, err := parser.ParseEnvironmentVariables() + if err != nil { + t.Fatalf(`Parsing failure: %v`, err) + } + + expected := []string{"audio", "image"} + if len(expected) != len(opts.ProxyMediaTypes()) { + t.Fatalf(`Unexpected PROXY_MEDIA_TYPES value, got %v instead of %v`, opts.ProxyMediaTypes(), expected) + } + + resultMap := make(map[string]bool) + for _, mediaType := range opts.ProxyMediaTypes() { + resultMap[mediaType] = true + } + + for _, mediaType := range expected { + if !resultMap[mediaType] { + t.Fatalf(`Unexpected PROXY_MEDIA_TYPES value, got %v instead of %v`, opts.ProxyMediaTypes(), expected) + } + } +} + +func TestProxyImagesOptionBackwardCompatibility(t *testing.T) { + os.Clearenv() + os.Setenv("PROXY_IMAGES", "all") + + parser := NewParser() + opts, err := parser.ParseEnvironmentVariables() + if err != nil { + t.Fatalf(`Parsing failure: %v`, err) + } + + expected := []string{"image"} + if len(expected) != len(opts.ProxyMediaTypes()) { + t.Fatalf(`Unexpected PROXY_MEDIA_TYPES value, got %v instead of %v`, opts.ProxyMediaTypes(), expected) + } + + resultMap := make(map[string]bool) + for _, mediaType := range opts.ProxyMediaTypes() { + resultMap[mediaType] = true + } + + for _, mediaType := range expected { + if !resultMap[mediaType] { + t.Fatalf(`Unexpected PROXY_MEDIA_TYPES value, got %v instead of %v`, opts.ProxyMediaTypes(), expected) + } + } + + expectedProxyOption := "all" + result := opts.ProxyOption() + if result != expectedProxyOption { + t.Fatalf(`Unexpected PROXY_OPTION value, got %q instead of %q`, result, expectedProxyOption) + } +} + func TestDefaultProxyMediaTypes(t *testing.T) { os.Clearenv() diff --git a/config/parser.go b/config/parser.go index 6ffca5c4..770870d0 100644 --- a/config/parser.go +++ b/config/parser.go @@ -141,7 +141,6 @@ func (p *Parser) parseLines(lines []string) (err error) { // kept for compatibility purpose case "PROXY_IMAGES": p.opts.proxyOption = parseString(value, defaultProxyOption) - p.opts.proxyMediaTypes = append(p.opts.proxyMediaTypes, "image") case "PROXY_HTTP_CLIENT_TIMEOUT": p.opts.proxyHTTPClientTimeout = parseInt(value, defaultProxyHTTPClientTimeout) case "PROXY_OPTION": @@ -297,9 +296,16 @@ func parseStringList(value string, fallback []string) []string { } var strList []string + strMap := make(map[string]bool) + items := strings.Split(value, ",") for _, item := range items { - strList = append(strList, strings.TrimSpace(item)) + itemValue := strings.TrimSpace(item) + + if _, found := strMap[itemValue]; !found { + strMap[itemValue] = true + strList = append(strList, itemValue) + } } return strList