Implement --host-rules switch.

This commit is contained in:
Cheng Zhao 2014-08-15 12:30:50 +08:00
parent 8aa422a3e8
commit 052ddd961f
2 changed files with 34 additions and 0 deletions

View file

@ -8,12 +8,14 @@
#include "browser/network_delegate.h" #include "browser/network_delegate.h"
#include "base/command_line.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/threading/sequenced_worker_pool.h" #include "base/threading/sequenced_worker_pool.h"
#include "base/threading/worker_pool.h" #include "base/threading/worker_pool.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/cookie_store_factory.h" #include "content/public/browser/cookie_store_factory.h"
#include "content/public/common/url_constants.h" #include "content/public/common/url_constants.h"
#include "net/base/host_mapping_rules.h"
#include "net/cert/cert_verifier.h" #include "net/cert/cert_verifier.h"
#include "net/cookies/cookie_monster.h" #include "net/cookies/cookie_monster.h"
#include "net/http/http_auth_handler_factory.h" #include "net/http/http_auth_handler_factory.h"
@ -40,6 +42,27 @@ using content::BrowserThread;
namespace brightray { namespace brightray {
namespace {
// Comma-separated list of rules that control how hostnames are mapped.
//
// For example:
// "MAP * 127.0.0.1" --> Forces all hostnames to be mapped to 127.0.0.1
// "MAP *.google.com proxy" --> Forces all google.com subdomains to be
// resolved to "proxy".
// "MAP test.com [::1]:77 --> Forces "test.com" to resolve to IPv6 loopback.
// Will also force the port of the resulting
// socket address to be 77.
// "MAP * baz, EXCLUDE www.google.com" --> Remaps everything to "baz",
// except for "www.google.com".
//
// These mappings apply to the endpoint host in a net::URLRequest (the TCP
// connect and host resolver in a direct connection, and the CONNECT in an http
// proxy connection, and the endpoint host in a SOCKS proxy connection).
const char kHostRules[] = "host-rules";
} // namespace
URLRequestContextGetter::URLRequestContextGetter( URLRequestContextGetter::URLRequestContextGetter(
const base::FilePath& base_path, const base::FilePath& base_path,
base::MessageLoop* io_loop, base::MessageLoop* io_loop,
@ -73,6 +96,8 @@ net::HostResolver* URLRequestContextGetter::host_resolver() {
net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
if (!url_request_context_.get()) { if (!url_request_context_.get()) {
url_request_context_.reset(new net::URLRequestContext()); url_request_context_.reset(new net::URLRequestContext());
network_delegate_ = network_delegate_factory_.Run().Pass(); network_delegate_ = network_delegate_factory_.Run().Pass();
@ -142,6 +167,13 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
url_request_context_->http_server_properties(); url_request_context_->http_server_properties();
network_session_params.ignore_certificate_errors = false; network_session_params.ignore_certificate_errors = false;
// --host-rules
if (command_line.HasSwitch(kHostRules)) {
host_mapping_rules_.reset(new net::HostMappingRules);
host_mapping_rules_->SetRulesFromString(command_line.GetSwitchValueASCII(kHostRules));
network_session_params.host_mapping_rules = host_mapping_rules_.get();
}
// Give |storage_| ownership at the end in case it's |mapped_host_resolver|. // Give |storage_| ownership at the end in case it's |mapped_host_resolver|.
storage_->set_host_resolver(host_resolver.Pass()); storage_->set_host_resolver(host_resolver.Pass());
network_session_params.host_resolver = network_session_params.host_resolver =

View file

@ -16,6 +16,7 @@ class MessageLoop;
} }
namespace net { namespace net {
class HostMappingRules;
class HostResolver; class HostResolver;
class ProxyConfigService; class ProxyConfigService;
class URLRequestContextStorage; class URLRequestContextStorage;
@ -59,6 +60,7 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
scoped_ptr<NetworkDelegate> network_delegate_; scoped_ptr<NetworkDelegate> network_delegate_;
scoped_ptr<net::URLRequestContextStorage> storage_; scoped_ptr<net::URLRequestContextStorage> storage_;
scoped_ptr<net::URLRequestContext> url_request_context_; scoped_ptr<net::URLRequestContext> url_request_context_;
scoped_ptr<net::HostMappingRules> host_mapping_rules_;
content::ProtocolHandlerMap protocol_handlers_; content::ProtocolHandlerMap protocol_handlers_;
content::ProtocolHandlerScopedVector protocol_interceptors_; content::ProtocolHandlerScopedVector protocol_interceptors_;