Skip to content

Commit

Permalink
Fix wrong Content-Length after ESI parsing (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
p0358 committed Feb 9, 2024
1 parent 8bbbd77 commit 6a3a385
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
10 changes: 5 additions & 5 deletions pkg/middleware/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package middleware
import (
"bytes"
"net/http"
"strconv"
"sync"

"github.com/darkweak/go-esi/esi"
Expand Down Expand Up @@ -79,13 +80,12 @@ func (r *CustomWriter) Write(b []byte) (int, error) {

// Send delays the response to handle Cache-Status
func (r *CustomWriter) Send() (int, error) {
contentLength := r.Header().Get(rfc.StoredLengthHeader)
if contentLength != "" {
r.Header().Set("Content-Length", contentLength)
}

defer r.Buf.Reset()
r.Header().Set("Content-Length", r.Header().Get(rfc.StoredLengthHeader))
b := esi.Parse(r.Buf.Bytes(), r.Req)
if len(b) != 0 {
r.Header().Set("Content-Length", strconv.Itoa(len(b)))
}
r.Header().Del(rfc.StoredLengthHeader)
r.Header().Del(rfc.StoredTTLHeader)

Expand Down
63 changes: 63 additions & 0 deletions plugins/caddy/httpcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,3 +787,66 @@ func TestVaryHandler(t *testing.T) {
checker(res, 119)
}
}

func TestESITags(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(`
{
admin localhost:2999
http_port 9080
https_port 9443
cache {
ttl 1000s
}
}
localhost:9080 {
route /esi-include-1 {
cache
respond "esi-include-1 with some long content to ensure the compute works well. Also add some dummy text with some $pecial characters without recursive esi includes"
}
route /esi-include-2 {
cache
respond "esi-include-2"
}
route /esi-path {
cache
header Cache-Control "max-age=60"
respond "Hello <esi:include src=\"http://localhost:9080/esi-include-1\"/> and <esi:include src=\"http://localhost:9080/esi-include-2\"/>!"
}
}`, "caddyfile")

resp1, _ := tester.AssertGetResponse(`http://localhost:9080/esi-path`, 200, "Hello esi-include-1 with some long content to ensure the compute works well. Also add some dummy text with some $pecial characters without recursive esi includes and esi-include-2!")
if resp1.Header.Get("Age") != "" {
t.Errorf("unexpected Age header %v", resp1.Header.Get("Age"))
}
if resp1.Header.Get("Cache-Status") != "Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/esi-path" {
t.Errorf("unexpected Cache-Status header %v", resp1.Header.Get("Cache-Status"))
}
if resp1.Header.Get("Content-Length") != "180" {
t.Errorf("unexpected Content-Length header %v", resp1.Header.Get("Content-Length"))
}

resp2, _ := tester.AssertGetResponse(`http://localhost:9080/esi-path`, 200, "Hello esi-include-1 with some long content to ensure the compute works well. Also add some dummy text with some $pecial characters without recursive esi includes and esi-include-2!")
if resp2.Header.Get("Age") == "" {
t.Error("Age header should be present")
}
if resp2.Header.Get("Age") != "1" {
t.Error("Age header should be present")
}

resp3, _ := tester.AssertGetResponse(`http://localhost:9080/esi-include-1`, 200, "esi-include-1 with some long content to ensure the compute works well. Also add some dummy text with some $pecial characters without recursive esi includes")
if resp3.Header.Get("Age") == "" {
t.Error("Age header should be present")
}
if resp3.Header.Get("Cache-Status") == "Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/esi-include-1" {
t.Error("Cache-Status should be already stored")
}

resp4, _ := tester.AssertGetResponse(`http://localhost:9080/esi-include-2`, 200, "esi-include-2")
if resp4.Header.Get("Age") == "" {
t.Error("Age header should be present")
}
if resp4.Header.Get("Cache-Status") == "Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/esi-include-2" {
t.Error("Cache-Status should be already stored")
}
}

0 comments on commit 6a3a385

Please sign in to comment.