From ce0988f48a62bad7e2ca9509311ea77545af27b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?W=C3=A8i=20C=C5=8Dngru=C3=AC?= Date: Mon, 19 Nov 2018 05:06:54 +0800 Subject: [PATCH] templates: delete ETag and Last-Modified headers (#2338) Fixes #1920 --- caddyhttp/templates/templates.go | 4 ++++ caddyhttp/templates/templates_test.go | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/caddyhttp/templates/templates.go b/caddyhttp/templates/templates.go index 4f7dce506dd..9a1ae4ba163 100644 --- a/caddyhttp/templates/templates.go +++ b/caddyhttp/templates/templates.go @@ -110,6 +110,10 @@ func (t Templates) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error // set the actual content length now that the template was executed w.Header().Set("Content-Length", strconv.Itoa(buf.Len())) + // delete the headers related to cache + w.Header().Del("ETag") + w.Header().Del("Last-Modified") + // get the modification time in preparation for http.ServeContent modTime, _ := time.Parse(http.TimeFormat, w.Header().Get("Last-Modified")) diff --git a/caddyhttp/templates/templates_test.go b/caddyhttp/templates/templates_test.go index 289f1a85dbd..3d50496e483 100644 --- a/caddyhttp/templates/templates_test.go +++ b/caddyhttp/templates/templates_test.go @@ -70,6 +70,7 @@ func TestTemplates(t *testing.T) { req string respCode int res string + bypass bool }{ { tpl: tmpl, @@ -113,6 +114,7 @@ func TestTemplates(t *testing.T) { respCode: http.StatusOK, res: `as it is{{.Include "header.html"}} `, + bypass: true, }, } { c := c @@ -135,6 +137,14 @@ func TestTemplates(t *testing.T) { if respBody != c.res { t.Fatalf("Test: the expected body %v is different from the response one: %v", c.res, respBody) } + + if !c.bypass { + eTag := rec.Header().Get("ETag") + lastModified := rec.Header().Get("Last-Modified") + if eTag != "" || lastModified != "" { + t.Fatalf("Test: expect a response without ETag or Last-Modified, got %v %v", eTag, lastModified) + } + } }) } }