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
fcf2622c
Commit
fcf2622c
authored
Jul 13, 2015
by
Matt Holt
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #187 from evermax/master
redir: Preserve query string on catch-all redirect (fixes #180)
parents
1a82943d
d9ebc539
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
1 deletion
+55
-1
middleware/redirect/redirect.go
middleware/redirect/redirect.go
+18
-1
middleware/redirect/redirect_test.go
middleware/redirect/redirect_test.go
+37
-0
No files found.
middleware/redirect/redirect.go
View file @
fcf2622c
...
...
@@ -6,6 +6,8 @@ import (
"fmt"
"html"
"net/http"
"net/url"
"path"
"strings"
"github.com/mholt/caddy/middleware"
...
...
@@ -22,7 +24,22 @@ func (rd Redirect) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
for
_
,
rule
:=
range
rd
.
Rules
{
if
rule
.
From
==
"/"
{
// Catchall redirect preserves path (TODO: Standardize/formalize this behavior)
newPath
:=
strings
.
TrimSuffix
(
rule
.
To
,
"/"
)
+
r
.
URL
.
Path
toURL
,
err
:=
url
.
Parse
(
rule
.
To
)
if
err
!=
nil
{
return
http
.
StatusInternalServerError
,
err
}
newPath
:=
path
.
Join
(
toURL
.
Host
,
toURL
.
Path
,
r
.
URL
.
Path
)
if
strings
.
HasSuffix
(
r
.
URL
.
Path
,
"/"
)
{
newPath
=
newPath
+
"/"
}
newPath
=
toURL
.
Scheme
+
"://"
+
newPath
parameters
:=
toURL
.
Query
()
for
k
,
v
:=
range
r
.
URL
.
Query
()
{
parameters
.
Set
(
k
,
v
[
0
])
}
if
len
(
parameters
)
>
0
{
newPath
=
newPath
+
"?"
+
parameters
.
Encode
()
}
if
rule
.
Meta
{
fmt
.
Fprintf
(
w
,
metaRedir
,
html
.
EscapeString
(
newPath
))
}
else
{
...
...
middleware/redirect/redirect_test.go
View file @
fcf2622c
...
...
@@ -39,6 +39,43 @@ func TestMetaRedirect(t *testing.T) {
}
}
func
TestParametersRedirect
(
t
*
testing
.
T
)
{
re
:=
Redirect
{
Rules
:
[]
Rule
{
{
From
:
"/"
,
Meta
:
false
,
To
:
"http://example.com/"
},
},
}
req
,
err
:=
http
.
NewRequest
(
"GET"
,
"/a?b=c"
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"Test: Could not create HTTP request: %v"
,
err
)
}
rec
:=
httptest
.
NewRecorder
()
re
.
ServeHTTP
(
rec
,
req
)
if
"http://example.com/a?b=c"
!=
rec
.
Header
()
.
Get
(
"Location"
)
{
t
.
Fatalf
(
"Test: expected location header %q but was %q"
,
"http://example.com/a?b=c"
,
rec
.
Header
()
.
Get
(
"Location"
))
}
re
=
Redirect
{
Rules
:
[]
Rule
{
{
From
:
"/"
,
Meta
:
false
,
To
:
"http://example.com/a?b=c"
},
},
}
req
,
err
=
http
.
NewRequest
(
"GET"
,
"/d?e=f"
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"Test: Could not create HTTP request: %v"
,
err
)
}
re
.
ServeHTTP
(
rec
,
req
)
if
"http://example.com/a/d?b=c&e=f"
!=
rec
.
Header
()
.
Get
(
"Location"
)
{
t
.
Fatalf
(
"Test: expected location header %q but was %q"
,
"http://example.com/a/d?b=c&e=f"
,
rec
.
Header
()
.
Get
(
"Location"
))
}
}
func
TestRedirect
(
t
*
testing
.
T
)
{
for
i
,
test
:=
range
[]
struct
{
from
string
...
...
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