2015-01-30 00:06:19 -05:00
|
|
|
// Package rewrite is middleware for rewriting requests internally to
|
|
|
|
// a different path.
|
|
|
|
package rewrite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2015-05-14 20:43:29 -05:00
|
|
|
"fmt"
|
2015-01-30 00:06:19 -05:00
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-05-14 20:43:29 -05:00
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
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 != "!/" {
|
|
|
|
return nil, fmt.Errorf("Invalid extension %v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &RegexpRule{
|
|
|
|
base,
|
|
|
|
to,
|
|
|
|
ext,
|
|
|
|
r,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-05-16 07:03:48 -05:00
|
|
|
// regexpVars are variables that can be used for To (rewrite destination path).
|
2015-05-15 12:47:26 -05:00
|
|
|
var regexpVars []string = []string{
|
|
|
|
"{path}",
|
|
|
|
"{query}",
|
2015-05-16 10:27:41 -05:00
|
|
|
"{file}",
|
|
|
|
"{dir}",
|
|
|
|
"{frag}",
|
2015-05-14 20:43:29 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if !strings.HasPrefix(rPath, 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
|
|
|
|
|
|
|
// validate regexp
|
|
|
|
if !r.MatchString(rPath[len(r.Base):]) {
|
2015-05-14 20:43:29 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-05-16 07:03:48 -05:00
|
|
|
to := r.To
|
2015-05-14 20:43:29 -05:00
|
|
|
|
|
|
|
// check variables
|
|
|
|
for _, v := range regexpVars {
|
2015-05-16 07:03:48 -05:00
|
|
|
if strings.Contains(r.To, v) {
|
2015-05-14 20:43:29 -05:00
|
|
|
switch v {
|
2015-05-16 07:03:48 -05:00
|
|
|
case "{path}":
|
2015-05-14 20:43:29 -05:00
|
|
|
to = strings.Replace(to, v, req.URL.Path[1:], -1)
|
2015-05-16 07:03:48 -05:00
|
|
|
case "{query}":
|
2015-05-14 20:43:29 -05:00
|
|
|
to = strings.Replace(to, v, req.URL.RawQuery, -1)
|
2015-05-16 10:27:41 -05:00
|
|
|
case "{frag}":
|
|
|
|
to = strings.Replace(to, v, req.URL.Fragment, -1)
|
|
|
|
case "{file}":
|
|
|
|
_, file := path.Split(req.URL.Path)
|
|
|
|
to = strings.Replace(to, v, file, -1)
|
|
|
|
case "{dir}":
|
|
|
|
dir, _ := path.Split(req.URL.Path)
|
|
|
|
to = path.Clean(strings.Replace(to, v, dir, -1))
|
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
|
|
|
}
|