Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. | 3 // LICENSE file. |
| 4 | 4 |
| 5 #include "build/build_config.h" | |
| 6 | |
| 7 #if defined(OS_WIN) | |
| 8 #include <ws2tcpip.h> | |
| 9 #else | |
| 10 #include <netdb.h> | |
| 11 #endif | |
| 12 | |
| 5 #include "net/proxy/proxy_resolver_js_bindings.h" | 13 #include "net/proxy/proxy_resolver_js_bindings.h" |
| 6 | 14 |
| 7 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 8 #include "base/logging.h" | 16 #include "base/logging.h" |
| 9 #include "base/message_loop.h" | 17 #include "base/message_loop.h" |
| 10 #include "base/waitable_event.h" | 18 #include "base/waitable_event.h" |
| 11 #include "net/base/address_list.h" | 19 #include "net/base/address_list.h" |
| 12 #include "net/base/host_resolver.h" | 20 #include "net/base/host_resolver.h" |
| 13 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 14 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 MessageLoop* host_resolver_loop) | 98 MessageLoop* host_resolver_loop) |
| 91 : host_resolver_(new SyncHostResolverBridge( | 99 : host_resolver_(new SyncHostResolverBridge( |
| 92 host_resolver, host_resolver_loop)) {} | 100 host_resolver, host_resolver_loop)) {} |
| 93 | 101 |
| 94 // Handler for "alert(message)". | 102 // Handler for "alert(message)". |
| 95 virtual void Alert(const std::string& message) { | 103 virtual void Alert(const std::string& message) { |
| 96 LOG(INFO) << "PAC-alert: " << message; | 104 LOG(INFO) << "PAC-alert: " << message; |
| 97 } | 105 } |
| 98 | 106 |
| 99 // Handler for "myIpAddress()". Returns empty string on failure. | 107 // Handler for "myIpAddress()". Returns empty string on failure. |
| 108 // TODO(eroman): Perhaps enumerate the interfaces directly, using | |
| 109 // getifaddrs(). | |
| 100 virtual std::string MyIpAddress() { | 110 virtual std::string MyIpAddress() { |
| 101 // DnsResolve("") returns "", so no need to check for failure. | 111 // DnsResolve("") returns "", so no need to check for failure. |
| 102 return DnsResolve(GetHostName()); | 112 return DnsResolve(GetHostName()); |
| 103 } | 113 } |
| 104 | 114 |
| 115 // Handler for "myIpAddressEx()". Returns empty string on failure. | |
| 116 virtual std::string MyIpAddressEx() { | |
| 117 return DnsResolveEx(GetHostName()); | |
| 118 } | |
| 119 | |
| 105 // Handler for "dnsResolve(host)". Returns empty string on failure. | 120 // Handler for "dnsResolve(host)". Returns empty string on failure. |
| 106 virtual std::string DnsResolve(const std::string& host) { | 121 virtual std::string DnsResolve(const std::string& host) { |
| 107 // TODO(eroman): Should this return our IP address, or fail, or | |
| 108 // simply be unspecified (works differently on windows and mac os x). | |
| 109 if (host.empty()) | |
| 110 return std::string(); | |
| 111 | |
| 112 // Do a sync resolve of the hostname. | 122 // Do a sync resolve of the hostname. |
| 113 // Disable IPv6 results. We do this because Internet Explorer does it -- | 123 // Disable IPv6 results. We do this because Internet Explorer does it -- |
|
wtc
2009/10/24 04:40:59
I studied the original specification of the PAC sc
| |
| 114 // consequently a lot of existing PAC scripts assume they will only get | 124 // consequently a lot of existing PAC scripts assume they will only get |
| 115 // IPv4 results, and will misbehave if they get an IPv6 result. | 125 // IPv4 results, and will misbehave if they get an IPv6 result. |
| 116 // See http://crbug.com/24641 for more details. | 126 // See http://crbug.com/24641 for more details. |
| 117 net::AddressList address_list; | 127 net::AddressList address_list; |
| 118 int result = host_resolver_->Resolve(host, | 128 int result = host_resolver_->Resolve(host, |
| 119 ADDRESS_FAMILY_IPV4, | 129 ADDRESS_FAMILY_IPV4, |
| 120 &address_list); | 130 &address_list); |
| 121 | 131 |
| 122 if (result != OK) | 132 if (result != OK) |
| 123 return std::string(); // Failed. | 133 return std::string(); // Failed. |
| 124 | 134 |
| 125 if (!address_list.head()) | 135 if (!address_list.head()) |
| 126 return std::string(); | 136 return std::string(); |
| 127 | 137 |
| 128 // There may be multiple results; we will just use the first one. | 138 // There may be multiple results; we will just use the first one. |
| 129 // This returns empty string on failure. | 139 // This returns empty string on failure. |
| 130 return net::NetAddressToString(address_list.head()); | 140 return net::NetAddressToString(address_list.head()); |
| 131 } | 141 } |
| 132 | 142 |
| 143 // Handler for "dnsResolveEx(host)". Returns empty string on failure. | |
| 144 virtual std::string DnsResolveEx(const std::string& host) { | |
| 145 // Do a sync resolve of the hostname. | |
| 146 net::AddressList address_list; | |
| 147 int result = host_resolver_->Resolve(host, | |
| 148 ADDRESS_FAMILY_UNSPECIFIED, | |
| 149 &address_list); | |
| 150 | |
| 151 if (result != OK) | |
| 152 return std::string(); // Failed. | |
| 153 | |
| 154 // Stringify all of the addresses in the address list, separated | |
| 155 // by semicolons. | |
| 156 std::string address_list_str; | |
| 157 const struct addrinfo* current_address = address_list.head(); | |
| 158 while (current_address) { | |
| 159 if (!address_list_str.empty()) | |
| 160 address_list_str += ";"; | |
| 161 address_list_str += net::NetAddressToString(current_address); | |
| 162 current_address = current_address->ai_next; | |
| 163 } | |
| 164 | |
| 165 return address_list_str; | |
| 166 } | |
| 167 | |
| 133 // Handler for when an error is encountered. |line_number| may be -1. | 168 // Handler for when an error is encountered. |line_number| may be -1. |
| 134 virtual void OnError(int line_number, const std::string& message) { | 169 virtual void OnError(int line_number, const std::string& message) { |
| 135 if (line_number == -1) | 170 if (line_number == -1) |
| 136 LOG(INFO) << "PAC-error: " << message; | 171 LOG(INFO) << "PAC-error: " << message; |
| 137 else | 172 else |
| 138 LOG(INFO) << "PAC-error: " << "line: " << line_number << ": " << message; | 173 LOG(INFO) << "PAC-error: " << "line: " << line_number << ": " << message; |
| 139 } | 174 } |
| 140 | 175 |
| 141 private: | 176 private: |
| 142 scoped_refptr<SyncHostResolverBridge> host_resolver_; | 177 scoped_refptr<SyncHostResolverBridge> host_resolver_; |
| 143 }; | 178 }; |
| 144 | 179 |
| 145 } // namespace | 180 } // namespace |
| 146 | 181 |
| 147 // static | 182 // static |
| 148 ProxyResolverJSBindings* ProxyResolverJSBindings::CreateDefault( | 183 ProxyResolverJSBindings* ProxyResolverJSBindings::CreateDefault( |
| 149 HostResolver* host_resolver, MessageLoop* host_resolver_loop) { | 184 HostResolver* host_resolver, MessageLoop* host_resolver_loop) { |
| 150 return new DefaultJSBindings(host_resolver, host_resolver_loop); | 185 return new DefaultJSBindings(host_resolver, host_resolver_loop); |
| 151 } | 186 } |
| 152 | 187 |
| 153 } // namespace net | 188 } // namespace net |
| OLD | NEW |