0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00
caddy/middleware/rewrite/rewrite.go
2015-03-28 16:49:18 -06:00

49 lines
987 B
Go

// Package rewrite is middleware for rewriting requests internally to
// a different path.
package rewrite
import (
"net/http"
"github.com/mholt/caddy/middleware"
)
// New instantiates a new rewrite middleware.
func New(c middleware.Controller) (middleware.Middleware, error) {
var rewrites []rewrite
for c.Next() {
var rule rewrite
if !c.NextArg() {
return nil, c.ArgErr()
}
rule.From = c.Val()
if !c.NextArg() {
return nil, c.ArgErr()
}
rule.To = c.Val()
rewrites = append(rewrites, rule)
}
// TODO: Why can't we just return an http.Handler here instead?
return func(next middleware.HandlerFunc) middleware.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) (int, error) {
for _, rule := range rewrites {
if r.URL.Path == rule.From {
r.URL.Path = rule.To
break
}
}
return next(w, r)
}
}, nil
}
// rewrite describes an internal location rewrite rule.
type rewrite struct {
From string
To string
}