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
|
|
|
"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 {
|
2015-12-22 17:19:22 -05:00
|
|
|
Next middleware.Handler
|
|
|
|
FileSys http.FileSystem
|
|
|
|
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-12-22 17:19:22 -05:00
|
|
|
if ok := rule.Rewrite(rw.FileSys, 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-12-22 17:19:22 -05:00
|
|
|
Rewrite(http.FileSystem, *http.Request) bool
|
2015-05-14 20:43:29 -05:00
|
|
|
}
|
|
|
|
|
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-12-22 17:19:22 -05:00
|
|
|
func (s SimpleRule) Rewrite(fs http.FileSystem, r *http.Request) bool {
|
2015-05-16 07:03:48 -05:00
|
|
|
if s.From == r.URL.Path {
|
2015-09-27 19:27:45 -05:00
|
|
|
// take note of this rewrite for internal use by fastcgi
|
|
|
|
// all we need is the URI, not full URL
|
2015-09-28 15:54:48 -05:00
|
|
|
r.Header.Set(headerFieldName, r.URL.RequestURI())
|
2015-11-24 06:20:39 -05:00
|
|
|
|
2015-12-22 17:19:22 -05:00
|
|
|
// attempt rewrite
|
|
|
|
return To(fs, r, s.To)
|
2015-05-14 20:43:29 -05:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-12-22 17:19:22 -05:00
|
|
|
// ComplexRule is a rewrite rule based on a regular expression
|
|
|
|
type ComplexRule 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-12-22 17:19:22 -05:00
|
|
|
// Rewrite conditions
|
|
|
|
Ifs []If
|
|
|
|
|
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-12-22 17:19:22 -05:00
|
|
|
func NewComplexRule(base, pattern, to string, ext []string, ifs []If) (*ComplexRule, error) {
|
|
|
|
// validate regexp if present
|
|
|
|
var r *regexp.Regexp
|
|
|
|
if pattern != "" {
|
|
|
|
var err error
|
|
|
|
r, err = regexp.Compile(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-14 20:43:29 -05:00
|
|
|
}
|
|
|
|
|
2015-12-22 17:19:22 -05:00
|
|
|
// validate extensions if present
|
2015-05-14 20:43:29 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-22 17:19:22 -05:00
|
|
|
return &ComplexRule{
|
|
|
|
Base: base,
|
|
|
|
To: to,
|
|
|
|
Exts: ext,
|
|
|
|
Ifs: ifs,
|
|
|
|
Regexp: r,
|
2015-05-14 20:43:29 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-05-16 07:03:48 -05:00
|
|
|
// Rewrite rewrites the internal location of the current request.
|
2015-12-22 17:19:22 -05:00
|
|
|
func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) bool {
|
2015-05-14 20:43:29 -05:00
|
|
|
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, "/") {
|
2015-10-09 17:35:34 -05:00
|
|
|
start--
|
2015-09-20 02:49:55 -05:00
|
|
|
}
|
|
|
|
|
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-12-22 17:19:22 -05:00
|
|
|
// attempt rewrite
|
|
|
|
return To(fs, req, r.To)
|
2015-05-14 20:43:29 -05:00
|
|
|
}
|
|
|
|
|
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-12-22 17:19:22 -05:00
|
|
|
func (r *ComplexRule) matchExt(rPath string) bool {
|
2015-05-14 20:43:29 -05:00
|
|
|
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
|
|
|
}
|
2015-09-28 15:54:48 -05: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"
|