2015-01-30 13:06:19 +08:00
|
|
|
// Package rewrite is middleware for rewriting requests internally to
|
|
|
|
// a different path.
|
|
|
|
package rewrite
|
|
|
|
|
|
|
|
import (
|
2015-05-15 09:43:29 +08:00
|
|
|
"fmt"
|
2015-09-20 15:49:55 +08:00
|
|
|
"net/http"
|
2015-05-15 09:43:29 +08:00
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2015-05-25 09:00:54 +08:00
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-01-30 13:06:19 +08:00
|
|
|
)
|
|
|
|
|
2015-04-12 06:58:34 +08:00
|
|
|
// Rewrite is middleware to rewrite request locations internally before being handled.
|
|
|
|
type Rewrite struct {
|
|
|
|
Next middleware.Handler
|
2015-05-04 20:53:54 +08:00
|
|
|
Rules []Rule
|
2015-04-12 06:58:34 +08: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-15 09:43:29 +08:00
|
|
|
if ok := rule.Rewrite(r); ok {
|
2015-04-12 06:58:34 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rw.Next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2015-05-16 20:03:48 +08:00
|
|
|
// Rule describes an internal location rewrite rule.
|
2015-05-15 09:43:29 +08:00
|
|
|
type Rule interface {
|
2015-05-16 20:03:48 +08:00
|
|
|
// Rewrite rewrites the internal location of the current request.
|
2015-05-15 09:43:29 +08:00
|
|
|
Rewrite(*http.Request) bool
|
|
|
|
}
|
|
|
|
|
2015-05-16 20:03:48 +08:00
|
|
|
// SimpleRule is a simple rewrite rule.
|
|
|
|
type SimpleRule struct {
|
|
|
|
From, To string
|
|
|
|
}
|
2015-05-15 09:43:29 +08:00
|
|
|
|
2015-05-16 20:03:48 +08:00
|
|
|
// NewSimpleRule creates a new Simple Rule
|
2015-05-15 09:43:29 +08:00
|
|
|
func NewSimpleRule(from, to string) SimpleRule {
|
|
|
|
return SimpleRule{from, to}
|
|
|
|
}
|
|
|
|
|
2015-05-16 20:03:48 +08:00
|
|
|
// Rewrite rewrites the internal location of the current request.
|
2015-05-15 09:43:29 +08:00
|
|
|
func (s SimpleRule) Rewrite(r *http.Request) bool {
|
2015-05-16 20:03:48 +08:00
|
|
|
if s.From == r.URL.Path {
|
2015-09-28 08:27:45 +08:00
|
|
|
// take note of this rewrite for internal use by fastcgi
|
|
|
|
// all we need is the URI, not full URL
|
2015-09-29 04:54:48 +08:00
|
|
|
r.Header.Set(headerFieldName, r.URL.RequestURI())
|
2015-05-16 20:03:48 +08:00
|
|
|
r.URL.Path = s.To
|
2015-05-15 09:43:29 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-05-16 20:03:48 +08:00
|
|
|
// RegexpRule is a rewrite rule based on a regular expression
|
2015-05-15 09:43:29 +08:00
|
|
|
type RegexpRule struct {
|
2015-05-16 20:03:48 +08: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-15 09:43:29 +08:00
|
|
|
*regexp.Regexp
|
|
|
|
}
|
|
|
|
|
2015-05-16 20:03:48 +08:00
|
|
|
// NewRegexpRule creates a new RegexpRule. It returns an error if regexp
|
|
|
|
// pattern (pattern) or extensions (ext) are invalid.
|
2015-05-15 09:43:29 +08: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 20:03:48 +08:00
|
|
|
// check if no extension is specified
|
2015-05-15 09:43:29 +08:00
|
|
|
if v != "/" && v != "!/" {
|
2015-06-08 08:49:17 +08:00
|
|
|
return nil, fmt.Errorf("invalid extension %v", v)
|
2015-05-15 09:43:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &RegexpRule{
|
|
|
|
base,
|
|
|
|
to,
|
|
|
|
ext,
|
|
|
|
r,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-05-16 20:03:48 +08:00
|
|
|
// Rewrite rewrites the internal location of the current request.
|
2015-05-15 09:43:29 +08:00
|
|
|
func (r *RegexpRule) Rewrite(req *http.Request) bool {
|
|
|
|
rPath := req.URL.Path
|
2015-05-16 20:03:48 +08:00
|
|
|
|
|
|
|
// validate base
|
2015-05-16 23:57:57 +08:00
|
|
|
if !middleware.Path(rPath).Matches(r.Base) {
|
2015-05-15 09:43:29 +08:00
|
|
|
return false
|
|
|
|
}
|
2015-05-16 20:03:48 +08:00
|
|
|
|
|
|
|
// validate extensions
|
2015-05-15 09:43:29 +08:00
|
|
|
if !r.matchExt(rPath) {
|
|
|
|
return false
|
|
|
|
}
|
2015-05-16 20:03:48 +08:00
|
|
|
|
2015-09-20 15:49:55 +08:00
|
|
|
// include trailing slash in regexp if present
|
|
|
|
start := len(r.Base)
|
|
|
|
if strings.HasSuffix(r.Base, "/") {
|
|
|
|
start -= 1
|
|
|
|
}
|
|
|
|
|
2015-05-16 20:03:48 +08:00
|
|
|
// validate regexp
|
2015-09-20 15:49:55 +08:00
|
|
|
if !r.MatchString(rPath[start:]) {
|
2015-05-15 09:43:29 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-09-20 15:49:55 +08:00
|
|
|
// replace variables
|
|
|
|
to := path.Clean(middleware.NewReplacer(req, nil, "").Replace(r.To))
|
2015-05-15 09:43:29 +08:00
|
|
|
|
2015-05-16 20:03:48 +08:00
|
|
|
// validate resulting path
|
2015-05-15 09:43:29 +08:00
|
|
|
url, err := url.Parse(to)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-09-28 08:27:45 +08:00
|
|
|
// take note of this rewrite for internal use by fastcgi
|
|
|
|
// all we need is the URI, not full URL
|
2015-09-29 04:54:48 +08:00
|
|
|
req.Header.Set(headerFieldName, req.URL.RequestURI())
|
2015-09-28 08:27:45 +08:00
|
|
|
|
2015-05-16 20:03:48 +08:00
|
|
|
// perform rewrite
|
2015-05-15 09:43:29 +08:00
|
|
|
req.URL.Path = url.Path
|
2015-05-16 20:03:48 +08:00
|
|
|
if url.RawQuery != "" {
|
|
|
|
// overwrite query string if present
|
|
|
|
req.URL.RawQuery = url.RawQuery
|
|
|
|
}
|
2015-05-15 09:43:29 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-05-16 20:03:48 +08:00
|
|
|
// matchExt matches rPath against registered file extensions.
|
|
|
|
// Returns true if a match is found and false otherwise.
|
2015-05-15 09:43:29 +08:00
|
|
|
func (r *RegexpRule) matchExt(rPath string) bool {
|
|
|
|
f := filepath.Base(rPath)
|
|
|
|
ext := path.Ext(f)
|
|
|
|
if ext == "" {
|
|
|
|
ext = "/"
|
|
|
|
}
|
2015-05-16 01:47:26 +08:00
|
|
|
|
2015-05-15 09:43:29 +08:00
|
|
|
mustUse := false
|
2015-05-16 20:03:48 +08:00
|
|
|
for _, v := range r.Exts {
|
2015-05-15 09:43:29 +08:00
|
|
|
use := true
|
|
|
|
if v[0] == '!' {
|
|
|
|
use = false
|
|
|
|
v = v[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if use {
|
|
|
|
mustUse = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if ext == v {
|
|
|
|
return use
|
|
|
|
}
|
|
|
|
}
|
2015-05-16 01:47:26 +08:00
|
|
|
|
2015-05-15 09:43:29 +08:00
|
|
|
if mustUse {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2015-01-30 13:06:19 +08:00
|
|
|
}
|
2015-09-29 04:54:48 +08:00
|
|
|
|
|
|
|
// When a rewrite is performed, this header is added to the request
|
|
|
|
// and is for internal use only, specifically the fastcgi middleware.
|
|
|
|
// It contains the original request URI before the rewrite.
|
|
|
|
const headerFieldName = "Caddy-Rewrite-Original-URI"
|