@marcel91200 / caddy Mirror · read-only / commits / 26e55966
Fork 0

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, 05:15 UTC

httpcaddyfile: Support single-line matchers (#3263)

* httpcaddyfile: Support single-line matchers * httpcaddyfile: Add single-line matcher test * httpcaddyfile: Add a matcher syntax adapt test

Francis Lavoie committed May 5, 2020 at 14:29 UTC 26e559662d677062874afed9775f660f1f5f9c1e
3 files changed +88 -1
caddyconfig/httpcaddyfile/httptype.go
+1 -1
@@ -1001,7 +1001,7 @@ func parseMatcherDefinitions(d *caddyfile.Dispenser, matchers map[string]caddy.M
1001 // handle more than one segment); otherwise, we'd overwrite other
1002 // instances of the matcher in this set
1003 tokensByMatcherName := make(map[string][]caddyfile.Token)
1004 - for nesting := d.Nesting(); d.NextBlock(nesting); {
1004 + for nesting := d.Nesting(); d.NextArg() || d.NextBlock(nesting); {
1005 matcherName := d.Val()
1006 tokensByMatcherName[matcherName] = append(tokensByMatcherName[matcherName], d.NextSegment()...)
1007 }
caddyconfig/httpcaddyfile/httptype_test.go
+7
@@ -50,6 +50,13 @@ func TestMatcherSyntax(t *testing.T) {
50 expectWarn: false,
51 expectError: false,
52 },
53 + {
54 + input: `http://localhost
55 + @debug not path /somepath*
56 + `,
57 + expectWarn: false,
58 + expectError: false,
59 + },
60 } {
61
62 adapter := caddyfile.Adapter{
caddytest/integration/caddyfile_adapt_test.go
+80
@@ -283,3 +283,83 @@ func TestHttpOnlyOnNonStandardPort(t *testing.T) {
283 }
284 }`)
285 }
286 +
287 +func TestMatcherSyntax(t *testing.T) {
288 + caddytest.AssertAdapt(t, `
289 + :80 {
290 + @matcher {
291 + method GET
292 + }
293 + respond @matcher "get"
294 +
295 + @matcher2 method POST
296 + respond @matcher2 "post"
297 +
298 + @matcher3 not method PUT
299 + respond @matcher3 "not put"
300 + }
301 + `, "caddyfile", `{
302 + "apps": {
303 + "http": {
304 + "servers": {
305 + "srv0": {
306 + "listen": [
307 + ":80"
308 + ],
309 + "routes": [
310 + {
311 + "match": [
312 + {
313 + "method": [
314 + "GET"
315 + ]
316 + }
317 + ],
318 + "handle": [
319 + {
320 + "body": "get",
321 + "handler": "static_response"
322 + }
323 + ]
324 + },
325 + {
326 + "match": [
327 + {
328 + "method": [
329 + "POST"
330 + ]
331 + }
332 + ],
333 + "handle": [
334 + {
335 + "body": "post",
336 + "handler": "static_response"
337 + }
338 + ]
339 + },
340 + {
341 + "match": [
342 + {
343 + "not": [
344 + {
345 + "method": [
346 + "PUT"
347 + ]
348 + }
349 + ]
350 + }
351 + ],
352 + "handle": [
353 + {
354 + "body": "not put",
355 + "handler": "static_response"
356 + }
357 + ]
358 + }
359 + ]
360 + }
361 + }
362 + }
363 + }
364 +}`)
365 +}
\ No newline at end of file