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
0a9a1930
Commit
0a9a1930
authored
Mar 29, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Made catch-all redirects possible
parent
4e9c432c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
26 deletions
+28
-26
middleware/redirect/redirect.go
middleware/redirect/redirect.go
+28
-26
No files found.
middleware/redirect/redirect.go
View file @
0a9a1930
...
@@ -4,47 +4,49 @@ package redirect
...
@@ -4,47 +4,49 @@ package redirect
import
(
import
(
"net/http"
"net/http"
"strings"
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware"
)
)
// New creates a new redirect middleware.
// New creates a new redirect middleware.
func
New
(
c
middleware
.
Controller
)
(
middleware
.
Middleware
,
error
)
{
func
New
(
c
middleware
.
Controller
)
(
middleware
.
Middleware
,
error
)
{
var
redirects
[]
r
edirect
var
redirects
[]
R
edirect
for
c
.
Next
()
{
for
c
.
Next
()
{
var
rule
redirect
var
rule
Redirect
args
:=
c
.
RemainingArgs
()
// From
if
len
(
args
)
==
1
{
if
!
c
.
NextArg
()
{
// Only 'To' specified
return
nil
,
c
.
ArgErr
()
rule
.
From
=
"/"
}
rule
.
To
=
c
.
Val
()
rule
.
From
=
c
.
Val
()
rule
.
Code
=
307
// TODO: Consider 301 instead?
redirects
=
append
(
redirects
,
rule
)
// To
}
else
if
len
(
args
)
==
3
{
if
!
c
.
NextArg
()
{
// From, To, and Code specified
return
nil
,
c
.
ArgErr
()
rule
.
From
=
args
[
0
]
}
rule
.
To
=
args
[
1
]
rule
.
To
=
c
.
Val
()
if
code
,
ok
:=
httpRedirs
[
args
[
2
]];
!
ok
{
return
nil
,
c
.
Err
(
"Invalid redirect code '"
+
c
.
Val
()
+
"'"
)
// Status Code
}
else
{
if
!
c
.
NextArg
()
{
rule
.
Code
=
code
return
nil
,
c
.
ArgErr
()
}
}
redirects
=
append
(
redirects
,
rule
)
if
code
,
ok
:=
httpRedirs
[
c
.
Val
()];
!
ok
{
return
nil
,
c
.
Err
(
"Invalid redirect code '"
+
c
.
Val
()
+
"'"
)
}
else
{
}
else
{
r
ule
.
Code
=
code
r
eturn
nil
,
c
.
ArgErr
()
}
}
redirects
=
append
(
redirects
,
rule
)
}
}
return
func
(
next
middleware
.
HandlerFunc
)
middleware
.
HandlerFunc
{
return
func
(
next
middleware
.
HandlerFunc
)
middleware
.
HandlerFunc
{
return
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
return
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
for
_
,
rule
:=
range
redirects
{
for
_
,
rule
:=
range
redirects
{
if
r
.
URL
.
Path
==
rule
.
From
{
if
middleware
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
rule
.
From
)
{
if
rule
.
From
==
"/"
{
// Catchall redirect preserves path (TODO: This should be made more consistent...)
http
.
Redirect
(
w
,
r
,
strings
.
TrimSuffix
(
rule
.
To
,
"/"
)
+
r
.
URL
.
Path
,
rule
.
Code
)
return
0
,
nil
}
http
.
Redirect
(
w
,
r
,
rule
.
To
,
rule
.
Code
)
http
.
Redirect
(
w
,
r
,
rule
.
To
,
rule
.
Code
)
return
0
,
nil
return
0
,
nil
}
}
...
@@ -55,7 +57,7 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
...
@@ -55,7 +57,7 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
}
}
// redirect describes an HTTP redirect rule.
// redirect describes an HTTP redirect rule.
type
r
edirect
struct
{
type
R
edirect
struct
{
From
string
From
string
To
string
To
string
Code
int
Code
int
...
...
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