mirror of
https://github.com/caddyserver/caddy.git
synced 2025-05-31 12:15:56 -04: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 {
|
path := "/"
|
||||||
// Nothing specified; use defaults
|
format := DefaultLogFormat
|
||||||
rules = appendEntry(rules, "/", &Entry{
|
output := DefaultLogFilename
|
||||||
Log: &httpserver.Logger{
|
|
||||||
Output: DefaultLogFilename,
|
switch len(args) {
|
||||||
Roller: logRoller,
|
case 0:
|
||||||
},
|
// nothing to change
|
||||||
Format: DefaultLogFormat,
|
case 1:
|
||||||
})
|
|
||||||
} else if len(args) == 1 {
|
|
||||||
// Only an output file specified
|
// Only an output file specified
|
||||||
rules = appendEntry(rules, "/", &Entry{
|
output = args[0]
|
||||||
Log: &httpserver.Logger{
|
case 2, 3:
|
||||||
Output: args[0],
|
|
||||||
Roller: logRoller,
|
|
||||||
},
|
|
||||||
Format: DefaultLogFormat,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
// Path scope, output file, and maybe a format specified
|
// Path scope, output file, and maybe a format specified
|
||||||
|
path = args[0]
|
||||||
format := DefaultLogFormat
|
output = args[1]
|
||||||
|
|
||||||
if len(args) > 2 {
|
if len(args) > 2 {
|
||||||
format = strings.Replace(args[2], "{common}", CommonLogFormat, -1)
|
format = strings.Replace(args[2], "{common}", CommonLogFormat, -1)
|
||||||
format = strings.Replace(format, "{combined}", CombinedLogFormat, -1)
|
format = strings.Replace(format, "{combined}", CombinedLogFormat, -1)
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
rules = appendEntry(rules, args[0], &Entry{
|
// Maximum number of args in log directive is 3.
|
||||||
Log: &httpserver.Logger{
|
return nil, c.ArgErr()
|
||||||
Output: args[1],
|
|
||||||
Roller: logRoller,
|
|
||||||
},
|
|
||||||
Format: format,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rules = appendEntry(rules, path, &Entry{
|
||||||
|
Log: &httpserver.Logger{
|
||||||
|
Output: output,
|
||||||
|
Roller: logRoller,
|
||||||
|
},
|
||||||
|
Format: format,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return rules, nil
|
return rules, nil
|
||||||
|
@ -227,6 +227,7 @@ func TestLogParse(t *testing.T) {
|
|||||||
}}},
|
}}},
|
||||||
{`log access.log { rotate_size }`, true, nil},
|
{`log access.log { rotate_size }`, true, nil},
|
||||||
{`log access.log { invalid_option 1 }`, 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 {
|
for i, test := range tests {
|
||||||
c := caddy.NewTestController("http", test.inputLogRules)
|
c := caddy.NewTestController("http", test.inputLogRules)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user