2015-05-04 12:04:17 -05:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
"github.com/mholt/caddy/middleware/redirect"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Redir configures a new Redirect middleware instance.
|
|
|
|
func Redir(c *Controller) (middleware.Middleware, error) {
|
|
|
|
rules, err := redirParse(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(next middleware.Handler) middleware.Handler {
|
|
|
|
return redirect.Redirect{Next: next, Rules: rules}
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func redirParse(c *Controller) ([]redirect.Rule, error) {
|
|
|
|
var redirects []redirect.Rule
|
|
|
|
|
2015-07-24 13:46:03 -05:00
|
|
|
// setRedirCode sets the redirect code for rule if it can, or returns an error
|
|
|
|
setRedirCode := func(code string, rule *redirect.Rule) error {
|
|
|
|
if code == "meta" {
|
|
|
|
rule.Meta = true
|
|
|
|
} else if codeNumber, ok := httpRedirs[code]; ok {
|
|
|
|
rule.Code = codeNumber
|
|
|
|
} else {
|
|
|
|
return c.Errf("Invalid redirect code '%v'", code)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkAndSaveRule checks the rule for validity (except the redir code)
|
|
|
|
// and saves it if it's valid, or returns an error.
|
|
|
|
checkAndSaveRule := func(rule redirect.Rule) error {
|
2015-09-30 09:38:31 -05:00
|
|
|
if rule.FromPath == rule.To {
|
2015-07-24 13:46:03 -05:00
|
|
|
return c.Err("'from' and 'to' values of redirect rule cannot be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, otherRule := range redirects {
|
2015-09-30 09:38:31 -05:00
|
|
|
if otherRule.FromPath == rule.FromPath {
|
|
|
|
return c.Errf("rule with duplicate 'from' value: %s -> %s", otherRule.FromPath, otherRule.To)
|
2015-07-24 13:46:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
redirects = append(redirects, rule)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-04 12:04:17 -05:00
|
|
|
for c.Next() {
|
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
2015-07-24 13:46:03 -05:00
|
|
|
var hadOptionalBlock bool
|
|
|
|
for c.NextBlock() {
|
|
|
|
hadOptionalBlock = true
|
|
|
|
|
|
|
|
var rule redirect.Rule
|
|
|
|
|
2015-09-30 09:38:31 -05:00
|
|
|
if c.Config.TLS.Enabled {
|
|
|
|
rule.FromScheme = "https"
|
|
|
|
} else {
|
|
|
|
rule.FromScheme = "http"
|
|
|
|
}
|
|
|
|
|
2015-07-24 13:46:03 -05:00
|
|
|
// Set initial redirect code
|
|
|
|
// BUG: If the code is specified for a whole block and that code is invalid,
|
|
|
|
// the line number will appear on the first line inside the block, even if that
|
|
|
|
// line overwrites the block-level code with a valid redirect code. The program
|
|
|
|
// still functions correctly, but the line number in the error reporting is
|
|
|
|
// misleading to the user.
|
|
|
|
if len(args) == 1 {
|
|
|
|
err := setRedirCode(args[0], &rule)
|
|
|
|
if err != nil {
|
|
|
|
return redirects, err
|
|
|
|
}
|
2015-05-04 12:04:17 -05:00
|
|
|
} else {
|
2015-07-24 13:46:03 -05:00
|
|
|
rule.Code = http.StatusMovedPermanently // default code
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
2015-07-24 13:46:03 -05:00
|
|
|
|
|
|
|
// RemainingArgs only gets the values after the current token, but in our
|
|
|
|
// case we want to include the current token to get an accurate count.
|
|
|
|
insideArgs := append([]string{c.Val()}, c.RemainingArgs()...)
|
|
|
|
|
|
|
|
switch len(insideArgs) {
|
|
|
|
case 1:
|
|
|
|
// To specified (catch-all redirect)
|
2015-07-25 16:17:58 -05:00
|
|
|
// Not sure why user is doing this in a table, as it causes all other redirects to be ignored.
|
|
|
|
// As such, this feature remains undocumented.
|
2015-09-30 09:38:31 -05:00
|
|
|
rule.FromPath = "/"
|
2015-07-24 13:46:03 -05:00
|
|
|
rule.To = insideArgs[0]
|
|
|
|
case 2:
|
|
|
|
// From and To specified
|
2015-09-30 09:38:31 -05:00
|
|
|
rule.FromPath = insideArgs[0]
|
2015-07-24 13:46:03 -05:00
|
|
|
rule.To = insideArgs[1]
|
|
|
|
case 3:
|
|
|
|
// From, To, and Code specified
|
2015-09-30 09:38:31 -05:00
|
|
|
rule.FromPath = insideArgs[0]
|
2015-07-24 13:46:03 -05:00
|
|
|
rule.To = insideArgs[1]
|
|
|
|
err := setRedirCode(insideArgs[2], &rule)
|
|
|
|
if err != nil {
|
|
|
|
return redirects, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return redirects, c.ArgErr()
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
|
|
|
|
2015-07-24 13:46:03 -05:00
|
|
|
err := checkAndSaveRule(rule)
|
|
|
|
if err != nil {
|
|
|
|
return redirects, err
|
|
|
|
}
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
|
|
|
|
2015-07-24 13:46:03 -05:00
|
|
|
if !hadOptionalBlock {
|
|
|
|
var rule redirect.Rule
|
2015-09-30 09:38:31 -05:00
|
|
|
|
|
|
|
if c.Config.TLS.Enabled {
|
|
|
|
rule.FromScheme = "https"
|
|
|
|
} else {
|
|
|
|
rule.FromScheme = "http"
|
|
|
|
}
|
|
|
|
|
2015-07-24 13:46:03 -05:00
|
|
|
rule.Code = http.StatusMovedPermanently // default
|
|
|
|
|
|
|
|
switch len(args) {
|
|
|
|
case 1:
|
|
|
|
// To specified (catch-all redirect)
|
2015-09-30 09:38:31 -05:00
|
|
|
rule.FromPath = "/"
|
2015-07-24 13:46:03 -05:00
|
|
|
rule.To = args[0]
|
|
|
|
case 2:
|
|
|
|
// To and Code specified (catch-all redirect)
|
2015-09-30 09:38:31 -05:00
|
|
|
rule.FromPath = "/"
|
2015-07-24 13:46:03 -05:00
|
|
|
rule.To = args[0]
|
|
|
|
err := setRedirCode(args[1], &rule)
|
|
|
|
if err != nil {
|
|
|
|
return redirects, err
|
|
|
|
}
|
|
|
|
case 3:
|
|
|
|
// From, To, and Code specified
|
2015-09-30 09:38:31 -05:00
|
|
|
rule.FromPath = args[0]
|
2015-07-24 13:46:03 -05:00
|
|
|
rule.To = args[1]
|
|
|
|
err := setRedirCode(args[2], &rule)
|
|
|
|
if err != nil {
|
|
|
|
return redirects, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return redirects, c.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
err := checkAndSaveRule(rule)
|
|
|
|
if err != nil {
|
|
|
|
return redirects, err
|
|
|
|
}
|
|
|
|
}
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirects, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// httpRedirs is a list of supported HTTP redirect codes.
|
|
|
|
var httpRedirs = map[string]int{
|
2015-09-30 09:38:31 -05:00
|
|
|
"300": http.StatusMultipleChoices,
|
|
|
|
"301": http.StatusMovedPermanently,
|
|
|
|
"302": http.StatusFound, // (NOT CORRECT for "Temporary Redirect", see 307)
|
|
|
|
"303": http.StatusSeeOther,
|
|
|
|
"304": http.StatusNotModified,
|
|
|
|
"305": http.StatusUseProxy,
|
|
|
|
"307": http.StatusTemporaryRedirect,
|
2015-07-24 13:46:03 -05:00
|
|
|
"308": 308, // Permanent Redirect
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|