mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-20 22:52:58 -05:00
Moved rewrite middleware into its own package
This commit is contained in:
parent
b8c43e55db
commit
261beb046e
2 changed files with 48 additions and 44 deletions
|
@ -1,44 +0,0 @@
|
||||||
package middleware
|
|
||||||
|
|
||||||
import "net/http"
|
|
||||||
|
|
||||||
// Rewrite is middleware for rewriting requests internally to
|
|
||||||
// a different path.
|
|
||||||
func Rewrite(p parser) Middleware {
|
|
||||||
|
|
||||||
// Rewrite describes an internal location rewrite rule.
|
|
||||||
type rewrite struct {
|
|
||||||
From string
|
|
||||||
To string
|
|
||||||
}
|
|
||||||
|
|
||||||
var rewrites []rewrite
|
|
||||||
|
|
||||||
for p.Next() {
|
|
||||||
var rule rewrite
|
|
||||||
|
|
||||||
if !p.NextArg() {
|
|
||||||
return p.ArgErr()
|
|
||||||
}
|
|
||||||
rule.From = p.Val()
|
|
||||||
|
|
||||||
if !p.NextArg() {
|
|
||||||
return p.ArgErr()
|
|
||||||
}
|
|
||||||
rule.To = p.Val()
|
|
||||||
|
|
||||||
rewrites = append(rewrites, rule)
|
|
||||||
}
|
|
||||||
|
|
||||||
return func(next http.HandlerFunc) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
for _, rule := range rewrites {
|
|
||||||
if r.URL.Path == rule.From {
|
|
||||||
r.URL.Path = rule.To
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
next(w, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
48
middleware/rewrite/rewrite.go
Normal file
48
middleware/rewrite/rewrite.go
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(next http.HandlerFunc) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
for _, rule := range rewrites {
|
||||||
|
if r.URL.Path == rule.From {
|
||||||
|
r.URL.Path = rule.To
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next(w, r)
|
||||||
|
}
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// rewrite describes an internal location rewrite rule.
|
||||||
|
type rewrite struct {
|
||||||
|
From string
|
||||||
|
To string
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue