Merge pull request #69 from brightray/host-rules
Add support for --host-rules and --host-resolver-rules
This commit is contained in:
commit
59398bc24b
2 changed files with 45 additions and 0 deletions
|
@ -8,14 +8,18 @@
|
|||
|
||||
#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/content_switches.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/dns/mapped_host_resolver.h"
|
||||
#include "net/http/http_auth_handler_factory.h"
|
||||
#include "net/http/http_cache.h"
|
||||
#include "net/http/http_server_properties_impl.h"
|
||||
|
@ -40,6 +44,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 +98,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();
|
||||
|
@ -95,6 +122,15 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
|
|||
scoped_ptr<net::HostResolver> host_resolver(
|
||||
net::HostResolver::CreateDefaultResolver(NULL));
|
||||
|
||||
// --host-resolver-rules
|
||||
if (command_line.HasSwitch(switches::kHostResolverRules)) {
|
||||
scoped_ptr<net::MappedHostResolver> remapped_resolver(
|
||||
new net::MappedHostResolver(host_resolver.Pass()));
|
||||
remapped_resolver->SetRulesFromString(
|
||||
command_line.GetSwitchValueASCII(switches::kHostResolverRules));
|
||||
host_resolver = remapped_resolver.PassAs<net::HostResolver>();
|
||||
}
|
||||
|
||||
net::DhcpProxyScriptFetcherFactory dhcp_factory;
|
||||
storage_->set_proxy_service(
|
||||
net::CreateProxyServiceUsingV8ProxyResolver(
|
||||
|
@ -142,6 +178,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 =
|
||||
|
|
|
@ -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<NetworkDelegate> network_delegate_;
|
||||
scoped_ptr<net::URLRequestContextStorage> storage_;
|
||||
scoped_ptr<net::URLRequestContext> url_request_context_;
|
||||
scoped_ptr<net::HostMappingRules> host_mapping_rules_;
|
||||
content::ProtocolHandlerMap protocol_handlers_;
|
||||
content::ProtocolHandlerScopedVector protocol_interceptors_;
|
||||
|
||||
|
|
Loading…
Reference in a new issue