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
104a5998
Commit
104a5998
authored
Sep 23, 2015
by
Matt Holt
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #251 from abiosoft/master
rewrite: Use middleware.Replacer
parents
6cbd3ab0
7f9fa573
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
20 additions
and
34 deletions
+20
-34
middleware/replacer.go
middleware/replacer.go
+9
-0
middleware/rewrite/rewrite.go
middleware/rewrite/rewrite.go
+10
-33
middleware/rewrite/rewrite_test.go
middleware/rewrite/rewrite_test.go
+1
-1
No files found.
middleware/replacer.go
View file @
104a5998
...
...
@@ -3,6 +3,7 @@ package middleware
import
(
"net"
"net/http"
"path"
"strconv"
"strings"
"time"
...
...
@@ -63,6 +64,14 @@ func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Repla
"{when}"
:
func
()
string
{
return
time
.
Now
()
.
Format
(
timeFormat
)
}(),
"{file}"
:
func
()
string
{
_
,
file
:=
path
.
Split
(
r
.
URL
.
Path
)
return
file
}(),
"{dir}"
:
func
()
string
{
dir
,
_
:=
path
.
Split
(
r
.
URL
.
Path
)
return
dir
}(),
},
emptyValue
:
emptyValue
,
}
...
...
middleware/rewrite/rewrite.go
View file @
104a5998
...
...
@@ -3,9 +3,8 @@
package
rewrite
import
(
"net/http"
"fmt"
"net/http"
"net/url"
"path"
"path/filepath"
...
...
@@ -96,15 +95,6 @@ func NewRegexpRule(base, pattern, to string, ext []string) (*RegexpRule, error)
},
nil
}
// regexpVars are variables that can be used for To (rewrite destination path).
var
regexpVars
=
[]
string
{
"{path}"
,
"{query}"
,
"{file}"
,
"{dir}"
,
"{frag}"
,
}
// Rewrite rewrites the internal location of the current request.
func
(
r
*
RegexpRule
)
Rewrite
(
req
*
http
.
Request
)
bool
{
rPath
:=
req
.
URL
.
Path
...
...
@@ -119,32 +109,19 @@ func (r *RegexpRule) Rewrite(req *http.Request) bool {
return
false
}
// include trailing slash in regexp if present
start
:=
len
(
r
.
Base
)
if
strings
.
HasSuffix
(
r
.
Base
,
"/"
)
{
start
-=
1
}
// validate regexp
if
!
r
.
MatchString
(
rPath
[
len
(
r
.
Base
)
:
])
{
if
!
r
.
MatchString
(
rPath
[
start
:
])
{
return
false
}
to
:=
r
.
To
// check variables
for
_
,
v
:=
range
regexpVars
{
if
strings
.
Contains
(
r
.
To
,
v
)
{
switch
v
{
case
"{path}"
:
to
=
strings
.
Replace
(
to
,
v
,
req
.
URL
.
Path
[
1
:
],
-
1
)
case
"{query}"
:
to
=
strings
.
Replace
(
to
,
v
,
req
.
URL
.
RawQuery
,
-
1
)
case
"{frag}"
:
to
=
strings
.
Replace
(
to
,
v
,
req
.
URL
.
Fragment
,
-
1
)
case
"{file}"
:
_
,
file
:=
path
.
Split
(
req
.
URL
.
Path
)
to
=
strings
.
Replace
(
to
,
v
,
file
,
-
1
)
case
"{dir}"
:
dir
,
_
:=
path
.
Split
(
req
.
URL
.
Path
)
to
=
path
.
Clean
(
strings
.
Replace
(
to
,
v
,
dir
,
-
1
))
}
}
}
// replace variables
to
:=
path
.
Clean
(
middleware
.
NewReplacer
(
req
,
nil
,
""
)
.
Replace
(
r
.
To
))
// validate resulting path
url
,
err
:=
url
.
Parse
(
to
)
...
...
middleware/rewrite/rewrite_test.go
View file @
104a5998
...
...
@@ -28,7 +28,7 @@ func TestRewrite(t *testing.T) {
[]
string
{
"/ab/"
,
"ab"
,
"/ab?type=html&{query}"
,
".html|"
},
[]
string
{
"/abc/"
,
"ab"
,
"/abc/{file}"
,
".html|"
},
[]
string
{
"/abcd/"
,
"ab"
,
"/a/{dir}/{file}"
,
".html|"
},
[]
string
{
"/abcde/"
,
"ab"
,
"/a#{frag}"
,
".html|"
},
[]
string
{
"/abcde/"
,
"ab"
,
"/a#{frag
ment
}"
,
".html|"
},
[]
string
{
"/ab/"
,
`.*\.jpg`
,
"/ajpg"
,
""
},
}
...
...
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