Skip to content

Commit

Permalink
caddyfile: Add parse error on site address in {
Browse files Browse the repository at this point in the history
This is an incredibly common mistake made by users, so we should catch it earlier in the parser and give a more friendly message. Often it ends up adapting but with mistakes, or erroring out later due to other site addresses being read as directives.

There's not really ever a situation where a lone '{' is valid at the end of a site address (but I suppose there are edgecases where the user wants to use a path matcher where it ends specifically in `{`, but... why?), so this should be fine.
  • Loading branch information
francislavoie committed May 12, 2021
1 parent 2aefe15 commit 4e4b548
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions caddyconfig/caddyfile/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ func (p *parser) addresses() error {
break
}

// Users commonly forget to place a space between the address and the '{'
if strings.HasSuffix(tkn, "{") {
return p.Errf("Addresses cannot end in '{', got '%s' - did you mean to put a space between the address and '{'?", tkn)
}

if tkn != "" { // empty token possible if user typed ""
// Trailing comma indicates another address will follow, which
// may possibly be on the next line
Expand Down
4 changes: 4 additions & 0 deletions caddyconfig/caddyfile/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ func TestParseOneAndImport(t *testing.T) {
"localhost",
}, []int{}},

{`localhost{
dir1
}`, true, []string{}, []int{}},

{`localhost
dir1 {
nested {
Expand Down

0 comments on commit 4e4b548

Please sign in to comment.