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
1183d91c
Commit
1183d91c
authored
Feb 15, 2017
by
Matt Holt
Committed by
GitHub
Feb 15, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1365 from tw4452852/1297
redirect: determine the FromScheme at runtime (#1297)
parents
463c9d9d
eeb23a24
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
20 additions
and
18 deletions
+20
-18
caddyhttp/redirect/redirect.go
caddyhttp/redirect/redirect.go
+6
-5
caddyhttp/redirect/redirect_test.go
caddyhttp/redirect/redirect_test.go
+9
-9
caddyhttp/redirect/setup.go
caddyhttp/redirect/setup.go
+5
-4
No files found.
caddyhttp/redirect/redirect.go
View file @
1183d91c
...
...
@@ -34,13 +34,14 @@ func (rd Redirect) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
}
func
schemeMatches
(
rule
Rule
,
req
*
http
.
Request
)
bool
{
return
(
rule
.
FromScheme
==
"https"
&&
req
.
TLS
!=
nil
)
||
(
rule
.
FromScheme
!=
"https"
&&
req
.
TLS
==
nil
)
return
(
rule
.
FromScheme
()
==
"https"
&&
req
.
TLS
!=
nil
)
||
(
rule
.
FromScheme
()
!=
"https"
&&
req
.
TLS
==
nil
)
}
// Rule describes an HTTP redirect rule.
type
Rule
struct
{
FromScheme
,
FromPath
,
To
string
FromScheme
func
()
string
FromPath
,
To
string
Code
int
Meta
bool
httpserver
.
RequestMatcher
...
...
caddyhttp/redirect/redirect_test.go
View file @
1183d91c
...
...
@@ -47,16 +47,16 @@ func TestRedirect(t *testing.T) {
return
0
,
nil
}),
Rules
:
[]
Rule
{
{
FromPath
:
"/from"
,
To
:
"/to"
,
Code
:
http
.
StatusMovedPermanently
,
RequestMatcher
:
httpserver
.
IfMatcher
{}},
{
FromPath
:
"/a"
,
To
:
"/b"
,
Code
:
http
.
StatusTemporaryRedirect
,
RequestMatcher
:
httpserver
.
IfMatcher
{}},
{
From
Scheme
:
func
()
string
{
return
"http"
},
From
Path
:
"/from"
,
To
:
"/to"
,
Code
:
http
.
StatusMovedPermanently
,
RequestMatcher
:
httpserver
.
IfMatcher
{}},
{
From
Scheme
:
func
()
string
{
return
"http"
},
From
Path
:
"/a"
,
To
:
"/b"
,
Code
:
http
.
StatusTemporaryRedirect
,
RequestMatcher
:
httpserver
.
IfMatcher
{}},
// These http and https schemes would never actually be mixed in the same
// redirect rule with Caddy because http and https schemes have different listeners,
// so they don't share a redirect rule. So although these tests prove something
// impossible with Caddy, it's extra bulletproofing at very little cost.
{
FromScheme
:
"http"
,
FromPath
:
"/scheme"
,
To
:
"https://localhost/scheme"
,
Code
:
http
.
StatusMovedPermanently
,
RequestMatcher
:
httpserver
.
IfMatcher
{}},
{
FromScheme
:
"https"
,
FromPath
:
"/scheme2"
,
To
:
"http://localhost/scheme2"
,
Code
:
http
.
StatusMovedPermanently
,
RequestMatcher
:
httpserver
.
IfMatcher
{}},
{
FromScheme
:
""
,
FromPath
:
"/scheme3"
,
To
:
"https://localhost/scheme3"
,
Code
:
http
.
StatusMovedPermanently
,
RequestMatcher
:
httpserver
.
IfMatcher
{}},
{
FromScheme
:
func
()
string
{
return
"http"
}
,
FromPath
:
"/scheme"
,
To
:
"https://localhost/scheme"
,
Code
:
http
.
StatusMovedPermanently
,
RequestMatcher
:
httpserver
.
IfMatcher
{}},
{
FromScheme
:
func
()
string
{
return
"https"
}
,
FromPath
:
"/scheme2"
,
To
:
"http://localhost/scheme2"
,
Code
:
http
.
StatusMovedPermanently
,
RequestMatcher
:
httpserver
.
IfMatcher
{}},
{
FromScheme
:
func
()
string
{
return
""
}
,
FromPath
:
"/scheme3"
,
To
:
"https://localhost/scheme3"
,
Code
:
http
.
StatusMovedPermanently
,
RequestMatcher
:
httpserver
.
IfMatcher
{}},
},
}
...
...
@@ -90,7 +90,7 @@ func TestRedirect(t *testing.T) {
func
TestParametersRedirect
(
t
*
testing
.
T
)
{
re
:=
Redirect
{
Rules
:
[]
Rule
{
{
FromPath
:
"/"
,
Meta
:
false
,
To
:
"http://example.com{uri}"
,
RequestMatcher
:
httpserver
.
IfMatcher
{}},
{
From
Scheme
:
func
()
string
{
return
"http"
},
From
Path
:
"/"
,
Meta
:
false
,
To
:
"http://example.com{uri}"
,
RequestMatcher
:
httpserver
.
IfMatcher
{}},
},
}
...
...
@@ -108,7 +108,7 @@ func TestParametersRedirect(t *testing.T) {
re
=
Redirect
{
Rules
:
[]
Rule
{
{
FromPath
:
"/"
,
Meta
:
false
,
To
:
"http://example.com/a{path}?b=c&{query}"
,
RequestMatcher
:
httpserver
.
IfMatcher
{}},
{
From
Scheme
:
func
()
string
{
return
"http"
},
From
Path
:
"/"
,
Meta
:
false
,
To
:
"http://example.com/a{path}?b=c&{query}"
,
RequestMatcher
:
httpserver
.
IfMatcher
{}},
},
}
...
...
@@ -127,8 +127,8 @@ func TestParametersRedirect(t *testing.T) {
func
TestMetaRedirect
(
t
*
testing
.
T
)
{
re
:=
Redirect
{
Rules
:
[]
Rule
{
{
FromPath
:
"/whatever"
,
Meta
:
true
,
To
:
"/something"
,
RequestMatcher
:
httpserver
.
IfMatcher
{}},
{
FromPath
:
"/"
,
Meta
:
true
,
To
:
"https://example.com/"
,
RequestMatcher
:
httpserver
.
IfMatcher
{}},
{
From
Scheme
:
func
()
string
{
return
"http"
},
From
Path
:
"/whatever"
,
Meta
:
true
,
To
:
"/something"
,
RequestMatcher
:
httpserver
.
IfMatcher
{}},
{
From
Scheme
:
func
()
string
{
return
"http"
},
From
Path
:
"/"
,
Meta
:
true
,
To
:
"https://example.com/"
,
RequestMatcher
:
httpserver
.
IfMatcher
{}},
},
}
...
...
caddyhttp/redirect/setup.go
View file @
1183d91c
...
...
@@ -34,10 +34,11 @@ func redirParse(c *caddy.Controller) ([]Rule, error) {
cfg
:=
httpserver
.
GetConfig
(
c
)
initRule
:=
func
(
rule
*
Rule
,
defaultCode
string
,
args
[]
string
)
error
{
rule
.
FromScheme
=
func
()
string
{
if
cfg
.
TLS
.
Enabled
{
rule
.
FromScheme
=
"https"
}
else
{
r
ule
.
FromScheme
=
"http"
return
"https"
}
r
eturn
"http"
}
var
(
...
...
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