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(multiple): december 2024 wave #589

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions context/now.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ func (*nowContext) SetContextWithBaseRequest(req *http.Request, _ *http.Request)
func (cc *nowContext) SetupContext(_ configurationtypes.AbstractConfigurationInterface) {}

func (cc *nowContext) SetContext(req *http.Request) *http.Request {
now := time.Now().UTC()
req.Header.Set("Date", now.Format(time.RFC1123))
var now time.Time
var e error

now, e = time.Parse(time.RFC1123, req.Header.Get("Date"))

if e != nil {
now = time.Now().UTC()
req.Header.Set("Date", now.Format(time.RFC1123))
}

return req.WithContext(context.WithValue(req.Context(), Now, now))
}

Expand Down
15 changes: 13 additions & 2 deletions pkg/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,17 @@ func (s *SouinBaseHandler) Revalidate(validator *core.Revalidator, next handlerF
return nil, errors.New("")
}

if validator.IfModifiedSincePresent {
if lastModified, err := time.Parse(time.RFC1123, customWriter.Header().Get("Last-Modified")); err == nil && validator.IfModifiedSince.Sub(lastModified) > 0 {
customWriter.handleBuffer(func(b *bytes.Buffer) {
b.Reset()
})
customWriter.Rw.WriteHeader(http.StatusNotModified)

return nil, errors.New("")
}
}

if statusCode != http.StatusNotModified {
err = s.Store(customWriter, rq, requestCc, cachedKey, uri)
}
Expand Down Expand Up @@ -922,10 +933,10 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n
case <-req.Context().Done():
switch req.Context().Err() {
case baseCtx.DeadlineExceeded:
customWriter.WriteHeader(http.StatusGatewayTimeout)
s.Configuration.GetLogger().Infof("Internal server error on endpoint %s: %v", req.URL, s.Storers)
rw.Header().Set("Cache-Status", cacheName+"; fwd=bypass; detail=DEADLINE-EXCEEDED")
customWriter.Rw.WriteHeader(http.StatusGatewayTimeout)
_, _ = customWriter.Rw.Write([]byte("Internal server error"))
s.Configuration.GetLogger().Infof("Internal server error on endpoint %s: %v", req.URL, s.Storers)
return baseCtx.DeadlineExceeded
case baseCtx.Canceled:
return baseCtx.Canceled
Expand Down
2 changes: 1 addition & 1 deletion pkg/surrogate/providers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func uniqueTag(values []string) []string {
if _, found := tmp[item]; !found {
tmp[item] = true

if strings.Contains(item, "%3B") || strings.Contains(item, "%3A") {
if strings.Contains(item, "%") {
item, _ = url.QueryUnescape(item)
}
list = append(list, item)
Expand Down
59 changes: 59 additions & 0 deletions plugins/caddy/httpcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1281,3 +1281,62 @@ func TestAllowedAdditionalStatusCode(t *testing.T) {
t.Error("Age header should be present")
}
}

type testTimeoutHandler struct {
iterator int
}

func (t *testTimeoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t.iterator++
if t.iterator%2 == 0 {
time.Sleep(5 * time.Second)

return
}

w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("Hello timeout!"))
}

func TestTimeout(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(`
{
admin localhost:2999
http_port 9080
cache {
ttl 1ns
stale 1ns
timeout {
backend 1s
}
}
}
localhost:9080 {
route /cache-timeout {
cache
reverse_proxy localhost:9086
}
}`, "caddyfile")

go func() {
errorHandler := testTimeoutHandler{}
_ = http.ListenAndServe(":9086", &errorHandler)
}()
time.Sleep(time.Second)
resp1, _ := tester.AssertGetResponse(`http://localhost:9080/cache-timeout`, http.StatusOK, "Hello timeout!")
time.Sleep(time.Millisecond)
resp2, _ := tester.AssertGetResponse(`http://localhost:9080/cache-timeout`, http.StatusGatewayTimeout, "Internal server error")

if resp1.Header.Get("Cache-Status") != "Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/cache-timeout" {
t.Errorf("unexpected resp1 Cache-Status header %v", resp1.Header.Get("Cache-Status"))
}

if resp1.Header.Get("Age") != "" {
t.Errorf("unexpected resp1 Age header %v", resp1.Header.Get("Age"))
}

if resp2.Header.Get("Cache-Status") != "Souin; fwd=bypass; detail=DEADLINE-EXCEEDED" {
t.Errorf("unexpected resp2 Cache-Status header %v", resp2.Header.Get("Cache-Status"))
}
}
Loading