Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 01:30 UTC
fileserver: Fix compression breaks using httpInclude (#4352) (#4358)
Y.Horie committed
Oct 17, 2021 at 00:09 UTC
64f8b557b1b1d4ca869ee7e0cba91f09e86883ef
2 files changed
+70
-29
modules/caddyhttp/templates/tplcontext.go
+1
@@ -160,6 +160,7 @@ func (c TemplateContext) funcHTTPInclude(uri string) (string, error) {
160
}
161
virtReq.Host = c.Req.Host
162
virtReq.Header = c.Req.Header.Clone()
163
+ virtReq.Header.Set("Accept-Encoding", "identity") // https://github.com/caddyserver/caddy/issues/4352
164
virtReq.Trailer = c.Req.Trailer.Clone()
165
virtReq.Header.Set(recursionPreventionHeader, strconv.Itoa(recursionCount))
166
modules/caddyhttp/templates/tplcontext_test.go
+69
-29
@@ -16,6 +16,7 @@ package templates
16
17
import (
18
"bytes"
19
+ "context"
20
"fmt"
21
"net/http"
22
"os"
@@ -25,10 +26,49 @@ import (
26
"strings"
27
"testing"
28
"time"
29
+
30
+ "github.com/caddyserver/caddy/v2/modules/caddyhttp"
31
)
32
33
+type handle struct {
34
+}
35
+
36
+func (h *handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
37
+ if r.Header.Get("Accept-Encoding") == "identity" {
38
+ w.Write([]byte("good contents"))
39
+ } else {
40
+ w.Write([]byte("bad cause Accept-Encoding: " + r.Header.Get("Accept-Encoding")))
41
+ }
42
+}
43
+
44
+func TestHTTPInclude(t *testing.T) {
45
+ tplContext := getContextOrFail(t)
46
+ for i, test := range []struct {
47
+ uri string
48
+ handler *handle
49
+ expect string
50
+ }{
51
+ {
52
+ uri: "https://example.com/foo/bar",
53
+ handler: &handle{},
54
+ expect: "good contents",
55
+ },
56
+ } {
57
+ ctx := context.WithValue(tplContext.Req.Context(), caddyhttp.ServerCtxKey, test.handler)
58
+ tplContext.Req = tplContext.Req.WithContext(ctx)
59
+ tplContext.Req.Header.Add("Accept-Encoding", "gzip")
60
+ result, err := tplContext.funcHTTPInclude(test.uri)
61
+ if result != test.expect {
62
+ t.Errorf("Test %d: expected '%s' but got '%s'", i, test.expect, result)
63
+ }
64
+ if err != nil {
65
+ t.Errorf("Test %d: got error: %v", i, result)
66
+ }
67
+ }
68
+}
69
+
70
func TestMarkdown(t *testing.T) {
31
- context := getContextOrFail(t)
71
+ tplContext := getContextOrFail(t)
72
73
for i, test := range []struct {
74
body string
@@ -39,7 +79,7 @@ func TestMarkdown(t *testing.T) {
79
expect: "<ul>\n<li>str1</li>\n<li>str2</li>\n</ul>\n",
80
},
81
} {
42
- result, err := context.funcMarkdown(test.body)
82
+ result, err := tplContext.funcMarkdown(test.body)
83
if result != test.expect {
84
t.Errorf("Test %d: expected '%s' but got '%s'", i, test.expect, result)
85
}
@@ -80,9 +120,9 @@ func TestCookie(t *testing.T) {
120
expect: "cookieValue",
121
},
122
} {
83
- context := getContextOrFail(t)
84
- context.Req.AddCookie(test.cookie)
85
- actual := context.Cookie(test.cookieName)
123
+ tplContext := getContextOrFail(t)
124
+ tplContext.Req.AddCookie(test.cookie)
125
+ actual := tplContext.Cookie(test.cookieName)
126
if actual != test.expect {
127
t.Errorf("Test %d: Expected cookie value '%s' but got '%s' for cookie with name '%s'",
128
i, test.expect, actual, test.cookieName)
@@ -111,12 +151,12 @@ func TestImport(t *testing.T) {
151
shouldErr: true,
152
},
153
} {
114
- context := getContextOrFail(t)
154
+ tplContext := getContextOrFail(t)
155
var absFilePath string
156
157
// create files for test case
158
if test.fileName != "" {
119
- absFilePath := filepath.Join(fmt.Sprintf("%s", context.Root), test.fileName)
159
+ absFilePath := filepath.Join(fmt.Sprintf("%s", tplContext.Root), test.fileName)
160
if err := os.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil {
161
os.Remove(absFilePath)
162
t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
@@ -124,9 +164,9 @@ func TestImport(t *testing.T) {
164
}
165
166
// perform test
127
- context.NewTemplate("parent")
128
- actual, err := context.funcImport(test.fileName)
129
- templateWasDefined := strings.Contains(context.tpl.DefinedTemplates(), test.expect)
167
+ tplContext.NewTemplate("parent")
168
+ actual, err := tplContext.funcImport(test.fileName)
169
+ templateWasDefined := strings.Contains(tplContext.tpl.DefinedTemplates(), test.expect)
170
if err != nil {
171
if !test.shouldErr {
172
t.Errorf("Test %d: Expected no error, got: '%s'", i, err)
@@ -135,7 +175,7 @@ func TestImport(t *testing.T) {
175
t.Errorf("Test %d: Expected error but had none", i)
176
} else if !templateWasDefined && actual != "" {
177
// template should be defined, return value should be an empty string
138
- t.Errorf("Test %d: Expected template %s to be define but got %s", i, test.expect, context.tpl.DefinedTemplates())
178
+ t.Errorf("Test %d: Expected template %s to be define but got %s", i, test.expect, tplContext.tpl.DefinedTemplates())
179
180
}
181
@@ -191,12 +231,12 @@ func TestInclude(t *testing.T) {
231
args: "text",
232
},
233
} {
194
- context := getContextOrFail(t)
234
+ tplContext := getContextOrFail(t)
235
var absFilePath string
236
237
// create files for test case
238
if test.fileName != "" {
199
- absFilePath := filepath.Join(fmt.Sprintf("%s", context.Root), test.fileName)
239
+ absFilePath := filepath.Join(fmt.Sprintf("%s", tplContext.Root), test.fileName)
240
if err := os.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil {
241
os.Remove(absFilePath)
242
t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
@@ -204,7 +244,7 @@ func TestInclude(t *testing.T) {
244
}
245
246
// perform test
207
- actual, err := context.funcInclude(test.fileName, test.args)
247
+ actual, err := tplContext.funcInclude(test.fileName, test.args)
248
if err != nil {
249
if !test.shouldErr {
250
t.Errorf("Test %d: Expected no error, got: '%s'", i, err)
@@ -225,12 +265,12 @@ func TestInclude(t *testing.T) {
265
}
266
267
func TestCookieMultipleCookies(t *testing.T) {
228
- context := getContextOrFail(t)
268
+ tplContext := getContextOrFail(t)
269
270
cookieNameBase, cookieValueBase := "cookieName", "cookieValue"
271
272
for i := 0; i < 10; i++ {
233
- context.Req.AddCookie(&http.Cookie{
273
+ tplContext.Req.AddCookie(&http.Cookie{
274
Name: fmt.Sprintf("%s%d", cookieNameBase, i),
275
Value: fmt.Sprintf("%s%d", cookieValueBase, i),
276
})
@@ -238,7 +278,7 @@ func TestCookieMultipleCookies(t *testing.T) {
278
279
for i := 0; i < 10; i++ {
280
expectedCookieVal := fmt.Sprintf("%s%d", cookieValueBase, i)
241
- actualCookieVal := context.Cookie(fmt.Sprintf("%s%d", cookieNameBase, i))
281
+ actualCookieVal := tplContext.Cookie(fmt.Sprintf("%s%d", cookieNameBase, i))
282
if actualCookieVal != expectedCookieVal {
283
t.Errorf("Expected cookie value %s, found %s", expectedCookieVal, actualCookieVal)
284
}
@@ -246,7 +286,7 @@ func TestCookieMultipleCookies(t *testing.T) {
286
}
287
288
func TestIP(t *testing.T) {
249
- context := getContextOrFail(t)
289
+ tplContext := getContextOrFail(t)
290
for i, test := range []struct {
291
inputRemoteAddr string
292
expect string
@@ -257,15 +297,15 @@ func TestIP(t *testing.T) {
297
{"[2001:db8:a0b:12f0::1]", "[2001:db8:a0b:12f0::1]"},
298
{`[fe80:1::3%eth0]:44`, `fe80:1::3%eth0`},
299
} {
260
- context.Req.RemoteAddr = test.inputRemoteAddr
261
- if actual := context.RemoteIP(); actual != test.expect {
300
+ tplContext.Req.RemoteAddr = test.inputRemoteAddr
301
+ if actual := tplContext.RemoteIP(); actual != test.expect {
302
t.Errorf("Test %d: Expected %s but got %s", i, test.expect, actual)
303
}
304
}
305
}
306
307
func TestStripHTML(t *testing.T) {
268
- context := getContextOrFail(t)
308
+ tplContext := getContextOrFail(t)
309
310
for i, test := range []struct {
311
input string
@@ -302,7 +342,7 @@ func TestStripHTML(t *testing.T) {
342
expect: `<h1hi`,
343
},
344
} {
305
- actual := context.funcStripHTML(test.input)
345
+ actual := tplContext.funcStripHTML(test.input)
346
if actual != test.expect {
347
t.Errorf("Test %d: Expected %s, found %s. Input was StripHTML(%s)", i, test.expect, actual, test.input)
348
}
@@ -350,13 +390,13 @@ func TestFileListing(t *testing.T) {
390
verifyErr: os.IsNotExist,
391
},
392
} {
353
- context := getContextOrFail(t)
393
+ tplContext := getContextOrFail(t)
394
var dirPath string
395
var err error
396
397
// create files for test case
398
if test.fileNames != nil {
359
- dirPath, err = os.MkdirTemp(fmt.Sprintf("%s", context.Root), "caddy_ctxtest")
399
+ dirPath, err = os.MkdirTemp(fmt.Sprintf("%s", tplContext.Root), "caddy_ctxtest")
400
if err != nil {
401
t.Fatalf("Test %d: Expected no error creating directory, got: '%s'", i, err.Error())
402
}
@@ -371,7 +411,7 @@ func TestFileListing(t *testing.T) {
411
412
// perform test
413
input := filepath.ToSlash(filepath.Join(filepath.Base(dirPath), test.inputBase))
374
- actual, err := context.funcListFiles(input)
414
+ actual, err := tplContext.funcListFiles(input)
415
if err != nil {
416
if !test.shouldErr {
417
t.Errorf("Test %d: Expected no error, got: '%s'", i, err)
@@ -404,7 +444,7 @@ func TestFileListing(t *testing.T) {
444
}
445
446
func TestSplitFrontMatter(t *testing.T) {
407
- context := getContextOrFail(t)
447
+ tplContext := getContextOrFail(t)
448
449
for i, test := range []struct {
450
input string
@@ -465,7 +505,7 @@ title = "Welcome"
505
body: "\n### Test",
506
},
507
} {
468
- result, _ := context.funcSplitFrontMatter(test.input)
508
+ result, _ := tplContext.funcSplitFrontMatter(test.input)
509
if result.Meta["title"] != test.expect {
510
t.Errorf("Test %d: Expected %s, found %s. Input was SplitFrontMatter(%s)", i, test.expect, result.Meta["title"], test.input)
511
}
@@ -477,11 +517,11 @@ title = "Welcome"
517
}
518
519
func getContextOrFail(t *testing.T) TemplateContext {
480
- context, err := initTestContext()
520
+ tplContext, err := initTestContext()
521
if err != nil {
522
t.Fatalf("failed to prepare test context: %v", err)
523
}
484
- return context
524
+ return tplContext
525
}
526
527
func initTestContext() (TemplateContext, error) {