| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package caddyhttp |
| 16 | |
| 17 | import ( |
| 18 | "context" |
| 19 | "fmt" |
| 20 | "net/http" |
| 21 | "net/http/httptest" |
| 22 | "net/url" |
| 23 | "os" |
| 24 | "runtime" |
| 25 | "testing" |
| 26 | |
| 27 | "github.com/caddyserver/caddy/v2" |
| 28 | ) |
| 29 | |
| 30 | func TestHostMatcher(t *testing.T) { |
| 31 | err := os.Setenv("GO_BENCHMARK_DOMAIN", "localhost") |
| 32 | if err != nil { |
| 33 | t.Errorf("error while setting up environment: %v", err) |
| 34 | } |
| 35 | |
| 36 | for i, tc := range []struct { |
| 37 | match MatchHost |
| 38 | input string |
| 39 | expect bool |
| 40 | }{ |
| 41 | { |
| 42 | match: MatchHost{}, |
| 43 | input: "example.com", |
| 44 | expect: false, |
| 45 | }, |
| 46 | { |
| 47 | match: MatchHost{"example.com"}, |
| 48 | input: "example.com", |
| 49 | expect: true, |
| 50 | }, |
| 51 | { |
| 52 | match: MatchHost{"EXAMPLE.COM"}, |
| 53 | input: "example.com", |
| 54 | expect: true, |
| 55 | }, |
| 56 | { |
| 57 | match: MatchHost{"example.com"}, |
| 58 | input: "EXAMPLE.COM", |
| 59 | expect: true, |
| 60 | }, |
| 61 | { |
| 62 | match: MatchHost{"example.com"}, |
| 63 | input: "foo.example.com", |
| 64 | expect: false, |
| 65 | }, |
| 66 | { |
| 67 | match: MatchHost{"example.com"}, |
| 68 | input: "EXAMPLE.COM", |
| 69 | expect: true, |
| 70 | }, |
| 71 | { |
| 72 | match: MatchHost{"foo.example.com"}, |
| 73 | input: "foo.example.com", |
| 74 | expect: true, |
| 75 | }, |
| 76 | { |
| 77 | match: MatchHost{"foo.example.com"}, |
| 78 | input: "bar.example.com", |
| 79 | expect: false, |
| 80 | }, |
| 81 | { |
| 82 | match: MatchHost{"éxàmplê.com"}, |
| 83 | input: "xn--xmpl-0na6cm.com", |
| 84 | expect: true, |
| 85 | }, |
| 86 | { |
| 87 | match: MatchHost{"*.example.com"}, |
| 88 | input: "example.com", |
| 89 | expect: false, |
| 90 | }, |
| 91 | { |
| 92 | match: MatchHost{"*.example.com"}, |
| 93 | input: "SUB.EXAMPLE.COM", |
| 94 | expect: true, |
| 95 | }, |
| 96 | { |
| 97 | match: MatchHost{"*.example.com"}, |
| 98 | input: "foo.example.com", |
| 99 | expect: true, |
| 100 | }, |
| 101 | { |
| 102 | match: MatchHost{"*.example.com"}, |
| 103 | input: "foo.bar.example.com", |
| 104 | expect: false, |
| 105 | }, |
| 106 | { |
| 107 | match: MatchHost{"*.example.com", "example.net"}, |
| 108 | input: "example.net", |
| 109 | expect: true, |
| 110 | }, |
| 111 | { |
| 112 | match: MatchHost{"example.net", "*.example.com"}, |
| 113 | input: "foo.example.com", |
| 114 | expect: true, |
| 115 | }, |
| 116 | { |
| 117 | match: MatchHost{"*.example.net", "*.*.example.com"}, |
| 118 | input: "foo.bar.example.com", |
| 119 | expect: true, |
| 120 | }, |
| 121 | { |
| 122 | match: MatchHost{"*.example.net", "sub.*.example.com"}, |
| 123 | input: "sub.foo.example.com", |
| 124 | expect: true, |
| 125 | }, |
| 126 | { |
| 127 | match: MatchHost{"*.example.net", "sub.*.example.com"}, |
| 128 | input: "sub.foo.example.net", |
| 129 | expect: false, |
| 130 | }, |
| 131 | { |
| 132 | match: MatchHost{"www.*.*"}, |
| 133 | input: "www.example.com", |
| 134 | expect: true, |
| 135 | }, |
| 136 | { |
| 137 | match: MatchHost{"example.com"}, |
| 138 | input: "example.com:5555", |
| 139 | expect: true, |
| 140 | }, |
| 141 | { |
| 142 | match: MatchHost{"{env.GO_BENCHMARK_DOMAIN}"}, |
| 143 | input: "localhost", |
| 144 | expect: true, |
| 145 | }, |
| 146 | { |
| 147 | match: MatchHost{"{env.GO_NONEXISTENT}"}, |
| 148 | input: "localhost", |
| 149 | expect: false, |
| 150 | }, |
| 151 | } { |
| 152 | req := &http.Request{Host: tc.input} |
| 153 | repl := caddy.NewReplacer() |
| 154 | ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl) |
| 155 | req = req.WithContext(ctx) |
| 156 | |
| 157 | if err := tc.match.Provision(caddy.Context{}); err != nil { |
| 158 | t.Errorf("Test %d %v: provisioning failed: %v", i, tc.match, err) |
| 159 | } |
| 160 | |
| 161 | actual, err := tc.match.MatchWithError(req) |
| 162 | if err != nil { |
| 163 | t.Errorf("Test %d %v: matching failed: %v", i, tc.match, err) |
| 164 | } |
| 165 | if actual != tc.expect { |
| 166 | t.Errorf("Test %d %v: Expected %t, got %t for '%s'", i, tc.match, tc.expect, actual, tc.input) |
| 167 | continue |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | func TestPathMatcher(t *testing.T) { |
| 173 | for i, tc := range []struct { |
| 174 | match MatchPath // not URI-encoded because not parsing from a URI |
| 175 | input string // should be valid URI encoding (escaped) since it will become part of a request |
| 176 | expect bool |
| 177 | provisionErr bool |
| 178 | }{ |
| 179 | { |
| 180 | match: MatchPath{}, |
| 181 | input: "/", |
| 182 | expect: false, |
| 183 | }, |
| 184 | { |
| 185 | match: MatchPath{"/"}, |
| 186 | input: "/", |
| 187 | expect: true, |
| 188 | }, |
| 189 | { |
| 190 | match: MatchPath{"/foo/bar"}, |
| 191 | input: "/", |
| 192 | expect: false, |
| 193 | }, |
| 194 | { |
| 195 | match: MatchPath{"/foo/bar"}, |
| 196 | input: "/foo/bar", |
| 197 | expect: true, |
| 198 | }, |
| 199 | { |
| 200 | match: MatchPath{"/foo/bar/"}, |
| 201 | input: "/foo/bar", |
| 202 | expect: false, |
| 203 | }, |
| 204 | { |
| 205 | match: MatchPath{"/foo/bar/"}, |
| 206 | input: "/foo/bar/", |
| 207 | expect: true, |
| 208 | }, |
| 209 | { |
| 210 | match: MatchPath{"/foo/bar/", "/other"}, |
| 211 | input: "/other/", |
| 212 | expect: false, |
| 213 | }, |
| 214 | { |
| 215 | match: MatchPath{"/foo/bar/", "/other"}, |
| 216 | input: "/other", |
| 217 | expect: true, |
| 218 | }, |
| 219 | { |
| 220 | match: MatchPath{"*.ext"}, |
| 221 | input: "/foo/bar.ext", |
| 222 | expect: true, |
| 223 | }, |
| 224 | { |
| 225 | match: MatchPath{"*.php"}, |
| 226 | input: "/index.PHP", |
| 227 | expect: true, |
| 228 | }, |
| 229 | { |
| 230 | match: MatchPath{"*.ext"}, |
| 231 | input: "/foo/bar.ext", |
| 232 | expect: true, |
| 233 | }, |
| 234 | { |
| 235 | match: MatchPath{"/foo/*/baz"}, |
| 236 | input: "/foo/bar/baz", |
| 237 | expect: true, |
| 238 | }, |
| 239 | { |
| 240 | match: MatchPath{"/foo/*/baz/bam"}, |
| 241 | input: "/foo/bar/bam", |
| 242 | expect: false, |
| 243 | }, |
| 244 | { |
| 245 | match: MatchPath{"*substring*"}, |
| 246 | input: "/foo/substring/bar.txt", |
| 247 | expect: true, |
| 248 | }, |
| 249 | { |
| 250 | match: MatchPath{"/foo"}, |
| 251 | input: "/foo/bar", |
| 252 | expect: false, |
| 253 | }, |
| 254 | { |
| 255 | match: MatchPath{"/foo"}, |
| 256 | input: "/foo/bar", |
| 257 | expect: false, |
| 258 | }, |
| 259 | { |
| 260 | match: MatchPath{"/foo"}, |
| 261 | input: "/FOO", |
| 262 | expect: true, |
| 263 | }, |
| 264 | { |
| 265 | match: MatchPath{"/foo*"}, |
| 266 | input: "/FOOOO", |
| 267 | expect: true, |
| 268 | }, |
| 269 | { |
| 270 | match: MatchPath{"/foo/bar.txt"}, |
| 271 | input: "/foo/BAR.txt", |
| 272 | expect: true, |
| 273 | }, |
| 274 | { |
| 275 | match: MatchPath{"/foo*"}, |
| 276 | input: "//foo/bar", |
| 277 | expect: true, |
| 278 | }, |
| 279 | { |
| 280 | match: MatchPath{"/foo"}, |
| 281 | input: "//foo", |
| 282 | expect: true, |
| 283 | }, |
| 284 | { |
| 285 | match: MatchPath{"//foo"}, |
| 286 | input: "/foo", |
| 287 | expect: false, |
| 288 | }, |
| 289 | { |
| 290 | match: MatchPath{"//foo"}, |
| 291 | input: "//foo", |
| 292 | expect: true, |
| 293 | }, |
| 294 | { |
| 295 | match: MatchPath{"/foo//*"}, |
| 296 | input: "/foo//bar", |
| 297 | expect: true, |
| 298 | }, |
| 299 | { |
| 300 | match: MatchPath{"/foo//*"}, |
| 301 | input: "/foo/%2Fbar", |
| 302 | expect: true, |
| 303 | }, |
| 304 | { |
| 305 | match: MatchPath{"/foo/%2F*"}, |
| 306 | input: "/foo/%2Fbar", |
| 307 | expect: true, |
| 308 | }, |
| 309 | { |
| 310 | match: MatchPath{"/foo/%2F*"}, |
| 311 | input: "/foo//bar", |
| 312 | expect: false, |
| 313 | }, |
| 314 | { |
| 315 | match: MatchPath{"/foo//bar"}, |
| 316 | input: "/foo//bar", |
| 317 | expect: true, |
| 318 | }, |
| 319 | { |
| 320 | match: MatchPath{"/foo/*//bar"}, |
| 321 | input: "/foo///bar", |
| 322 | expect: true, |
| 323 | }, |
| 324 | { |
| 325 | match: MatchPath{"/foo/%*//bar"}, |
| 326 | input: "/foo///bar", |
| 327 | expect: true, |
| 328 | }, |
| 329 | { |
| 330 | match: MatchPath{"/foo/%*//bar"}, |
| 331 | input: "/foo//%2Fbar", |
| 332 | expect: true, |
| 333 | }, |
| 334 | { |
| 335 | match: MatchPath{"/foo*"}, |
| 336 | input: "/%2F/foo", |
| 337 | expect: true, |
| 338 | }, |
| 339 | { |
| 340 | match: MatchPath{"*"}, |
| 341 | input: "/", |
| 342 | expect: true, |
| 343 | }, |
| 344 | { |
| 345 | match: MatchPath{"*"}, |
| 346 | input: "/foo/bar", |
| 347 | expect: true, |
| 348 | }, |
| 349 | { |
| 350 | match: MatchPath{"**"}, |
| 351 | input: "/", |
| 352 | expect: true, |
| 353 | }, |
| 354 | { |
| 355 | match: MatchPath{"**"}, |
| 356 | input: "/foo/bar", |
| 357 | expect: true, |
| 358 | }, |
| 359 | // notice these next three test cases are the same normalized path but are written differently |
| 360 | { |
| 361 | match: MatchPath{"/%[email protected]"}, |
| 362 | input: "/%[email protected]", |
| 363 | expect: true, |
| 364 | }, |
| 365 | { |
| 366 | match: MatchPath{"/%[email protected]"}, |
| 367 | input: "/%25%40.txt", |
| 368 | expect: true, |
| 369 | }, |
| 370 | { |
| 371 | match: MatchPath{"/%25%40.txt"}, |
| 372 | input: "/%25%40.txt", |
| 373 | expect: true, |
| 374 | }, |
| 375 | { |
| 376 | match: MatchPath{"/bands/*/*"}, |
| 377 | input: "/bands/AC%2FDC/T.N.T", |
| 378 | expect: false, // because * operates in normalized space |
| 379 | }, |
| 380 | { |
| 381 | match: MatchPath{"/bands/%*/%*"}, |
| 382 | input: "/bands/AC%2FDC/T.N.T", |
| 383 | expect: true, |
| 384 | }, |
| 385 | { |
| 386 | match: MatchPath{"/bands/%*/%*"}, |
| 387 | input: "/bands/AC/DC/T.N.T", |
| 388 | expect: false, |
| 389 | }, |
| 390 | { |
| 391 | match: MatchPath{"/bands/%*"}, |
| 392 | input: "/bands/AC/DC", |
| 393 | expect: false, // not a suffix match |
| 394 | }, |
| 395 | { |
| 396 | match: MatchPath{"/bands/%*"}, |
| 397 | input: "/bands/AC%2FDC", |
| 398 | expect: true, |
| 399 | }, |
| 400 | { |
| 401 | match: MatchPath{"/foo%2fbar/baz"}, |
| 402 | input: "/foo%2Fbar/baz", |
| 403 | expect: true, |
| 404 | }, |
| 405 | { |
| 406 | match: MatchPath{"/foo%2fbar/baz"}, |
| 407 | input: "/foo/bar/baz", |
| 408 | expect: false, |
| 409 | }, |
| 410 | { |
| 411 | match: MatchPath{"/foo/bar/baz"}, |
| 412 | input: "/foo%2fbar/baz", |
| 413 | expect: true, |
| 414 | }, |
| 415 | { |
| 416 | match: MatchPath{"/admin%2fpanel"}, |
| 417 | input: "/ADMIN%2fpanel", |
| 418 | expect: true, |
| 419 | }, |
| 420 | { |
| 421 | match: MatchPath{"/admin%2fpa*el"}, |
| 422 | input: "/ADMIN%2fPaAzZLm123NEL", |
| 423 | expect: true, |
| 424 | }, |
| 425 | } { |
| 426 | err := tc.match.Provision(caddy.Context{}) |
| 427 | if err == nil && tc.provisionErr { |
| 428 | t.Errorf("Test %d %v: Expected error provisioning, but there was no error", i, tc.match) |
| 429 | } |
| 430 | if err != nil && !tc.provisionErr { |
| 431 | t.Errorf("Test %d %v: Expected no error provisioning, but there was an error: %v", i, tc.match, err) |
| 432 | } |
| 433 | if tc.provisionErr { |
| 434 | continue // if it's not supposed to provision properly, pointless to test it |
| 435 | } |
| 436 | |
| 437 | u, err := url.ParseRequestURI(tc.input) |
| 438 | if err != nil { |
| 439 | t.Fatalf("Test %d (%v): Invalid request URI (should be rejected by Go's HTTP server): %v", i, tc.input, err) |
| 440 | } |
| 441 | req := &http.Request{URL: u} |
| 442 | repl := caddy.NewReplacer() |
| 443 | ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl) |
| 444 | req = req.WithContext(ctx) |
| 445 | |
| 446 | actual, err := tc.match.MatchWithError(req) |
| 447 | if err != nil { |
| 448 | t.Errorf("Test %d %v: matching failed: %v", i, tc.match, err) |
| 449 | } |
| 450 | if actual != tc.expect { |
| 451 | t.Errorf("Test %d %v: Expected %t, got %t for '%s'", i, tc.match, tc.expect, actual, tc.input) |
| 452 | continue |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | func TestPathMatcherWindows(t *testing.T) { |
| 458 | // only Windows has this bug where it will ignore |
| 459 | // trailing dots and spaces in a filename |
| 460 | if runtime.GOOS != "windows" { |
| 461 | return |
| 462 | } |
| 463 | |
| 464 | repl := caddy.NewReplacer() |
| 465 | |
| 466 | for _, tc := range []struct { |
| 467 | name string |
| 468 | path string |
| 469 | requestTarget string |
| 470 | match MatchPath |
| 471 | }{ |
| 472 | { |
| 473 | name: "trailing dots and spaces", |
| 474 | path: "/index.php . . ..", |
| 475 | match: MatchPath{"*.php"}, |
| 476 | }, |
| 477 | { |
| 478 | name: "encoded backslash path separator", |
| 479 | requestTarget: `/private%5csecret.txt`, |
| 480 | match: MatchPath{"/private/*"}, |
| 481 | }, |
| 482 | { |
| 483 | name: "encoded backslash path separator with escaped wildcard", |
| 484 | requestTarget: `/private%5csecret.txt`, |
| 485 | match: MatchPath{"/private/%*"}, |
| 486 | }, |
| 487 | { |
| 488 | name: "uppercase encoded backslash path separator with escaped wildcard", |
| 489 | requestTarget: `/private%5Csecret.txt`, |
| 490 | match: MatchPath{"/private/%*"}, |
| 491 | }, |
| 492 | { |
| 493 | name: "encoded backslash in escaped pattern", |
| 494 | requestTarget: `/private%5csecret.txt`, |
| 495 | match: MatchPath{"/private%5c%*"}, |
| 496 | }, |
| 497 | } { |
| 498 | t.Run(tc.name, func(t *testing.T) { |
| 499 | u := &url.URL{Path: tc.path} |
| 500 | if tc.requestTarget != "" { |
| 501 | var err error |
| 502 | u, err = url.ParseRequestURI(tc.requestTarget) |
| 503 | if err != nil { |
| 504 | t.Fatalf("Parsing request target: %v", err) |
| 505 | } |
| 506 | } |
| 507 | req := &http.Request{URL: u} |
| 508 | ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl) |
| 509 | req = req.WithContext(ctx) |
| 510 | |
| 511 | matched, err := tc.match.MatchWithError(req) |
| 512 | if err != nil { |
| 513 | t.Errorf("Expected no error, but got: %v", err) |
| 514 | } |
| 515 | if !matched { |
| 516 | t.Errorf("Expected %q to match %v", req.URL.Path, tc.match) |
| 517 | } |
| 518 | }) |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | func TestPathREMatcher(t *testing.T) { |
| 523 | for i, tc := range []struct { |
| 524 | match MatchPathRE |
| 525 | input string |
| 526 | expect bool |
| 527 | expectRepl map[string]string |
| 528 | }{ |
| 529 | { |
| 530 | match: MatchPathRE{}, |
| 531 | input: "/", |
| 532 | expect: true, |
| 533 | }, |
| 534 | { |
| 535 | match: MatchPathRE{MatchRegexp{Pattern: "/"}}, |
| 536 | input: "/", |
| 537 | expect: true, |
| 538 | }, |
| 539 | { |
| 540 | match: MatchPathRE{MatchRegexp{Pattern: "^/foo"}}, |
| 541 | input: "/foo", |
| 542 | expect: true, |
| 543 | }, |
| 544 | { |
| 545 | match: MatchPathRE{MatchRegexp{Pattern: "^/foo"}}, |
| 546 | input: "/foo/", |
| 547 | expect: true, |
| 548 | }, |
| 549 | { |
| 550 | match: MatchPathRE{MatchRegexp{Pattern: "^/foo"}}, |
| 551 | input: "//foo", |
| 552 | expect: true, |
| 553 | }, |
| 554 | { |
| 555 | match: MatchPathRE{MatchRegexp{Pattern: "^/foo"}}, |
| 556 | input: "//foo/", |
| 557 | expect: true, |
| 558 | }, |
| 559 | { |
| 560 | match: MatchPathRE{MatchRegexp{Pattern: "^/foo"}}, |
| 561 | input: "/%2F/foo/", |
| 562 | expect: true, |
| 563 | }, |
| 564 | { |
| 565 | match: MatchPathRE{MatchRegexp{Pattern: "/bar"}}, |
| 566 | input: "/foo/", |
| 567 | expect: false, |
| 568 | }, |
| 569 | { |
| 570 | match: MatchPathRE{MatchRegexp{Pattern: "^/bar"}}, |
| 571 | input: "/foo/bar", |
| 572 | expect: false, |
| 573 | }, |
| 574 | { |
| 575 | match: MatchPathRE{MatchRegexp{Pattern: "^/foo/(.*)/baz$", Name: "name"}}, |
| 576 | input: "/foo/bar/baz", |
| 577 | expect: true, |
| 578 | expectRepl: map[string]string{"name.1": "bar"}, |
| 579 | }, |
| 580 | { |
| 581 | match: MatchPathRE{MatchRegexp{Pattern: "^/foo/(?P<myparam>.*)/baz$", Name: "name"}}, |
| 582 | input: "/foo/bar/baz", |
| 583 | expect: true, |
| 584 | expectRepl: map[string]string{"name.myparam": "bar"}, |
| 585 | }, |
| 586 | { |
| 587 | match: MatchPathRE{MatchRegexp{Pattern: "^/%@.txt"}}, |
| 588 | input: "/%[email protected]", |
| 589 | expect: true, |
| 590 | }, |
| 591 | { |
| 592 | match: MatchPathRE{MatchRegexp{Pattern: "^/%[email protected]"}}, |
| 593 | input: "/%[email protected]", |
| 594 | expect: false, |
| 595 | }, |
| 596 | } { |
| 597 | // compile the regexp and validate its name |
| 598 | err := tc.match.Provision(caddy.Context{}) |
| 599 | if err != nil { |
| 600 | t.Errorf("Test %d %v: Provisioning: %v", i, tc.match, err) |
| 601 | continue |
| 602 | } |
| 603 | err = tc.match.Validate() |
| 604 | if err != nil { |
| 605 | t.Errorf("Test %d %v: Validating: %v", i, tc.match, err) |
| 606 | continue |
| 607 | } |
| 608 | |
| 609 | // set up the fake request and its Replacer |
| 610 | u, err := url.ParseRequestURI(tc.input) |
| 611 | if err != nil { |
| 612 | t.Fatalf("Test %d: Bad input URI: %v", i, err) |
| 613 | } |
| 614 | req := &http.Request{URL: u} |
| 615 | repl := caddy.NewReplacer() |
| 616 | ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl) |
| 617 | req = req.WithContext(ctx) |
| 618 | addHTTPVarsToReplacer(repl, req, httptest.NewRecorder()) |
| 619 | |
| 620 | actual, err := tc.match.MatchWithError(req) |
| 621 | if err != nil { |
| 622 | t.Errorf("Test %d %v: matching failed: %v", i, tc.match, err) |
| 623 | } |
| 624 | if actual != tc.expect { |
| 625 | t.Errorf("Test %d [%v]: Expected %t, got %t for input '%s'", |
| 626 | i, tc.match.Pattern, tc.expect, actual, tc.input) |
| 627 | continue |
| 628 | } |
| 629 | |
| 630 | for key, expectVal := range tc.expectRepl { |
| 631 | placeholder := fmt.Sprintf("{http.regexp.%s}", key) |
| 632 | actualVal := repl.ReplaceAll(placeholder, "<empty>") |
| 633 | if actualVal != expectVal { |
| 634 | t.Errorf("Test %d [%v]: Expected placeholder {http.regexp.%s} to be '%s' but got '%s'", |
| 635 | i, tc.match.Pattern, key, expectVal, actualVal) |
| 636 | continue |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | func TestHeaderMatcher(t *testing.T) { |
| 643 | repl := caddy.NewReplacer() |
| 644 | repl.Set("a", "foobar") |
| 645 | |
| 646 | for i, tc := range []struct { |
| 647 | match MatchHeader |
| 648 | input http.Header // make sure these are canonical cased (std lib will do that in a real request) |
| 649 | host string |
| 650 | expect bool |
| 651 | }{ |
| 652 | { |
| 653 | match: MatchHeader{"Field": []string{"foo"}}, |
| 654 | input: http.Header{"Field": []string{"foo"}}, |
| 655 | expect: true, |
| 656 | }, |
| 657 | { |
| 658 | match: MatchHeader{"Field": []string{"foo", "bar"}}, |
| 659 | input: http.Header{"Field": []string{"bar"}}, |
| 660 | expect: true, |
| 661 | }, |
| 662 | { |
| 663 | match: MatchHeader{"Field": []string{"foo", "bar"}}, |
| 664 | input: http.Header{"Alakazam": []string{"kapow"}}, |
| 665 | expect: false, |
| 666 | }, |
| 667 | { |
| 668 | match: MatchHeader{"Field": []string{"foo", "bar"}}, |
| 669 | input: http.Header{"Field": []string{"kapow"}}, |
| 670 | expect: false, |
| 671 | }, |
| 672 | { |
| 673 | match: MatchHeader{"Field": []string{"foo", "bar"}}, |
| 674 | input: http.Header{"Field": []string{"kapow", "foo"}}, |
| 675 | expect: true, |
| 676 | }, |
| 677 | { |
| 678 | match: MatchHeader{"Field1": []string{"foo"}, "Field2": []string{"bar"}}, |
| 679 | input: http.Header{"Field1": []string{"foo"}, "Field2": []string{"bar"}}, |
| 680 | expect: true, |
| 681 | }, |
| 682 | { |
| 683 | match: MatchHeader{"field1": []string{"foo"}, "field2": []string{"bar"}}, |
| 684 | input: http.Header{"Field1": []string{"foo"}, "Field2": []string{"bar"}}, |
| 685 | expect: true, |
| 686 | }, |
| 687 | { |
| 688 | match: MatchHeader{"field1": []string{"foo"}, "field2": []string{"bar"}}, |
| 689 | input: http.Header{"Field1": []string{"foo"}, "Field2": []string{"kapow"}}, |
| 690 | expect: false, |
| 691 | }, |
| 692 | { |
| 693 | match: MatchHeader{"field1": []string{"*"}}, |
| 694 | input: http.Header{"Field1": []string{"foo"}}, |
| 695 | expect: true, |
| 696 | }, |
| 697 | { |
| 698 | match: MatchHeader{"field1": []string{"*"}}, |
| 699 | input: http.Header{"Field2": []string{"foo"}}, |
| 700 | expect: false, |
| 701 | }, |
| 702 | { |
| 703 | match: MatchHeader{"Field1": []string{"foo*"}}, |
| 704 | input: http.Header{"Field1": []string{"foo"}}, |
| 705 | expect: true, |
| 706 | }, |
| 707 | { |
| 708 | match: MatchHeader{"Field1": []string{"foo*"}}, |
| 709 | input: http.Header{"Field1": []string{"asdf", "foobar"}}, |
| 710 | expect: true, |
| 711 | }, |
| 712 | { |
| 713 | match: MatchHeader{"Field1": []string{"*bar"}}, |
| 714 | input: http.Header{"Field1": []string{"asdf", "foobar"}}, |
| 715 | expect: true, |
| 716 | }, |
| 717 | { |
| 718 | match: MatchHeader{"host": []string{"localhost"}}, |
| 719 | input: http.Header{}, |
| 720 | host: "localhost", |
| 721 | expect: true, |
| 722 | }, |
| 723 | { |
| 724 | match: MatchHeader{"host": []string{"localhost"}}, |
| 725 | input: http.Header{}, |
| 726 | host: "caddyserver.com", |
| 727 | expect: false, |
| 728 | }, |
| 729 | { |
| 730 | match: MatchHeader{"Must-Not-Exist": nil}, |
| 731 | input: http.Header{}, |
| 732 | expect: true, |
| 733 | }, |
| 734 | { |
| 735 | match: MatchHeader{"Must-Not-Exist": nil}, |
| 736 | input: http.Header{"Must-Not-Exist": []string{"do not match"}}, |
| 737 | expect: false, |
| 738 | }, |
| 739 | { |
| 740 | match: MatchHeader{"Foo": []string{"{a}"}}, |
| 741 | input: http.Header{"Foo": []string{"foobar"}}, |
| 742 | expect: true, |
| 743 | }, |
| 744 | { |
| 745 | match: MatchHeader{"Foo": []string{"{a}"}}, |
| 746 | input: http.Header{"Foo": []string{"asdf"}}, |
| 747 | expect: false, |
| 748 | }, |
| 749 | { |
| 750 | match: MatchHeader{"Foo": []string{"{a}*"}}, |
| 751 | input: http.Header{"Foo": []string{"foobar-baz"}}, |
| 752 | expect: true, |
| 753 | }, |
| 754 | } { |
| 755 | req := &http.Request{Header: tc.input, Host: tc.host} |
| 756 | ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl) |
| 757 | req = req.WithContext(ctx) |
| 758 | |
| 759 | actual, err := tc.match.MatchWithError(req) |
| 760 | if err != nil { |
| 761 | t.Errorf("Test %d %v: matching failed: %v", i, tc.match, err) |
| 762 | } |
| 763 | if actual != tc.expect { |
| 764 | t.Errorf("Test %d %v: Expected %t, got %t for '%s'", i, tc.match, tc.expect, actual, tc.input) |
| 765 | continue |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | func TestQueryMatcher(t *testing.T) { |
| 771 | for i, tc := range []struct { |
| 772 | scenario string |
| 773 | match MatchQuery |
| 774 | input string |
| 775 | expect bool |
| 776 | }{ |
| 777 | { |
| 778 | scenario: "non match against a specific value", |
| 779 | match: MatchQuery{"debug": []string{"1"}}, |
| 780 | input: "/", |
| 781 | expect: false, |
| 782 | }, |
| 783 | { |
| 784 | scenario: "match against a specific value", |
| 785 | match: MatchQuery{"debug": []string{"1"}}, |
| 786 | input: "/?debug=1", |
| 787 | expect: true, |
| 788 | }, |
| 789 | { |
| 790 | scenario: "match against a wildcard", |
| 791 | match: MatchQuery{"debug": []string{"*"}}, |
| 792 | input: "/?debug=something", |
| 793 | expect: true, |
| 794 | }, |
| 795 | { |
| 796 | scenario: "non match against a wildcarded", |
| 797 | match: MatchQuery{"debug": []string{"*"}}, |
| 798 | input: "/?other=something", |
| 799 | expect: false, |
| 800 | }, |
| 801 | { |
| 802 | scenario: "match against an empty value", |
| 803 | match: MatchQuery{"debug": []string{""}}, |
| 804 | input: "/?debug", |
| 805 | expect: true, |
| 806 | }, |
| 807 | { |
| 808 | scenario: "non match against an empty value", |
| 809 | match: MatchQuery{"debug": []string{""}}, |
| 810 | input: "/?someparam", |
| 811 | expect: false, |
| 812 | }, |
| 813 | { |
| 814 | scenario: "empty matcher value should match empty query", |
| 815 | match: MatchQuery{}, |
| 816 | input: "/?", |
| 817 | expect: true, |
| 818 | }, |
| 819 | { |
| 820 | scenario: "nil matcher value should NOT match a non-empty query", |
| 821 | match: MatchQuery{}, |
| 822 | input: "/?foo=bar", |
| 823 | expect: false, |
| 824 | }, |
| 825 | { |
| 826 | scenario: "non-nil matcher should NOT match an empty query", |
| 827 | match: MatchQuery{"": nil}, |
| 828 | input: "/?", |
| 829 | expect: false, |
| 830 | }, |
| 831 | { |
| 832 | scenario: "match against a placeholder value", |
| 833 | match: MatchQuery{"debug": []string{"{http.vars.debug}"}}, |
| 834 | input: "/?debug=1", |
| 835 | expect: true, |
| 836 | }, |
| 837 | { |
| 838 | scenario: "match against a placeholder key", |
| 839 | match: MatchQuery{"{http.vars.key}": []string{"1"}}, |
| 840 | input: "/?somekey=1", |
| 841 | expect: true, |
| 842 | }, |
| 843 | { |
| 844 | scenario: "do not match when not all query params are present", |
| 845 | match: MatchQuery{"debug": []string{"1"}, "foo": []string{"bar"}}, |
| 846 | input: "/?debug=1", |
| 847 | expect: false, |
| 848 | }, |
| 849 | { |
| 850 | scenario: "match when all query params are present", |
| 851 | match: MatchQuery{"debug": []string{"1"}, "foo": []string{"bar"}}, |
| 852 | input: "/?debug=1&foo=bar", |
| 853 | expect: true, |
| 854 | }, |
| 855 | { |
| 856 | scenario: "do not match when the value of a query param does not match", |
| 857 | match: MatchQuery{"debug": []string{"1"}, "foo": []string{"bar"}}, |
| 858 | input: "/?debug=2&foo=bar", |
| 859 | expect: false, |
| 860 | }, |
| 861 | { |
| 862 | scenario: "do not match when all the values the query params do not match", |
| 863 | match: MatchQuery{"debug": []string{"1"}, "foo": []string{"bar"}}, |
| 864 | input: "/?debug=2&foo=baz", |
| 865 | expect: false, |
| 866 | }, |
| 867 | { |
| 868 | scenario: "match against two values for the same key", |
| 869 | match: MatchQuery{"debug": []string{"1"}}, |
| 870 | input: "/?debug=1&debug=2", |
| 871 | expect: true, |
| 872 | }, |
| 873 | { |
| 874 | scenario: "match against two values for the same key", |
| 875 | match: MatchQuery{"debug": []string{"2", "1"}}, |
| 876 | input: "/?debug=2&debug=1", |
| 877 | expect: true, |
| 878 | }, |
| 879 | } { |
| 880 | |
| 881 | u, _ := url.Parse(tc.input) |
| 882 | |
| 883 | req := &http.Request{URL: u} |
| 884 | repl := caddy.NewReplacer() |
| 885 | ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl) |
| 886 | repl.Set("http.vars.debug", "1") |
| 887 | repl.Set("http.vars.key", "somekey") |
| 888 | req = req.WithContext(ctx) |
| 889 | actual, err := tc.match.MatchWithError(req) |
| 890 | if err != nil { |
| 891 | t.Errorf("Test %d %v: matching failed: %v", i, tc.match, err) |
| 892 | } |
| 893 | if actual != tc.expect { |
| 894 | t.Errorf("Test %d %v: Expected %t, got %t for '%s'", i, tc.match, tc.expect, actual, tc.input) |
| 895 | continue |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | func TestHeaderREMatcher(t *testing.T) { |
| 901 | for i, tc := range []struct { |
| 902 | match MatchHeaderRE |
| 903 | input http.Header // make sure these are canonical cased (std lib will do that in a real request) |
| 904 | host string |
| 905 | expect bool |
| 906 | expectRepl map[string]string |
| 907 | }{ |
| 908 | { |
| 909 | match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "foo"}}, |
| 910 | input: http.Header{"Field": []string{"foo"}}, |
| 911 | expect: true, |
| 912 | }, |
| 913 | { |
| 914 | match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "$foo^"}}, |
| 915 | input: http.Header{"Field": []string{"foobar"}}, |
| 916 | expect: false, |
| 917 | }, |
| 918 | { |
| 919 | match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "^foo(.*)$", Name: "name"}}, |
| 920 | input: http.Header{"Field": []string{"foobar"}}, |
| 921 | expect: true, |
| 922 | expectRepl: map[string]string{"name.1": "bar"}, |
| 923 | }, |
| 924 | { |
| 925 | match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "^foo.*$", Name: "name"}}, |
| 926 | input: http.Header{"Field": []string{"barfoo", "foobar"}}, |
| 927 | expect: true, |
| 928 | }, |
| 929 | { |
| 930 | match: MatchHeaderRE{"host": &MatchRegexp{Pattern: "^localhost$", Name: "name"}}, |
| 931 | input: http.Header{}, |
| 932 | host: "localhost", |
| 933 | expect: true, |
| 934 | }, |
| 935 | { |
| 936 | match: MatchHeaderRE{"host": &MatchRegexp{Pattern: "^local$", Name: "name"}}, |
| 937 | input: http.Header{}, |
| 938 | host: "localhost", |
| 939 | expect: false, |
| 940 | }, |
| 941 | } { |
| 942 | // compile the regexp and validate its name |
| 943 | err := tc.match.Provision(caddy.Context{}) |
| 944 | if err != nil { |
| 945 | t.Errorf("Test %d %v: Provisioning: %v", i, tc.match, err) |
| 946 | continue |
| 947 | } |
| 948 | err = tc.match.Validate() |
| 949 | if err != nil { |
| 950 | t.Errorf("Test %d %v: Validating: %v", i, tc.match, err) |
| 951 | continue |
| 952 | } |
| 953 | |
| 954 | // set up the fake request and its Replacer |
| 955 | req := &http.Request{Header: tc.input, URL: new(url.URL), Host: tc.host} |
| 956 | repl := caddy.NewReplacer() |
| 957 | ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl) |
| 958 | req = req.WithContext(ctx) |
| 959 | addHTTPVarsToReplacer(repl, req, httptest.NewRecorder()) |
| 960 | |
| 961 | actual, err := tc.match.MatchWithError(req) |
| 962 | if err != nil { |
| 963 | t.Errorf("Test %d %v: matching failed: %v", i, tc.match, err) |
| 964 | } |
| 965 | if actual != tc.expect { |
| 966 | t.Errorf("Test %d [%v]: Expected %t, got %t for input '%s'", |
| 967 | i, tc.match, tc.expect, actual, tc.input) |
| 968 | continue |
| 969 | } |
| 970 | |
| 971 | for key, expectVal := range tc.expectRepl { |
| 972 | placeholder := fmt.Sprintf("{http.regexp.%s}", key) |
| 973 | actualVal := repl.ReplaceAll(placeholder, "<empty>") |
| 974 | if actualVal != expectVal { |
| 975 | t.Errorf("Test %d [%v]: Expected placeholder {http.regexp.%s} to be '%s' but got '%s'", |
| 976 | i, tc.match, key, expectVal, actualVal) |
| 977 | continue |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | func BenchmarkHeaderREMatcher(b *testing.B) { |
| 984 | i := 0 |
| 985 | match := MatchHeaderRE{"Field": &MatchRegexp{Pattern: "^foo(.*)$", Name: "name"}} |
| 986 | input := http.Header{"Field": []string{"foobar"}} |
| 987 | var host string |
| 988 | err := match.Provision(caddy.Context{}) |
| 989 | if err != nil { |
| 990 | b.Errorf("Test %d %v: Provisioning: %v", i, match, err) |
| 991 | } |
| 992 | err = match.Validate() |
| 993 | if err != nil { |
| 994 | b.Errorf("Test %d %v: Validating: %v", i, match, err) |
| 995 | } |
| 996 | |
| 997 | // set up the fake request and its Replacer |
| 998 | req := &http.Request{Header: input, URL: new(url.URL), Host: host} |
| 999 | repl := caddy.NewReplacer() |
| 1000 | ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl) |
| 1001 | req = req.WithContext(ctx) |
| 1002 | addHTTPVarsToReplacer(repl, req, httptest.NewRecorder()) |
| 1003 | for b.Loop() { |
| 1004 | match.MatchWithError(req) |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | func TestVarREMatcher(t *testing.T) { |
| 1009 | for i, tc := range []struct { |
| 1010 | desc string |
| 1011 | match MatchVarsRE |
| 1012 | input VarsMiddleware |
| 1013 | headers http.Header |
| 1014 | expect bool |
| 1015 | expectRepl map[string]string |
| 1016 | }{ |
| 1017 | { |
| 1018 | desc: "match static value within var set by the VarsMiddleware succeeds", |
| 1019 | match: MatchVarsRE{"Var1": &MatchRegexp{Pattern: "foo"}}, |
| 1020 | input: VarsMiddleware{"Var1": "here is foo val"}, |
| 1021 | expect: true, |
| 1022 | }, |
| 1023 | { |
| 1024 | desc: "value set by VarsMiddleware not satisfying regexp matcher fails to match", |
| 1025 | match: MatchVarsRE{"Var1": &MatchRegexp{Pattern: "$foo^"}}, |
| 1026 | input: VarsMiddleware{"Var1": "foobar"}, |
| 1027 | expect: false, |
| 1028 | }, |
| 1029 | { |
| 1030 | desc: "successfully matched value is captured and its placeholder is added to replacer", |
| 1031 | match: MatchVarsRE{"Var1": &MatchRegexp{Pattern: "^foo(.*)$", Name: "name"}}, |
| 1032 | input: VarsMiddleware{"Var1": "foobar"}, |
| 1033 | expect: true, |
| 1034 | expectRepl: map[string]string{"name.1": "bar"}, |
| 1035 | }, |
| 1036 | { |
| 1037 | desc: "matching against a value of standard variables succeeds", |
| 1038 | match: MatchVarsRE{"{http.request.method}": &MatchRegexp{Pattern: "^G.[tT]$"}}, |
| 1039 | input: VarsMiddleware{}, |
| 1040 | expect: true, |
| 1041 | }, |
| 1042 | { |
| 1043 | desc: "matching against value of var set by the VarsMiddleware and referenced by its placeholder succeeds", |
| 1044 | match: MatchVarsRE{"{http.vars.Var1}": &MatchRegexp{Pattern: "[vV]ar[0-9]"}}, |
| 1045 | input: VarsMiddleware{"Var1": "var1Value"}, |
| 1046 | expect: true, |
| 1047 | }, |
| 1048 | { |
| 1049 | desc: "placeholder key value containing braces is not double-expanded", |
| 1050 | match: MatchVarsRE{"{http.request.header.X-Input}": &MatchRegexp{Pattern: ".+", Name: "val"}}, |
| 1051 | input: VarsMiddleware{}, |
| 1052 | headers: http.Header{"X-Input": []string{"{env.HOME}"}}, |
| 1053 | expect: true, |
| 1054 | expectRepl: map[string]string{"val.0": "{env.HOME}"}, |
| 1055 | }, |
| 1056 | } { |
| 1057 | t.Run(tc.desc, func(t *testing.T) { |
| 1058 | t.Parallel() |
| 1059 | // compile the regexp and validate its name |
| 1060 | err := tc.match.Provision(caddy.Context{}) |
| 1061 | if err != nil { |
| 1062 | t.Errorf("Test %d %v: Provisioning: %v", i, tc.match, err) |
| 1063 | return |
| 1064 | } |
| 1065 | err = tc.match.Validate() |
| 1066 | if err != nil { |
| 1067 | t.Errorf("Test %d %v: Validating: %v", i, tc.match, err) |
| 1068 | return |
| 1069 | } |
| 1070 | |
| 1071 | // set up the fake request and its Replacer |
| 1072 | req := &http.Request{URL: new(url.URL), Method: http.MethodGet, Header: tc.headers} |
| 1073 | repl := caddy.NewReplacer() |
| 1074 | ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl) |
| 1075 | ctx = context.WithValue(ctx, VarsCtxKey, make(map[string]any)) |
| 1076 | req = req.WithContext(ctx) |
| 1077 | |
| 1078 | addHTTPVarsToReplacer(repl, req, httptest.NewRecorder()) |
| 1079 | |
| 1080 | tc.input.ServeHTTP(httptest.NewRecorder(), req, emptyHandler) |
| 1081 | |
| 1082 | actual, err := tc.match.MatchWithError(req) |
| 1083 | if err != nil { |
| 1084 | t.Errorf("Test %d %v: matching failed: %v", i, tc.match, err) |
| 1085 | } |
| 1086 | if actual != tc.expect { |
| 1087 | t.Errorf("Test %d [%v]: Expected %t, got %t for input '%s'", |
| 1088 | i, tc.match, tc.expect, actual, tc.input) |
| 1089 | return |
| 1090 | } |
| 1091 | |
| 1092 | for key, expectVal := range tc.expectRepl { |
| 1093 | placeholder := fmt.Sprintf("{http.regexp.%s}", key) |
| 1094 | actualVal := repl.ReplaceAll(placeholder, "<empty>") |
| 1095 | if actualVal != expectVal { |
| 1096 | t.Errorf("Test %d [%v]: Expected placeholder {http.regexp.%s} to be '%s' but got '%s'", |
| 1097 | i, tc.match, key, expectVal, actualVal) |
| 1098 | return |
| 1099 | } |
| 1100 | } |
| 1101 | }) |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | func TestNotMatcher(t *testing.T) { |
| 1106 | for i, tc := range []struct { |
| 1107 | host, path string |
| 1108 | match MatchNot |
| 1109 | expect bool |
| 1110 | }{ |
| 1111 | { |
| 1112 | host: "example.com", path: "/", |
| 1113 | match: MatchNot{}, |
| 1114 | expect: true, |
| 1115 | }, |
| 1116 | { |
| 1117 | host: "example.com", path: "/foo", |
| 1118 | match: MatchNot{ |
| 1119 | MatcherSets: []MatcherSet{ |
| 1120 | { |
| 1121 | MatchPath{"/foo"}, |
| 1122 | }, |
| 1123 | }, |
| 1124 | }, |
| 1125 | expect: false, |
| 1126 | }, |
| 1127 | { |
| 1128 | host: "example.com", path: "/bar", |
| 1129 | match: MatchNot{ |
| 1130 | MatcherSets: []MatcherSet{ |
| 1131 | { |
| 1132 | MatchPath{"/foo"}, |
| 1133 | }, |
| 1134 | }, |
| 1135 | }, |
| 1136 | expect: true, |
| 1137 | }, |
| 1138 | { |
| 1139 | host: "example.com", path: "/bar", |
| 1140 | match: MatchNot{ |
| 1141 | MatcherSets: []MatcherSet{ |
| 1142 | { |
| 1143 | MatchPath{"/foo"}, |
| 1144 | }, |
| 1145 | { |
| 1146 | MatchHost{"example.com"}, |
| 1147 | }, |
| 1148 | }, |
| 1149 | }, |
| 1150 | expect: false, |
| 1151 | }, |
| 1152 | { |
| 1153 | host: "example.com", path: "/bar", |
| 1154 | match: MatchNot{ |
| 1155 | MatcherSets: []MatcherSet{ |
| 1156 | { |
| 1157 | MatchPath{"/bar"}, |
| 1158 | }, |
| 1159 | { |
| 1160 | MatchHost{"example.com"}, |
| 1161 | }, |
| 1162 | }, |
| 1163 | }, |
| 1164 | expect: false, |
| 1165 | }, |
| 1166 | { |
| 1167 | host: "example.com", path: "/foo", |
| 1168 | match: MatchNot{ |
| 1169 | MatcherSets: []MatcherSet{ |
| 1170 | { |
| 1171 | MatchPath{"/bar"}, |
| 1172 | }, |
| 1173 | { |
| 1174 | MatchHost{"sub.example.com"}, |
| 1175 | }, |
| 1176 | }, |
| 1177 | }, |
| 1178 | expect: true, |
| 1179 | }, |
| 1180 | { |
| 1181 | host: "example.com", path: "/foo", |
| 1182 | match: MatchNot{ |
| 1183 | MatcherSets: []MatcherSet{ |
| 1184 | { |
| 1185 | MatchPath{"/foo"}, |
| 1186 | MatchHost{"example.com"}, |
| 1187 | }, |
| 1188 | }, |
| 1189 | }, |
| 1190 | expect: false, |
| 1191 | }, |
| 1192 | { |
| 1193 | host: "example.com", path: "/foo", |
| 1194 | match: MatchNot{ |
| 1195 | MatcherSets: []MatcherSet{ |
| 1196 | { |
| 1197 | MatchPath{"/bar"}, |
| 1198 | MatchHost{"example.com"}, |
| 1199 | }, |
| 1200 | }, |
| 1201 | }, |
| 1202 | expect: true, |
| 1203 | }, |
| 1204 | } { |
| 1205 | req := &http.Request{Host: tc.host, URL: &url.URL{Path: tc.path}} |
| 1206 | repl := caddy.NewReplacer() |
| 1207 | ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl) |
| 1208 | req = req.WithContext(ctx) |
| 1209 | |
| 1210 | actual, err := tc.match.MatchWithError(req) |
| 1211 | if err != nil { |
| 1212 | t.Errorf("Test %d %v: matching failed: %v", i, tc.match, err) |
| 1213 | } |
| 1214 | if actual != tc.expect { |
| 1215 | t.Errorf("Test %d %+v: Expected %t, got %t for: host=%s path=%s'", i, tc.match, tc.expect, actual, tc.host, tc.path) |
| 1216 | continue |
| 1217 | } |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | func BenchmarkLargeHostMatcher(b *testing.B) { |
| 1222 | // this benchmark simulates a large host matcher (thousands of entries) where each |
| 1223 | // value is an exact hostname (not a placeholder or wildcard) - compare the results |
| 1224 | // of this with and without the binary search (comment out the various fast path |
| 1225 | // sections in Match) to conduct experiments |
| 1226 | |
| 1227 | const n = 10000 |
| 1228 | lastHost := fmt.Sprintf("%d.example.com", n-1) |
| 1229 | req := &http.Request{Host: lastHost} |
| 1230 | repl := caddy.NewReplacer() |
| 1231 | ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl) |
| 1232 | req = req.WithContext(ctx) |
| 1233 | |
| 1234 | matcher := make(MatchHost, n) |
| 1235 | for i := 0; i < n; i++ { |
| 1236 | matcher[i] = fmt.Sprintf("%d.example.com", i) |
| 1237 | } |
| 1238 | err := matcher.Provision(caddy.Context{}) |
| 1239 | if err != nil { |
| 1240 | b.Fatal(err) |
| 1241 | } |
| 1242 | |
| 1243 | for b.Loop() { |
| 1244 | matcher.MatchWithError(req) |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | func BenchmarkHostMatcherWithoutPlaceholder(b *testing.B) { |
| 1249 | req := &http.Request{Host: "localhost"} |
| 1250 | repl := caddy.NewReplacer() |
| 1251 | ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl) |
| 1252 | req = req.WithContext(ctx) |
| 1253 | |
| 1254 | match := MatchHost{"localhost"} |
| 1255 | |
| 1256 | for b.Loop() { |
| 1257 | match.MatchWithError(req) |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | func BenchmarkHostMatcherWithPlaceholder(b *testing.B) { |
| 1262 | err := os.Setenv("GO_BENCHMARK_DOMAIN", "localhost") |
| 1263 | if err != nil { |
| 1264 | b.Errorf("error while setting up environment: %v", err) |
| 1265 | } |
| 1266 | |
| 1267 | req := &http.Request{Host: "localhost"} |
| 1268 | repl := caddy.NewReplacer() |
| 1269 | ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl) |
| 1270 | req = req.WithContext(ctx) |
| 1271 | match := MatchHost{"{env.GO_BENCHMARK_DOMAIN}"} |
| 1272 | |
| 1273 | for b.Loop() { |
| 1274 | match.MatchWithError(req) |
| 1275 | } |
| 1276 | } |