2017-09-23 00:56:58 -05:00
|
|
|
// Copyright 2015 Light Code Labs, LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-06-05 22:51:56 -05:00
|
|
|
// Package rewrite is middleware for rewriting requests internally to
|
|
|
|
// a different path.
|
|
|
|
package rewrite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Result is the result of a rewrite
|
|
|
|
type Result int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// RewriteIgnored is returned when rewrite is not done on request.
|
|
|
|
RewriteIgnored Result = iota
|
|
|
|
// RewriteDone is returned when rewrite is done on request.
|
|
|
|
RewriteDone
|
|
|
|
)
|
|
|
|
|
|
|
|
// Rewrite is middleware to rewrite request locations internally before being handled.
|
|
|
|
type Rewrite struct {
|
|
|
|
Next httpserver.Handler
|
|
|
|
FileSys http.FileSystem
|
2016-07-08 19:12:52 -05:00
|
|
|
Rules []httpserver.HandlerConfig
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP implements the httpserver.Handler interface.
|
|
|
|
func (rw Rewrite) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
2016-07-08 19:12:52 -05:00
|
|
|
if rule := httpserver.ConfigSelector(rw.Rules).Select(r); rule != nil {
|
2016-08-29 09:14:06 -05:00
|
|
|
rule.(Rule).Rewrite(rw.FileSys, r)
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
2016-08-29 09:14:06 -05:00
|
|
|
|
2016-06-05 22:51:56 -05:00
|
|
|
return rw.Next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rule describes an internal location rewrite rule.
|
|
|
|
type Rule interface {
|
2016-07-08 19:12:52 -05:00
|
|
|
httpserver.HandlerConfig
|
2016-06-05 22:51:56 -05:00
|
|
|
// Rewrite rewrites the internal location of the current request.
|
|
|
|
Rewrite(http.FileSystem, *http.Request) Result
|
|
|
|
}
|
|
|
|
|
|
|
|
// SimpleRule is a simple rewrite rule.
|
|
|
|
type SimpleRule struct {
|
2018-04-14 20:40:55 -05:00
|
|
|
Regexp *regexp.Regexp
|
|
|
|
To string
|
|
|
|
Negate bool
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSimpleRule creates a new Simple Rule
|
2018-04-14 20:40:55 -05:00
|
|
|
func NewSimpleRule(from, to string, negate bool) (*SimpleRule, error) {
|
|
|
|
r, err := regexp.Compile(from)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &SimpleRule{
|
|
|
|
Regexp: r,
|
|
|
|
To: to,
|
|
|
|
Negate: negate,
|
|
|
|
}, nil
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
|
2016-07-08 19:12:52 -05:00
|
|
|
// BasePath satisfies httpserver.Config
|
2018-04-14 20:40:55 -05:00
|
|
|
func (s SimpleRule) BasePath() string { return "/" }
|
2016-07-08 19:12:52 -05:00
|
|
|
|
|
|
|
// Match satisfies httpserver.Config
|
2018-04-14 20:40:55 -05:00
|
|
|
func (s *SimpleRule) Match(r *http.Request) bool {
|
|
|
|
matches := regexpMatches(s.Regexp, "/", r.URL.Path)
|
|
|
|
if s.Negate {
|
|
|
|
return len(matches) == 0
|
|
|
|
}
|
|
|
|
return len(matches) > 0
|
|
|
|
}
|
2016-07-08 19:12:52 -05:00
|
|
|
|
2016-06-05 22:51:56 -05:00
|
|
|
// Rewrite rewrites the internal location of the current request.
|
2018-04-14 20:40:55 -05:00
|
|
|
func (s *SimpleRule) Rewrite(fs http.FileSystem, r *http.Request) Result {
|
2016-06-05 22:51:56 -05:00
|
|
|
|
2016-07-08 19:12:52 -05:00
|
|
|
// attempt rewrite
|
|
|
|
return To(fs, r, s.To, newReplacer(r))
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ComplexRule is a rewrite rule based on a regular expression
|
|
|
|
type ComplexRule struct {
|
|
|
|
// 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
|
|
|
|
|
2016-06-21 09:59:29 -05:00
|
|
|
// Request matcher
|
|
|
|
httpserver.RequestMatcher
|
2016-06-05 22:51:56 -05:00
|
|
|
|
2017-07-30 10:33:59 -05:00
|
|
|
Regexp *regexp.Regexp
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewComplexRule creates a new RegexpRule. It returns an error if regexp
|
|
|
|
// pattern (pattern) or extensions (ext) are invalid.
|
2017-07-30 10:33:59 -05:00
|
|
|
func NewComplexRule(base, pattern, to string, ext []string, matcher httpserver.RequestMatcher) (ComplexRule, error) {
|
2016-06-05 22:51:56 -05:00
|
|
|
// validate regexp if present
|
|
|
|
var r *regexp.Regexp
|
|
|
|
if pattern != "" {
|
|
|
|
var err error
|
|
|
|
r, err = regexp.Compile(pattern)
|
|
|
|
if err != nil {
|
2017-07-30 10:33:59 -05:00
|
|
|
return ComplexRule{}, err
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate extensions if present
|
|
|
|
for _, v := range ext {
|
|
|
|
if len(v) < 2 || (len(v) < 3 && v[0] == '!') {
|
|
|
|
// check if no extension is specified
|
|
|
|
if v != "/" && v != "!/" {
|
2017-07-30 10:33:59 -05:00
|
|
|
return ComplexRule{}, fmt.Errorf("invalid extension %v", v)
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-08 19:12:52 -05:00
|
|
|
// use both IfMatcher and PathMatcher
|
|
|
|
matcher = httpserver.MergeRequestMatchers(
|
|
|
|
// If condition matcher
|
|
|
|
matcher,
|
|
|
|
// Base path matcher
|
|
|
|
httpserver.PathMatcher(base),
|
|
|
|
)
|
|
|
|
|
2017-07-30 10:33:59 -05:00
|
|
|
return ComplexRule{
|
2016-06-21 09:59:29 -05:00
|
|
|
Base: base,
|
|
|
|
To: to,
|
|
|
|
Exts: ext,
|
2016-07-08 19:12:52 -05:00
|
|
|
RequestMatcher: matcher,
|
2016-06-21 09:59:29 -05:00
|
|
|
Regexp: r,
|
2016-06-05 22:51:56 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-07-08 19:12:52 -05:00
|
|
|
// BasePath satisfies httpserver.Config
|
2017-07-30 10:33:59 -05:00
|
|
|
func (r ComplexRule) BasePath() string { return r.Base }
|
2016-06-05 22:51:56 -05:00
|
|
|
|
2016-07-08 19:12:52 -05:00
|
|
|
// Match satisfies httpserver.Config.
|
|
|
|
//
|
|
|
|
// Though ComplexRule embeds a RequestMatcher, additional
|
|
|
|
// checks are needed which requires a custom implementation.
|
2017-07-30 10:33:59 -05:00
|
|
|
func (r ComplexRule) Match(req *http.Request) bool {
|
2016-07-08 19:12:52 -05:00
|
|
|
// validate RequestMatcher
|
|
|
|
// includes if and path
|
|
|
|
if !r.RequestMatcher.Match(req) {
|
|
|
|
return false
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate extensions
|
2016-07-08 19:12:52 -05:00
|
|
|
if !r.matchExt(req.URL.Path) {
|
|
|
|
return false
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
|
2016-07-08 19:12:52 -05:00
|
|
|
// if regex is nil, ignore
|
|
|
|
if r.Regexp == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// otherwise validate regex
|
2018-04-14 20:40:55 -05:00
|
|
|
return regexpMatches(r.Regexp, r.Base, req.URL.Path) != nil
|
2016-07-08 19:12:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rewrite rewrites the internal location of the current request.
|
2017-07-30 10:33:59 -05:00
|
|
|
func (r ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) (re Result) {
|
2016-07-08 19:12:52 -05:00
|
|
|
replacer := newReplacer(req)
|
|
|
|
|
2016-06-05 22:51:56 -05:00
|
|
|
// validate regexp if present
|
|
|
|
if r.Regexp != nil {
|
2018-04-14 20:40:55 -05:00
|
|
|
matches := regexpMatches(r.Regexp, r.Base, req.URL.Path)
|
2016-06-05 22:51:56 -05:00
|
|
|
switch len(matches) {
|
|
|
|
case 0:
|
|
|
|
// no match
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// set regexp match variables {1}, {2} ...
|
|
|
|
|
|
|
|
// url escaped values of ? and #.
|
|
|
|
q, f := url.QueryEscape("?"), url.QueryEscape("#")
|
|
|
|
|
|
|
|
for i := 1; i < len(matches); i++ {
|
|
|
|
// Special case of unescaped # and ? by stdlib regexp.
|
|
|
|
// Reverse the unescape.
|
|
|
|
if strings.ContainsAny(matches[i], "?#") {
|
|
|
|
matches[i] = strings.NewReplacer("?", q, "#", f).Replace(matches[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
replacer.Set(fmt.Sprint(i), matches[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// attempt rewrite
|
|
|
|
return To(fs, req, r.To, replacer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// matchExt matches rPath against registered file extensions.
|
|
|
|
// Returns true if a match is found and false otherwise.
|
2017-07-30 10:33:59 -05:00
|
|
|
func (r ComplexRule) matchExt(rPath string) bool {
|
2016-06-05 22:51:56 -05:00
|
|
|
f := filepath.Base(rPath)
|
|
|
|
ext := path.Ext(f)
|
|
|
|
if ext == "" {
|
|
|
|
ext = "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
mustUse := false
|
|
|
|
for _, v := range r.Exts {
|
|
|
|
use := true
|
|
|
|
if v[0] == '!' {
|
|
|
|
use = false
|
|
|
|
v = v[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if use {
|
|
|
|
mustUse = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if ext == v {
|
|
|
|
return use
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 11:45:30 -05:00
|
|
|
return !mustUse
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
|
2018-04-14 20:40:55 -05:00
|
|
|
func regexpMatches(regexp *regexp.Regexp, base, rPath string) []string {
|
|
|
|
if regexp != nil {
|
2016-07-08 19:12:52 -05:00
|
|
|
// include trailing slash in regexp if present
|
2018-04-14 20:40:55 -05:00
|
|
|
start := len(base)
|
|
|
|
if strings.HasSuffix(base, "/") {
|
2016-07-08 19:12:52 -05:00
|
|
|
start--
|
|
|
|
}
|
2018-04-14 20:40:55 -05:00
|
|
|
return regexp.FindStringSubmatch(rPath[start:])
|
2016-07-08 19:12:52 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-21 09:59:29 -05:00
|
|
|
func newReplacer(r *http.Request) httpserver.Replacer {
|
|
|
|
return httpserver.NewReplacer(r, nil, "")
|
|
|
|
}
|