Skip to content

Commit

Permalink
Fix path bug, and only inject into html responses
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonMTS committed Jun 16, 2024
1 parent 254c11b commit 55d6902
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/http"
"net/url"
"strings"
"time"
)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 55d6902

Please sign in to comment.