2015-01-30 00:06:19 -05:00
|
|
|
// Package rewrite is middleware for rewriting requests internally to
|
|
|
|
// a different path.
|
|
|
|
package rewrite
|
|
|
|
|
|
|
|
import (
|
2015-05-14 20:43:29 -05:00
|
|
|
"fmt"
|
2015-09-20 02:49:55 -05:00
|
|
|
"net/http"
|
2015-05-14 20:43:29 -05:00
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2015-05-24 20:00:54 -05:00
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-01-30 00:06:19 -05:00
|
|
|
)
|
|
|
|
|
2015-04-11 17:58:34 -05:00
|
|
|
// Rewrite is middleware to rewrite request locations internally before being handled.
|
|
|
|
type Rewrite struct {
|
|
|
|
Next middleware.Handler
|
2015-05-04 07:53:54 -05:00
|
|
|
Rules []Rule
|
2015-04-11 17:58:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP implements the middleware.Handler interface.
|
|
|
|
func (rw Rewrite) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
for _, rule := range rw.Rules {
|
2015-05-14 20:43:29 -05:00
|
|
|
if ok := rule.Rewrite(r); ok {
|
2015-04-11 17:58:34 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rw.Next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2015-05-16 07:03:48 -05:00
|
|
|
// Rule describes an internal location rewrite rule.
|
2015-05-14 20:43:29 -05:00
|
|
|
type Rule interface {
|
2015-05-16 07:03:48 -05:00
|
|
|
// Rewrite rewrites the internal location of the current request.
|
2015-05-14 20:43:29 -05:00
|
|
|
Rewrite(*http.Request) bool
|
|
|
|
}
|
|
|
|
|
2015-05-16 07:03:48 -05:00
|
|
|
// SimpleRule is a simple rewrite rule.
|
|
|
|
type SimpleRule struct {
|
|
|
|
From, To string
|
|
|
|
}
|
2015-05-14 20:43:29 -05:00
|
|
|
|
2015-05-16 07:03:48 -05:00
|
|
|
// NewSimpleRule creates a new Simple Rule
|
2015-05-14 20:43:29 -05:00
|
|
|
func NewSimpleRule(from, to string) SimpleRule {
|
|
|
|
return SimpleRule{from, to}
|
|
|
|
}
|
|
|
|
|
2015-05-16 07:03:48 -05:00
|
|
|
// Rewrite rewrites the internal location of the current request.
|
2015-05-14 20:43:29 -05:00
|
|
|
func (s SimpleRule) Rewrite(r *http.Request) bool {
|
2015-05-16 07:03:48 -05:00
|
|
|
if s.From == r.URL.Path {
|
|
|
|
r.URL.Path = s.To
|
2015-05-14 20:43:29 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-05-16 07:03:48 -05:00
|
|
|
// RegexpRule is a rewrite rule based on a regular expression
|
2015-05-14 20:43:29 -05:00
|
|
|
type RegexpRule struct {
|
2015-05-16 07:03:48 -05:00
|
|
|
// Path base. Request to this path and subpaths will be rewritten
|
|
|
|
Base string
|
|
|
|
|
|
|
|
// Path to rewrite to
|
|
|
|
To string
|
|
|
|
|
|
|
|
// Extensions to filter by
|
|
|
|
Exts []string
|
|
|
|
|
2015-05-14 20:43:29 -05:00
|
|
|
*regexp.Regexp
|
|
|
|
}
|
|
|
|
|
2015-05-16 07:03:48 -05:00
|
|
|
// NewRegexpRule creates a new RegexpRule. It returns an error if regexp
|
|
|
|
// pattern (pattern) or extensions (ext) are invalid.
|
2015-05-14 20:43:29 -05:00
|
|
|
func NewRegexpRule(base, pattern, to string, ext []string) (*RegexpRule, error) {
|
|
|
|
r, err := regexp.Compile(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate extensions
|
|
|
|
for _, v := range ext {
|
|
|
|
if len(v) < 2 || (len(v) < 3 && v[0] == '!') {
|
2015-05-16 07:03:48 -05:00
|
|
|
// check if no extension is specified
|
2015-05-14 20:43:29 -05:00
|
|
|
if v != "/" && v != "!/" {
|
2015-06-07 19:49:17 -05:00
|
|
|
return nil, fmt.Errorf("invalid extension %v", v)
|
2015-05-14 20:43:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &RegexpRule{
|
|
|
|
base,
|
|
|
|
to,
|
|
|
|
ext,
|
|
|
|
r,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-05-16 07:03:48 -05:00
|
|
|
// Rewrite rewrites the internal location of the current request.
|
2015-05-14 20:43:29 -05:00
|
|
|
func (r *RegexpRule) Rewrite(req *http.Request) bool {
|
|
|
|
rPath := req.URL.Path
|
2015-05-16 07:03:48 -05:00
|
|
|
|
|
|
|
// validate base
|
2015-05-16 10:57:57 -05:00
|
|
|
if !middleware.Path(rPath).Matches(r.Base) {
|
2015-05-14 20:43:29 -05:00
|
|
|
return false
|
|
|
|
}
|
2015-05-16 07:03:48 -05:00
|
|
|
|
|
|
|
// validate extensions
|
2015-05-14 20:43:29 -05:00
|
|
|
if !r.matchExt(rPath) {
|
|
|
|
return false
|
|
|
|
}
|
2015-05-16 07:03:48 -05:00
|
|
|
|
2015-09-20 02:49:55 -05:00
|
|
|
// include trailing slash in regexp if present
|
|
|
|
start := len(r.Base)
|
|
|
|
if strings.HasSuffix(r.Base, "/") {
|
|
|
|
start -= 1
|
|
|
|
}
|
|
|
|
|
2015-05-16 07:03:48 -05:00
|
|
|
// validate regexp
|
2015-09-20 02:49:55 -05:00
|
|
|
if !r.MatchString(rPath[start:]) {
|
2015-05-14 20:43:29 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-09-20 02:49:55 -05:00
|
|
|
// replace variables
|
|
|
|
to := path.Clean(middleware.NewReplacer(req, nil, "").Replace(r.To))
|
2015-05-14 20:43:29 -05:00
|
|
|
|
2015-05-16 07:03:48 -05:00
|
|
|
// validate resulting path
|
2015-05-14 20:43:29 -05:00
|
|
|
url, err := url.Parse(to)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-05-16 07:03:48 -05:00
|
|
|
// perform rewrite
|
2015-05-14 20:43:29 -05:00
|
|
|
req.URL.Path = url.Path
|
2015-05-16 07:03:48 -05:00
|
|
|
if url.RawQuery != "" {
|
|
|
|
// overwrite query string if present
|
|
|
|
req.URL.RawQuery = url.RawQuery
|
|
|
|
}
|
2015-05-14 20:43:29 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-05-16 07:03:48 -05:00
|
|
|
// matchExt matches rPath against registered file extensions.
|
|
|
|
// Returns true if a match is found and false otherwise.
|
2015-05-14 20:43:29 -05:00
|
|
|
func (r *RegexpRule) matchExt(rPath string) bool {
|
|
|
|
f := filepath.Base(rPath)
|
|
|
|
ext := path.Ext(f)
|
|
|
|
if ext == "" {
|
|
|
|
ext = "/"
|
|
|
|
}
|
2015-05-15 12:47:26 -05:00
|
|
|
|
2015-05-14 20:43:29 -05:00
|
|
|
mustUse := false
|
2015-05-16 07:03:48 -05:00
|
|
|
for _, v := range r.Exts {
|
2015-05-14 20:43:29 -05:00
|
|
|
use := true
|
|
|
|
if v[0] == '!' {
|
|
|
|
use = false
|
|
|
|
v = v[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if use {
|
|
|
|
mustUse = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if ext == v {
|
|
|
|
return use
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 12:47:26 -05:00
|
|
|
|
2015-05-14 20:43:29 -05:00
|
|
|
if mustUse {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2015-01-30 00:06:19 -05:00
|
|
|
}
|