From 441a8f5eff5720ad0e5c1ba1dbb12a44516d1a9f Mon Sep 17 00:00:00 2001 From: Volodymyr Galkin Date: Fri, 12 Aug 2016 16:47:00 +0300 Subject: [PATCH] Check for duplicate status code entries in 'errors' directive --- caddyhttp/errors/setup.go | 8 ++++++++ caddyhttp/errors/setup_test.go | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/caddyhttp/errors/setup.go b/caddyhttp/errors/setup.go index 0b6c266b9..ecaa92464 100644 --- a/caddyhttp/errors/setup.go +++ b/caddyhttp/errors/setup.go @@ -123,12 +123,20 @@ func errorsParse(c *caddy.Controller) (*ErrorHandler, error) { f.Close() if what == "*" { + if handler.GenericErrorPage != "" { + return hadBlock, c.Errf("Duplicate status code entry: %s", what) + } handler.GenericErrorPage = where } else { whatInt, err := strconv.Atoi(what) if err != nil { return hadBlock, c.Err("Expecting a numeric status code or '*', got '" + what + "'") } + + if _, exists := handler.ErrorPages[whatInt]; exists { + return hadBlock, c.Errf("Duplicate status code entry: %s", what) + } + handler.ErrorPages[whatInt] = where } } diff --git a/caddyhttp/errors/setup_test.go b/caddyhttp/errors/setup_test.go index b9a34f046..233b62e32 100644 --- a/caddyhttp/errors/setup_test.go +++ b/caddyhttp/errors/setup_test.go @@ -115,6 +115,15 @@ func TestErrorsParse(t *testing.T) { 503: "503.html", }, }}, + // Next two test cases is the detection of duplicate status codes + {`errors { + 503 503.html + 503 503.html +}`, true, ErrorHandler{}}, + {`errors { + * generic_error.html + * generic_error.html +}`, true, ErrorHandler{}}, } for i, test := range tests { actualErrorsRule, err := errorsParse(caddy.NewTestController("http", test.inputErrorsRules)) @@ -123,6 +132,8 @@ func TestErrorsParse(t *testing.T) { t.Errorf("Test %d didn't error, but it should have", i) } else if err != nil && !test.shouldErr { t.Errorf("Test %d errored, but it shouldn't have; got '%v'", i, err) + } else { + continue } if actualErrorsRule.LogFile != test.expectedErrorHandler.LogFile { t.Errorf("Test %d expected LogFile to be %s, but got %s",