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,42 +53,36 @@ func logParse(c *caddy.Controller) ([]*Rule, error) { ...@@ -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) { ...@@ -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)
......
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