Skip to content

Commit e6582ce

Browse files
authored
add HEAD process (#21)
1 parent 5800ff1 commit e6582ce

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

cmd/Caddyfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
cache_type in_memory
1717
match_path /
18-
match_header Content-Type image/jpg image/png "text/plain; charset=utf-8"
18+
match_header Content-Type image/jpg image/png "text/plain; charset=utf-8" "application/json" ""
1919

2020
}
2121

handler.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"runtime/debug"
87
"time"
98

109
"net/http"
@@ -66,6 +65,13 @@ func (h *Handler) addStatusHeaderIfConfigured(w http.ResponseWriter, status stri
6665
func (h *Handler) respond(w http.ResponseWriter, entry *Entry, cacheStatus string) error {
6766
h.addStatusHeaderIfConfigured(w, cacheStatus)
6867
copyHeaders(entry.Response.snapHeader, w.Header())
68+
69+
// when the request method is head, we don't need ot perform write body
70+
if entry.Request.Method == "HEAD" {
71+
w.WriteHeader(entry.Response.Code)
72+
return nil
73+
}
74+
6975
err := entry.WriteBodyTo(w)
7076
return err
7177
}
@@ -264,6 +270,10 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyht
264270
if exists && previousEntry.isPublic {
265271
if err := h.respond(w, previousEntry, cacheHit); err == nil {
266272
return nil
273+
} else if _, ok := err.(backends.NoPreCollectError); ok {
274+
// if the err is No pre collect, just return nil
275+
w.WriteHeader(previousEntry.Response.Code)
276+
return nil
267277
}
268278
}
269279

@@ -325,7 +335,6 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyht
325335
err = h.respond(w, entry, cacheMiss)
326336
if err != nil {
327337
h.logger.Error("cache handler", zap.Error(err))
328-
debug.PrintStack()
329338
return caddyhttp.Error(entry.Response.Code, err)
330339
}
331340

@@ -335,7 +344,6 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyht
335344
err = h.respond(w, entry, cacheSkip)
336345
if err != nil {
337346
h.logger.Error("cache handler", zap.Error(err))
338-
debug.PrintStack()
339347
return caddyhttp.Error(entry.Response.Code, err)
340348
}
341349

response.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
func copyHeaders(from http.Header, to http.Header) {
1515
for k, values := range from {
1616
for _, v := range values {
17-
to.Add(k, v)
17+
to.Set(k, v)
1818
}
1919
}
2020
}

0 commit comments

Comments
 (0)