Merge branch 'master' into feat/admin-info-endpoint

This commit is contained in:
Zack Wagner 2026-10-11 16:46:24 -04:00 • committed by GitHub
commit ad5a9802f5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 122 additions and 27 deletions

View file

@ -0,0 +1,69 @@
package integration
import (
"fmt"
"net/http"
"os"
"path/filepath"
"testing"
"github.com/caddyserver/caddy/v2/caddytest"
)
func TestTryFilesErrorCodeAfterQueryItem(t *testing.T) {
root := t.TempDir()
for name, content := range map[string]string{
"style.css": "css",
"about.html": "about",
} {
if err := os.WriteFile(filepath.Join(root, name), []byte(content), 0o600); err != nil {
t.Fatal(err)
}
}
tester := caddytest.NewTester(t)
tester.InitServer(fmt.Sprintf(`
{
skip_install_trust
admin localhost:2999
http_port 9080
grace_period 1ns
}
http://localhost:9080 {
root * %q
try_files {path} {path}.html?{query} =404
file_server
}
`, filepath.ToSlash(root)), "caddyfile")
tester.AssertGetResponse("http://localhost:9080/style.css", 200, "css")
tester.AssertGetResponse("http://localhost:9080/about?x=1", 200, "about")
req, err := http.NewRequest(http.MethodGet, "http://localhost:9080/missing", nil)
if err != nil {
t.Fatal(err)
}
tester.AssertResponseCode(req, http.StatusNotFound)
}
func TestSkippedGroupedRouteKeepsRegexpCapture(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(`
{
skip_install_trust
admin localhost:2999
http_port 9080
grace_period 1ns
}
http://localhost:9080 {
@x path_regexp x ^/x/(.*)$
@y path_regexp x ^/(.*)$
rewrite @x /one/{re.x.1}
rewrite @y /two/{re.x.1}
respond "path={path} cap={re.x.1}"
}
`, "caddyfile")
tester.AssertGetResponse("http://localhost:9080/x/q", 200, "path=/one/q cap=q")
tester.AssertGetResponse("http://localhost:9080/z", 200, "path=/two/z cap=z")
}

View file

@ -243,25 +243,29 @@ func (m *MatchExpression) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
// CEL expression (e.g. strings) and we should retain that
if d.CountRemainingArgs() > 1 {
m.Expr = strings.Join(d.RemainingArgsRaw(), " ")
return nil
} else {
// there should at least be one arg
if !d.NextArg() {
return d.ArgErr()
}
// if there's only one token, then we can safely grab the
// cleaned token (no quotes) and use that as the expression
// because there's no valid CEL expression that is only a
// quoted string; commonly quotes are used in Caddyfile to
// define the expression
m.Expr = d.Val()
// use the named matcher's name, to fill regexp
// matchers names by default
m.Name = d.GetContextString(caddyfile.MatcherNameCtxKey)
}
// there should at least be one arg
if !d.NextArg() {
return d.ArgErr()
// a matcher set holds only one expression matcher
if d.Next() {
return d.Err("repeated expression matchers are not supported, combine them into one expression with &&")
}
// if there's only one token, then we can safely grab the
// cleaned token (no quotes) and use that as the expression
// because there's no valid CEL expression that is only a
// quoted string; commonly quotes are used in Caddyfile to
// define the expression
m.Expr = d.Val()
// use the named matcher's name, to fill regexp
// matchers names by default
m.Name = d.GetContextString(caddyfile.MatcherNameCtxKey)
return nil
}

View file

@ -21,9 +21,11 @@ import (
"encoding/pem"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
)
var (
@ -605,3 +607,20 @@ func TestMatchExpressionProvision(t *testing.T) {
})
}
}
func TestMatchExpressionUnmarshalCaddyfileRepeated(t *testing.T) {
for _, input := range []string{`
expression {http.request.method} == 'GET'
expression {http.request.uri.path}.startsWith('/public/')
`, `
expression "{http.request.method} == 'GET'"
expression {http.request.uri.path}.startsWith('/public/')
`} {
d := caddyfile.NewTestDispenser(input)
m := new(MatchExpression)
err := m.UnmarshalCaddyfile(d)
if err == nil || !strings.Contains(err.Error(), "repeated expression matchers are not supported") {
t.Errorf("UnmarshalCaddyfile() error = %v, want repeated expression error (expression: %s)", err, m.Expr)
}
}
}

View file

@ -268,6 +268,19 @@ func wrapRoute(route Route) Middleware {
// but I just thought this made more sense
nextCopy := next
// if route is part of a group, ensure only the
// first matching route in the group is applied
var groups map[string]struct{}
if route.Group != "" {
groups = req.Context().Value(routeGroupCtxKey).(map[string]struct{})
if _, ok := groups[route.Group]; ok {
// this group has already been satisfied by a
// matching route; skip without running matchers
return nextCopy.ServeHTTP(rw, req)
}
}
// route must match at least one of the matcher sets
matches, err := route.MatcherSets.AnyMatchWithError(req)
if err != nil {
@ -281,18 +294,8 @@ func wrapRoute(route Route) Middleware {
return nextCopy.ServeHTTP(rw, req)
}
// if route is part of a group, ensure only the
// first matching route in the group is applied
if route.Group != "" {
groups := req.Context().Value(routeGroupCtxKey).(map[string]struct{})
if _, ok := groups[route.Group]; ok {
// this group has already been
// satisfied by a matching route
return nextCopy.ServeHTTP(rw, req)
}
// this matching route satisfies the group
// this matching route satisfies the group
if groups != nil {
groups[route.Group] = struct{}{}
}