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
a93db401
Commit
a93db401
authored
Apr 12, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improvements to the redirect middleware
parent
b7c8afab
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
13 deletions
+22
-13
middleware/redirect/redirect.go
middleware/redirect/redirect.go
+22
-13
No files found.
middleware/redirect/redirect.go
View file @
a93db401
...
...
@@ -30,12 +30,12 @@ type Redirect struct {
// ServeHTTP implements the middleware.Handler interface.
func
(
rd
Redirect
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
for
_
,
rule
:=
range
rd
.
Rules
{
if
r
.
URL
.
Path
==
rule
.
From
{
if
rule
.
From
==
"/"
{
// Catchall redirect preserves path (TODO: This behavior should be more standardized...
)
// Catchall redirect preserves path (TODO: Standardize/formalize this behavior
)
http
.
Redirect
(
w
,
r
,
strings
.
TrimSuffix
(
rule
.
To
,
"/"
)
+
r
.
URL
.
Path
,
rule
.
Code
)
return
0
,
nil
}
if
r
.
URL
.
Path
==
rule
.
From
{
http
.
Redirect
(
w
,
r
,
rule
.
To
,
rule
.
Code
)
return
0
,
nil
}
...
...
@@ -50,21 +50,31 @@ func parse(c middleware.Controller) ([]Rule, error) {
var
rule
Rule
args
:=
c
.
RemainingArgs
()
if
len
(
args
)
==
1
{
// Only 'To' specified
switch
len
(
args
)
{
case
1
:
// To specified
rule
.
From
=
"/"
rule
.
To
=
c
.
Val
()
rule
.
Code
=
307
// TODO: Consider 301 instead?
}
else
if
len
(
args
)
==
3
{
rule
.
To
=
args
[
0
]
rule
.
Code
=
http
.
StatusTemporaryRedirect
case
2
:
// To and Code specified
rule
.
From
=
"/"
rule
.
To
=
args
[
0
]
if
code
,
ok
:=
httpRedirs
[
args
[
1
]];
!
ok
{
return
redirects
,
c
.
Err
(
"Invalid redirect code '"
+
args
[
1
]
+
"'"
)
}
else
{
rule
.
Code
=
code
}
case
3
:
// From, To, and Code specified
rule
.
From
=
args
[
0
]
rule
.
To
=
args
[
1
]
if
code
,
ok
:=
httpRedirs
[
args
[
2
]];
!
ok
{
return
redirects
,
c
.
Err
(
"Invalid redirect code '"
+
c
.
Val
()
+
"'"
)
return
redirects
,
c
.
Err
(
"Invalid redirect code '"
+
args
[
2
]
+
"'"
)
}
else
{
rule
.
Code
=
code
}
}
else
{
default
:
return
redirects
,
c
.
ArgErr
()
}
...
...
@@ -92,7 +102,6 @@ var httpRedirs = map[string]int{
"303"
:
303
,
"304"
:
304
,
"305"
:
305
,
"306"
:
306
,
"307"
:
307
,
"308"
:
308
,
}
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