Update to hyper 0.13

This commit is contained in:
Gleb Pomykalov
2019-12-11 03:24:05 +03:00
committed by Sean McArthur
parent db2de90e42
commit 0f32c4a01a
23 changed files with 469 additions and 219 deletions

View File

@@ -6,7 +6,7 @@ use std::task::{Context, Poll};
use bytes::Bytes;
use futures_core::Stream;
use http_body::Body as HttpBody;
use tokio::timer::Delay;
use tokio::time::Delay;
/// An asynchronous request body.
pub struct Body {
@@ -21,7 +21,7 @@ enum Inner {
Streaming {
body: Pin<
Box<
dyn HttpBody<Data = hyper::Chunk, Error = Box<dyn std::error::Error + Send + Sync>>
dyn HttpBody<Data = Bytes, Error = Box<dyn std::error::Error + Send + Sync>>
+ Send
+ Sync,
>,
@@ -73,7 +73,7 @@ impl Body {
where
S: futures_core::stream::TryStream + Send + Sync + 'static,
S::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
hyper::Chunk: From<S::Ok>,
Bytes: From<S::Ok>,
{
Body::stream(stream)
}
@@ -82,12 +82,12 @@ impl Body {
where
S: futures_core::stream::TryStream + Send + Sync + 'static,
S::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
hyper::Chunk: From<S::Ok>,
Bytes: From<S::Ok>,
{
use futures_util::TryStreamExt;
let body = Box::pin(WrapStream(
stream.map_ok(hyper::Chunk::from).map_err(Into::into),
stream.map_ok(Bytes::from).map_err(Into::into),
));
Body {
inner: Inner::Streaming {
@@ -198,7 +198,7 @@ impl fmt::Debug for Body {
// ===== impl ImplStream =====
impl HttpBody for ImplStream {
type Data = hyper::Chunk;
type Data = Bytes;
type Error = crate::Error;
fn poll_data(
@@ -291,10 +291,10 @@ impl Stream for ImplStream {
impl<S, D, E> HttpBody for WrapStream<S>
where
S: Stream<Item = Result<D, E>>,
D: Into<hyper::Chunk>,
D: Into<Bytes>,
E: Into<Box<dyn std::error::Error + Send + Sync>>,
{
type Data = hyper::Chunk;
type Data = Bytes;
type Error = E;
fn poll_data(
@@ -321,7 +321,7 @@ where
// ===== impl WrapHyper =====
impl HttpBody for WrapHyper {
type Data = hyper::Chunk;
type Data = Bytes;
type Error = Box<dyn std::error::Error + Send + Sync>;
fn poll_data(

View File

@@ -11,13 +11,14 @@ use http::header::{
CONTENT_TYPE, LOCATION, PROXY_AUTHORIZATION, RANGE, REFERER, TRANSFER_ENCODING, USER_AGENT,
};
use http::Uri;
use http::uri::Scheme;
use hyper::client::ResponseFuture;
#[cfg(feature = "default-tls")]
use native_tls::TlsConnector;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::{clock, timer::Delay};
use tokio::time::Delay;
use log::debug;
@@ -726,7 +727,7 @@ impl Client {
// insert default headers in the request headers
// without overwriting already appended headers.
for (key, value) in &self.inner.headers {
if let Ok(Entry::Vacant(entry)) = headers.entry(key) {
if let Entry::Vacant(entry) = headers.entry(key) {
entry.insert(value.clone());
}
}
@@ -772,7 +773,7 @@ impl Client {
let timeout = self
.inner
.request_timeout
.map(|dur| tokio::timer::delay(clock::now() + dur));
.map(|dur| tokio::time::delay_for(dur));
Pending {
inner: PendingInner::Request(PendingRequest {
@@ -799,7 +800,7 @@ impl Client {
// Only set the header here if the destination scheme is 'http',
// since otherwise, the header will be included in the CONNECT tunnel
// request instead.
if dst.scheme_part() != Some(&::http::uri::Scheme::HTTP) {
if dst.scheme() != Some(&Scheme::HTTP) {
return;
}

View File

@@ -192,7 +192,7 @@ mod imp {
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
use futures_util::StreamExt;
match futures_core::ready!(Pin::new(&mut self.0).peek(cx)) {
match futures_core::ready!(Pin::new(&mut self.0).poll_peek(cx)) {
Some(Ok(_)) => {
// fallthrough
}

View File

@@ -3,7 +3,7 @@ use std::borrow::Cow;
use std::fmt;
use std::pin::Pin;
use bytes::Bytes;
use bytes::{Bytes};
use http::HeaderMap;
use mime_guess::Mime;
use percent_encoding::{self, AsciiSet, NON_ALPHANUMERIC};
@@ -536,18 +536,18 @@ mod tests {
use super::*;
use futures_util::TryStreamExt;
use futures_util::{future, stream};
use tokio;
use tokio::{self, runtime};
#[test]
fn form_empty() {
let form = Form::new();
let mut rt = tokio::runtime::current_thread::Runtime::new().expect("new rt");
let mut rt = runtime::Builder::new().basic_scheduler().enable_all().build().expect("new rt");
let body = form.stream().into_stream();
let s = body.map(|try_c| try_c.map(Bytes::from)).try_concat();
let s = body.map_ok(|try_c| try_c.to_vec()).try_concat();
let out = rt.block_on(s);
assert_eq!(out.unwrap(), Vec::new());
assert!(out.unwrap().is_empty());
}
#[test]
@@ -590,9 +590,9 @@ mod tests {
--boundary\r\n\
Content-Disposition: form-data; name=\"key3\"; filename=\"filename\"\r\n\r\n\
value3\r\n--boundary--\r\n";
let mut rt = tokio::runtime::current_thread::Runtime::new().expect("new rt");
let mut rt = runtime::Builder::new().basic_scheduler().enable_all().build().expect("new rt");
let body = form.stream().into_stream();
let s = body.map(|try_c| try_c.map(Bytes::from)).try_concat();
let s = body.map(|try_c| try_c.map(|r| r.to_vec())).try_concat();
let out = rt.block_on(s).unwrap();
// These prints are for debug purposes in case the test fails
@@ -617,9 +617,9 @@ mod tests {
\r\n\
value2\r\n\
--boundary--\r\n";
let mut rt = tokio::runtime::current_thread::Runtime::new().expect("new rt");
let mut rt = runtime::Builder::new().basic_scheduler().enable_all().build().expect("new rt");
let body = form.stream().into_stream();
let s = body.map(|try_c| try_c.map(Bytes::from)).try_concat();
let s = body.map(|try_c| try_c.map(|r| r.to_vec())).try_concat();
let out = rt.block_on(s).unwrap();
// These prints are for debug purposes in case the test fails

View File

@@ -1,10 +1,10 @@
use std::convert::TryFrom;
use std::fmt;
use std::future::Future;
use std::io::Write;
use base64;
use base64::write::EncoderWriter as Base64Encoder;
use bytes::Bytes;
use serde::Serialize;
#[cfg(feature = "json")]
use serde_json;
@@ -16,7 +16,6 @@ use super::multipart;
use super::response::Response;
use crate::header::{HeaderMap, HeaderName, HeaderValue, CONTENT_LENGTH, CONTENT_TYPE};
use crate::{Method, Url};
use http::HttpTryFrom;
/// A request which can be executed with `Client::execute()`.
pub struct Request {
@@ -119,13 +118,15 @@ impl RequestBuilder {
/// Add a `Header` to this Request.
pub fn header<K, V>(mut self, key: K, value: V) -> RequestBuilder
where
HeaderName: HttpTryFrom<K>,
HeaderValue: HttpTryFrom<V>,
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<http::Error>,
HeaderValue: TryFrom<V>,
<HeaderValue as TryFrom<V>>::Error: Into<http::Error>,
{
let mut error = None;
if let Ok(ref mut req) = self.request {
match <HeaderName as HttpTryFrom<K>>::try_from(key) {
Ok(key) => match <HeaderValue as HttpTryFrom<V>>::try_from(value) {
match <HeaderName as TryFrom<K>>::try_from(key) {
Ok(key) => match <HeaderValue as TryFrom<V>>::try_from(value) {
Ok(value) => {
req.headers_mut().append(key, value);
}
@@ -166,7 +167,7 @@ impl RequestBuilder {
}
}
self.header(crate::header::AUTHORIZATION, Bytes::from(header_value))
self.header(crate::header::AUTHORIZATION, header_value)
}
/// Enable HTTP bearer authentication.

View File

@@ -2,9 +2,9 @@ use std::borrow::Cow;
use std::fmt;
use std::net::SocketAddr;
use bytes::Bytes;
use bytes::{Bytes, BytesMut};
use encoding_rs::{Encoding, UTF_8};
use futures_util::{StreamExt, TryStreamExt};
use futures_util::stream::StreamExt;
use http;
use hyper::client::connect::HttpInfo;
use hyper::header::CONTENT_LENGTH;
@@ -15,7 +15,7 @@ use mime::Mime;
use serde::de::DeserializeOwned;
#[cfg(feature = "json")]
use serde_json;
use tokio::timer::Delay;
use tokio::time::Delay;
use url::Url;
use super::body::Body;
@@ -260,8 +260,12 @@ impl Response {
/// # Ok(())
/// # }
/// ```
pub async fn bytes(self) -> crate::Result<Bytes> {
self.body.try_concat().await
pub async fn bytes(mut self) -> crate::Result<Bytes> {
let mut buf = BytesMut::new();
while let Some(chunk) = self.body.next().await {
buf.extend(chunk?);
}
Ok(buf.freeze())
}
/// Stream a chunk of the response body.
@@ -408,11 +412,12 @@ struct ResponseUrl(Url);
pub trait ResponseBuilderExt {
/// A builder method for the `http::response::Builder` type that allows the user to add a `Url`
/// to the `http::Response`
fn url(&mut self, url: Url) -> &mut Self;
fn url(self, url: Url) -> Self;
}
impl ResponseBuilderExt for http::response::Builder {
fn url(&mut self, url: Url) -> &mut Self {
fn url(self, url: Url) -> Self {
self.extension(ResponseUrl(url))
}
}