Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(418)

Unified Diff: net/base/net_util.cc

Issue 938093003: Always treat .localhost as loopback (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: move localhost rewriting up to HostResolverImpl Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/base/net_util.cc
diff --git a/net/base/net_util.cc b/net/base/net_util.cc
index 3b49dffb5c20d889003a09755276ddaa8e41542d..40c33b8c2d37695ce7bf4bbbb3c17a8f3f813ec2 100644
--- a/net/base/net_util.cc
+++ b/net/base/net_util.cc
@@ -144,6 +144,9 @@ static const int kAllowedFtpPorts[] = {
22, // ssh
};
+static const char kLocalhostTLD[] = ".localhost.";
+static const size_t kLocalhostTLDLength = strlen(kLocalhostTLD);
Ryan Sleevi 2015/02/27 03:21:10 DANGER: strlen is a function call, which violates
estark 2015/02/27 05:00:47 Done.
+
bool IPNumberPrefixCheck(const IPAddressNumber& ip_number,
const unsigned char* ip_prefix,
size_t prefix_length_in_bits) {
@@ -984,7 +987,8 @@ bool IsLocalhost(const std::string& host) {
if (host == "localhost" ||
host == "localhost.localdomain" ||
host == "localhost6" ||
- host == "localhost6.localdomain6")
+ host == "localhost6.localdomain6" ||
+ IsLocalhostTLD(host))
return true;
IPAddressNumber ip_number;
@@ -1014,6 +1018,24 @@ bool IsLocalhost(const std::string& host) {
return false;
}
+bool IsLocalhostTLD(const std::string& host) {
Ryan Sleevi 2015/02/27 03:21:10 You can avoid the double comparison if (host.empt
estark 2015/02/27 05:00:47 Done.
+ size_t host_length = host.length();
+
+ // First try to match against ".localhost", then against ".localhost."
+ // with the trailing dot.
+
+ if (host_length < kLocalhostTLDLength - 1)
+ return false;
+ if (host.compare(host_length - kLocalhostTLDLength + 1, std::string::npos,
+ kLocalhostTLD, 0, kLocalhostTLDLength - 1) == 0)
+ return true;
+
+ if (host_length < kLocalhostTLDLength)
+ return false;
+ return host.compare(host_length - kLocalhostTLDLength, std::string::npos,
+ kLocalhostTLD) == 0;
+}
+
NetworkInterface::NetworkInterface()
: type(NetworkChangeNotifier::CONNECTION_UNKNOWN), prefix_length(0) {
}

Powered by Google App Engine
This is Rietveld 408576698