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
5cdfa0aa
Commit
5cdfa0aa
authored
Jun 10, 2015
by
Matt Holt
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #128 from coolaj86/meta-redirects
redirect: add ability to do meta redirects
parents
00093a20
d6a7dfc1
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
5 deletions
+61
-5
config/setup/redir.go
config/setup/redir.go
+9
-3
middleware/redirect/redirect.go
middleware/redirect/redirect.go
+21
-2
middleware/redirect/redirect_test.go
middleware/redirect/redirect_test.go
+31
-0
No files found.
config/setup/redir.go
View file @
5cdfa0aa
...
@@ -26,17 +26,21 @@ func redirParse(c *Controller) ([]redirect.Rule, error) {
...
@@ -26,17 +26,21 @@ func redirParse(c *Controller) ([]redirect.Rule, error) {
var
rule
redirect
.
Rule
var
rule
redirect
.
Rule
args
:=
c
.
RemainingArgs
()
args
:=
c
.
RemainingArgs
()
// Always set the default Code, then overwrite
rule
.
Code
=
http
.
StatusMovedPermanently
switch
len
(
args
)
{
switch
len
(
args
)
{
case
1
:
case
1
:
// To specified
// To specified
rule
.
From
=
"/"
rule
.
From
=
"/"
rule
.
To
=
args
[
0
]
rule
.
To
=
args
[
0
]
rule
.
Code
=
http
.
StatusMovedPermanently
case
2
:
case
2
:
// To and Code specified
// To and Code specified
rule
.
From
=
"/"
rule
.
From
=
"/"
rule
.
To
=
args
[
0
]
rule
.
To
=
args
[
0
]
if
code
,
ok
:=
httpRedirs
[
args
[
1
]];
!
ok
{
if
"meta"
==
args
[
1
]
{
rule
.
Meta
=
true
}
else
if
code
,
ok
:=
httpRedirs
[
args
[
1
]];
!
ok
{
return
redirects
,
c
.
Err
(
"Invalid redirect code '"
+
args
[
1
]
+
"'"
)
return
redirects
,
c
.
Err
(
"Invalid redirect code '"
+
args
[
1
]
+
"'"
)
}
else
{
}
else
{
rule
.
Code
=
code
rule
.
Code
=
code
...
@@ -45,7 +49,9 @@ func redirParse(c *Controller) ([]redirect.Rule, error) {
...
@@ -45,7 +49,9 @@ func redirParse(c *Controller) ([]redirect.Rule, error) {
// From, To, and Code specified
// From, To, and Code specified
rule
.
From
=
args
[
0
]
rule
.
From
=
args
[
0
]
rule
.
To
=
args
[
1
]
rule
.
To
=
args
[
1
]
if
code
,
ok
:=
httpRedirs
[
args
[
2
]];
!
ok
{
if
"meta"
==
args
[
2
]
{
rule
.
Meta
=
true
}
else
if
code
,
ok
:=
httpRedirs
[
args
[
2
]];
!
ok
{
return
redirects
,
c
.
Err
(
"Invalid redirect code '"
+
args
[
2
]
+
"'"
)
return
redirects
,
c
.
Err
(
"Invalid redirect code '"
+
args
[
2
]
+
"'"
)
}
else
{
}
else
{
rule
.
Code
=
code
rule
.
Code
=
code
...
...
middleware/redirect/redirect.go
View file @
5cdfa0aa
...
@@ -3,6 +3,8 @@
...
@@ -3,6 +3,8 @@
package
redirect
package
redirect
import
(
import
(
"fmt"
"html"
"net/http"
"net/http"
"strings"
"strings"
...
@@ -20,11 +22,20 @@ func (rd Redirect) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
...
@@ -20,11 +22,20 @@ func (rd Redirect) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
for
_
,
rule
:=
range
rd
.
Rules
{
for
_
,
rule
:=
range
rd
.
Rules
{
if
rule
.
From
==
"/"
{
if
rule
.
From
==
"/"
{
// Catchall redirect preserves path (TODO: Standardize/formalize this behavior)
// Catchall redirect preserves path (TODO: Standardize/formalize this behavior)
http
.
Redirect
(
w
,
r
,
strings
.
TrimSuffix
(
rule
.
To
,
"/"
)
+
r
.
URL
.
Path
,
rule
.
Code
)
newPath
:=
strings
.
TrimSuffix
(
rule
.
To
,
"/"
)
+
r
.
URL
.
Path
if
rule
.
Meta
{
fmt
.
Fprintf
(
w
,
metaRedir
,
html
.
EscapeString
(
newPath
))
}
else
{
http
.
Redirect
(
w
,
r
,
newPath
,
rule
.
Code
)
}
return
0
,
nil
return
0
,
nil
}
}
if
r
.
URL
.
Path
==
rule
.
From
{
if
r
.
URL
.
Path
==
rule
.
From
{
if
rule
.
Meta
{
fmt
.
Fprintf
(
w
,
metaRedir
,
html
.
EscapeString
(
rule
.
To
))
}
else
{
http
.
Redirect
(
w
,
r
,
rule
.
To
,
rule
.
Code
)
http
.
Redirect
(
w
,
r
,
rule
.
To
,
rule
.
Code
)
}
return
0
,
nil
return
0
,
nil
}
}
}
}
...
@@ -35,4 +46,12 @@ func (rd Redirect) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
...
@@ -35,4 +46,12 @@ func (rd Redirect) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
type
Rule
struct
{
type
Rule
struct
{
From
,
To
string
From
,
To
string
Code
int
Code
int
Meta
bool
}
}
var
metaRedir
=
`<html>
<head>
<meta http-equiv="refresh" content="0;URL='%s'">
</head>
<body>redirecting...</body>
</html>`
middleware/redirect/redirect_test.go
View file @
5cdfa0aa
package
redirect
package
redirect
import
(
import
(
"bytes"
"io/ioutil"
"net/http"
"net/http"
"net/http/httptest"
"net/http/httptest"
"testing"
"testing"
...
@@ -8,6 +10,35 @@ import (
...
@@ -8,6 +10,35 @@ import (
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware"
)
)
func
TestMetaRedirect
(
t
*
testing
.
T
)
{
re
:=
Redirect
{
Rules
:
[]
Rule
{
{
From
:
"/"
,
Meta
:
true
,
To
:
"https://example.com/"
},
{
From
:
"/whatever"
,
Meta
:
true
,
To
:
"https://example.com/whatever"
},
},
}
for
i
,
test
:=
range
re
.
Rules
{
req
,
err
:=
http
.
NewRequest
(
"GET"
,
test
.
From
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"Test %d: Could not create HTTP request: %v"
,
i
,
err
)
}
rec
:=
httptest
.
NewRecorder
()
re
.
ServeHTTP
(
rec
,
req
)
body
,
err
:=
ioutil
.
ReadAll
(
rec
.
Body
)
if
err
!=
nil
{
t
.
Fatalf
(
"Test %d: Could not read HTTP response body: %v"
,
i
,
err
)
}
expectedSnippet
:=
`<meta http-equiv="refresh" content="0;URL='`
+
test
.
To
+
`'">`
if
!
bytes
.
Contains
(
body
,
[]
byte
(
expectedSnippet
))
{
t
.
Errorf
(
"Test %d: Expected Response Body to contain %q but was %q"
,
i
,
expectedSnippet
,
body
)
}
}
}
func
TestRedirect
(
t
*
testing
.
T
)
{
func
TestRedirect
(
t
*
testing
.
T
)
{
for
i
,
test
:=
range
[]
struct
{
for
i
,
test
:=
range
[]
struct
{
from
string
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