0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2025-01-13 22:51:08 -05:00

Moved redirect middleware into its own package

This commit is contained in:
Matthew Holt 2015-01-29 22:05:54 -07:00
parent 13cf980879
commit b8c43e55db

View file

@ -1,42 +1,39 @@
package middleware
import "net/http"
// Redirect is middleware for redirecting certain requests // Redirect is middleware for redirecting certain requests
// to other locations. // to other locations.
func Redirect(p parser) Middleware { package redirect
// Redirect describes an HTTP redirect rule. import (
type redirect struct { "net/http"
From string
To string
Code int
}
"github.com/mholt/caddy/middleware"
)
// New creates a new redirect middleware.
func New(c middleware.Controller) (middleware.Middleware, error) {
var redirects []redirect var redirects []redirect
for p.Next() { for c.Next() {
var rule redirect var rule redirect
// From // From
if !p.NextArg() { if !c.NextArg() {
return p.ArgErr() return nil, c.ArgErr()
} }
rule.From = p.Val() rule.From = c.Val()
// To // To
if !p.NextArg() { if !c.NextArg() {
return p.ArgErr() return nil, c.ArgErr()
} }
rule.To = p.Val() rule.To = c.Val()
// Status Code // Status Code
if !p.NextArg() { if !c.NextArg() {
return p.ArgErr() return nil, c.ArgErr()
} }
if code, ok := httpRedirs[p.Val()]; !ok { if code, ok := httpRedirs[c.Val()]; !ok {
return p.Err("Invalid redirect code '" + p.Val() + "'") return nil, c.Err("Invalid redirect code '" + c.Val() + "'")
} else { } else {
rule.Code = code rule.Code = code
} }
@ -54,7 +51,14 @@ func Redirect(p parser) Middleware {
} }
next(w, r) 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. // httpRedirs is a list of supported HTTP redirect codes.