Skip to content

Commit

Permalink
ctx: simplify Protocol() (#2217)
Browse files Browse the repository at this point in the history
* ctx: simplify Protocol()

* ctx: also mention "X-Url-Scheme" header in Protocol()

* ctx: use the same warning comment about enabling Config.EnableTrustedProxyCheck everywhere
  • Loading branch information
leonklingele committed Nov 14, 2022
1 parent b288a9f commit 235cd9d
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions ctx.go
Expand Up @@ -1040,28 +1040,25 @@ func (c *Ctx) Path(override ...string) string {
}

// Protocol contains the request protocol string: http or https for TLS requests.
// Use Config.EnableTrustedProxyCheck to prevent header spoofing, in case when your app is behind the proxy.
// Please use Config.EnableTrustedProxyCheck to prevent header spoofing, in case when your app is behind the proxy.
func (c *Ctx) Protocol() string {
if c.fasthttp.IsTLS() {
return "https"
}
scheme := "http"
if !c.IsProxyTrusted() {
return scheme
return "http"
}

scheme := "http"
c.fasthttp.Request.Header.VisitAll(func(key, val []byte) {
if len(key) < 12 {
return // X-Forwarded-
} else if bytes.HasPrefix(key, []byte("X-Forwarded-")) {
v := c.app.getString(val)
if bytes.Equal(key, []byte(HeaderXForwardedProto)) {
commaPos := strings.Index(v, ",")
if commaPos != -1 {
scheme = v[:commaPos]
} else {
scheme = v
}
} else if bytes.Equal(key, []byte(HeaderXForwardedProtocol)) {
return // Neither "X-Forwarded-" nor "X-Url-Scheme"
}
switch {
case bytes.HasPrefix(key, []byte("X-Forwarded-")):
if bytes.Equal(key, []byte(HeaderXForwardedProto)) ||
bytes.Equal(key, []byte(HeaderXForwardedProtocol)) {
v := c.app.getString(val)
commaPos := strings.Index(v, ",")
if commaPos != -1 {
scheme = v[:commaPos]
Expand All @@ -1071,7 +1068,8 @@ func (c *Ctx) Protocol() string {
} else if bytes.Equal(key, []byte(HeaderXForwardedSsl)) && bytes.Equal(val, []byte("on")) {
scheme = "https"
}
} else if bytes.Equal(key, []byte(HeaderXUrlScheme)) {

case bytes.Equal(key, []byte(HeaderXUrlScheme)):
scheme = c.app.getString(val)
}
})
Expand Down

0 comments on commit 235cd9d

Please sign in to comment.