0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00
caddy/middleware/redirect/redirect.go
2015-01-29 22:05:54 -07:00

75 lines
1.3 KiB
Go

// Redirect is middleware for redirecting certain requests
// to other locations.
package redirect
import (
"net/http"
"github.com/mholt/caddy/middleware"
)
// New creates a new redirect middleware.
func New(c middleware.Controller) (middleware.Middleware, error) {
var redirects []redirect
for c.Next() {
var rule redirect
// From
if !c.NextArg() {
return nil, c.ArgErr()
}
rule.From = c.Val()
// To
if !c.NextArg() {
return nil, c.ArgErr()
}
rule.To = c.Val()
// Status Code
if !c.NextArg() {
return nil, c.ArgErr()
}
if code, ok := httpRedirs[c.Val()]; !ok {
return nil, c.Err("Invalid redirect code '" + c.Val() + "'")
} else {
rule.Code = code
}
redirects = append(redirects, rule)
}
return func(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
for _, rule := range redirects {
if r.URL.Path == rule.From {
http.Redirect(w, r, rule.To, rule.Code)
break
}
}
next(w, r)
}
}, nil
}
// redirect describes an HTTP redirect rule.
type redirect struct {
From string
To string
Code int
}
// httpRedirs is a list of supported HTTP redirect codes.
var httpRedirs = map[string]int{
"300": 300,
"301": 301,
"302": 302,
"303": 303,
"304": 304,
"305": 305,
"306": 306,
"307": 307,
"308": 308,
}