From c73a323c2a3aa95af67fb1f5204ed86b5b326993 Mon Sep 17 00:00:00 2001 From: Mihail-K Date: Thu, 12 May 2016 15:31:03 -0400 Subject: [PATCH] #1490 Add support for X-Forwarded-Port header. --- source/vibe/http/server.d | 47 +++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/source/vibe/http/server.d b/source/vibe/http/server.d index f71e496800..75c06680ba 100644 --- a/source/vibe/http/server.d +++ b/source/vibe/http/server.d @@ -707,28 +707,51 @@ final class HTTPServerRequest : HTTPRequest { @property URL fullURL() const { URL url; - auto fh = this.headers.get("X-Forwarded-Host", ""); - if (!fh.empty) { - url.schema = this.headers.get("X-Forwarded-Proto", "http"); - url.host = fh; + + // Set URL host segment. + if (auto xfh = this.headers.get("X-Forwarded-Host")) { + url.host = xfh; + } else if (!this.host.empty) { + url.host = this.host; + } else if (!m_settings.hostName.empty) { + url.host = m_settings.hostName; } else { - if (!this.host.empty) url.host = this.host; - else if (!m_settings.hostName.empty) url.host = m_settings.hostName; - else url.host = m_settings.bindAddresses[0]; + url.host = m_settings.bindAddresses[0]; + } + + // Set URL schema segment. + if (auto xfp = this.headers.get("X-Forwarded-Proto")) { + url.schema = xfp; + } else if (this.tls) { + url.schema = "https"; + } else { + url.schema = "http"; + } - if (this.tls) { - url.schema = "https"; - if (m_port != 443) url.port = m_port; + // Set URL port segment. + Nullable!ushort port; + if (auto xfp = this.headers.get("X-Forwarded-Port")) { + try { + port = xfp.to!ushort; + } catch (ConvException) { + // Value was not ushort. + } + } + if (port.isNull) { + if (url.schema == "https") { + url.port = 443U; } else { - url.schema = "http"; - if (m_port != 80) url.port = m_port; + url.port = 80U; } } + url.host = url.host.split(":")[0]; + url.port = port.get; url.username = this.username; url.password = this.password; url.path = Path(path); url.queryString = queryString; + return url; }