Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1490 Add support for X-Forwarded-Port header. #1491

Merged
merged 3 commits into from
May 18, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions source/vibe/http/server.d
Original file line number Diff line number Diff line change
Expand Up @@ -707,28 +707,47 @@ 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];
}

if (this.tls) {
url.schema = "https";
if (m_port != 443) url.port = m_port;
} else {
url.schema = "http";
if (m_port != 80) url.port = m_port;
// Set URL schema segment.
if (auto xfp = this.headers.get("X-Forwarded-Proto")) {
url.schema = xfp;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd argue this should set the port to the default port for that schema for cases where X-Forwarded-Port isn't set.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting no port (defaut of 0) is considered to be implicitly equal to setting the default port. Are you thinking of something in particular that is broken?

} else if (this.tls) {
url.schema = "https";
} else {
url.schema = "http";
}

// Set URL port segment.
if (auto xfp = this.headers.get("X-Forwarded-Port")) {
try {
url.port = xfp.to!ushort;
} catch (ConvException) {
// TODO : Consider responding with a 400/etc. error from here.
logWarn("X-Forwarded-Port header was not valid port (%s)", xfp);
}
} else if (url.schema == "https") {
if (m_port != 443U) url.port = m_port;
} else {
if (m_port != 80U) url.port = m_port;
}

url.host = url.host.split(":")[0];
url.username = this.username;
url.password = this.password;
url.path = Path(path);
url.queryString = queryString;

return url;
}

Expand Down