From 052ddd961f6e2c5bb20b4ccec45f2377910790e6 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 15 Aug 2014 12:30:50 +0800 Subject: [PATCH] Implement --host-rules switch. --- .../browser/url_request_context_getter.cc | 32 +++++++++++++++++++ .../browser/url_request_context_getter.h | 2 ++ 2 files changed, 34 insertions(+) diff --git a/brightray/browser/url_request_context_getter.cc b/brightray/browser/url_request_context_getter.cc index 8c772c81dd92..6ca9297b415b 100644 --- a/brightray/browser/url_request_context_getter.cc +++ b/brightray/browser/url_request_context_getter.cc @@ -8,12 +8,14 @@ #include "browser/network_delegate.h" +#include "base/command_line.h" #include "base/strings/string_util.h" #include "base/threading/sequenced_worker_pool.h" #include "base/threading/worker_pool.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/cookie_store_factory.h" #include "content/public/common/url_constants.h" +#include "net/base/host_mapping_rules.h" #include "net/cert/cert_verifier.h" #include "net/cookies/cookie_monster.h" #include "net/http/http_auth_handler_factory.h" @@ -40,6 +42,27 @@ using content::BrowserThread; 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( const base::FilePath& base_path, base::MessageLoop* io_loop, @@ -73,6 +96,8 @@ net::HostResolver* URLRequestContextGetter::host_resolver() { net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); + const CommandLine& command_line = *CommandLine::ForCurrentProcess(); + if (!url_request_context_.get()) { url_request_context_.reset(new net::URLRequestContext()); network_delegate_ = network_delegate_factory_.Run().Pass(); @@ -142,6 +167,13 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { url_request_context_->http_server_properties(); 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|. storage_->set_host_resolver(host_resolver.Pass()); network_session_params.host_resolver = diff --git a/brightray/browser/url_request_context_getter.h b/brightray/browser/url_request_context_getter.h index 716b7e00b75e..e179f4e15ec7 100644 --- a/brightray/browser/url_request_context_getter.h +++ b/brightray/browser/url_request_context_getter.h @@ -16,6 +16,7 @@ class MessageLoop; } namespace net { +class HostMappingRules; class HostResolver; class ProxyConfigService; class URLRequestContextStorage; @@ -59,6 +60,7 @@ class URLRequestContextGetter : public net::URLRequestContextGetter { scoped_ptr network_delegate_; scoped_ptr storage_; scoped_ptr url_request_context_; + scoped_ptr host_mapping_rules_; content::ProtocolHandlerMap protocol_handlers_; content::ProtocolHandlerScopedVector protocol_interceptors_;