Skip to content

Commit

Permalink
fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
1cedsoda committed Feb 23, 2024
1 parent 9f78f68 commit b335275
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
6 changes: 5 additions & 1 deletion demo/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ http:
headers:
customResponseHeaders:
Content-Type: "text/html"

compress:
compress: true

plain-to-html:
chain:
middlewares:
- plain-to-html-header
- plain-to-html-body
- plain-to-html-header
- compress
9 changes: 6 additions & 3 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,17 @@ func (h *PluginHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
myReq.SetSupportedEncoding()
h.next.ServeHTTP(myRw, &myReq.Request)

// check if response is injectable
if myRw.IsInjectable() {
// h.log(fmt.Sprintf("Inject %s", req.URL.EscapedPath()))
body, err := myRw.ReadDecoded()
if err != nil {
newBody := InsertAtBodyEnd(body, h.scriptHtml)
myRw.WriteEncoded(newBody, encoding)
injected = true
h.log(fmt.Sprintf("Error: %s", err))
}
newBody := InsertAtBodyEnd(body, h.scriptHtml)
myRw.WriteEncoded(newBody, encoding)
rw.Write(myRw.Read())
injected = true
}
}

Expand Down
8 changes: 8 additions & 0 deletions response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package traefik_umami_plugin

import (
"bytes"
"fmt"
"net/http"
)

Expand Down Expand Up @@ -47,12 +48,14 @@ func (w *ResponseWriter) Write(p []byte) (int, error) {
// Write body bytes
// Compresses the body to the target encoding.
func (w *ResponseWriter) WriteEncoded(plain []byte, encoding *Encoding) (int, error) {
size := len(plain)
encoded, err := Encode(plain, encoding)
if err != nil {
return 0, err
}
w.Write(encoded)
w.SetContentEncoding(encoding)
w.SetContentLength(size)
return len(plain), nil
}

Expand All @@ -66,3 +69,8 @@ func (w *ResponseWriter) GetContentEncoding() (*Encoding, error) {
func (w *ResponseWriter) SetContentEncoding(encoding *Encoding) {
w.Header().Set("Content-Encoding", encoding.name)
}

// set content size.
func (w *ResponseWriter) SetContentLength(size int) {
w.Header().Set("Content-Length", fmt.Sprintf("%d", size))
}

0 comments on commit b335275

Please sign in to comment.