mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-28 20:01:15 +08:00
httpcaddyfile: Warn on importing empty file; skip dotfiles (#5320)
* httpcaddyfile: Change the parse rules when empty file or dotfile with a glob. * Fixes #5295 * Empty file should just log a warning, and result in no tokens. * The last segment of the path is '*', it should skip any dotfiles. * The last segment of the path is '.*', it should read all dotfiles in a dir. * httpcaddyfile: Regard empty files as import files which include only white space.
This commit is contained in:
parent
8d6870fd06
commit
d6d7511699
|
@ -389,6 +389,20 @@ func (p *parser) doImport() error {
|
|||
} else {
|
||||
return p.Errf("File to import not found: %s", importPattern)
|
||||
}
|
||||
} else {
|
||||
// See issue #5295 - should skip any files that start with a . when iterating over them.
|
||||
sep := string(filepath.Separator)
|
||||
segGlobPattern := strings.Split(globPattern, sep)
|
||||
if strings.HasPrefix(segGlobPattern[len(segGlobPattern)-1], "*") {
|
||||
var tmpMatches []string
|
||||
for _, m := range matches {
|
||||
seg := strings.Split(m, sep)
|
||||
if !strings.HasPrefix(seg[len(seg)-1], ".") {
|
||||
tmpMatches = append(tmpMatches, m)
|
||||
}
|
||||
}
|
||||
matches = tmpMatches
|
||||
}
|
||||
}
|
||||
|
||||
// collect all the imported tokens
|
||||
|
@ -451,6 +465,12 @@ func (p *parser) doSingleImport(importFile string) ([]Token, error) {
|
|||
return nil, p.Errf("Could not read imported file %s: %v", importFile, err)
|
||||
}
|
||||
|
||||
// only warning in case of empty files
|
||||
if len(input) == 0 || len(strings.TrimSpace(string(input))) == 0 {
|
||||
caddy.Log().Warn("Import file is empty", zap.String("file", importFile))
|
||||
return []Token{}, nil
|
||||
}
|
||||
|
||||
importedTokens, err := allTokens(importFile, input)
|
||||
if err != nil {
|
||||
return nil, p.Errf("Could not read tokens while importing %s: %v", importFile, err)
|
||||
|
|
|
@ -187,6 +187,23 @@ func TestParseOneAndImport(t *testing.T) {
|
|||
|
||||
{`import testdata/not_found.txt`, true, []string{}, []int{}},
|
||||
|
||||
// empty file should just log a warning, and result in no tokens
|
||||
{`import testdata/empty.txt`, false, []string{}, []int{}},
|
||||
|
||||
{`import testdata/only_white_space.txt`, false, []string{}, []int{}},
|
||||
|
||||
// import path/to/dir/* should skip any files that start with a . when iterating over them.
|
||||
{`localhost
|
||||
dir1 arg1
|
||||
import testdata/glob/*`, false, []string{
|
||||
"localhost",
|
||||
}, []int{2, 3, 1}},
|
||||
|
||||
// import path/to/dir/.* should continue to read all dotfiles in a dir.
|
||||
{`import testdata/glob/.*`, false, []string{
|
||||
"host1",
|
||||
}, []int{1, 2}},
|
||||
|
||||
{`""`, false, []string{}, []int{}},
|
||||
|
||||
{``, false, []string{}, []int{}},
|
||||
|
|
0
caddyconfig/caddyfile/testdata/empty.txt
vendored
Normal file
0
caddyconfig/caddyfile/testdata/empty.txt
vendored
Normal file
4
caddyconfig/caddyfile/testdata/glob/.dotfile.txt
vendored
Normal file
4
caddyconfig/caddyfile/testdata/glob/.dotfile.txt
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
host1 {
|
||||
dir1
|
||||
dir2 arg1
|
||||
}
|
2
caddyconfig/caddyfile/testdata/glob/import_test1.txt
vendored
Normal file
2
caddyconfig/caddyfile/testdata/glob/import_test1.txt
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
dir2 arg1 arg2
|
||||
dir3
|
7
caddyconfig/caddyfile/testdata/only_white_space.txt
vendored
Normal file
7
caddyconfig/caddyfile/testdata/only_white_space.txt
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user