Commit a3b2a6a2 authored by Jonas Östanbäck's avatar Jonas Östanbäck Committed by Matt Holt

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: default avatarJonas Östanbäck <jonas.ostanback@gmail.com>

* Change else ifs into switch
Signed-off-by: default avatarJonas Östanbäck <jonas.ostanback@gmail.com>

* Refactor
Signed-off-by: default avatarJonas Östanbäck <jonas.ostanback@gmail.com>

* Typo
Signed-off-by: default avatarJonas Östanbäck <jonas.ostanback@gmail.com>
parent 724829b6
......@@ -53,43 +53,37 @@ 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 {
// Only an output file specified
rules = appendEntry(rules, "/", &Entry{
Log: &httpserver.Logger{
Output: args[0],
Roller: logRoller,
},
Format: DefaultLogFormat,
})
} else {
// Path scope, output file, and maybe a format specified
path := "/"
format := DefaultLogFormat
output := DefaultLogFilename
switch len(args) {
case 0:
// nothing to change
case 1:
// Only an output file specified
output = args[0]
case 2, 3:
// Path scope, output file, and maybe a format specified
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)
}
default:
// Maximum number of args in log directive is 3.
return nil, c.ArgErr()
}
rules = appendEntry(rules, args[0], &Entry{
rules = appendEntry(rules, path, &Entry{
Log: &httpserver.Logger{
Output: args[1],
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)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment