mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-21 19:07:26 +08:00
log: Add check for maximum number of arguments to log directive (#1672)
* Add check for maximum number of arguments to log directive * Add failing test case Signed-off-by: Jonas Östanbäck <jonas.ostanback@gmail.com> * Change else ifs into switch Signed-off-by: Jonas Östanbäck <jonas.ostanback@gmail.com> * Refactor Signed-off-by: Jonas Östanbäck <jonas.ostanback@gmail.com> * Typo Signed-off-by: Jonas Östanbäck <jonas.ostanback@gmail.com>
This commit is contained in:
parent
724829b689
commit
a3b2a6a296
@ -53,42 +53,36 @@ func logParse(c *caddy.Controller) ([]*Rule, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if len(args) == 0 {
|
||||
// Nothing specified; use defaults
|
||||
rules = appendEntry(rules, "/", &Entry{
|
||||
Log: &httpserver.Logger{
|
||||
Output: DefaultLogFilename,
|
||||
Roller: logRoller,
|
||||
},
|
||||
Format: DefaultLogFormat,
|
||||
})
|
||||
} else if len(args) == 1 {
|
||||
path := "/"
|
||||
format := DefaultLogFormat
|
||||
output := DefaultLogFilename
|
||||
|
||||
switch len(args) {
|
||||
case 0:
|
||||
// nothing to change
|
||||
case 1:
|
||||
// Only an output file specified
|
||||
rules = appendEntry(rules, "/", &Entry{
|
||||
Log: &httpserver.Logger{
|
||||
Output: args[0],
|
||||
Roller: logRoller,
|
||||
},
|
||||
Format: DefaultLogFormat,
|
||||
})
|
||||
} else {
|
||||
output = args[0]
|
||||
case 2, 3:
|
||||
// Path scope, output file, and maybe a format specified
|
||||
|
||||
format := DefaultLogFormat
|
||||
|
||||
path = args[0]
|
||||
output = args[1]
|
||||
if len(args) > 2 {
|
||||
format = strings.Replace(args[2], "{common}", CommonLogFormat, -1)
|
||||
format = strings.Replace(format, "{combined}", CombinedLogFormat, -1)
|
||||
}
|
||||
|
||||
rules = appendEntry(rules, args[0], &Entry{
|
||||
Log: &httpserver.Logger{
|
||||
Output: args[1],
|
||||
Roller: logRoller,
|
||||
},
|
||||
Format: format,
|
||||
})
|
||||
default:
|
||||
// Maximum number of args in log directive is 3.
|
||||
return nil, c.ArgErr()
|
||||
}
|
||||
|
||||
rules = appendEntry(rules, path, &Entry{
|
||||
Log: &httpserver.Logger{
|
||||
Output: output,
|
||||
Roller: logRoller,
|
||||
},
|
||||
Format: format,
|
||||
})
|
||||
}
|
||||
|
||||
return rules, nil
|
||||
|
@ -227,6 +227,7 @@ func TestLogParse(t *testing.T) {
|
||||
}}},
|
||||
{`log access.log { rotate_size }`, true, nil},
|
||||
{`log access.log { invalid_option 1 }`, true, nil},
|
||||
{`log / acccess.log "{remote} - [{when}] "{method} {port}" {scheme} {mitm} "`, true, nil},
|
||||
}
|
||||
for i, test := range tests {
|
||||
c := caddy.NewTestController("http", test.inputLogRules)
|
||||
|
Loading…
x
Reference in New Issue
Block a user