Commit b8c43e55 authored by Matthew Holt's avatar Matthew Holt

Moved redirect middleware into its own package

parent 13cf9808
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 "github.com/mholt/caddy/middleware"
Code int )
}
// 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 { ...@@ -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.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment