Skip to content

Commit 6b06bf1

Browse files
authored
Merge pull request #44 from Fishrock123/fix-hyper-body
src: correctly translate hyper body stream to ours
2 parents 3090eea + 1c59a52 commit 6b06bf1

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://book.async.rs/overview
77

88
## [Unreleased]
99

10+
## Fixed
11+
- Fixed a body stream translation bug in the `hyper_client`.
12+
1013
## [5.0.0] - 2020-09-18
1114

1215
This release includes an optional backend using [hyper.rs](https://hyper.rs/), and uses [async-trait](https://crates.io/crates/async-trait) for `HttpClient`.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ h1_client = ["async-h1", "async-std", "async-native-tls"]
2222
native_client = ["curl_client", "wasm_client"]
2323
curl_client = ["isahc", "async-std"]
2424
wasm_client = ["js-sys", "web-sys", "wasm-bindgen", "wasm-bindgen-futures", "futures"]
25-
hyper_client = ["hyper", "hyper-tls", "http-types/hyperium_http"]
25+
hyper_client = ["hyper", "hyper-tls", "http-types/hyperium_http", "futures-util"]
2626

2727
[dependencies]
2828
async-trait = "0.1.37"
@@ -37,6 +37,7 @@ async-native-tls = { version = "0.3.1", optional = true }
3737
# hyper_client
3838
hyper = { version = "0.13.6", features = ["tcp"], optional = true }
3939
hyper-tls = { version = "0.4.3", optional = true }
40+
futures-util = { version = "0.3.5", optional = true }
4041

4142
# curl_client
4243
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]

src/hyper.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
//! http-client implementation for reqwest
22
33
use super::{async_trait, Error, HttpClient, Request, Response};
4+
use futures_util::stream::TryStreamExt;
45
use http_types::headers::{HeaderName, HeaderValue};
56
use http_types::StatusCode;
67
use hyper::body::HttpBody;
78
use hyper_tls::HttpsConnector;
89
use std::convert::TryFrom;
10+
use std::io;
911
use std::str::FromStr;
1012

1113
/// Hyper-based HTTP Client.
@@ -100,20 +102,11 @@ struct HttpTypesResponse {
100102

101103
impl HttpTypesResponse {
102104
async fn try_from(value: hyper::Response<hyper::Body>) -> Result<Self, Error> {
103-
let (parts, mut body) = value.into_parts();
104-
105-
let body = match body.data().await {
106-
None => None,
107-
Some(Ok(b)) => Some(b),
108-
Some(Err(_)) => {
109-
return Err(Error::from_str(
110-
StatusCode::BadGateway,
111-
"unable to read HTTP response body",
112-
))
113-
}
114-
}
115-
.map(|b| http_types::Body::from_bytes(b.to_vec()))
116-
.unwrap_or(http_types::Body::empty());
105+
let (parts, body) = value.into_parts();
106+
107+
let size_hint = body.size_hint().upper().map(|s| s as usize);
108+
let body = body.map_err(|err| io::Error::new(io::ErrorKind::Other, err.to_string()));
109+
let body = http_types::Body::from_reader(body.into_async_read(), size_hint);
117110

118111
let mut res = Response::new(parts.status);
119112
res.set_version(Some(parts.version.into()));

0 commit comments

Comments
 (0)