| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/proxy/proxy_resolver_js_bindings.h" | 5 #include "net/proxy/proxy_resolver_js_bindings.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "net/base/address_list.h" | 11 #include "net/base/address_list.h" |
| 12 #include "net/base/host_cache.h" | 12 #include "net/base/host_cache.h" |
| 13 #include "net/base/host_resolver.h" | 13 #include "net/base/host_resolver.h" |
| 14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 15 #include "net/base/net_log.h" | 15 #include "net/base/net_log.h" |
| 16 #include "net/base/net_util.h" | 16 #include "net/base/net_util.h" |
| 17 #include "net/base/sys_addrinfo.h" | |
| 18 #include "net/proxy/proxy_resolver_error_observer.h" | 17 #include "net/proxy/proxy_resolver_error_observer.h" |
| 19 #include "net/proxy/proxy_resolver_request_context.h" | 18 #include "net/proxy/proxy_resolver_request_context.h" |
| 20 #include "net/proxy/sync_host_resolver.h" | 19 #include "net/proxy/sync_host_resolver.h" |
| 21 | 20 |
| 22 namespace net { | 21 namespace net { |
| 23 | 22 |
| 24 namespace { | 23 namespace { |
| 25 | 24 |
| 26 // TTL for the per-request DNS cache. Applies to both successful and failed | 25 // TTL for the per-request DNS cache. Applies to both successful and failed |
| 27 // DNS resolutions. | 26 // DNS resolutions. |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 HostResolver::RequestInfo info(HostPortPair(host, 80)); | 199 HostResolver::RequestInfo info(HostPortPair(host, 80)); |
| 201 info.set_address_family(ADDRESS_FAMILY_IPV4); | 200 info.set_address_family(ADDRESS_FAMILY_IPV4); |
| 202 AddressList address_list; | 201 AddressList address_list; |
| 203 | 202 |
| 204 int result = DnsResolveHelper(info, &address_list); | 203 int result = DnsResolveHelper(info, &address_list); |
| 205 if (result != OK) | 204 if (result != OK) |
| 206 return false; | 205 return false; |
| 207 | 206 |
| 208 // There may be multiple results; we will just use the first one. | 207 // There may be multiple results; we will just use the first one. |
| 209 // This returns empty string on failure. | 208 // This returns empty string on failure. |
| 210 *first_ip_address = net::NetAddressToString(address_list.head()); | 209 *first_ip_address = address_list.front().ToStringWithoutPort(); |
| 211 if (first_ip_address->empty()) | 210 if (first_ip_address->empty()) |
| 212 return false; | 211 return false; |
| 213 | 212 |
| 214 return true; | 213 return true; |
| 215 } | 214 } |
| 216 | 215 |
| 217 bool DnsResolveExImpl(const std::string& host, | 216 bool DnsResolveExImpl(const std::string& host, |
| 218 std::string* ip_address_list) { | 217 std::string* ip_address_list) { |
| 219 // Do a sync resolve of the hostname (port doesn't matter). | 218 // Do a sync resolve of the hostname (port doesn't matter). |
| 220 HostResolver::RequestInfo info(HostPortPair(host, 80)); | 219 HostResolver::RequestInfo info(HostPortPair(host, 80)); |
| 221 AddressList address_list; | 220 AddressList address_list; |
| 222 int result = DnsResolveHelper(info, &address_list); | 221 int result = DnsResolveHelper(info, &address_list); |
| 223 | 222 |
| 224 if (result != OK) | 223 if (result != OK) |
| 225 return false; | 224 return false; |
| 226 | 225 |
| 227 // Stringify all of the addresses in the address list, separated | 226 // Stringify all of the addresses in the address list, separated |
| 228 // by semicolons. | 227 // by semicolons. |
| 229 std::string address_list_str; | 228 std::string address_list_str; |
| 230 const struct addrinfo* current_address = address_list.head(); | 229 for (AddressList::const_iterator iter = address_list.begin(); |
| 231 while (current_address) { | 230 iter != address_list.end(); ++iter) { |
| 232 if (!address_list_str.empty()) | 231 if (!address_list_str.empty()) |
| 233 address_list_str += ";"; | 232 address_list_str += ";"; |
| 234 const std::string address_string = NetAddressToString(current_address); | 233 const std::string address_string = iter->ToStringWithoutPort(); |
| 235 if (address_string.empty()) | 234 if (address_string.empty()) |
| 236 return false; | 235 return false; |
| 237 address_list_str += address_string; | 236 address_list_str += address_string; |
| 238 current_address = current_address->ai_next; | |
| 239 } | 237 } |
| 240 | 238 |
| 241 *ip_address_list = address_list_str; | 239 *ip_address_list = address_list_str; |
| 242 return true; | 240 return true; |
| 243 } | 241 } |
| 244 | 242 |
| 245 // Helper to execute a synchronous DNS resolve, using the per-request | 243 // Helper to execute a synchronous DNS resolve, using the per-request |
| 246 // DNS cache if there is one. | 244 // DNS cache if there is one. |
| 247 int DnsResolveHelper(const HostResolver::RequestInfo& info, | 245 int DnsResolveHelper(const HostResolver::RequestInfo& info, |
| 248 AddressList* address_list) { | 246 AddressList* address_list) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 | 316 |
| 319 // static | 317 // static |
| 320 ProxyResolverJSBindings* ProxyResolverJSBindings::CreateDefault( | 318 ProxyResolverJSBindings* ProxyResolverJSBindings::CreateDefault( |
| 321 SyncHostResolver* host_resolver, | 319 SyncHostResolver* host_resolver, |
| 322 NetLog* net_log, | 320 NetLog* net_log, |
| 323 ProxyResolverErrorObserver* error_observer) { | 321 ProxyResolverErrorObserver* error_observer) { |
| 324 return new DefaultJSBindings(host_resolver, net_log, error_observer); | 322 return new DefaultJSBindings(host_resolver, net_log, error_observer); |
| 325 } | 323 } |
| 326 | 324 |
| 327 } // namespace net | 325 } // namespace net |
| OLD | NEW |