mirror of https://github.com/gofiber/fiber.git
ctx: simplify Protocol() (#2217)
* ctx: simplify Protocol() * ctx: also mention "X-Url-Scheme" header in Protocol() * ctx: use the same warning comment about enabling Config.EnableTrustedProxyCheck everywherepull/2222/head
parent
b288a9f54e
commit
235cd9df82
28
ctx.go
28
ctx.go
|
@ -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]
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue