Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Proxy support #1960

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ main_extern = [
"$rust_build:getopts",
"$rust_build:http",
"$rust_build:hyper",
"$rust_build:hyper-proxy",
"$rust_build:hyper_rustls",
"$rust_build:lazy_static",
"$rust_build:libc",
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ futures = "0.1.25"
getopts = "0.2.18"
http = "0.1.16"
hyper = "0.12.24"
hyper-proxy = "0.5.0"
hyper-rustls = "0.16.0"
integer-atomics = "1.0.2"
lazy_static = "1.3.0"
Expand Down
27 changes: 22 additions & 5 deletions src/http_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,30 @@ use crate::tokio_util;
use futures::future::{loop_fn, Loop};
use futures::{future, Future, Stream};
use hyper;
use hyper_proxy;
use hyper::client::{Client, HttpConnector};
use hyper::header::CONTENT_TYPE;
use hyper::Uri;
use hyper_proxy::{Intercept, Proxy, ProxyConnector};
use hyper_rustls;

type Connector = hyper_rustls::HttpsConnector<HttpConnector>;
type ProxConnector = hyper_proxy::ProxyConnector<Connector>;

lazy_static! {
static ref CONNECTOR: Connector = {
let num_dns_threads = 4;
Connector::new(num_dns_threads)
Connector::new(4)
};
}

pub fn get_proxy_client() -> Client<ProxConnector, hyper::Body> {
let c = CONNECTOR.clone();
let proxy_uri = Uri::from_static(env!("HTTP_PROXY"));
let mut proxy = Proxy::new(Intercept::All, proxy_uri);
let proxy_connector = ProxyConnector::from_proxy(c, proxy).unwrap();
Client::builder().build(proxy_connector)
}

pub fn get_client() -> Client<Connector, hyper::Body> {
// TODO use Hyper's connection pool.
let c = CONNECTOR.clone();
Expand Down Expand Up @@ -57,7 +67,12 @@ fn resolve_uri_from_location(base_uri: &Uri, location: &str) -> Uri {
// synchronous response, this utility method supports that.
pub fn fetch_sync_string(module_name: &str) -> DenoResult<(String, String)> {
let url = module_name.parse::<Uri>().unwrap();
let client = get_client();
let client;
if Some(env()["HTTP_PROXY"]) {
client = get_proxy_client();
}else{
client = get_client();
}
// TODO(kevinkassimo): consider set a max redirection counter
// to avoid bouncing between 2 or more urls
let fetch_future = loop_fn((client, url), |(client, url)| {
Expand Down Expand Up @@ -85,7 +100,8 @@ pub fn fetch_sync_string(module_name: &str) -> DenoResult<(String, String)> {
}
Ok(Loop::Break(response))
})
}).and_then(|response| {
})
.and_then(|response| {
let content_type = response
.headers()
.get(CONTENT_TYPE)
Expand All @@ -96,7 +112,8 @@ pub fn fetch_sync_string(module_name: &str) -> DenoResult<(String, String)> {
.map(|body| String::from_utf8(body.to_vec()).unwrap())
.map_err(DenoError::from);
body.join(future::ok(content_type))
}).and_then(|(body_string, maybe_content_type)| {
})
.and_then(|(body_string, maybe_content_type)| {
future::ok((body_string, maybe_content_type.unwrap()))
});

Expand Down