Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Łukasz Nowak
caddy
Commits
0e039a18
Commit
0e039a18
authored
9 years ago
by
Abiola Ibrahim
Browse files
Options
Download
Email Patches
Plain Diff
Rewrite: Use middleware.Replacer.
Bug fix for regexps starting with '/'.
parent
10ab0378
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
33 deletions
+20
-33
middleware/replacer.go
middleware/replacer.go
+10
-0
middleware/rewrite/rewrite.go
middleware/rewrite/rewrite.go
+10
-33
No files found.
middleware/replacer.go
View file @
0e039a18
...
@@ -3,6 +3,7 @@ package middleware
...
@@ -3,6 +3,7 @@ package middleware
import
(
import
(
"net"
"net"
"net/http"
"net/http"
"path"
"strconv"
"strconv"
"strings"
"strings"
"time"
"time"
...
@@ -40,6 +41,7 @@ func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Repla
...
@@ -40,6 +41,7 @@ func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Repla
"{host}"
:
r
.
Host
,
"{host}"
:
r
.
Host
,
"{path}"
:
r
.
URL
.
Path
,
"{path}"
:
r
.
URL
.
Path
,
"{query}"
:
r
.
URL
.
RawQuery
,
"{query}"
:
r
.
URL
.
RawQuery
,
"{frag}"
:
r
.
URL
.
Fragment
,
"{fragment}"
:
r
.
URL
.
Fragment
,
"{fragment}"
:
r
.
URL
.
Fragment
,
"{proto}"
:
r
.
Proto
,
"{proto}"
:
r
.
Proto
,
"{remote}"
:
func
()
string
{
"{remote}"
:
func
()
string
{
...
@@ -63,6 +65,14 @@ func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Repla
...
@@ -63,6 +65,14 @@ func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Repla
"{when}"
:
func
()
string
{
"{when}"
:
func
()
string
{
return
time
.
Now
()
.
Format
(
timeFormat
)
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
,
emptyValue
:
emptyValue
,
}
}
...
...
This diff is collapsed.
Click to expand it.
middleware/rewrite/rewrite.go
View file @
0e039a18
...
@@ -3,9 +3,8 @@
...
@@ -3,9 +3,8 @@
package
rewrite
package
rewrite
import
(
import
(
"net/http"
"fmt"
"fmt"
"net/http"
"net/url"
"net/url"
"path"
"path"
"path/filepath"
"path/filepath"
...
@@ -96,15 +95,6 @@ func NewRegexpRule(base, pattern, to string, ext []string) (*RegexpRule, error)
...
@@ -96,15 +95,6 @@ func NewRegexpRule(base, pattern, to string, ext []string) (*RegexpRule, error)
},
nil
},
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.
// Rewrite rewrites the internal location of the current request.
func
(
r
*
RegexpRule
)
Rewrite
(
req
*
http
.
Request
)
bool
{
func
(
r
*
RegexpRule
)
Rewrite
(
req
*
http
.
Request
)
bool
{
rPath
:=
req
.
URL
.
Path
rPath
:=
req
.
URL
.
Path
...
@@ -119,32 +109,19 @@ func (r *RegexpRule) Rewrite(req *http.Request) bool {
...
@@ -119,32 +109,19 @@ func (r *RegexpRule) Rewrite(req *http.Request) bool {
return
false
return
false
}
}
// include trailing slash in regexp if present
start
:=
len
(
r
.
Base
)
if
strings
.
HasSuffix
(
r
.
Base
,
"/"
)
{
start
-=
1
}
// validate regexp
// validate regexp
if
!
r
.
MatchString
(
rPath
[
len
(
r
.
Base
)
:
])
{
if
!
r
.
MatchString
(
rPath
[
start
:
])
{
return
false
return
false
}
}
to
:=
r
.
To
// replace variables
to
:=
path
.
Clean
(
middleware
.
NewReplacer
(
req
,
nil
,
""
)
.
Replace
(
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
))
}
}
}
// validate resulting path
// validate resulting path
url
,
err
:=
url
.
Parse
(
to
)
url
,
err
:=
url
.
Parse
(
to
)
...
...
This diff is collapsed.
Click to expand it.
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