|
|
@@ -1,7 +1,7 @@
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
-package proxy // import "miniflux.app/v2/internal/proxy"
|
|
|
+package mediaproxy // import "miniflux.app/v2/internal/mediaproxy"
|
|
|
|
|
|
import (
|
|
|
"net/http"
|
|
|
@@ -29,11 +29,11 @@ func TestProxyFilterWithHttpDefault(t *testing.T) {
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
|
|
|
|
|
input := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
|
expected := `<p><img src="/proxy/okK5PsdNY8F082UMQEAbLPeUFfbe2WnNfInNmR9T4WA=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" alt="Test"/></p>`
|
|
|
|
|
|
if expected != output {
|
|
|
- t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
+ t.Errorf(`Not expected output: got %q instead of %q`, output, expected)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -53,11 +53,11 @@ func TestProxyFilterWithHttpsDefault(t *testing.T) {
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
|
|
|
|
|
input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
|
expected := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
|
|
|
|
|
if expected != output {
|
|
|
- t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
+ t.Errorf(`Not expected output: got %q instead of %q`, output, expected)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -76,11 +76,11 @@ func TestProxyFilterWithHttpNever(t *testing.T) {
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
|
|
|
|
|
input := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
|
expected := input
|
|
|
|
|
|
if expected != output {
|
|
|
- t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
+ t.Errorf(`Not expected output: got %q instead of %q`, output, expected)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -99,11 +99,11 @@ func TestProxyFilterWithHttpsNever(t *testing.T) {
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
|
|
|
|
|
input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
|
expected := input
|
|
|
|
|
|
if expected != output {
|
|
|
- t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
+ t.Errorf(`Not expected output: got %q instead of %q`, output, expected)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -124,11 +124,11 @@ func TestProxyFilterWithHttpAlways(t *testing.T) {
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
|
|
|
|
|
input := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
|
expected := `<p><img src="/proxy/okK5PsdNY8F082UMQEAbLPeUFfbe2WnNfInNmR9T4WA=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" alt="Test"/></p>`
|
|
|
|
|
|
if expected != output {
|
|
|
- t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
+ t.Errorf(`Not expected output: got %q instead of %q`, output, expected)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -149,11 +149,11 @@ func TestProxyFilterWithHttpsAlways(t *testing.T) {
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
|
|
|
|
|
input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
|
expected := `<p><img src="/proxy/LdPNR1GBDigeeNp2ArUQRyZsVqT_PWLfHGjYFrrWWIY=/aHR0cHM6Ly93ZWJzaXRlL2ZvbGRlci9pbWFnZS5wbmc=" alt="Test"/></p>`
|
|
|
|
|
|
if expected != output {
|
|
|
- t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
+ t.Errorf(`Not expected output: got %q instead of %q`, output, expected)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -174,11 +174,62 @@ func TestAbsoluteProxyFilterWithHttpsAlways(t *testing.T) {
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
|
|
|
|
|
input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
|
|
- output := AbsoluteProxyRewriter(r, "localhost", input)
|
|
|
+ output := RewriteDocumentWithAbsoluteProxyURL(r, "localhost", input)
|
|
|
expected := `<p><img src="http://localhost/proxy/LdPNR1GBDigeeNp2ArUQRyZsVqT_PWLfHGjYFrrWWIY=/aHR0cHM6Ly93ZWJzaXRlL2ZvbGRlci9pbWFnZS5wbmc=" alt="Test"/></p>`
|
|
|
|
|
|
if expected != output {
|
|
|
- t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
+ t.Errorf(`Not expected output: got %q instead of %q`, output, expected)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestAbsoluteProxyFilterWithHttpsScheme(t *testing.T) {
|
|
|
+ os.Clearenv()
|
|
|
+ os.Setenv("PROXY_OPTION", "all")
|
|
|
+ os.Setenv("PROXY_MEDIA_TYPES", "image")
|
|
|
+ os.Setenv("PROXY_PRIVATE_KEY", "test")
|
|
|
+ os.Setenv("HTTPS", "1")
|
|
|
+
|
|
|
+ var err error
|
|
|
+ parser := config.NewParser()
|
|
|
+ config.Opts, err = parser.ParseEnvironmentVariables()
|
|
|
+ if err != nil {
|
|
|
+ t.Fatalf(`Parsing failure: %v`, err)
|
|
|
+ }
|
|
|
+
|
|
|
+ r := mux.NewRouter()
|
|
|
+ r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
|
|
+
|
|
|
+ input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
|
|
+ output := RewriteDocumentWithAbsoluteProxyURL(r, "localhost", input)
|
|
|
+ expected := `<p><img src="https://localhost/proxy/LdPNR1GBDigeeNp2ArUQRyZsVqT_PWLfHGjYFrrWWIY=/aHR0cHM6Ly93ZWJzaXRlL2ZvbGRlci9pbWFnZS5wbmc=" alt="Test"/></p>`
|
|
|
+
|
|
|
+ if expected != output {
|
|
|
+ t.Errorf(`Not expected output: got %q instead of %q`, output, expected)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestAbsoluteProxyFilterWithHttpsAlwaysAndAudioTag(t *testing.T) {
|
|
|
+ os.Clearenv()
|
|
|
+ os.Setenv("PROXY_OPTION", "all")
|
|
|
+ os.Setenv("PROXY_MEDIA_TYPES", "audio")
|
|
|
+ os.Setenv("PROXY_PRIVATE_KEY", "test")
|
|
|
+
|
|
|
+ var err error
|
|
|
+ parser := config.NewParser()
|
|
|
+ config.Opts, err = parser.ParseEnvironmentVariables()
|
|
|
+ if err != nil {
|
|
|
+ t.Fatalf(`Parsing failure: %v`, err)
|
|
|
+ }
|
|
|
+
|
|
|
+ r := mux.NewRouter()
|
|
|
+ r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
|
|
+
|
|
|
+ input := `<audio src="https://website/folder/audio.mp3"></audio>`
|
|
|
+ output := RewriteDocumentWithAbsoluteProxyURL(r, "localhost", input)
|
|
|
+ expected := `<audio src="http://localhost/proxy/EmBTvmU5B17wGuONkeknkptYopW_Tl6Y6_W8oYbN_Xs=/aHR0cHM6Ly93ZWJzaXRlL2ZvbGRlci9hdWRpby5tcDM="></audio>`
|
|
|
+
|
|
|
+ if expected != output {
|
|
|
+ t.Errorf(`Not expected output: got %q instead of %q`, output, expected)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -199,11 +250,11 @@ func TestProxyFilterWithHttpsAlwaysAndCustomProxyServer(t *testing.T) {
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
|
|
|
|
|
input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
|
expected := `<p><img src="https://proxy-example/proxy/aHR0cHM6Ly93ZWJzaXRlL2ZvbGRlci9pbWFnZS5wbmc=" alt="Test"/></p>`
|
|
|
|
|
|
if expected != output {
|
|
|
- t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
+ t.Errorf(`Not expected output: got %q instead of %q`, output, expected)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -224,19 +275,19 @@ func TestProxyFilterWithHttpsAlwaysAndIncorrectCustomProxyServer(t *testing.T) {
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
|
|
|
|
|
input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
|
expected := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
|
|
|
|
|
if expected != output {
|
|
|
- t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
+ t.Errorf(`Not expected output: got %q instead of %q`, output, expected)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func TestAbsoluteProxyFilterWithHttpsAlwaysAndCustomProxyServer(t *testing.T) {
|
|
|
os.Clearenv()
|
|
|
- os.Setenv("PROXY_OPTION", "all")
|
|
|
- os.Setenv("PROXY_MEDIA_TYPES", "image")
|
|
|
- os.Setenv("PROXY_URL", "https://proxy-example/proxy")
|
|
|
+ os.Setenv("MEDIA_PROXY_MODE", "all")
|
|
|
+ os.Setenv("MEDIA_PROXY_RESOURCE_TYPES", "image")
|
|
|
+ os.Setenv("MEDIA_PROXY_CUSTOM_URL", "https://proxy-example/proxy")
|
|
|
|
|
|
var err error
|
|
|
parser := config.NewParser()
|
|
|
@@ -249,11 +300,11 @@ func TestAbsoluteProxyFilterWithHttpsAlwaysAndCustomProxyServer(t *testing.T) {
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
|
|
|
|
|
input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithAbsoluteProxyURL(r, "localhost", input)
|
|
|
expected := `<p><img src="https://proxy-example/proxy/aHR0cHM6Ly93ZWJzaXRlL2ZvbGRlci9pbWFnZS5wbmc=" alt="Test"/></p>`
|
|
|
|
|
|
if expected != output {
|
|
|
- t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
+ t.Errorf(`Not expected output: got %q instead of %q`, output, expected)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -273,11 +324,11 @@ func TestProxyFilterWithHttpInvalid(t *testing.T) {
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
|
|
|
|
|
input := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
|
expected := `<p><img src="/proxy/okK5PsdNY8F082UMQEAbLPeUFfbe2WnNfInNmR9T4WA=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" alt="Test"/></p>`
|
|
|
|
|
|
if expected != output {
|
|
|
- t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
+ t.Errorf(`Not expected output: got %q instead of %q`, output, expected)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -297,11 +348,11 @@ func TestProxyFilterWithHttpsInvalid(t *testing.T) {
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
|
|
|
|
|
input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
|
expected := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
|
|
|
|
|
if expected != output {
|
|
|
- t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
+ t.Errorf(`Not expected output: got %q instead of %q`, output, expected)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -323,7 +374,7 @@ func TestProxyFilterWithSrcset(t *testing.T) {
|
|
|
|
|
|
input := `<p><img src="http://website/folder/image.png" srcset="http://website/folder/image2.png 656w, http://website/folder/image3.png 360w" alt="test"></p>`
|
|
|
expected := `<p><img src="/proxy/okK5PsdNY8F082UMQEAbLPeUFfbe2WnNfInNmR9T4WA=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" srcset="/proxy/aY5Hb4urDnUCly2vTJ7ExQeeaVS-52O7kjUr2v9VrAs=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlMi5wbmc= 656w, /proxy/QgAmrJWiAud_nNAsz3F8OTxaIofwAiO36EDzH_YfMzo=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlMy5wbmc= 360w" alt="test"/></p>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
|
|
|
|
if expected != output {
|
|
|
t.Errorf(`Not expected output: got %s`, output)
|
|
|
@@ -348,7 +399,7 @@ func TestProxyFilterWithEmptySrcset(t *testing.T) {
|
|
|
|
|
|
input := `<p><img src="http://website/folder/image.png" srcset="" alt="test"></p>`
|
|
|
expected := `<p><img src="/proxy/okK5PsdNY8F082UMQEAbLPeUFfbe2WnNfInNmR9T4WA=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" srcset="" alt="test"/></p>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
|
|
|
|
if expected != output {
|
|
|
t.Errorf(`Not expected output: got %s`, output)
|
|
|
@@ -373,7 +424,7 @@ func TestProxyFilterWithPictureSource(t *testing.T) {
|
|
|
|
|
|
input := `<picture><source srcset="http://website/folder/image2.png 656w, http://website/folder/image3.png 360w, https://website/some,image.png 2x"></picture>`
|
|
|
expected := `<picture><source srcset="/proxy/aY5Hb4urDnUCly2vTJ7ExQeeaVS-52O7kjUr2v9VrAs=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlMi5wbmc= 656w, /proxy/QgAmrJWiAud_nNAsz3F8OTxaIofwAiO36EDzH_YfMzo=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlMy5wbmc= 360w, /proxy/ZIw0hv8WhSTls5aSqhnFaCXlUrKIqTnBRaY0-NaLnds=/aHR0cHM6Ly93ZWJzaXRlL3NvbWUsaW1hZ2UucG5n 2x"/></picture>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
|
|
|
|
if expected != output {
|
|
|
t.Errorf(`Not expected output: got %s`, output)
|
|
|
@@ -398,7 +449,7 @@ func TestProxyFilterOnlyNonHTTPWithPictureSource(t *testing.T) {
|
|
|
|
|
|
input := `<picture><source srcset="http://website/folder/image2.png 656w, https://website/some,image.png 2x"></picture>`
|
|
|
expected := `<picture><source srcset="/proxy/aY5Hb4urDnUCly2vTJ7ExQeeaVS-52O7kjUr2v9VrAs=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlMi5wbmc= 656w, https://website/some,image.png 2x"/></picture>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
|
|
|
|
if expected != output {
|
|
|
t.Errorf(`Not expected output: got %s`, output)
|
|
|
@@ -422,7 +473,7 @@ func TestProxyWithImageDataURL(t *testing.T) {
|
|
|
|
|
|
input := `<img src="data:image/gif;base64,test">`
|
|
|
expected := `<img src="data:image/gif;base64,test"/>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
|
|
|
|
if expected != output {
|
|
|
t.Errorf(`Not expected output: got %s`, output)
|
|
|
@@ -446,7 +497,7 @@ func TestProxyWithImageSourceDataURL(t *testing.T) {
|
|
|
|
|
|
input := `<picture><source srcset="data:image/gif;base64,test"/></picture>`
|
|
|
expected := `<picture><source srcset="data:image/gif;base64,test"/></picture>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
|
|
|
|
if expected != output {
|
|
|
t.Errorf(`Not expected output: got %s`, output)
|
|
|
@@ -471,7 +522,7 @@ func TestProxyFilterWithVideo(t *testing.T) {
|
|
|
|
|
|
input := `<video poster="https://example.com/img.png" src="https://example.com/video.mp4"></video>`
|
|
|
expected := `<video poster="/proxy/aDFfroYL57q5XsojIzATT6OYUCkuVSPXYJQAVrotnLw=/aHR0cHM6Ly9leGFtcGxlLmNvbS9pbWcucG5n" src="/proxy/0y3LR8zlx8S8qJkj1qWFOO6x3a-5yf2gLWjGIJV5yyc=/aHR0cHM6Ly9leGFtcGxlLmNvbS92aWRlby5tcDQ="></video>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
|
|
|
|
if expected != output {
|
|
|
t.Errorf(`Not expected output: got %s`, output)
|
|
|
@@ -496,7 +547,7 @@ func TestProxyFilterVideoPoster(t *testing.T) {
|
|
|
|
|
|
input := `<video poster="https://example.com/img.png" src="https://example.com/video.mp4"></video>`
|
|
|
expected := `<video poster="/proxy/aDFfroYL57q5XsojIzATT6OYUCkuVSPXYJQAVrotnLw=/aHR0cHM6Ly9leGFtcGxlLmNvbS9pbWcucG5n" src="https://example.com/video.mp4"></video>`
|
|
|
- output := ProxyRewriter(r, input)
|
|
|
+ output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
|
|
|
|
if expected != output {
|
|
|
t.Errorf(`Not expected output: got %s`, output)
|