RedirectScheme redirects based on X-Forwarded-Proto header
This commit is contained in:
parent
55ba4356f2
commit
ff17ac53df
3 changed files with 31 additions and 5 deletions
|
@ -13,8 +13,9 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
typeSchemeName = "RedirectScheme"
|
||||
uriPattern = `^(https?:\/\/)?(\[[\w:.]+\]|[\w\._-]+)?(:\d+)?(.*)$`
|
||||
typeSchemeName = "RedirectScheme"
|
||||
uriPattern = `^(https?:\/\/)?(\[[\w:.]+\]|[\w\._-]+)?(:\d+)?(.*)$`
|
||||
xForwardedProto = "X-Forwarded-Proto"
|
||||
)
|
||||
|
||||
// NewRedirectScheme creates a new RedirectScheme middleware.
|
||||
|
@ -63,7 +64,11 @@ func rawURLScheme(req *http.Request) string {
|
|||
scheme = schemeHTTPS
|
||||
}
|
||||
|
||||
if scheme == schemeHTTP && port == ":80" || scheme == schemeHTTPS && port == ":443" || port == "" {
|
||||
if value := req.Header.Get(xForwardedProto); value != "" {
|
||||
scheme = value
|
||||
}
|
||||
|
||||
if scheme == schemeHTTP && port == ":80" || scheme == schemeHTTPS && port == ":443" {
|
||||
port = ""
|
||||
}
|
||||
|
||||
|
|
|
@ -47,11 +47,22 @@ func TestRedirectSchemeHandler(t *testing.T) {
|
|||
},
|
||||
url: "http://foo",
|
||||
headers: map[string]string{
|
||||
"X-Forwarded-Proto": "https",
|
||||
"X-Forwarded-Proto": "http",
|
||||
},
|
||||
expectedURL: "https://foo",
|
||||
expectedStatus: http.StatusFound,
|
||||
},
|
||||
{
|
||||
desc: "HTTP to HTTPS, with X-Forwarded-Proto to HTTPS",
|
||||
config: dynamic.RedirectScheme{
|
||||
Scheme: "https",
|
||||
},
|
||||
url: "http://foo",
|
||||
headers: map[string]string{
|
||||
"X-Forwarded-Proto": "https",
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
desc: "HTTP with port to HTTPS without port",
|
||||
config: dynamic.RedirectScheme{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue