summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
Diffstat (limited to 'net')
-rw-r--r--net/base/net_util.cc21
-rw-r--r--net/base/net_util.h3
-rw-r--r--net/base/net_util_unittest.cc8
-rw-r--r--net/proxy/proxy_resolver_v8.cc12
-rw-r--r--net/proxy/proxy_resolver_v8_unittest.cc15
5 files changed, 52 insertions, 7 deletions
diff --git a/net/base/net_util.cc b/net/base/net_util.cc
index 7527b6c..b88a3bf 100644
--- a/net/base/net_util.cc
+++ b/net/base/net_util.cc
@@ -991,7 +991,7 @@ bool GetHostAndPort(std::string::const_iterator host_and_port_begin,
if (!hostname_component.is_nonempty())
return false; // Failed parsing.
-
+
int parsed_port_number = -1;
if (port_component.is_nonempty()) {
parsed_port_number = url_parse::ParsePort(auth_begin, port_component);
@@ -1008,7 +1008,7 @@ bool GetHostAndPort(std::string::const_iterator host_and_port_begin,
// Pass results back to caller.
host->assign(auth_begin + hostname_component.begin, hostname_component.len);
*port = parsed_port_number;
-
+
return true; // Success.
}
@@ -1031,9 +1031,24 @@ std::string NetAddressToString(const struct addrinfo* net_address) {
if (result != 0) {
DLOG(INFO) << "getnameinfo() failed with " << result;
- return std::string();
+ buffer[0] = '\0';
}
+ return std::string(buffer);
+}
+std::string GetMyHostName() {
+#if defined(OS_WIN)
+ EnsureWinsockInit();
+#endif
+
+ // Maximum size of 256 is somewhat arbitrary. Mozilla uses a size of 100
+ // so this should cover the majority of cases.
+ char buffer[256];
+ int result = gethostname(buffer, sizeof(buffer));
+ if (result != 0) {
+ DLOG(INFO) << "gethostname() failed with " << result;
+ buffer[0] = '\0';
+ }
return std::string(buffer);
}
diff --git a/net/base/net_util.h b/net/base/net_util.h
index 6f3c1ff..014a83d 100644
--- a/net/base/net_util.h
+++ b/net/base/net_util.h
@@ -58,6 +58,9 @@ bool GetHostAndPort(const std::string& host_and_port,
// Returns empty string on failure.
std::string NetAddressToString(const struct addrinfo* net_address);
+// Returns the hostname of the current system. Returns empty string on failure.
+std::string GetMyHostName();
+
// Return the value of the HTTP response header with name 'name'. 'headers'
// should be in the format that URLRequest::GetResponseHeaders() returns.
// Returns the empty string if the header is not found.
diff --git a/net/base/net_util_unittest.cc b/net/base/net_util_unittest.cc
index 2293368..c4083dc 100644
--- a/net/base/net_util_unittest.cc
+++ b/net/base/net_util_unittest.cc
@@ -869,3 +869,11 @@ TEST(NetUtilTest, DISABLED_NetAddressToString_IPv6) {
EXPECT_EQ(std::string(tests[i].result), result);
}
}
+
+TEST(NetUtilTest, GetMyHostName) {
+ // We can't check the result of GetMyHostName() directly, since the result
+ // will differ across machines. Our goal here is to simply exercise the
+ // code path, and check that things "look about right".
+ std::string my_hostname = net::GetMyHostName();
+ EXPECT_FALSE(my_hostname.empty());
+}
diff --git a/net/proxy/proxy_resolver_v8.cc b/net/proxy/proxy_resolver_v8.cc
index 81c1138..ef4ffd8 100644
--- a/net/proxy/proxy_resolver_v8.cc
+++ b/net/proxy/proxy_resolver_v8.cc
@@ -60,13 +60,17 @@ class DefaultJSBindings : public ProxyResolverV8::JSBindings {
// Handler for "myIpAddress()". Returns empty string on failure.
virtual std::string MyIpAddress() {
- // TODO(eroman):
- NOTIMPLEMENTED();
- return "127.0.0.1";
+ // DnsResolve("") returns "", so no need to check for failure.
+ return DnsResolve(GetMyHostName());
}
// Handler for "dnsResolve(host)". Returns empty string on failure.
virtual std::string DnsResolve(const std::string& host) {
+ // TODO(eroman): Should this return our IP address, or fail, or
+ // simply be unspecified (works differently on windows and mac os x).
+ if (host.empty())
+ return std::string();
+
// Try to resolve synchronously.
net::AddressList address_list;
const int kPort = 80; // Doesn't matter what this is.
@@ -236,6 +240,8 @@ class ProxyResolverV8::Context {
// We shouldn't be called with any arguments, but will not complain if
// we are.
std::string result = context->js_bindings_->MyIpAddress();
+ if (result.empty())
+ result = "127.0.0.1";
return StdStringToV8String(result);
}
diff --git a/net/proxy/proxy_resolver_v8_unittest.cc b/net/proxy/proxy_resolver_v8_unittest.cc
index c2ddb9f..5cbdfd4 100644
--- a/net/proxy/proxy_resolver_v8_unittest.cc
+++ b/net/proxy/proxy_resolver_v8_unittest.cc
@@ -380,12 +380,14 @@ TEST(ProxyResolverV8DefaultBindingsTest, DnsResolve) {
net::ProxyResolverV8 resolver;
net::ProxyResolverV8::JSBindings* bindings = resolver.js_bindings();
+ // Considered an error.
+ EXPECT_EQ("", bindings->DnsResolve(""));
+
const struct {
const char* input;
const char* expected;
} tests[] = {
{"www.google.com", "127.0.0.1"},
- {"", ""},
{".", ""},
{"foo@google.com", ""},
{"@google.com", ""},
@@ -424,3 +426,14 @@ TEST(ProxyResolverV8DefaultBindingsTest, DnsResolve) {
}
}
+TEST(ProxyResolverV8DefaultBindingsTest, MyIpAddress) {
+ // Get a hold of a DefaultJSBindings* (it is a hidden impl class).
+ net::ProxyResolverV8 resolver;
+ net::ProxyResolverV8::JSBindings* bindings = resolver.js_bindings();
+
+ // Our ip address is always going to be 127.0.0.1, since we are using a
+ // mock host mapper when running in unit-test mode.
+ std::string my_ip_address = bindings->MyIpAddress();
+
+ EXPECT_EQ("127.0.0.1", my_ip_address);
+}