mirror of
https://github.com/caddyserver/caddy.git
synced 2026-11-16 13:12:08 +00:00
Merge 681ad6a170 into 098cc69fe4
This commit is contained in:
commit
d5ec749198
3 changed files with 99 additions and 20 deletions
|
|
@ -187,6 +187,24 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
|
|||
httpMethod: "PUT",
|
||||
wantResult: true,
|
||||
},
|
||||
{
|
||||
name: "extension method matches exact case (MatchMethod)",
|
||||
expression: &MatchExpression{
|
||||
Expr: `method('foo')`,
|
||||
},
|
||||
urlTarget: "https://foo.example.com",
|
||||
httpMethod: "foo",
|
||||
wantResult: true,
|
||||
},
|
||||
{
|
||||
name: "extension method does not fold case (MatchMethod)",
|
||||
expression: &MatchExpression{
|
||||
Expr: `method('foo')`,
|
||||
},
|
||||
urlTarget: "https://foo.example.com",
|
||||
httpMethod: "FOO",
|
||||
wantResult: false,
|
||||
},
|
||||
{
|
||||
name: "method error not enough arguments (MatchMethod)",
|
||||
expression: &MatchExpression{
|
||||
|
|
|
|||
|
|
@ -119,7 +119,7 @@ type (
|
|||
// (potentially leading to collisions).
|
||||
MatchPathRE struct{ MatchRegexp }
|
||||
|
||||
// MatchMethod matches requests by the method.
|
||||
// MatchMethod matches requests by the case-sensitive HTTP method token.
|
||||
MatchMethod []string
|
||||
|
||||
// MatchQuery matches requests by the URI's query string. It takes a JSON object
|
||||
|
|
@ -985,14 +985,9 @@ func (MatchMethod) CaddyModule() caddy.ModuleInfo {
|
|||
}
|
||||
}
|
||||
|
||||
// Provision uppercases all configured methods to ensure case-insensitive
|
||||
// matching against incoming requests, whose methods are always uppercase.
|
||||
func (m MatchMethod) Provision(_ caddy.Context) error {
|
||||
for i := range m {
|
||||
m[i] = strings.ToUpper(m[i])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// Provision is retained for compatibility. HTTP method tokens are
|
||||
// case-sensitive and need no preprocessing.
|
||||
func (MatchMethod) Provision(_ caddy.Context) error { return nil }
|
||||
|
||||
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
|
||||
func (m *MatchMethod) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
|
|
|
|||
|
|
@ -15,16 +15,21 @@
|
|||
package caddyhttp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
)
|
||||
|
||||
func TestHostMatcher(t *testing.T) {
|
||||
|
|
@ -1484,15 +1489,21 @@ func TestMethodMatcher(t *testing.T) {
|
|||
expect: true,
|
||||
},
|
||||
{
|
||||
scenario: "match lowercase get after Provision uppercases it",
|
||||
match: MatchMethod{"get"},
|
||||
input: http.MethodGet,
|
||||
scenario: "match lowercase extension method",
|
||||
match: MatchMethod{"foo"},
|
||||
input: "foo",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
scenario: "match mixed case Post after Provision uppercases it",
|
||||
match: MatchMethod{"Post"},
|
||||
input: http.MethodPost,
|
||||
scenario: "do not fold lowercase extension method",
|
||||
match: MatchMethod{"foo"},
|
||||
input: "FOO",
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
scenario: "match mixed case extension method",
|
||||
match: MatchMethod{"MiXeD"},
|
||||
input: "MiXeD",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
|
|
@ -1503,15 +1514,13 @@ func TestMethodMatcher(t *testing.T) {
|
|||
},
|
||||
{
|
||||
scenario: "match one of multiple methods",
|
||||
match: MatchMethod{"get", "post"},
|
||||
input: http.MethodPost,
|
||||
match: MatchMethod{"GET", "foo"},
|
||||
input: "foo",
|
||||
expect: true,
|
||||
},
|
||||
} {
|
||||
req := &http.Request{Method: tc.input}
|
||||
if err := tc.match.Provision(caddy.Context{}); err != nil {
|
||||
t.Errorf("Test %d %v: provisioning failed: %v", i, tc.scenario, err)
|
||||
}
|
||||
tc.match = provisionMethodMatcher(t, tc.match)
|
||||
actual, err := tc.match.MatchWithError(req)
|
||||
if err != nil {
|
||||
t.Errorf("Test %d %v: matching failed: %v", i, tc.scenario, err)
|
||||
|
|
@ -1523,6 +1532,63 @@ func TestMethodMatcher(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestMethodMatcherConfigurationPreservesCase(t *testing.T) {
|
||||
want := MatchMethod{"GET", "foo", "MiXeD"}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
load func(*testing.T) MatchMethod
|
||||
}{
|
||||
{
|
||||
name: "native JSON",
|
||||
load: func(t *testing.T) MatchMethod {
|
||||
var matcher MatchMethod
|
||||
if err := json.Unmarshal([]byte(`["GET","foo","MiXeD"]`), &matcher); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return matcher
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Caddyfile",
|
||||
load: func(t *testing.T) MatchMethod {
|
||||
var matcher MatchMethod
|
||||
if err := matcher.UnmarshalCaddyfile(caddyfile.NewTestDispenser("method GET foo MiXeD")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return matcher
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := provisionMethodMatcher(t, tc.load(t))
|
||||
if !slices.Equal(got, want) {
|
||||
t.Errorf("configured methods = %q, want %q", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMethodMatcherPreservesWireCase(t *testing.T) {
|
||||
req, err := http.ReadRequest(bufio.NewReader(strings.NewReader("foo / HTTP/1.1\r\nHost: example.com\r\n\r\n")))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer req.Body.Close()
|
||||
|
||||
matcher := provisionMethodMatcher(t, MatchMethod{"foo"})
|
||||
if matched, err := matcher.MatchWithError(req); err != nil || !matched {
|
||||
t.Fatalf("matching lower-case wire method: matched=%t, error=%v", matched, err)
|
||||
}
|
||||
}
|
||||
|
||||
func provisionMethodMatcher(t *testing.T, matcher MatchMethod) MatchMethod {
|
||||
t.Helper()
|
||||
if err := matcher.Provision(caddy.Context{}); err != nil {
|
||||
t.Fatalf("provisioning method matcher: %v", err)
|
||||
}
|
||||
return matcher
|
||||
}
|
||||
|
||||
func BenchmarkLargeHostMatcher(b *testing.B) {
|
||||
// this benchmark simulates a large host matcher (thousands of entries) where each
|
||||
// value is an exact hostname (not a placeholder or wildcard) - compare the results
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue