Commit 40b52fb0 authored by Matt Holt's avatar Matt Holt Committed by GitHub

Merge pull request #1775 from tw4452852/roller_parse

log,error: fix roller parser issue
parents f1dd9f2b 91150bb7
...@@ -44,18 +44,20 @@ func errorsParse(c *caddy.Controller) (*ErrorHandler, error) { ...@@ -44,18 +44,20 @@ func errorsParse(c *caddy.Controller) (*ErrorHandler, error) {
for c.NextBlock() { for c.NextBlock() {
what := c.Val() what := c.Val()
if !c.NextArg() { where := c.RemainingArgs()
return c.ArgErr()
}
where := c.Val()
if httpserver.IsLogRollerSubdirective(what) { if httpserver.IsLogRollerSubdirective(what) {
var err error var err error
err = httpserver.ParseRoller(handler.Log.Roller, what, where) err = httpserver.ParseRoller(handler.Log.Roller, what, where...)
if err != nil { if err != nil {
return err return err
} }
} else { } else {
if len(where) != 1 {
return c.ArgErr()
}
where := where[0]
// Error page; ensure it exists // Error page; ensure it exists
if !filepath.IsAbs(where) { if !filepath.IsAbs(where) {
where = filepath.Join(cfg.Root, where) where = filepath.Join(cfg.Root, where)
......
...@@ -85,7 +85,12 @@ func TestErrorsParse(t *testing.T) { ...@@ -85,7 +85,12 @@ func TestErrorsParse(t *testing.T) {
Roller: httpserver.DefaultLogRoller(), Roller: httpserver.DefaultLogRoller(),
}, },
}}, }},
{`errors errors.txt { rotate_size 2 rotate_age 10 rotate_keep 3 rotate_compress }`, false, ErrorHandler{ {`errors errors.txt {
rotate_size 2
rotate_age 10
rotate_keep 3
rotate_compress
}`, false, ErrorHandler{
ErrorPages: map[int]string{}, ErrorPages: map[int]string{},
Log: &httpserver.Logger{ Log: &httpserver.Logger{
Output: "errors.txt", Roller: &httpserver.LogRoller{ Output: "errors.txt", Roller: &httpserver.LogRoller{
...@@ -144,6 +149,12 @@ func TestErrorsParse(t *testing.T) { ...@@ -144,6 +149,12 @@ func TestErrorsParse(t *testing.T) {
}, },
Log: &httpserver.Logger{}, Log: &httpserver.Logger{},
}}, }},
{`errors errors.txt { rotate_size 2 rotate_age 10 rotate_keep 3 rotate_compress }`,
true, ErrorHandler{ErrorPages: map[int]string{}, Log: &httpserver.Logger{}}},
{`errors errors.txt {
rotate_compress invalid
}`,
true, ErrorHandler{ErrorPages: map[int]string{}, Log: &httpserver.Logger{}}},
// Next two test cases is the detection of duplicate status codes // Next two test cases is the detection of duplicate status codes
{`errors { {`errors {
503 503.html 503 503.html
......
package httpserver package httpserver
import ( import (
"errors"
"io" "io"
"path/filepath" "path/filepath"
"strconv" "strconv"
...@@ -54,17 +55,32 @@ func IsLogRollerSubdirective(subdir string) bool { ...@@ -54,17 +55,32 @@ func IsLogRollerSubdirective(subdir string) bool {
subdir == directiveRotateCompress subdir == directiveRotateCompress
} }
var invalidRollerParameterErr = errors.New("invalid roller parameter")
// ParseRoller parses roller contents out of c. // ParseRoller parses roller contents out of c.
func ParseRoller(l *LogRoller, what string, where string) error { func ParseRoller(l *LogRoller, what string, where ...string) error {
if l == nil { if l == nil {
l = DefaultLogRoller() l = DefaultLogRoller()
} }
var value int
var err error // rotate_compress doesn't accept any parameters.
value, err = strconv.Atoi(where) // others only accept one parameter
if what != directiveRotateCompress && err != nil { if (what == directiveRotateCompress && len(where) != 0) ||
return err (what != directiveRotateCompress && len(where) != 1) {
return invalidRollerParameterErr
}
var (
value int
err error
)
if what != directiveRotateCompress {
value, err = strconv.Atoi(where[0])
if err != nil {
return err
}
} }
switch what { switch what {
case directiveRotateSize: case directiveRotateSize:
l.MaxSize = value l.MaxSize = value
......
...@@ -38,17 +38,14 @@ func logParse(c *caddy.Controller) ([]*Rule, error) { ...@@ -38,17 +38,14 @@ func logParse(c *caddy.Controller) ([]*Rule, error) {
for c.NextBlock() { for c.NextBlock() {
what := c.Val() what := c.Val()
if !c.NextArg() { where := c.RemainingArgs()
return nil, c.ArgErr()
}
where := c.Val()
// only support roller related options inside a block // only support roller related options inside a block
if !httpserver.IsLogRollerSubdirective(what) { if !httpserver.IsLogRollerSubdirective(what) {
return nil, c.ArgErr() return nil, c.ArgErr()
} }
if err := httpserver.ParseRoller(logRoller, what, where); err != nil { if err := httpserver.ParseRoller(logRoller, what, where...); err != nil {
return nil, err return nil, err
} }
} }
......
...@@ -194,7 +194,12 @@ func TestLogParse(t *testing.T) { ...@@ -194,7 +194,12 @@ func TestLogParse(t *testing.T) {
Format: "{when}", Format: "{when}",
}}, }},
}}}, }}},
{`log access.log { rotate_size 2 rotate_age 10 rotate_keep 3 }`, false, []Rule{{ {`log access.log {
rotate_size 2
rotate_age 10
rotate_keep 3
rotate_compress
}`, false, []Rule{{
PathScope: "/", PathScope: "/",
Entries: []*Entry{{ Entries: []*Entry{{
Log: &httpserver.Logger{ Log: &httpserver.Logger{
...@@ -203,7 +208,7 @@ func TestLogParse(t *testing.T) { ...@@ -203,7 +208,7 @@ func TestLogParse(t *testing.T) {
MaxSize: 2, MaxSize: 2,
MaxAge: 10, MaxAge: 10,
MaxBackups: 3, MaxBackups: 3,
Compress: false, Compress: true,
LocalTime: true, LocalTime: true,
}}, }},
Format: DefaultLogFormat, Format: DefaultLogFormat,
...@@ -226,6 +231,8 @@ func TestLogParse(t *testing.T) { ...@@ -226,6 +231,8 @@ func TestLogParse(t *testing.T) {
Format: "{when}", Format: "{when}",
}}, }},
}}}, }}},
{`log access.log { rotate_size 2 rotate_age 10 rotate_keep 3 }`, true, nil},
{`log access.log { rotate_compress invalid }`, true, nil},
{`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}, {`log / acccess.log "{remote} - [{when}] "{method} {port}" {scheme} {mitm} "`, true, nil},
......
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