mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-24 16:59:00 +01:00
caddyfile: Stricter parsing, error for brace on new line (#5505)
This commit is contained in:
parent
c6ac350a3b
commit
53b6fab125
5 changed files with 533 additions and 521 deletions
|
@ -520,6 +520,9 @@ func (p *parser) directive() error {
|
||||||
if !p.isNextOnNewLine() && p.Token().wasQuoted == 0 {
|
if !p.isNextOnNewLine() && p.Token().wasQuoted == 0 {
|
||||||
return p.Err("Unexpected next token after '{' on same line")
|
return p.Err("Unexpected next token after '{' on same line")
|
||||||
}
|
}
|
||||||
|
if p.isNewLine() {
|
||||||
|
return p.Err("Unexpected '{' on a new line; did you mean to place the '{' on the previous line?")
|
||||||
|
}
|
||||||
} else if p.Val() == "{}" {
|
} else if p.Val() == "{}" {
|
||||||
if p.isNextOnNewLine() && p.Token().wasQuoted == 0 {
|
if p.isNextOnNewLine() && p.Token().wasQuoted == 0 {
|
||||||
return p.Err("Unexpected '{}' at end of line")
|
return p.Err("Unexpected '{}' at end of line")
|
||||||
|
|
|
@ -293,6 +293,14 @@ func TestParseOneAndImport(t *testing.T) {
|
||||||
// Unexpected next token after '{' on same line
|
// Unexpected next token after '{' on same line
|
||||||
{`localhost
|
{`localhost
|
||||||
dir1 { a b }`, true, []string{"localhost"}, []int{}},
|
dir1 { a b }`, true, []string{"localhost"}, []int{}},
|
||||||
|
|
||||||
|
// Unexpected '{' on a new line
|
||||||
|
{`localhost
|
||||||
|
dir1
|
||||||
|
{
|
||||||
|
a b
|
||||||
|
}`, true, []string{"localhost"}, []int{}},
|
||||||
|
|
||||||
// Workaround with quotes
|
// Workaround with quotes
|
||||||
{`localhost
|
{`localhost
|
||||||
dir1 "{" a b "}"`, false, []string{"localhost"}, []int{5}},
|
dir1 "{" a b "}"`, false, []string{"localhost"}, []int{5}},
|
||||||
|
|
|
@ -161,7 +161,7 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for d.Next() {
|
d.Next() // consume the directive name
|
||||||
for _, up := range d.RemainingArgs() {
|
for _, up := range d.RemainingArgs() {
|
||||||
err := appendUpstream(up)
|
err := appendUpstream(up)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -169,7 +169,7 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for d.NextBlock(0) {
|
for nesting := d.Nesting(); d.NextBlock(nesting); {
|
||||||
// if the subdirective has an "@" prefix then we
|
// if the subdirective has an "@" prefix then we
|
||||||
// parse it as a response matcher for use with "handle_response"
|
// parse it as a response matcher for use with "handle_response"
|
||||||
if strings.HasPrefix(d.Val(), matcherPrefix) {
|
if strings.HasPrefix(d.Val(), matcherPrefix) {
|
||||||
|
@ -712,7 +712,6 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||||
return d.Errf("unrecognized subdirective %s", d.Val())
|
return d.Errf("unrecognized subdirective %s", d.Val())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// if the scheme inferred from the backends' addresses is
|
// if the scheme inferred from the backends' addresses is
|
||||||
// HTTPS, we will need a non-nil transport to enable TLS,
|
// HTTPS, we will need a non-nil transport to enable TLS,
|
||||||
|
|
|
@ -373,6 +373,7 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
|
||||||
|
|
||||||
// the rest of the config is specified by the user
|
// the rest of the config is specified by the user
|
||||||
// using the reverse_proxy directive syntax
|
// using the reverse_proxy directive syntax
|
||||||
|
dispenser.Next() // consume the directive name
|
||||||
err = rpHandler.UnmarshalCaddyfile(dispenser)
|
err = rpHandler.UnmarshalCaddyfile(dispenser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -216,6 +216,7 @@ func parseCaddyfile(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
|
||||||
|
|
||||||
// the rest of the config is specified by the user
|
// the rest of the config is specified by the user
|
||||||
// using the reverse_proxy directive syntax
|
// using the reverse_proxy directive syntax
|
||||||
|
dispenser.Next() // consume the directive name
|
||||||
err = rpHandler.UnmarshalCaddyfile(dispenser)
|
err = rpHandler.UnmarshalCaddyfile(dispenser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in a new issue