#hyper #proxy #tokio #ssl

hyper-proxy

A proxy connector for Hyper-based applications

5 releases (3 breaking)

0.4.1 Mar 23, 2018
0.4.0 Dec 21, 2017
0.3.0 Dec 19, 2017
0.2.0 Dec 15, 2017
0.1.0 Dec 14, 2017

#15 in HTTP client

Download history 155/week @ 2018-05-06 220/week @ 2018-05-13 283/week @ 2018-05-20 318/week @ 2018-05-27 293/week @ 2018-06-03 197/week @ 2018-06-10 256/week @ 2018-06-17 212/week @ 2018-06-24 259/week @ 2018-07-01 229/week @ 2018-07-08 188/week @ 2018-07-15 240/week @ 2018-07-22 236/week @ 2018-07-29

350 downloads per month

hyper-proxy

Travis Build Status MIT licensed crates.io

A proxy connector for hyper based applications.

Documentation

Example

extern crate hyper;
extern crate hyper_proxy;
extern crate futures;
extern crate tokio_core;

use hyper::{Chunk, Client, Request, Method, Uri};
use hyper::client::HttpConnector;
use hyper::header::Basic;
use futures::{Future, Stream};
use hyper_proxy::{Proxy, ProxyConnector, Intercept};
use tokio_core::reactor::Core;

fn main() {
    let mut core = Core::new().unwrap();
    let handle = core.handle();

    let proxy = {
        let proxy_uri = "http://my-proxy:8080".parse().unwrap();
        let mut proxy = Proxy::new(Intercept::All, proxy_uri);
        proxy.set_authorization(Basic {
                                   username: "John Doe".into(),
                                   password: Some("Agent1234".into()),
                               });
        let connector = HttpConnector::new(4, &handle);
        let proxy_connector = ProxyConnector::from_proxy(connector, proxy).unwrap();
        proxy_connector
    };

    // Connecting to http will trigger regular GETs and POSTs.
    // We need to manually append the relevant headers to the request
    let uri: Uri = "http://my-remote-website.com".parse().unwrap();
    let mut req = Request::new(Method::Get, uri.clone());
    if let Some(headers) = proxy.http_headers(&uri) {
        req.headers_mut().extend(headers.iter());
        req.set_proxy(true);
    }
    let client = Client::configure().connector(proxy).build(&handle);
    let fut_http = client.request(req)
        .and_then(|res| res.body().concat2())
        .map(move |body: Chunk| ::std::str::from_utf8(&body).unwrap().to_string());

    // Connecting to an https uri is straightforward (uses 'CONNECT' method underneath)
    let uri = "https://my-remote-websitei-secured.com".parse().unwrap();
    let fut_https = client
        .get(uri)
        .and_then(|res| res.body().concat2())
        .map(move |body: Chunk| ::std::str::from_utf8(&body).unwrap().to_string());

    let futs = fut_http.join(fut_https);

    let (_http_res, _https_res) = core.run(futs).unwrap();
}

Credits

Large part of the code comes from reqwest. The core part as just been extracted and slightly enhanced.

Main changes are:

  • support for authentication
  • add non secured tunneling
  • add the possibility to add additional headers when connecting to the proxy
MIT license

Dependencies

Reverse deps