From d37d58a1b183181618b1e145ab109bf6d0366345 Mon Sep 17 00:00:00 2001 From: Jannis Date: Thu, 4 Apr 2019 13:47:38 +0200 Subject: ipfs-api: Add hyper_tls for added HTTPS support The `HttpsConnector` provided by hyper-tls supports both HTTP and HTTPS. Using this instead of the default `HttpConnector` allows to use the IpfsClient against HTTPS endpoints such as https://ipfs.infura.io:5001. To make creating an IpfsClient with an HTTPS URI possible, this commit adds a new constructor for `IpfsClient`: ```rust struct IpfsClient { ... pub fn new_from_uri(uri: &str) -> Result { .... } } ``` The existing `IpfsClient::new` constructor is updated to use this. Tested with the current `ipfs-cli` as well as changing `ipfs-cli` to use `IpfsClient::new_from_uri("https://ipfs.infura.io:5001")` instead of `IpfsClient::default()`. Both work. --- ipfs-api/Cargo.toml | 4 +++- ipfs-api/src/client.rs | 21 ++++++++++++++++----- ipfs-api/src/lib.rs | 2 ++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/ipfs-api/Cargo.toml b/ipfs-api/Cargo.toml index be56b84..a1fb349 100644 --- a/ipfs-api/Cargo.toml +++ b/ipfs-api/Cargo.toml @@ -14,7 +14,7 @@ license = "MIT OR Apache-2.0" travis-ci = { repository = "ferristseng/rust-ipfs-api" } [features] -default = ["hyper", "hyper-multipart-rfc7578"] +default = ["hyper", "hyper-multipart-rfc7578", "hyper-tls"] actix = ["actix-web", "actix-multipart-rfc7578"] [dependencies] @@ -25,6 +25,7 @@ failure = "0.1.2" futures = "0.1" http = "0.1" hyper = { version = "0.12", optional = true } +hyper-tls = { version = "0.3.2", optional = true } hyper-multipart-rfc7578 = { version = "0.3", optional = true } serde = "1.0" serde_derive = "1.0" @@ -41,5 +42,6 @@ multiaddr = "0.3.1" actix-multipart-rfc7578 = "0.1" actix-web = "0.7" hyper = "0.12" +hyper-tls = "0.3.2" tokio-timer = "0.2" tar = "0.4" diff --git a/ipfs-api/src/client.rs b/ipfs-api/src/client.rs index 69411ee..6c1e3bf 100644 --- a/ipfs-api/src/client.rs +++ b/ipfs-api/src/client.rs @@ -22,6 +22,8 @@ use http::StatusCode; use hyper::client::{Client, HttpConnector}; #[cfg(feature = "hyper")] use hyper_multipart::client::multipart; +#[cfg(feature = "hyper")] +use hyper_tls::HttpsConnector; use multiaddr::{AddrComponent, ToMultiaddr}; use read::{JsonLineDecoder, LineDecoder, StreamReader}; use request::{self, ApiRequest}; @@ -66,7 +68,7 @@ type Response = http::Response; pub struct IpfsClient { base: Uri, #[cfg(feature = "hyper")] - client: Client, + client: Client, hyper::Body>, } impl Default for IpfsClient { @@ -113,19 +115,28 @@ impl IpfsClient { /// #[inline] pub fn new(host: &str, port: u16) -> Result { - let base_path = IpfsClient::build_base_path(host, port)?; + Self::new_from_uri(format!("http://{}:{}", host, port).as_str()) + } + + /// Creates a new `IpfsClient` for any given URI. + #[inline] + pub fn new_from_uri(uri: &str) -> Result { + let base_path = IpfsClient::build_base_path(uri)?; Ok(IpfsClient { base: base_path, #[cfg(feature = "hyper")] - client: Client::builder().keep_alive(false).build_http(), + client: { + let connector = HttpsConnector::new(4).unwrap(); + Client::builder().keep_alive(false).build(connector) + }, }) } /// Builds the base url path for the Ipfs api. /// - fn build_base_path(host: &str, port: u16) -> Result { - format!("http://{}:{}/api/v0", host, port).parse() + fn build_base_path(uri: &str) -> Result { + format!("{}/api/v0", uri).parse() } /// Builds the url for an api call. diff --git a/ipfs-api/src/lib.rs b/ipfs-api/src/lib.rs index 22568d8..a977c67 100644 --- a/ipfs-api/src/lib.rs +++ b/ipfs-api/src/lib.rs @@ -182,6 +182,8 @@ extern crate actix_web; extern crate hyper; #[cfg(feature = "hyper")] extern crate hyper_multipart_rfc7578 as hyper_multipart; +#[cfg(feature = "hyper")] +extern crate hyper_tls; extern crate bytes; #[macro_use] -- cgit v1.2.3