From cc0131c9c6bea175853c995768fc817b5b066be7 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Mon, 22 Feb 2021 20:42:21 -0500 Subject: add hyper backend --- ipfs-api-backend-hyper/Cargo.toml | 26 +++++++ ipfs-api-backend-hyper/src/backend.rs | 132 ++++++++++++++++++++++++++++++++++ ipfs-api-backend-hyper/src/error.rs | 43 +++++++++++ ipfs-api-backend-hyper/src/lib.rs | 14 ++++ 4 files changed, 215 insertions(+) create mode 100644 ipfs-api-backend-hyper/Cargo.toml create mode 100644 ipfs-api-backend-hyper/src/backend.rs create mode 100644 ipfs-api-backend-hyper/src/error.rs create mode 100644 ipfs-api-backend-hyper/src/lib.rs (limited to 'ipfs-api-backend-hyper') diff --git a/ipfs-api-backend-hyper/Cargo.toml b/ipfs-api-backend-hyper/Cargo.toml new file mode 100644 index 0000000..9ebd7d0 --- /dev/null +++ b/ipfs-api-backend-hyper/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "ipfs-api-backend-hyper" +description = "Hyper implementation of IPFS HTTP API" +authors = ["Ferris Tseng "] +edition = "2018" +documentation = "https://docs.rs/ipfs-api" +repository = "https://github.com/ferristseng/rust-ipfs-api" +keywords = ["ipfs"] +categories = ["filesystem", "web-programming"] +version = "0.1.0" +readme = "../README.md" +license = "MIT OR Apache-2.0" + +[dependencies] +async-trait = "0.1" +bytes = "1.0" +futures = "0.3" +http = "0.2" +hyper = { version = "0.14", features = ["http1", "http2", "client"] } +hyper-multipart-rfc7578 = "0.5" +hyper-tls = "0.5" +ipfs-api-prelude = { version = "0.1.0", path = "../ipfs-api-prelude" } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +serde_urlencoded = "0.7" +thiserror = "1.0" diff --git a/ipfs-api-backend-hyper/src/backend.rs b/ipfs-api-backend-hyper/src/backend.rs new file mode 100644 index 0000000..047fbb6 --- /dev/null +++ b/ipfs-api-backend-hyper/src/backend.rs @@ -0,0 +1,132 @@ +use crate::error::Error; +use async_trait::async_trait; +use bytes::Bytes; +use futures::{FutureExt, Stream, StreamExt, TryFutureExt, TryStreamExt}; +use http::{ + header::{HeaderName, HeaderValue}, + uri::Scheme, + StatusCode, Uri, +}; +use hyper::{ + body, + client::{self, Builder, HttpConnector}, +}; +use hyper_tls::HttpsConnector; +use ipfs_api_prelude::{ApiRequest, Backend, TryFromUri}; +use serde::Serialize; + +pub struct HyperBackend { + base: Uri, + client: client::Client, hyper::Body>, +} + +impl Default for HyperBackend { + fn default() -> Self { + Self::from_ipfs_config() + .unwrap_or_else(|| Self::from_host_and_port(Scheme::HTTP, "localhost", 5001).unwrap()) + } +} + +impl TryFromUri for HyperBackend { + fn build_with_base_uri(base: Uri) -> Self { + let client = Builder::default() + .pool_max_idle_per_host(0) + .build(HttpsConnector::new()); + + HyperBackend { base, client } + } +} + +#[async_trait(?Send)] +impl Backend for HyperBackend { + type HttpRequest = http::Request; + + type HttpResponse = http::Response; + + type MultipartForm = multipart::client::multipart::Form<'static>; + + type Error = Error; + + fn build_base_request( + &self, + req: &Req, + form: Option, + ) -> Result + where + Req: ApiRequest, + { + req.absolute_url(&self.base).and_then(move |url| { + let builder = http::Request::builder(); + let builder = builder.method(Req::METHOD.clone()).uri(url); + + let req = if let Some(form) = form { + form.set_body_convert::(builder) + } else { + builder.body(hyper::Body::empty()) + }; + + req.map_err(From::from) + }) + } + + fn get_header<'a>(res: &'a Self::HttpResponse, key: HeaderName) -> Option<&'a HeaderValue> { + res.headers().get(key) + } + + async fn request_raw( + &self, + req: Req, + form: Option, + ) -> Result<(StatusCode, Bytes), Self::Error> + where + Req: ApiRequest + Serialize, + { + let req = self.build_base_request(&req, form)?; + let res = self.client.request(req).await?; + let status = res.status(); + let body = body::to_bytes(res.into_body()).await?; + + Ok((status, body)) + } + + fn response_to_byte_stream( + res: Self::HttpResponse, + ) -> Box> + Unpin> { + Box::new(res.into_body().err_into()) + } + + fn request_stream( + &self, + req: Self::HttpRequest, + process: F, + ) -> Box> + Unpin> + where + OutStream: Stream> + Unpin, + F: 'static + Fn(Self::HttpResponse) -> OutStream, + { + let stream = self + .client + .request(req) + .err_into() + .map_ok(move |res| { + match res.status() { + StatusCode::OK => process(res).right_stream(), + // If the server responded with an error status code, the body + // still needs to be read so an error can be built. This block will + // read the entire body stream, then immediately return an error. + // + _ => body::to_bytes(res.into_body()) + .boxed() + .map(|maybe_body| match maybe_body { + Ok(body) => Err(Self::process_error_from_body(body)), + Err(e) => Err(e.into()), + }) + .into_stream() + .left_stream(), + } + }) + .try_flatten_stream(); + + Box::new(stream) + } +} diff --git a/ipfs-api-backend-hyper/src/error.rs b/ipfs-api-backend-hyper/src/error.rs new file mode 100644 index 0000000..9db1959 --- /dev/null +++ b/ipfs-api-backend-hyper/src/error.rs @@ -0,0 +1,43 @@ +// Copyright 2019 rust-ipfs-api Developers +// +// Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be +// copied, modified, or distributed except according to those terms. +// + +use std::string::FromUtf8Error; +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum Error { + #[error("api returned error `{0}`")] + Api(ipfs_api_prelude::ApiError), + + #[error("hyper client error `{0}`")] + Client(#[from] hyper::Error), + + #[error("http error `{0}`")] + Http(#[from] http::Error), + + #[error("json parse error `{0}`")] + Parse(#[from] serde_json::Error), + + #[error("utf8 decoding error `{0}`")] + ParseUtf8(#[from] FromUtf8Error), + + #[error("uri error `{0}`")] + Url(#[from] http::uri::InvalidUri), + + #[error("url encoding error `{0}`")] + EncodeUrl(#[from] serde_urlencoded::ser::Error), + + #[error("ipfs client error `{0}`")] + IpfsClientError(#[from] ipfs_api_prelude::Error), +} + +impl From for Error { + fn from(err: ipfs_api_prelude::ApiError) -> Self { + Error::Api(err) + } +} diff --git a/ipfs-api-backend-hyper/src/lib.rs b/ipfs-api-backend-hyper/src/lib.rs new file mode 100644 index 0000000..276d34a --- /dev/null +++ b/ipfs-api-backend-hyper/src/lib.rs @@ -0,0 +1,14 @@ +// Copyright 2019 rust-ipfs-api Developers +// +// Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be +// copied, modified, or distributed except according to those terms. +// + +extern crate hyper_multipart_rfc7578 as multipart; + +mod backend; +mod error; + +pub use crate::{backend::HyperBackend as IpfsApi, error::Error}; -- cgit v1.2.3 From 4ede307efb87bad2627190665279123e588a3494 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Mon, 22 Feb 2021 22:56:04 -0500 Subject: centralize errors in prelude as much as possible --- ipfs-api-backend-hyper/Cargo.toml | 4 +--- ipfs-api-backend-hyper/src/backend.rs | 24 ++++++++++++------------ ipfs-api-backend-hyper/src/error.rs | 13 ------------- 3 files changed, 13 insertions(+), 28 deletions(-) (limited to 'ipfs-api-backend-hyper') diff --git a/ipfs-api-backend-hyper/Cargo.toml b/ipfs-api-backend-hyper/Cargo.toml index 9ebd7d0..03ee234 100644 --- a/ipfs-api-backend-hyper/Cargo.toml +++ b/ipfs-api-backend-hyper/Cargo.toml @@ -20,7 +20,5 @@ hyper = { version = "0.14", features = ["http1", "http2", "c hyper-multipart-rfc7578 = "0.5" hyper-tls = "0.5" ipfs-api-prelude = { version = "0.1.0", path = "../ipfs-api-prelude" } -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" -serde_urlencoded = "0.7" +serde = "1.0" thiserror = "1.0" diff --git a/ipfs-api-backend-hyper/src/backend.rs b/ipfs-api-backend-hyper/src/backend.rs index 047fbb6..bbe9a56 100644 --- a/ipfs-api-backend-hyper/src/backend.rs +++ b/ipfs-api-backend-hyper/src/backend.rs @@ -55,18 +55,18 @@ impl Backend for HyperBackend { where Req: ApiRequest, { - req.absolute_url(&self.base).and_then(move |url| { - let builder = http::Request::builder(); - let builder = builder.method(Req::METHOD.clone()).uri(url); - - let req = if let Some(form) = form { - form.set_body_convert::(builder) - } else { - builder.body(hyper::Body::empty()) - }; - - req.map_err(From::from) - }) + let url = req.absolute_url(&self.base)?; + + let builder = http::Request::builder(); + let builder = builder.method(Req::METHOD.clone()).uri(url); + + let req = if let Some(form) = form { + form.set_body_convert::(builder) + } else { + builder.body(hyper::Body::empty()) + }?; + + Ok(req) } fn get_header<'a>(res: &'a Self::HttpResponse, key: HeaderName) -> Option<&'a HeaderValue> { diff --git a/ipfs-api-backend-hyper/src/error.rs b/ipfs-api-backend-hyper/src/error.rs index 9db1959..4760fc4 100644 --- a/ipfs-api-backend-hyper/src/error.rs +++ b/ipfs-api-backend-hyper/src/error.rs @@ -6,7 +6,6 @@ // copied, modified, or distributed except according to those terms. // -use std::string::FromUtf8Error; use thiserror::Error; #[derive(Debug, Error)] @@ -20,18 +19,6 @@ pub enum Error { #[error("http error `{0}`")] Http(#[from] http::Error), - #[error("json parse error `{0}`")] - Parse(#[from] serde_json::Error), - - #[error("utf8 decoding error `{0}`")] - ParseUtf8(#[from] FromUtf8Error), - - #[error("uri error `{0}`")] - Url(#[from] http::uri::InvalidUri), - - #[error("url encoding error `{0}`")] - EncodeUrl(#[from] serde_urlencoded::ser::Error), - #[error("ipfs client error `{0}`")] IpfsClientError(#[from] ipfs_api_prelude::Error), } -- cgit v1.2.3 From 93e8c2e2ad4a3c755a707d268ede8e661c45c68d Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Mon, 22 Feb 2021 22:56:45 -0500 Subject: use common form obj --- ipfs-api-backend-hyper/src/backend.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'ipfs-api-backend-hyper') diff --git a/ipfs-api-backend-hyper/src/backend.rs b/ipfs-api-backend-hyper/src/backend.rs index bbe9a56..a23bbd3 100644 --- a/ipfs-api-backend-hyper/src/backend.rs +++ b/ipfs-api-backend-hyper/src/backend.rs @@ -13,6 +13,7 @@ use hyper::{ }; use hyper_tls::HttpsConnector; use ipfs_api_prelude::{ApiRequest, Backend, TryFromUri}; +use multipart::client::multipart; use serde::Serialize; pub struct HyperBackend { @@ -43,14 +44,12 @@ impl Backend for HyperBackend { type HttpResponse = http::Response; - type MultipartForm = multipart::client::multipart::Form<'static>; - type Error = Error; fn build_base_request( &self, req: &Req, - form: Option, + form: Option>, ) -> Result where Req: ApiRequest, @@ -76,7 +75,7 @@ impl Backend for HyperBackend { async fn request_raw( &self, req: Req, - form: Option, + form: Option>, ) -> Result<(StatusCode, Bytes), Self::Error> where Req: ApiRequest + Serialize, -- cgit v1.2.3 From 5cb668119fdf4bc400a5b8432f2aec7547f1e717 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Mon, 22 Feb 2021 22:57:11 -0500 Subject: expose stuff in crates --- ipfs-api-backend-hyper/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'ipfs-api-backend-hyper') diff --git a/ipfs-api-backend-hyper/src/lib.rs b/ipfs-api-backend-hyper/src/lib.rs index 276d34a..f6b4448 100644 --- a/ipfs-api-backend-hyper/src/lib.rs +++ b/ipfs-api-backend-hyper/src/lib.rs @@ -11,4 +11,5 @@ extern crate hyper_multipart_rfc7578 as multipart; mod backend; mod error; -pub use crate::{backend::HyperBackend as IpfsApi, error::Error}; +pub use crate::{backend::HyperBackend as IpfsClient, error::Error}; +pub use ipfs_api_prelude::{request, response, IpfsApi}; -- cgit v1.2.3 From 2d32fa5de144690fcd7d26064a1a4e66c5a7c0b4 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Tue, 23 Feb 2021 23:24:18 -0500 Subject: export right stuff to make doc examples work --- ipfs-api-backend-hyper/src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'ipfs-api-backend-hyper') diff --git a/ipfs-api-backend-hyper/src/lib.rs b/ipfs-api-backend-hyper/src/lib.rs index f6b4448..3fd6232 100644 --- a/ipfs-api-backend-hyper/src/lib.rs +++ b/ipfs-api-backend-hyper/src/lib.rs @@ -12,4 +12,7 @@ mod backend; mod error; pub use crate::{backend::HyperBackend as IpfsClient, error::Error}; -pub use ipfs_api_prelude::{request, response, IpfsApi}; +pub use ipfs_api_prelude::{ + request::{self, KeyType, Logger, LoggingLevel, ObjectTemplate}, + response, IpfsApi, TryFromUri, +}; -- cgit v1.2.3 From 2263944af0da6b0b837285af78dafad765853853 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Tue, 23 Feb 2021 23:25:19 -0500 Subject: add documentation --- ipfs-api-backend-hyper/src/backend.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'ipfs-api-backend-hyper') diff --git a/ipfs-api-backend-hyper/src/backend.rs b/ipfs-api-backend-hyper/src/backend.rs index a23bbd3..0798fc9 100644 --- a/ipfs-api-backend-hyper/src/backend.rs +++ b/ipfs-api-backend-hyper/src/backend.rs @@ -22,6 +22,9 @@ pub struct HyperBackend { } impl Default for HyperBackend { + /// Creates an `IpfsClient` connected to the endpoint specified in ~/.ipfs/api. + /// If not found, tries to connect to `localhost:5001`. + /// fn default() -> Self { Self::from_ipfs_config() .unwrap_or_else(|| Self::from_host_and_port(Scheme::HTTP, "localhost", 5001).unwrap()) -- cgit v1.2.3 From dd03b0edb4928894b64b85cc44a4984a721650c2 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Tue, 23 Feb 2021 23:44:34 -0500 Subject: fixing actix examples by upgrading to new rt --- ipfs-api-backend-hyper/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ipfs-api-backend-hyper') diff --git a/ipfs-api-backend-hyper/Cargo.toml b/ipfs-api-backend-hyper/Cargo.toml index 03ee234..d45e069 100644 --- a/ipfs-api-backend-hyper/Cargo.toml +++ b/ipfs-api-backend-hyper/Cargo.toml @@ -17,7 +17,7 @@ bytes = "1.0" futures = "0.3" http = "0.2" hyper = { version = "0.14", features = ["http1", "http2", "client"] } -hyper-multipart-rfc7578 = "0.5" +hyper-multipart-rfc7578 = { version = "0.5", git = "https://github.com/ferristseng/rust-multipart-rfc7578", branch = "ftseng/upgrade-actix-rt" } hyper-tls = "0.5" ipfs-api-prelude = { version = "0.1.0", path = "../ipfs-api-prelude" } serde = "1.0" -- cgit v1.2.3 From fc3cd21efe52b0575617bcd249b30e8846ad2278 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Tue, 23 Feb 2021 23:50:30 -0500 Subject: update copyright headers --- ipfs-api-backend-hyper/src/backend.rs | 8 ++++++++ ipfs-api-backend-hyper/src/error.rs | 2 +- ipfs-api-backend-hyper/src/lib.rs | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) (limited to 'ipfs-api-backend-hyper') diff --git a/ipfs-api-backend-hyper/src/backend.rs b/ipfs-api-backend-hyper/src/backend.rs index 0798fc9..01e30de 100644 --- a/ipfs-api-backend-hyper/src/backend.rs +++ b/ipfs-api-backend-hyper/src/backend.rs @@ -1,6 +1,14 @@ use crate::error::Error; use async_trait::async_trait; use bytes::Bytes; +// Copyright 2021 rust-ipfs-api Developers +// +// Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be +// copied, modified, or distributed except according to those terms. +// + use futures::{FutureExt, Stream, StreamExt, TryFutureExt, TryStreamExt}; use http::{ header::{HeaderName, HeaderValue}, diff --git a/ipfs-api-backend-hyper/src/error.rs b/ipfs-api-backend-hyper/src/error.rs index 4760fc4..17e42ff 100644 --- a/ipfs-api-backend-hyper/src/error.rs +++ b/ipfs-api-backend-hyper/src/error.rs @@ -1,4 +1,4 @@ -// Copyright 2019 rust-ipfs-api Developers +// Copyright 2021 rust-ipfs-api Developers // // Licensed under the Apache License, Version 2.0, or the MIT license or the MIT license Date: Sun, 4 Apr 2021 18:48:28 -0400 Subject: upgrade dependency --- ipfs-api-backend-hyper/Cargo.toml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'ipfs-api-backend-hyper') diff --git a/ipfs-api-backend-hyper/Cargo.toml b/ipfs-api-backend-hyper/Cargo.toml index d45e069..dad8651 100644 --- a/ipfs-api-backend-hyper/Cargo.toml +++ b/ipfs-api-backend-hyper/Cargo.toml @@ -17,8 +17,7 @@ bytes = "1.0" futures = "0.3" http = "0.2" hyper = { version = "0.14", features = ["http1", "http2", "client"] } -hyper-multipart-rfc7578 = { version = "0.5", git = "https://github.com/ferristseng/rust-multipart-rfc7578", branch = "ftseng/upgrade-actix-rt" } -hyper-tls = "0.5" +hyper-multipart-rfc7578 = "0.5" ipfs-api-prelude = { version = "0.1.0", path = "../ipfs-api-prelude" } serde = "1.0" thiserror = "1.0" -- cgit v1.2.3 From 6b12cd5ad2ae7212e2ad3077463cbec453da4774 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Sun, 4 Apr 2021 18:49:05 -0400 Subject: add features to ipfs-api-backend-hyper --- ipfs-api-backend-hyper/Cargo.toml | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'ipfs-api-backend-hyper') diff --git a/ipfs-api-backend-hyper/Cargo.toml b/ipfs-api-backend-hyper/Cargo.toml index dad8651..55d50fb 100644 --- a/ipfs-api-backend-hyper/Cargo.toml +++ b/ipfs-api-backend-hyper/Cargo.toml @@ -11,6 +11,10 @@ version = "0.1.0" readme = "../README.md" license = "MIT OR Apache-2.0" +[features] +with-hyper-tls = ["hyper-tls"] +with-hyper-rustls = ["hyper-rustls"] + [dependencies] async-trait = "0.1" bytes = "1.0" @@ -18,6 +22,8 @@ futures = "0.3" http = "0.2" hyper = { version = "0.14", features = ["http1", "http2", "client"] } hyper-multipart-rfc7578 = "0.5" +hyper-rustls = { version = "0.22", optional = true } +hyper-tls = { version = "0.5", optional = true } ipfs-api-prelude = { version = "0.1.0", path = "../ipfs-api-prelude" } serde = "1.0" thiserror = "1.0" -- cgit v1.2.3 From b76b054118f29aa8ab31722831518398b3b7f40d Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Sun, 4 Apr 2021 18:50:07 -0400 Subject: make HyperBackend generic --- ipfs-api-backend-hyper/src/backend.rs | 56 ++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 21 deletions(-) (limited to 'ipfs-api-backend-hyper') diff --git a/ipfs-api-backend-hyper/src/backend.rs b/ipfs-api-backend-hyper/src/backend.rs index 01e30de..c496de6 100644 --- a/ipfs-api-backend-hyper/src/backend.rs +++ b/ipfs-api-backend-hyper/src/backend.rs @@ -17,40 +17,54 @@ use http::{ }; use hyper::{ body, - client::{self, Builder, HttpConnector}, + client::{self, Builder, connect::Connect, HttpConnector}, }; -use hyper_tls::HttpsConnector; use ipfs_api_prelude::{ApiRequest, Backend, TryFromUri}; use multipart::client::multipart; use serde::Serialize; -pub struct HyperBackend { +pub struct HyperBackend where C: Connect + Clone + Send + Sync + 'static { base: Uri, - client: client::Client, hyper::Body>, + client: client::Client, } -impl Default for HyperBackend { - /// Creates an `IpfsClient` connected to the endpoint specified in ~/.ipfs/api. - /// If not found, tries to connect to `localhost:5001`. - /// - fn default() -> Self { - Self::from_ipfs_config() - .unwrap_or_else(|| Self::from_host_and_port(Scheme::HTTP, "localhost", 5001).unwrap()) - } +macro_rules! impl_default { + ($http_connector:path) => { + impl_default!($http_connector, <$http_connector>::new()); + }; + ($http_connector:path, $constructor:expr) => { + impl Default for HyperBackend<$http_connector> { + /// Creates an `IpfsClient` connected to the endpoint specified in ~/.ipfs/api. + /// If not found, tries to connect to `localhost:5001`. + /// + fn default() -> Self { + Self::from_ipfs_config() + .unwrap_or_else(|| Self::from_host_and_port(Scheme::HTTP, "localhost", 5001).unwrap()) + } + } + + impl TryFromUri for HyperBackend<$http_connector> { + fn build_with_base_uri(base: Uri) -> Self { + let client = Builder::default() + .pool_max_idle_per_host(0) + .build($constructor); + + HyperBackend { base, client } + } + } + }; } -impl TryFromUri for HyperBackend { - fn build_with_base_uri(base: Uri) -> Self { - let client = Builder::default() - .pool_max_idle_per_host(0) - .build(HttpsConnector::new()); +impl_default!(HttpConnector); - HyperBackend { base, client } - } -} +#[cfg(feature = "with-hyper-tls")] +impl_default!(hyper_tls::HttpsConnector); + +#[cfg(feature = "with-hyper-rustls")] +impl_default!(hyper_rustls::HttpsConnector, hyper_rustls::HttpsConnector::with_native_roots()); #[async_trait(?Send)] -impl Backend for HyperBackend { +impl Backend for HyperBackend where C: Connect + Clone + Send + Sync + 'static { type HttpRequest = http::Request; type HttpResponse = http::Response; -- cgit v1.2.3 From c569ecafface0fbaaf15e293d15df17e2761ae8b Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Sun, 4 Apr 2021 19:36:39 -0400 Subject: add tcp feature to fix build --- ipfs-api-backend-hyper/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ipfs-api-backend-hyper') diff --git a/ipfs-api-backend-hyper/Cargo.toml b/ipfs-api-backend-hyper/Cargo.toml index 55d50fb..3be4d04 100644 --- a/ipfs-api-backend-hyper/Cargo.toml +++ b/ipfs-api-backend-hyper/Cargo.toml @@ -20,7 +20,7 @@ async-trait = "0.1" bytes = "1.0" futures = "0.3" http = "0.2" -hyper = { version = "0.14", features = ["http1", "http2", "client"] } +hyper = { version = "0.14", features = ["http1", "http2", "client", "tcp"] } hyper-multipart-rfc7578 = "0.5" hyper-rustls = { version = "0.22", optional = true } hyper-tls = { version = "0.5", optional = true } -- cgit v1.2.3 From 822e0552619877634bbb6bc82ac2a398914dfc95 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Sun, 4 Apr 2021 19:40:49 -0400 Subject: formatting --- ipfs-api-backend-hyper/src/backend.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'ipfs-api-backend-hyper') diff --git a/ipfs-api-backend-hyper/src/backend.rs b/ipfs-api-backend-hyper/src/backend.rs index c496de6..63359c3 100644 --- a/ipfs-api-backend-hyper/src/backend.rs +++ b/ipfs-api-backend-hyper/src/backend.rs @@ -17,13 +17,16 @@ use http::{ }; use hyper::{ body, - client::{self, Builder, connect::Connect, HttpConnector}, + client::{self, connect::Connect, Builder, HttpConnector}, }; use ipfs_api_prelude::{ApiRequest, Backend, TryFromUri}; use multipart::client::multipart; use serde::Serialize; -pub struct HyperBackend where C: Connect + Clone + Send + Sync + 'static { +pub struct HyperBackend +where + C: Connect + Clone + Send + Sync + 'static, +{ base: Uri, client: client::Client, } @@ -38,8 +41,9 @@ macro_rules! impl_default { /// If not found, tries to connect to `localhost:5001`. /// fn default() -> Self { - Self::from_ipfs_config() - .unwrap_or_else(|| Self::from_host_and_port(Scheme::HTTP, "localhost", 5001).unwrap()) + Self::from_ipfs_config().unwrap_or_else(|| { + Self::from_host_and_port(Scheme::HTTP, "localhost", 5001).unwrap() + }) } } @@ -61,10 +65,16 @@ impl_default!(HttpConnector); impl_default!(hyper_tls::HttpsConnector); #[cfg(feature = "with-hyper-rustls")] -impl_default!(hyper_rustls::HttpsConnector, hyper_rustls::HttpsConnector::with_native_roots()); +impl_default!( + hyper_rustls::HttpsConnector, + hyper_rustls::HttpsConnector::with_native_roots() +); #[async_trait(?Send)] -impl Backend for HyperBackend where C: Connect + Clone + Send + Sync + 'static { +impl Backend for HyperBackend +where + C: Connect + Clone + Send + Sync + 'static, +{ type HttpRequest = http::Request; type HttpResponse = http::Response; -- cgit v1.2.3 From 8ae619d45b39e70d85215795b5e91e3944a30fc9 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Sun, 4 Apr 2021 19:57:45 -0400 Subject: more clippy changes --- ipfs-api-backend-hyper/src/backend.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ipfs-api-backend-hyper') diff --git a/ipfs-api-backend-hyper/src/backend.rs b/ipfs-api-backend-hyper/src/backend.rs index 63359c3..6ef9f7d 100644 --- a/ipfs-api-backend-hyper/src/backend.rs +++ b/ipfs-api-backend-hyper/src/backend.rs @@ -92,7 +92,7 @@ where let url = req.absolute_url(&self.base)?; let builder = http::Request::builder(); - let builder = builder.method(Req::METHOD.clone()).uri(url); + let builder = builder.method(Req::METHOD).uri(url); let req = if let Some(form) = form { form.set_body_convert::(builder) @@ -103,7 +103,7 @@ where Ok(req) } - fn get_header<'a>(res: &'a Self::HttpResponse, key: HeaderName) -> Option<&'a HeaderValue> { + fn get_header(res: &Self::HttpResponse, key: HeaderName) -> Option<&HeaderValue> { res.headers().get(key) } -- cgit v1.2.3