2016-06-05 07:48:27 +03: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 18:02:09 +03:00
|
|
|
|
2016-06-05 07:48:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-08 18:02:09 +03:00
|
|
|
handler.Log.Attach(c)
|
2016-06-24 03:02:12 +03:00
|
|
|
|
2016-06-20 20:44:20 +03:00
|
|
|
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
|
2016-06-05 07:48:27 +03:00
|
|
|
handler.Next = next
|
|
|
|
return handler
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func errorsParse(c *caddy.Controller) (*ErrorHandler, error) {
|
2017-02-08 18:02:09 +03:00
|
|
|
|
2016-06-05 07:48:27 +03: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 18:02:09 +03:00
|
|
|
handler := &ErrorHandler{
|
|
|
|
ErrorPages: make(map[int]string),
|
|
|
|
Log: &httpserver.Logger{},
|
|
|
|
}
|
2016-06-05 07:48:27 +03:00
|
|
|
|
2016-06-20 20:44:20 +03:00
|
|
|
cfg := httpserver.GetConfig(c)
|
2016-06-05 07:48:27 +03:00
|
|
|
|
|
|
|
optionalBlock := func() (bool, error) {
|
|
|
|
var hadBlock bool
|
|
|
|
|
|
|
|
for c.NextBlock() {
|
|
|
|
hadBlock = true
|
|
|
|
|
|
|
|
what := c.Val()
|
|
|
|
if !c.NextArg() {
|
|
|
|
return hadBlock, c.ArgErr()
|
|
|
|
}
|
|
|
|
where := c.Val()
|
|
|
|
|
|
|
|
if what == "log" {
|
|
|
|
if where == "visible" {
|
|
|
|
handler.Debug = true
|
|
|
|
} else {
|
2017-02-08 18:02:09 +03:00
|
|
|
handler.Log.Output = where
|
2016-06-05 07:48:27 +03:00
|
|
|
if c.NextArg() {
|
|
|
|
if c.Val() == "{" {
|
|
|
|
c.IncrNest()
|
|
|
|
logRoller, err := httpserver.ParseRoller(c)
|
|
|
|
if err != nil {
|
|
|
|
return hadBlock, err
|
|
|
|
}
|
2017-02-08 18:02:09 +03:00
|
|
|
handler.Log.Roller = logRoller
|
2016-06-05 07:48:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Error page; ensure it exists
|
2016-10-18 04:16:26 +03:00
|
|
|
if !filepath.IsAbs(where) {
|
|
|
|
where = filepath.Join(cfg.Root, where)
|
|
|
|
}
|
2017-02-08 18:02:09 +03:00
|
|
|
|
2016-06-05 07:48:27 +03: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 15:51:15 +03:00
|
|
|
if what == "*" {
|
2016-08-12 16:47:00 +03:00
|
|
|
if handler.GenericErrorPage != "" {
|
|
|
|
return hadBlock, c.Errf("Duplicate status code entry: %s", what)
|
|
|
|
}
|
2016-08-11 15:51:15 +03:00
|
|
|
handler.GenericErrorPage = where
|
|
|
|
} else {
|
|
|
|
whatInt, err := strconv.Atoi(what)
|
|
|
|
if err != nil {
|
|
|
|
return hadBlock, c.Err("Expecting a numeric status code or '*', got '" + what + "'")
|
|
|
|
}
|
2016-08-12 16:47:00 +03:00
|
|
|
|
|
|
|
if _, exists := handler.ErrorPages[whatInt]; exists {
|
|
|
|
return hadBlock, c.Errf("Duplicate status code entry: %s", what)
|
|
|
|
}
|
|
|
|
|
2016-08-11 15:51:15 +03:00
|
|
|
handler.ErrorPages[whatInt] = where
|
2016-06-05 07:48:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hadBlock, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for c.Next() {
|
|
|
|
// weird hack to avoid having the handler values overwritten.
|
|
|
|
if c.Val() == "}" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Configuration may be in a block
|
|
|
|
hadBlock, err := optionalBlock()
|
|
|
|
if err != nil {
|
|
|
|
return handler, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, the only argument would be an error log file name or 'visible'
|
|
|
|
if !hadBlock {
|
|
|
|
if c.NextArg() {
|
|
|
|
if c.Val() == "visible" {
|
|
|
|
handler.Debug = true
|
|
|
|
} else {
|
2017-02-08 18:02:09 +03:00
|
|
|
handler.Log.Output = c.Val()
|
2016-06-05 07:48:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler, nil
|
|
|
|
}
|