Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong Content-Length after ESI parsing #445

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}
Loading