From 55d6902276bc4f87d9afff97b3e85b8ab5af136e Mon Sep 17 00:00:00 2001 From: Simon Striekwold Date: Sun, 16 Jun 2024 11:17:07 +0200 Subject: [PATCH] Fix path bug, and only inject into html responses --- proxy.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/proxy.go b/proxy.go index e153e96..83275df 100644 --- a/proxy.go +++ b/proxy.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "net/url" + "strings" "time" ) @@ -61,7 +62,6 @@ func proxyHandler( return func(w http.ResponseWriter, req *http.Request) { req.URL.Scheme = origin.Scheme req.URL.Host = origin.Host - req.URL.Path = origin.Path req.Host = origin.Host req.RequestURI = "" req.Header.Set("X-Forwarded-For", req.RemoteAddr) @@ -91,13 +91,16 @@ func proxyHandler( return } - if bytes.Contains(body, endBodyTag) { - body = bytes.Replace( - body, endBodyTag, - append(script, endBodyTag...), - 1) - } else { - body = append(body, script...) + ct := resp.Header.Get("Content-Type") + if strings.Contains(ct, "text/html") { + if bytes.Contains(body, endBodyTag) { + body = bytes.Replace( + body, endBodyTag, + append(script, endBodyTag...), + 1) + } else { + body = append(body, script...) + } } w.WriteHeader(resp.StatusCode)