Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
3c086fb2
Commit
3c086fb2
authored
Dec 30, 2015
by
Abiola Ibrahim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support for rewrite match group.
parent
55aa492d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
25 additions
and
6 deletions
+25
-6
middleware/rewrite/rewrite.go
middleware/rewrite/rewrite.go
+12
-3
middleware/rewrite/rewrite_test.go
middleware/rewrite/rewrite_test.go
+9
-0
middleware/rewrite/to.go
middleware/rewrite/to.go
+3
-2
middleware/rewrite/to_test.go
middleware/rewrite/to_test.go
+1
-1
No files found.
middleware/rewrite/rewrite.go
View file @
3c086fb2
...
@@ -54,7 +54,7 @@ func (s SimpleRule) Rewrite(fs http.FileSystem, r *http.Request) bool {
...
@@ -54,7 +54,7 @@ func (s SimpleRule) Rewrite(fs http.FileSystem, r *http.Request) bool {
r
.
Header
.
Set
(
headerFieldName
,
r
.
URL
.
RequestURI
())
r
.
Header
.
Set
(
headerFieldName
,
r
.
URL
.
RequestURI
())
// attempt rewrite
// attempt rewrite
return
To
(
fs
,
r
,
s
.
To
)
return
To
(
fs
,
r
,
s
.
To
,
newReplacer
(
r
)
)
}
}
return
false
return
false
}
}
...
@@ -111,6 +111,7 @@ func NewComplexRule(base, pattern, to string, ext []string, ifs []If) (*ComplexR
...
@@ -111,6 +111,7 @@ func NewComplexRule(base, pattern, to string, ext []string, ifs []If) (*ComplexR
// Rewrite rewrites the internal location of the current request.
// Rewrite rewrites the internal location of the current request.
func
(
r
*
ComplexRule
)
Rewrite
(
fs
http
.
FileSystem
,
req
*
http
.
Request
)
bool
{
func
(
r
*
ComplexRule
)
Rewrite
(
fs
http
.
FileSystem
,
req
*
http
.
Request
)
bool
{
rPath
:=
req
.
URL
.
Path
rPath
:=
req
.
URL
.
Path
replacer
:=
newReplacer
(
req
)
// validate base
// validate base
if
!
middleware
.
Path
(
rPath
)
.
Matches
(
r
.
Base
)
{
if
!
middleware
.
Path
(
rPath
)
.
Matches
(
r
.
Base
)
{
...
@@ -130,8 +131,16 @@ func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) bool {
...
@@ -130,8 +131,16 @@ func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) bool {
// validate regexp if present
// validate regexp if present
if
r
.
Regexp
!=
nil
{
if
r
.
Regexp
!=
nil
{
if
!
r
.
MatchString
(
rPath
[
start
:
])
{
matches
:=
r
.
FindStringSubmatch
(
rPath
[
start
:
])
switch
len
(
matches
)
{
case
0
:
// no match
return
false
return
false
default
:
// set regexp match variables {1}, {2} ...
for
i
:=
1
;
i
<
len
(
matches
);
i
++
{
replacer
.
Set
(
fmt
.
Sprint
(
i
),
matches
[
i
])
}
}
}
}
}
...
@@ -143,7 +152,7 @@ func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) bool {
...
@@ -143,7 +152,7 @@ func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) bool {
}
}
// attempt rewrite
// attempt rewrite
return
To
(
fs
,
req
,
r
.
To
)
return
To
(
fs
,
req
,
r
.
To
,
replacer
)
}
}
// matchExt matches rPath against registered file extensions.
// matchExt matches rPath against registered file extensions.
...
...
middleware/rewrite/rewrite_test.go
View file @
3c086fb2
...
@@ -31,6 +31,9 @@ func TestRewrite(t *testing.T) {
...
@@ -31,6 +31,9 @@ func TestRewrite(t *testing.T) {
{
"/abcd/"
,
"ab"
,
"/a/{dir}/{file}"
,
".html|"
},
{
"/abcd/"
,
"ab"
,
"/a/{dir}/{file}"
,
".html|"
},
{
"/abcde/"
,
"ab"
,
"/a#{fragment}"
,
".html|"
},
{
"/abcde/"
,
"ab"
,
"/a#{fragment}"
,
".html|"
},
{
"/ab/"
,
`.*\.jpg`
,
"/ajpg"
,
""
},
{
"/ab/"
,
`.*\.jpg`
,
"/ajpg"
,
""
},
{
"/reggrp"
,
`/ad/([0-9]+)([a-z]*)`
,
"/a{1}/{2}"
,
""
},
{
"/reg2grp"
,
`(.*)`
,
"/{1}"
,
""
},
{
"/reg3grp"
,
`(.*)/(.*)/(.*)`
,
"/{1}{2}{3}"
,
""
},
}
}
for
_
,
regexpRule
:=
range
regexps
{
for
_
,
regexpRule
:=
range
regexps
{
...
@@ -81,6 +84,12 @@ func TestRewrite(t *testing.T) {
...
@@ -81,6 +84,12 @@ func TestRewrite(t *testing.T) {
{
"/abcde/abcde.html"
,
"/a"
},
{
"/abcde/abcde.html"
,
"/a"
},
{
"/abcde/abcde.html#1234"
,
"/a#1234"
},
{
"/abcde/abcde.html#1234"
,
"/a#1234"
},
{
"/ab/ab.jpg"
,
"/ajpg"
},
{
"/ab/ab.jpg"
,
"/ajpg"
},
{
"/reggrp/ad/12"
,
"/a12"
},
{
"/reggrp/ad/124a"
,
"/a124/a"
},
{
"/reggrp/ad/124abc"
,
"/a124/abc"
},
{
"/reg2grp/ad/124abc"
,
"/ad/124abc"
},
{
"/reg3grp/ad/aa/66"
,
"/adaa66"
},
{
"/reg3grp/ad612/n1n/ab"
,
"/ad612n1nab"
},
}
}
for
i
,
test
:=
range
tests
{
for
i
,
test
:=
range
tests
{
...
...
middleware/rewrite/to.go
View file @
3c086fb2
...
@@ -6,14 +6,15 @@ import (
...
@@ -6,14 +6,15 @@ import (
"net/url"
"net/url"
"path"
"path"
"strings"
"strings"
"github.com/mholt/caddy/middleware"
)
)
// To attempts rewrite. It attempts to rewrite to first valid path
// To attempts rewrite. It attempts to rewrite to first valid path
// or the last path if none of the paths are valid.
// or the last path if none of the paths are valid.
// Returns true if rewrite is successful and false otherwise.
// Returns true if rewrite is successful and false otherwise.
func
To
(
fs
http
.
FileSystem
,
r
*
http
.
Request
,
to
string
)
bool
{
func
To
(
fs
http
.
FileSystem
,
r
*
http
.
Request
,
to
string
,
replacer
middleware
.
Replacer
)
bool
{
tos
:=
strings
.
Fields
(
to
)
tos
:=
strings
.
Fields
(
to
)
replacer
:=
newReplacer
(
r
)
// try each rewrite paths
// try each rewrite paths
t
:=
""
t
:=
""
...
...
middleware/rewrite/to_test.go
View file @
3c086fb2
...
@@ -36,7 +36,7 @@ func TestTo(t *testing.T) {
...
@@ -36,7 +36,7 @@ func TestTo(t *testing.T) {
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Error
(
err
)
t
.
Error
(
err
)
}
}
To
(
fs
,
r
,
test
.
to
)
To
(
fs
,
r
,
test
.
to
,
newReplacer
(
r
)
)
if
uri
(
r
.
URL
)
!=
test
.
expected
{
if
uri
(
r
.
URL
)
!=
test
.
expected
{
t
.
Errorf
(
"Test %v: expected %v found %v"
,
i
,
test
.
expected
,
uri
(
r
.
URL
))
t
.
Errorf
(
"Test %v: expected %v found %v"
,
i
,
test
.
expected
,
uri
(
r
.
URL
))
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment