aboutsummaryrefslogtreecommitdiff
path: root/middleware/rewrite/rewrite.go
diff options
context:
space:
mode:
Diffstat (limited to 'middleware/rewrite/rewrite.go')
-rw-r--r--middleware/rewrite/rewrite.go140
1 files changed, 0 insertions, 140 deletions
diff --git a/middleware/rewrite/rewrite.go b/middleware/rewrite/rewrite.go
index 9d8450a3e..2f2f404c9 100644
--- a/middleware/rewrite/rewrite.go
+++ b/middleware/rewrite/rewrite.go
@@ -117,143 +117,3 @@ func (s SimpleRule) Rewrite(r *dns.Msg) Result {
}
return RewriteIgnored
}
-
-/*
-// 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
-
- // If set, neither performs rewrite nor proceeds
- // with request. Only returns code.
- Status int
-
- // Extensions to filter by
- Exts []string
-
- // Rewrite conditions
- Ifs []If
-
- *regexp.Regexp
-}
-
-// NewComplexRule creates a new RegexpRule. It returns an error if regexp
-// pattern (pattern) or extensions (ext) are invalid.
-func NewComplexRule(base, pattern, to string, status int, 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
- }
- }
-
- // 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 != "!/" {
- return nil, fmt.Errorf("invalid extension %v", v)
- }
- }
- }
-
- return &ComplexRule{
- Base: base,
- To: to,
- Status: status,
- Exts: ext,
- Ifs: ifs,
- Regexp: r,
- }, nil
-}
-
-// Rewrite rewrites the internal location of the current request.
-func (r *ComplexRule) Rewrite(req *dns.Msg) (re Result) {
- rPath := req.URL.Path
- replacer := newReplacer(req)
-
- // validate base
- if !middleware.Path(rPath).Matches(r.Base) {
- return
- }
-
- // validate extensions
- if !r.matchExt(rPath) {
- return
- }
-
- // validate regexp if present
- if r.Regexp != nil {
- // include trailing slash in regexp if present
- start := len(r.Base)
- if strings.HasSuffix(r.Base, "/") {
- start--
- }
-
- matches := r.FindStringSubmatch(rPath[start:])
- switch len(matches) {
- case 0:
- // no match
- return
- default:
- // set regexp match variables {1}, {2} ...
- for i := 1; i < len(matches); i++ {
- replacer.Set(fmt.Sprint(i), matches[i])
- }
- }
- }
-
- // validate rewrite conditions
- for _, i := range r.Ifs {
- if !i.True(req) {
- return
- }
- }
-
- // if status is present, stop rewrite and return it.
- if r.Status != 0 {
- return RewriteStatus
- }
-
- // 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.
-func (r *ComplexRule) matchExt(rPath string) bool {
- 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
- }
- }
-
- if mustUse {
- return false
- }
- return true
-}
-*/