mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-26 13:43:47 +03:00
httpcaddyfile: Fix expression matcher shortcut in snippets (#6288)
This commit is contained in:
parent
d129ae6aec
commit
feeb6af403
3 changed files with 73 additions and 9 deletions
|
@ -340,6 +340,8 @@ func (l *lexer) finalizeHeredoc(val []rune, marker string) ([]rune, error) {
|
||||||
return []rune(out), nil
|
return []rune(out), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Quoted returns true if the token was enclosed in quotes
|
||||||
|
// (i.e. double quotes, backticks, or heredoc).
|
||||||
func (t Token) Quoted() bool {
|
func (t Token) Quoted() bool {
|
||||||
return t.wasQuoted > 0
|
return t.wasQuoted > 0
|
||||||
}
|
}
|
||||||
|
@ -356,6 +358,19 @@ func (t Token) NumLineBreaks() int {
|
||||||
return lineBreaks
|
return lineBreaks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clone returns a deep copy of the token.
|
||||||
|
func (t Token) Clone() Token {
|
||||||
|
return Token{
|
||||||
|
File: t.File,
|
||||||
|
imports: append([]string{}, t.imports...),
|
||||||
|
Line: t.Line,
|
||||||
|
Text: t.Text,
|
||||||
|
wasQuoted: t.wasQuoted,
|
||||||
|
heredocMarker: t.heredocMarker,
|
||||||
|
snippetName: t.snippetName,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var heredocMarkerRegexp = regexp.MustCompile("^[A-Za-z0-9_-]+$")
|
var heredocMarkerRegexp = regexp.MustCompile("^[A-Za-z0-9_-]+$")
|
||||||
|
|
||||||
// isNextOnNewLine tests whether t2 is on a different line from t1
|
// isNextOnNewLine tests whether t2 is on a different line from t1
|
||||||
|
|
|
@ -1282,19 +1282,24 @@ func matcherSetFromMatcherToken(
|
||||||
if tkn.Text == "*" {
|
if tkn.Text == "*" {
|
||||||
// match all requests == no matchers, so nothing to do
|
// match all requests == no matchers, so nothing to do
|
||||||
return nil, true, nil
|
return nil, true, nil
|
||||||
} else if strings.HasPrefix(tkn.Text, "/") {
|
}
|
||||||
// convenient way to specify a single path match
|
|
||||||
|
// convenient way to specify a single path match
|
||||||
|
if strings.HasPrefix(tkn.Text, "/") {
|
||||||
return caddy.ModuleMap{
|
return caddy.ModuleMap{
|
||||||
"path": caddyconfig.JSON(caddyhttp.MatchPath{tkn.Text}, warnings),
|
"path": caddyconfig.JSON(caddyhttp.MatchPath{tkn.Text}, warnings),
|
||||||
}, true, nil
|
}, true, nil
|
||||||
} else if strings.HasPrefix(tkn.Text, matcherPrefix) {
|
}
|
||||||
// pre-defined matcher
|
|
||||||
|
// pre-defined matcher
|
||||||
|
if strings.HasPrefix(tkn.Text, matcherPrefix) {
|
||||||
m, ok := matcherDefs[tkn.Text]
|
m, ok := matcherDefs[tkn.Text]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, false, fmt.Errorf("unrecognized matcher name: %+v", tkn.Text)
|
return nil, false, fmt.Errorf("unrecognized matcher name: %+v", tkn.Text)
|
||||||
}
|
}
|
||||||
return m, true, nil
|
return m, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, false, nil
|
return nil, false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1430,11 +1435,13 @@ func parseMatcherDefinitions(d *caddyfile.Dispenser, matchers map[string]caddy.M
|
||||||
if d.NextArg() {
|
if d.NextArg() {
|
||||||
if d.Token().Quoted() {
|
if d.Token().Quoted() {
|
||||||
// since it was missing the matcher name, we insert a token
|
// since it was missing the matcher name, we insert a token
|
||||||
// in front of the expression token itself
|
// in front of the expression token itself; we use Clone() to
|
||||||
err := makeMatcher("expression", []caddyfile.Token{
|
// make the new token to keep the same the import location as
|
||||||
{Text: "expression", File: d.File(), Line: d.Line()},
|
// the next token, if this is within a snippet or imported file.
|
||||||
d.Token(),
|
// see https://github.com/caddyserver/caddy/issues/6287
|
||||||
})
|
expressionToken := d.Token().Clone()
|
||||||
|
expressionToken.Text = "expression"
|
||||||
|
err := makeMatcher("expression", []caddyfile.Token{expressionToken, d.Token()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
(snippet) {
|
||||||
|
@g `{http.error.status_code} == 404`
|
||||||
|
}
|
||||||
|
|
||||||
example.com
|
example.com
|
||||||
|
|
||||||
@a expression {http.error.status_code} == 400
|
@a expression {http.error.status_code} == 400
|
||||||
|
@ -14,6 +18,12 @@ abort @d
|
||||||
|
|
||||||
@e expression `{http.error.status_code} == 404`
|
@e expression `{http.error.status_code} == 404`
|
||||||
abort @e
|
abort @e
|
||||||
|
|
||||||
|
@f `{http.error.status_code} == 404`
|
||||||
|
abort @f
|
||||||
|
|
||||||
|
import snippet
|
||||||
|
abort @g
|
||||||
----------
|
----------
|
||||||
{
|
{
|
||||||
"apps": {
|
"apps": {
|
||||||
|
@ -106,6 +116,38 @@ abort @e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"abort": true,
|
||||||
|
"handler": "static_response"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"expression": {
|
||||||
|
"expr": "{http.error.status_code} == 404",
|
||||||
|
"name": "f"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"abort": true,
|
||||||
|
"handler": "static_response"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"expression": {
|
||||||
|
"expr": "{http.error.status_code} == 404",
|
||||||
|
"name": "g"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue