Skip to content

Commit

Permalink
Merge pull request #4 from m-s-1-3/feature/content-type-check
Browse files Browse the repository at this point in the history
Improve Content-Type check
  • Loading branch information
1cedsoda committed Apr 1, 2024
2 parents 5eb6805 + ca62b43 commit e62694b
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"
"net/http"
"os"
"strings"
"time"
)

Expand Down Expand Up @@ -151,12 +152,17 @@ func (h *PluginHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
myrw.Header().Set("Accept-Encoding", "identity")
h.next.ServeHTTP(myrw, req)

if myrw.Header().Get("Content-Type") == "text/html" {
if strings.HasPrefix(myrw.Header().Get("Content-Type"), "text/html") {
// h.log(fmt.Sprintf("Inject %s", req.URL.EscapedPath()))
bytes := myrw.buffer.Bytes()
newBytes := regexReplaceSingle(bytes, insertBeforeRegex, h.scriptHtml)
rw.Write(newBytes)
injected = true
origBytes := myrw.buffer.Bytes()
newBytes := regexReplaceSingle(origBytes, insertBeforeRegex, h.scriptHtml)
if !bytes.Equal(origBytes, newBytes) {
_, err := rw.Write(newBytes)
if err != nil {
h.log(err.Error())
}
injected = true
}
}
}

Expand All @@ -179,6 +185,5 @@ type responseWriter struct {
}

func (w *responseWriter) Write(p []byte) (int, error) {
w.buffer.Reset()
return w.buffer.Write(p)
}

0 comments on commit e62694b

Please sign in to comment.