Compare commits

...

3 commits

Author SHA1 Message Date
Zen Dodd
681ad6a170
Merge branch 'master' into method-matcher-case 2026-11-05 18:58:12 +10:00
steadytao
283701f2dc
Merge master 2026-11-05 19:49:10 +11:00
steadytao
2ad90a593a
caddyhttp: preserve method matcher case 2026-10-17 18:39:23 +10:00
3 changed files with 99 additions and 20 deletions

View file

@ -187,6 +187,24 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
httpMethod: "PUT", httpMethod: "PUT",
wantResult: true, 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)", name: "method error not enough arguments (MatchMethod)",
expression: &MatchExpression{ expression: &MatchExpression{

View file

@ -118,7 +118,7 @@ type (
// (potentially leading to collisions). // (potentially leading to collisions).
MatchPathRE struct{ MatchRegexp } MatchPathRE struct{ MatchRegexp }
// MatchMethod matches requests by the method. // MatchMethod matches requests by the case-sensitive HTTP method token.
MatchMethod []string MatchMethod []string
// MatchQuery matches requests by the URI's query string. It takes a JSON object // MatchQuery matches requests by the URI's query string. It takes a JSON object
@ -964,14 +964,9 @@ func (MatchMethod) CaddyModule() caddy.ModuleInfo {
} }
} }
// Provision uppercases all configured methods to ensure case-insensitive // Provision is retained for compatibility. HTTP method tokens are
// matching against incoming requests, whose methods are always uppercase. // case-sensitive and need no preprocessing.
func (m MatchMethod) Provision(_ caddy.Context) error { func (MatchMethod) Provision(_ caddy.Context) error { return nil }
for i := range m {
m[i] = strings.ToUpper(m[i])
}
return nil
}
// UnmarshalCaddyfile implements caddyfile.Unmarshaler. // UnmarshalCaddyfile implements caddyfile.Unmarshaler.
func (m *MatchMethod) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { func (m *MatchMethod) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {

View file

@ -15,16 +15,21 @@
package caddyhttp package caddyhttp
import ( import (
"bufio"
"context" "context"
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"os" "os"
"runtime" "runtime"
"slices"
"strings"
"testing" "testing"
"github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
) )
func TestHostMatcher(t *testing.T) { func TestHostMatcher(t *testing.T) {
@ -1396,15 +1401,21 @@ func TestMethodMatcher(t *testing.T) {
expect: true, expect: true,
}, },
{ {
scenario: "match lowercase get after Provision uppercases it", scenario: "match lowercase extension method",
match: MatchMethod{"get"}, match: MatchMethod{"foo"},
input: http.MethodGet, input: "foo",
expect: true, expect: true,
}, },
{ {
scenario: "match mixed case Post after Provision uppercases it", scenario: "do not fold lowercase extension method",
match: MatchMethod{"Post"}, match: MatchMethod{"foo"},
input: http.MethodPost, input: "FOO",
expect: false,
},
{
scenario: "match mixed case extension method",
match: MatchMethod{"MiXeD"},
input: "MiXeD",
expect: true, expect: true,
}, },
{ {
@ -1415,15 +1426,13 @@ func TestMethodMatcher(t *testing.T) {
}, },
{ {
scenario: "match one of multiple methods", scenario: "match one of multiple methods",
match: MatchMethod{"get", "post"}, match: MatchMethod{"GET", "foo"},
input: http.MethodPost, input: "foo",
expect: true, expect: true,
}, },
} { } {
req := &http.Request{Method: tc.input} req := &http.Request{Method: tc.input}
if err := tc.match.Provision(caddy.Context{}); err != nil { tc.match = provisionMethodMatcher(t, tc.match)
t.Errorf("Test %d %v: provisioning failed: %v", i, tc.scenario, err)
}
actual, err := tc.match.MatchWithError(req) actual, err := tc.match.MatchWithError(req)
if err != nil { if err != nil {
t.Errorf("Test %d %v: matching failed: %v", i, tc.scenario, err) t.Errorf("Test %d %v: matching failed: %v", i, tc.scenario, err)
@ -1435,6 +1444,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) { func BenchmarkLargeHostMatcher(b *testing.B) {
// this benchmark simulates a large host matcher (thousands of entries) where each // 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 // value is an exact hostname (not a placeholder or wildcard) - compare the results