mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-03 23:09:57 -05:00
Improvements to the redirect middleware
This commit is contained in:
parent
b7c8afab2f
commit
a93db40138
1 changed files with 22 additions and 13 deletions
|
@ -30,12 +30,12 @@ type Redirect struct {
|
||||||
// ServeHTTP implements the middleware.Handler interface.
|
// ServeHTTP implements the middleware.Handler interface.
|
||||||
func (rd Redirect) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
func (rd Redirect) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
for _, rule := range rd.Rules {
|
for _, rule := range rd.Rules {
|
||||||
if r.URL.Path == rule.From {
|
|
||||||
if rule.From == "/" {
|
if rule.From == "/" {
|
||||||
// Catchall redirect preserves path (TODO: This behavior should be more standardized...)
|
// Catchall redirect preserves path (TODO: Standardize/formalize this behavior)
|
||||||
http.Redirect(w, r, strings.TrimSuffix(rule.To, "/")+r.URL.Path, rule.Code)
|
http.Redirect(w, r, strings.TrimSuffix(rule.To, "/")+r.URL.Path, rule.Code)
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
if r.URL.Path == rule.From {
|
||||||
http.Redirect(w, r, rule.To, rule.Code)
|
http.Redirect(w, r, rule.To, rule.Code)
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
@ -50,21 +50,31 @@ func parse(c middleware.Controller) ([]Rule, error) {
|
||||||
var rule Rule
|
var rule Rule
|
||||||
args := c.RemainingArgs()
|
args := c.RemainingArgs()
|
||||||
|
|
||||||
if len(args) == 1 {
|
switch len(args) {
|
||||||
// Only 'To' specified
|
case 1:
|
||||||
|
// To specified
|
||||||
rule.From = "/"
|
rule.From = "/"
|
||||||
rule.To = c.Val()
|
rule.To = args[0]
|
||||||
rule.Code = 307 // TODO: Consider 301 instead?
|
rule.Code = http.StatusTemporaryRedirect
|
||||||
} else if len(args) == 3 {
|
case 2:
|
||||||
|
// To and Code specified
|
||||||
|
rule.From = "/"
|
||||||
|
rule.To = args[0]
|
||||||
|
if code, ok := httpRedirs[args[1]]; !ok {
|
||||||
|
return redirects, c.Err("Invalid redirect code '" + args[1] + "'")
|
||||||
|
} else {
|
||||||
|
rule.Code = code
|
||||||
|
}
|
||||||
|
case 3:
|
||||||
// From, To, and Code specified
|
// From, To, and Code specified
|
||||||
rule.From = args[0]
|
rule.From = args[0]
|
||||||
rule.To = args[1]
|
rule.To = args[1]
|
||||||
if code, ok := httpRedirs[args[2]]; !ok {
|
if code, ok := httpRedirs[args[2]]; !ok {
|
||||||
return redirects, c.Err("Invalid redirect code '" + c.Val() + "'")
|
return redirects, c.Err("Invalid redirect code '" + args[2] + "'")
|
||||||
} else {
|
} else {
|
||||||
rule.Code = code
|
rule.Code = code
|
||||||
}
|
}
|
||||||
} else {
|
default:
|
||||||
return redirects, c.ArgErr()
|
return redirects, c.ArgErr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,7 +102,6 @@ var httpRedirs = map[string]int{
|
||||||
"303": 303,
|
"303": 303,
|
||||||
"304": 304,
|
"304": 304,
|
||||||
"305": 305,
|
"305": 305,
|
||||||
"306": 306,
|
|
||||||
"307": 307,
|
"307": 307,
|
||||||
"308": 308,
|
"308": 308,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue