diff options
-rw-r--r-- | ipfs-api/Cargo.toml | 4 | ||||
-rw-r--r-- | ipfs-api/src/client.rs | 21 | ||||
-rw-r--r-- | 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<hyper::Body>; pub struct IpfsClient { base: Uri, #[cfg(feature = "hyper")] - client: Client<HttpConnector, hyper::Body>, + client: Client<HttpsConnector<HttpConnector>, hyper::Body>, } impl Default for IpfsClient { @@ -113,19 +115,28 @@ impl IpfsClient { /// #[inline] pub fn new(host: &str, port: u16) -> Result<IpfsClient, InvalidUri> { - 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<IpfsClient, InvalidUri> { + 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<Uri, InvalidUri> { - format!("http://{}:{}/api/v0", host, port).parse() + fn build_base_path(uri: &str) -> Result<Uri, InvalidUri> { + 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] |