Skip to content

Commit e33e60f

Browse files
committed
Enable hyperium_http
1 parent 0d15dad commit e33e60f

File tree

2 files changed

+10
-58
lines changed

2 files changed

+10
-58
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ hyper_client = ["hyper", "hyper-tls"]
2626

2727
[dependencies]
2828
futures = { version = "0.3.1" }
29-
http-types = "2.0.1"
29+
http-types = { version = "2.0.1", features = ["hyperium_http"] }
3030
log = "0.4.7"
3131

3232
# h1-client

src/hyper.rs

Lines changed: 9 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
33
use super::{Error, HttpClient, Request, Response};
44
use http_types::headers::{HeaderName, HeaderValue};
5-
use http_types::{Method, StatusCode, Version};
5+
use http_types::StatusCode;
66
use hyper::body::HttpBody;
77
use hyper_tls::HttpsConnector;
8-
use std::convert::{TryFrom, TryInto};
8+
use std::convert::TryFrom;
99
use std::str::FromStr;
1010

1111
/// Hyper-based HTTP Client.
@@ -53,43 +53,10 @@ struct HyperHttpRequest {
5353

5454
impl HyperHttpRequest {
5555
async fn try_from(mut value: Request) -> Result<Self, Error> {
56-
let method = match value.method() {
57-
Method::Get => hyper::Method::GET,
58-
Method::Head => hyper::Method::HEAD,
59-
Method::Post => hyper::Method::POST,
60-
Method::Put => hyper::Method::PUT,
61-
Method::Patch => hyper::Method::PATCH,
62-
Method::Options => hyper::Method::OPTIONS,
63-
Method::Trace => hyper::Method::TRACE,
64-
Method::Connect => hyper::Method::CONNECT,
65-
_ => {
66-
return Err(Error::from_str(
67-
StatusCode::BadRequest,
68-
"unrecognized HTTP method",
69-
))
70-
}
71-
};
72-
73-
let version = value
74-
.version()
75-
.map(|v| match v {
76-
Version::Http0_9 => Ok(hyper::Version::HTTP_09),
77-
Version::Http1_0 => Ok(hyper::Version::HTTP_10),
78-
Version::Http1_1 => Ok(hyper::Version::HTTP_11),
79-
Version::Http2_0 => Ok(hyper::Version::HTTP_2),
80-
Version::Http3_0 => Ok(hyper::Version::HTTP_3),
81-
_ => Err(Error::from_str(
82-
StatusCode::BadRequest,
83-
"unrecognized HTTP version",
84-
)),
85-
})
86-
.or(Some(Ok(hyper::Version::default())))
87-
.unwrap()?;
88-
8956
// UNWRAP: This unwrap is unjustified in `http-types`, need to check if it's actually safe.
9057
let uri = hyper::Uri::try_from(&format!("{}", value.url())).unwrap();
9158

92-
// `HttpClient` depends on the scheme being either "http" or "https"
59+
// `HyperClient` depends on the scheme being either "http" or "https"
9360
match uri.scheme_str() {
9461
Some("http") | Some("https") => (),
9562
_ => return Err(Error::from_str(StatusCode::BadRequest, "invalid scheme")),
@@ -114,13 +81,13 @@ impl HyperHttpRequest {
11481
let body = value.body_bytes().await?;
11582
let body = hyper::Body::from(body);
11683

117-
let req = hyper::Request::builder()
118-
.method(method)
119-
.version(version)
84+
let request = request
85+
.method(value.method())
86+
.version(value.version().map(|v| v.into()).unwrap_or_default())
12087
.uri(uri)
12188
.body(body)?;
12289

123-
Ok(HyperHttpRequest { inner: req })
90+
Ok(HyperHttpRequest { inner: request })
12491
}
12592

12693
fn into_inner(self) -> hyper::Request<hyper::Body> {
@@ -136,21 +103,6 @@ impl HttpTypesResponse {
136103
async fn try_from(value: hyper::Response<hyper::Body>) -> Result<Self, Error> {
137104
let (parts, mut body) = value.into_parts();
138105

139-
// UNWRAP: http and http-types implement the same status codes
140-
let status: StatusCode = parts.status.as_u16().try_into().unwrap();
141-
142-
let version = match parts.version {
143-
hyper::Version::HTTP_09 => Ok(http_types::Version::Http0_9),
144-
hyper::Version::HTTP_10 => Ok(http_types::Version::Http1_0),
145-
hyper::Version::HTTP_11 => Ok(http_types::Version::Http1_1),
146-
hyper::Version::HTTP_2 => Ok(http_types::Version::Http2_0),
147-
hyper::Version::HTTP_3 => Ok(http_types::Version::Http3_0),
148-
_ => Err(Error::from_str(
149-
StatusCode::BadGateway,
150-
"unrecognized HTTP response version",
151-
)),
152-
}?;
153-
154106
let body = match body.data().await {
155107
None => None,
156108
Some(Ok(b)) => Some(b),
@@ -164,8 +116,8 @@ impl HttpTypesResponse {
164116
.map(|b| http_types::Body::from_bytes(b.to_vec()))
165117
.unwrap_or(http_types::Body::empty());
166118

167-
let mut res = Response::new(status);
168-
res.set_version(Some(version));
119+
let mut res = Response::new(parts.status);
120+
res.set_version(Some(parts.version.into()));
169121

170122
for (name, value) in parts.headers {
171123
let value = value.as_bytes().to_owned();

0 commit comments

Comments
 (0)