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
|
|
|
"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
|
|
|
)
|
|
|
|
|
2016-01-07 13:23:18 +08:00
|
|
|
// Result is the result of a rewrite
|
|
|
|
type Result int
|
2016-01-01 14:05:30 +08:00
|
|
|
|
|
|
|
const (
|
|
|
|
// RewriteIgnored is returned when rewrite is not done on request.
|
2016-01-07 13:23:18 +08:00
|
|
|
RewriteIgnored Result = iota
|
2016-01-01 14:05:30 +08:00
|
|
|
// RewriteDone is returned when rewrite is done on request.
|
|
|
|
RewriteDone
|
|
|
|
// RewriteStatus is returned when rewrite is not needed and status code should be set
|
|
|
|
// for the request.
|
|
|
|
RewriteStatus
|
|
|
|
)
|
|
|
|
|
2015-04-12 06:58:34 +08:00
|
|
|
// Rewrite is middleware to rewrite request locations internally before being handled.
|
|
|
|
type Rewrite struct {
|
2015-12-23 06:19:22 +08:00
|
|
|
Next middleware.Handler
|
|
|
|
FileSys http.FileSystem
|
|
|
|
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) {
|
2016-01-01 14:05:30 +08:00
|
|
|
outer:
|
2015-04-12 06:58:34 +08:00
|
|
|
for _, rule := range rw.Rules {
|
2016-01-01 14:05:30 +08:00
|
|
|
switch result := rule.Rewrite(rw.FileSys, r); result {
|
|
|
|
case RewriteDone:
|
|
|
|
break outer
|
|
|
|
case RewriteIgnored:
|
2015-04-12 06:58:34 +08:00
|
|
|
break
|
2016-01-01 14:05:30 +08:00
|
|
|
case RewriteStatus:
|
|
|
|
// only valid for complex rules.
|
2016-01-01 06:19:11 +08:00
|
|
|
if cRule, ok := rule.(*ComplexRule); ok && cRule.Status != 0 {
|
|
|
|
return cRule.Status, nil
|
|
|
|
}
|
2015-04-12 06:58:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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.
|
2016-01-07 13:23:18 +08:00
|
|
|
Rewrite(http.FileSystem, *http.Request) Result
|
2015-05-15 09:43:29 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2016-01-07 13:23:18 +08:00
|
|
|
func (s SimpleRule) Rewrite(fs http.FileSystem, r *http.Request) Result {
|
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-11-24 19:20:39 +08:00
|
|
|
|
2015-12-23 06:19:22 +08:00
|
|
|
// attempt rewrite
|
2015-12-31 04:47:37 +08:00
|
|
|
return To(fs, r, s.To, newReplacer(r))
|
2015-05-15 09:43:29 +08:00
|
|
|
}
|
2016-01-01 14:05:30 +08:00
|
|
|
return RewriteIgnored
|
2015-05-15 09:43:29 +08:00
|
|
|
}
|
|
|
|
|
2015-12-23 06:19:22 +08:00
|
|
|
// ComplexRule is a rewrite rule based on a regular expression
|
|
|
|
type ComplexRule 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
|
|
|
|
|
2016-01-01 06:19:11 +08:00
|
|
|
// If set, neither performs rewrite nor proceeds
|
|
|
|
// with request. Only returns code.
|
|
|
|
Status int
|
|
|
|
|
2015-05-16 20:03:48 +08:00
|
|
|
// Extensions to filter by
|
|
|
|
Exts []string
|
|
|
|
|
2015-12-23 06:19:22 +08:00
|
|
|
// Rewrite conditions
|
|
|
|
Ifs []If
|
|
|
|
|
2015-05-15 09:43:29 +08:00
|
|
|
*regexp.Regexp
|
|
|
|
}
|
|
|
|
|
2016-01-07 07:04:33 +08:00
|
|
|
// NewComplexRule creates a new RegexpRule. It returns an error if regexp
|
2015-05-16 20:03:48 +08:00
|
|
|
// pattern (pattern) or extensions (ext) are invalid.
|
2016-01-01 06:19:11 +08:00
|
|
|
func NewComplexRule(base, pattern, to string, status int, ext []string, ifs []If) (*ComplexRule, error) {
|
2015-12-23 06:19:22 +08:00
|
|
|
// 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-15 09:43:29 +08:00
|
|
|
}
|
|
|
|
|
2015-12-23 06:19:22 +08:00
|
|
|
// validate extensions if present
|
2015-05-15 09:43:29 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-23 06:19:22 +08:00
|
|
|
return &ComplexRule{
|
|
|
|
Base: base,
|
|
|
|
To: to,
|
2016-01-01 06:19:11 +08:00
|
|
|
Status: status,
|
2015-12-23 06:19:22 +08:00
|
|
|
Exts: ext,
|
|
|
|
Ifs: ifs,
|
|
|
|
Regexp: r,
|
2015-05-15 09:43:29 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-05-16 20:03:48 +08:00
|
|
|
// Rewrite rewrites the internal location of the current request.
|
2016-01-07 13:23:18 +08:00
|
|
|
func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) (re Result) {
|
2015-05-15 09:43:29 +08:00
|
|
|
rPath := req.URL.Path
|
2015-12-31 04:47:37 +08:00
|
|
|
replacer := newReplacer(req)
|
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) {
|
2016-01-01 14:05:30 +08:00
|
|
|
return
|
2015-05-15 09:43:29 +08:00
|
|
|
}
|
2015-05-16 20:03:48 +08:00
|
|
|
|
|
|
|
// validate extensions
|
2015-05-15 09:43:29 +08:00
|
|
|
if !r.matchExt(rPath) {
|
2016-01-01 14:05:30 +08:00
|
|
|
return
|
2015-09-20 15:49:55 +08:00
|
|
|
}
|
|
|
|
|
2015-12-23 16:02:52 +08:00
|
|
|
// validate regexp if present
|
|
|
|
if r.Regexp != nil {
|
2016-01-01 14:05:30 +08:00
|
|
|
// include trailing slash in regexp if present
|
|
|
|
start := len(r.Base)
|
|
|
|
if strings.HasSuffix(r.Base, "/") {
|
|
|
|
start--
|
|
|
|
}
|
|
|
|
|
2015-12-31 04:47:37 +08:00
|
|
|
matches := r.FindStringSubmatch(rPath[start:])
|
|
|
|
switch len(matches) {
|
|
|
|
case 0:
|
|
|
|
// no match
|
2016-01-01 14:05:30 +08:00
|
|
|
return
|
2015-12-31 04:47:37 +08:00
|
|
|
default:
|
|
|
|
// set regexp match variables {1}, {2} ...
|
|
|
|
for i := 1; i < len(matches); i++ {
|
|
|
|
replacer.Set(fmt.Sprint(i), matches[i])
|
|
|
|
}
|
2015-12-23 16:02:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate rewrite conditions
|
|
|
|
for _, i := range r.Ifs {
|
|
|
|
if !i.True(req) {
|
2016-01-01 14:05:30 +08:00
|
|
|
return
|
2015-12-23 16:02:52 +08:00
|
|
|
}
|
2015-05-15 09:43:29 +08:00
|
|
|
}
|
|
|
|
|
2016-01-02 15:08:55 +08:00
|
|
|
// if status is present, stop rewrite and return it.
|
|
|
|
if r.Status != 0 {
|
|
|
|
return RewriteStatus
|
|
|
|
}
|
|
|
|
|
2015-12-23 06:19:22 +08:00
|
|
|
// attempt rewrite
|
2015-12-31 04:47:37 +08:00
|
|
|
return To(fs, req, r.To, replacer)
|
2015-05-15 09:43:29 +08:00
|
|
|
}
|
|
|
|
|
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-12-23 06:19:22 +08:00
|
|
|
func (r *ComplexRule) matchExt(rPath string) bool {
|
2015-05-15 09:43:29 +08:00
|
|
|
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"
|