2017-09-23 13:56:58 +08:00
|
|
|
// Copyright 2015 Light Code Labs, LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-06-05 12:48:27 +08:00
|
|
|
package errors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy"
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
|
|
|
// setup configures a new errors middleware instance.
|
|
|
|
func setup(c *caddy.Controller) error {
|
|
|
|
handler, err := errorsParse(c)
|
2017-02-08 23:02:09 +08:00
|
|
|
|
2016-06-05 12:48:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-08 23:02:09 +08:00
|
|
|
handler.Log.Attach(c)
|
2016-06-24 08:02:12 +08:00
|
|
|
|
2016-06-21 01:44:20 +08:00
|
|
|
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
|
2016-06-05 12:48:27 +08:00
|
|
|
handler.Next = next
|
|
|
|
return handler
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func errorsParse(c *caddy.Controller) (*ErrorHandler, error) {
|
2017-02-08 23:02:09 +08:00
|
|
|
|
2016-06-05 12:48:27 +08:00
|
|
|
// Very important that we make a pointer because the startup
|
|
|
|
// function that opens the log file must have access to the
|
|
|
|
// same instance of the handler, not a copy.
|
2017-02-08 23:02:09 +08:00
|
|
|
handler := &ErrorHandler{
|
|
|
|
ErrorPages: make(map[int]string),
|
|
|
|
Log: &httpserver.Logger{},
|
|
|
|
}
|
2016-06-05 12:48:27 +08:00
|
|
|
|
2016-06-21 01:44:20 +08:00
|
|
|
cfg := httpserver.GetConfig(c)
|
2016-06-05 12:48:27 +08:00
|
|
|
|
2017-02-09 00:23:33 +08:00
|
|
|
optionalBlock := func() error {
|
2016-06-05 12:48:27 +08:00
|
|
|
for c.NextBlock() {
|
|
|
|
|
|
|
|
what := c.Val()
|
2017-07-20 15:16:43 +08:00
|
|
|
where := c.RemainingArgs()
|
2016-06-05 12:48:27 +08:00
|
|
|
|
2017-02-09 00:23:33 +08:00
|
|
|
if httpserver.IsLogRollerSubdirective(what) {
|
|
|
|
var err error
|
2017-07-20 15:16:43 +08:00
|
|
|
err = httpserver.ParseRoller(handler.Log.Roller, what, where...)
|
2017-02-09 00:23:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-06-05 12:48:27 +08:00
|
|
|
}
|
|
|
|
} else {
|
2017-07-20 15:16:43 +08:00
|
|
|
if len(where) != 1 {
|
|
|
|
return c.ArgErr()
|
|
|
|
}
|
|
|
|
where := where[0]
|
|
|
|
|
2016-06-05 12:48:27 +08:00
|
|
|
// Error page; ensure it exists
|
2016-10-18 09:16:26 +08:00
|
|
|
if !filepath.IsAbs(where) {
|
|
|
|
where = filepath.Join(cfg.Root, where)
|
|
|
|
}
|
2017-02-08 23:02:09 +08:00
|
|
|
|
2016-06-05 12:48:27 +08:00
|
|
|
f, err := os.Open(where)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[WARNING] Unable to open error page '%s': %v", where, err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
|
2016-08-11 20:51:15 +08:00
|
|
|
if what == "*" {
|
2016-08-12 21:47:00 +08:00
|
|
|
if handler.GenericErrorPage != "" {
|
2017-02-09 00:23:33 +08:00
|
|
|
return c.Errf("Duplicate status code entry: %s", what)
|
2016-08-12 21:47:00 +08:00
|
|
|
}
|
2016-08-11 20:51:15 +08:00
|
|
|
handler.GenericErrorPage = where
|
|
|
|
} else {
|
|
|
|
whatInt, err := strconv.Atoi(what)
|
|
|
|
if err != nil {
|
2017-02-09 00:23:33 +08:00
|
|
|
return c.Err("Expecting a numeric status code or '*', got '" + what + "'")
|
2016-08-11 20:51:15 +08:00
|
|
|
}
|
2016-08-12 21:47:00 +08:00
|
|
|
|
|
|
|
if _, exists := handler.ErrorPages[whatInt]; exists {
|
2017-02-09 00:23:33 +08:00
|
|
|
return c.Errf("Duplicate status code entry: %s", what)
|
2016-08-12 21:47:00 +08:00
|
|
|
}
|
|
|
|
|
2016-08-11 20:51:15 +08:00
|
|
|
handler.ErrorPages[whatInt] = where
|
2016-06-05 12:48:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-09 00:23:33 +08:00
|
|
|
return nil
|
2016-06-05 12:48:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for c.Next() {
|
|
|
|
// weird hack to avoid having the handler values overwritten.
|
|
|
|
if c.Val() == "}" {
|
|
|
|
continue
|
|
|
|
}
|
2017-02-09 00:23:33 +08:00
|
|
|
|
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
|
|
|
if len(args) == 1 {
|
|
|
|
switch args[0] {
|
|
|
|
case "visible":
|
|
|
|
handler.Debug = true
|
|
|
|
default:
|
|
|
|
handler.Log.Output = args[0]
|
|
|
|
handler.Log.Roller = httpserver.DefaultLogRoller()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-05 12:48:27 +08:00
|
|
|
// Configuration may be in a block
|
2017-02-09 00:23:33 +08:00
|
|
|
err := optionalBlock()
|
2016-06-05 12:48:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return handler, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler, nil
|
|
|
|
}
|