style(lib): run rustfmt and enforce in CI

This commit is contained in:
Sean McArthur
2019-12-05 13:30:53 -08:00
parent b0060f277e
commit 0dc89680cd
69 changed files with 2982 additions and 2499 deletions

View File

@@ -1,15 +1,15 @@
use futures_channel::{mpsc, oneshot};
use futures_util::future::{self, FutureExt as _, TryFutureExt as _, Either};
use futures_util::future::{self, Either, FutureExt as _, TryFutureExt as _};
use futures_util::stream::StreamExt as _;
use h2::client::{Builder, SendRequest};
use tokio::io::{AsyncRead, AsyncWrite};
use crate::headers::content_length_parse_all;
use crate::body::Payload;
use crate::common::{Exec, Future, Never, Pin, Poll, task};
use crate::headers;
use crate::proto::Dispatched;
use super::{PipeToSendStream, SendBuf};
use crate::body::Payload;
use crate::common::{task, Exec, Future, Never, Pin, Poll};
use crate::headers;
use crate::headers::content_length_parse_all;
use crate::proto::Dispatched;
use crate::{Body, Request, Response};
type ClientRx<B> = crate::client::dispatch::Receiver<Request<B>, Response<Body>>;
@@ -45,13 +45,10 @@ where
let (conn_drop_ref, rx) = mpsc::channel(1);
let (cancel_tx, conn_eof) = oneshot::channel();
let conn_drop_rx = rx.into_future()
.map(|(item, _rx)| {
match item {
Some(never) => match never {},
None => (),
}
});
let conn_drop_rx = rx.into_future().map(|(item, _rx)| match item {
Some(never) => match never {},
None => (),
});
let conn = conn.map_err(|e| debug!("connection error: {}", e));
@@ -138,12 +135,11 @@ where
};
if !eos {
let mut pipe = PipeToSendStream::new(body, body_tx)
.map(|res| {
if let Err(e) = res {
debug!("client request body error: {}", e);
}
});
let mut pipe = PipeToSendStream::new(body, body_tx).map(|res| {
if let Err(e) = res {
debug!("client request body error: {}", e);
}
});
// eagerly see if the body pipe is ready and
// can thus skip allocating in the executor
@@ -152,45 +148,39 @@ where
Poll::Pending => {
let conn_drop_ref = self.conn_drop_ref.clone();
let pipe = pipe.map(move |x| {
drop(conn_drop_ref);
x
});
drop(conn_drop_ref);
x
});
self.executor.execute(pipe);
}
}
}
let fut = fut
.map(move |result| {
match result {
Ok(res) => {
let content_length = content_length_parse_all(res.headers());
let res = res.map(|stream|
crate::Body::h2(stream, content_length));
Ok(res)
},
Err(err) => {
debug!("client response error: {}", err);
Err((crate::Error::new_h2(err), None))
}
}
});
let fut = fut.map(move |result| match result {
Ok(res) => {
let content_length = content_length_parse_all(res.headers());
let res = res.map(|stream| crate::Body::h2(stream, content_length));
Ok(res)
}
Err(err) => {
debug!("client response error: {}", err);
Err((crate::Error::new_h2(err), None))
}
});
self.executor.execute(cb.send_when(fut));
continue;
},
}
Poll::Ready(None) => {
trace!("client::dispatch::Sender dropped");
return Poll::Ready(Ok(Dispatched::Shutdown));
}
Poll::Pending => {
match ready!(Pin::new(&mut self.conn_eof).poll(cx)) {
Ok(never) => match never {},
Err(_conn_is_eof) => {
trace!("connection task is closed, closing dispatch task");
return Poll::Ready(Ok(Dispatched::Shutdown));
}
Poll::Pending => match ready!(Pin::new(&mut self.conn_eof).poll(cx)) {
Ok(never) => match never {},
Err(_conn_is_eof) => {
trace!("connection task is closed, closing dispatch task");
return Poll::Ready(Ok(Dispatched::Shutdown));
}
},
}

View File

@@ -1,5 +1,5 @@
use bytes::Buf;
use h2::{SendStream};
use h2::SendStream;
use http::header::{
HeaderName, CONNECTION, PROXY_AUTHENTICATE, PROXY_AUTHORIZATION, TE, TRAILER,
TRANSFER_ENCODING, UPGRADE,
@@ -7,7 +7,7 @@ use http::header::{
use http::HeaderMap;
use crate::body::Payload;
use crate::common::{Future, Pin, Poll, task};
use crate::common::{task, Future, Pin, Poll};
pub(crate) mod client;
pub(crate) mod server;
@@ -38,7 +38,11 @@ fn strip_connection_headers(headers: &mut HeaderMap, is_request: bool) {
}
if is_request {
if headers.get(TE).map(|te_header| te_header != "trailers").unwrap_or(false) {
if headers
.get(TE)
.map(|te_header| te_header != "trailers")
.unwrap_or(false)
{
warn!("TE headers not set to \"trailers\" are illegal in HTTP/2 requests");
headers.remove(TE);
}
@@ -123,19 +127,24 @@ where
if self.body_tx.capacity() == 0 {
loop {
match ready!(self.body_tx.poll_capacity(cx)) {
Some(Ok(0)) => {},
Some(Ok(0)) => {}
Some(Ok(_)) => break,
Some(Err(e)) => return Poll::Ready(Err(crate::Error::new_body_write(e))) ,
Some(Err(e)) => {
return Poll::Ready(Err(crate::Error::new_body_write(e)))
}
None => return Poll::Ready(Err(crate::Error::new_canceled())),
}
}
} else {
if let Poll::Ready(reason) =
self.body_tx.poll_reset(cx).map_err(crate::Error::new_body_write)?
if let Poll::Ready(reason) = self
.body_tx
.poll_reset(cx)
.map_err(crate::Error::new_body_write)?
{
debug!("stream received RST_STREAM: {:?}", reason);
return Poll::Ready(Err(crate::Error::new_body_write(::h2::Error::from(reason))));
return Poll::Ready(Err(crate::Error::new_body_write(::h2::Error::from(
reason,
))));
}
}
@@ -170,11 +179,15 @@ where
}
}
} else {
if let Poll::Ready(reason) =
self.body_tx.poll_reset(cx).map_err(|e| crate::Error::new_body_write(e))?
if let Poll::Ready(reason) = self
.body_tx
.poll_reset(cx)
.map_err(|e| crate::Error::new_body_write(e))?
{
debug!("stream received RST_STREAM: {:?}", reason);
return Poll::Ready(Err(crate::Error::new_body_write(::h2::Error::from(reason))));
return Poll::Ready(Err(crate::Error::new_body_write(::h2::Error::from(
reason,
))));
}
match ready!(Pin::new(&mut self.stream).poll_trailers(cx)) {

View File

@@ -1,19 +1,19 @@
use std::error::Error as StdError;
use std::marker::Unpin;
use pin_project::{pin_project, project};
use h2::Reason;
use h2::server::{Builder, Connection, Handshake, SendResponse};
use h2::Reason;
use pin_project::{pin_project, project};
use tokio::io::{AsyncRead, AsyncWrite};
use super::{PipeToSendStream, SendBuf};
use crate::body::Payload;
use crate::common::exec::H2Exec;
use crate::common::{Future, Pin, Poll, task};
use crate::common::{task, Future, Pin, Poll};
use crate::headers;
use crate::headers::content_length_parse_all;
use crate::service::HttpService;
use crate::proto::Dispatched;
use super::{PipeToSendStream, SendBuf};
use crate::service::HttpService;
use crate::{Body, Response};
@@ -45,11 +45,10 @@ where
closing: Option<crate::Error>,
}
impl<T, S, B, E> Server<T, S, B, E>
where
T: AsyncRead + AsyncWrite + Unpin,
S: HttpService<Body, ResBody=B>,
S: HttpService<Body, ResBody = B>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
B: Payload,
B::Data: Unpin,
@@ -69,13 +68,13 @@ where
match self.state {
State::Handshaking(..) => {
// fall-through, to replace state with Closed
},
}
State::Serving(ref mut srv) => {
if srv.closing.is_none() {
srv.conn.graceful_shutdown();
}
return;
},
}
State::Closed => {
return;
}
@@ -87,7 +86,7 @@ where
impl<T, S, B, E> Future for Server<T, S, B, E>
where
T: AsyncRead + AsyncWrite + Unpin,
S: HttpService<Body, ResBody=B>,
S: HttpService<Body, ResBody = B>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
B: Payload,
B::Data: Unpin,
@@ -105,7 +104,7 @@ where
conn,
closing: None,
})
},
}
State::Serving(ref mut srv) => {
ready!(srv.poll_server(cx, &mut me.service, &mut me.exec))?;
return Poll::Ready(Ok(Dispatched::Shutdown));
@@ -127,12 +126,14 @@ where
B: Payload,
B::Data: Unpin,
{
fn poll_server<S, E>(&mut self, cx: &mut task::Context<'_>, service: &mut S, exec: &mut E) -> Poll<crate::Result<()>>
fn poll_server<S, E>(
&mut self,
cx: &mut task::Context<'_>,
service: &mut S,
exec: &mut E,
) -> Poll<crate::Result<()>>
where
S: HttpService<
Body,
ResBody=B,
>,
S: HttpService<Body, ResBody = B>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
E: H2Exec<S::Future, B>,
{
@@ -171,25 +172,26 @@ where
Some(Ok((req, respond))) => {
trace!("incoming request");
let content_length = content_length_parse_all(req.headers());
let req = req.map(|stream| {
crate::Body::h2(stream, content_length)
});
let req = req.map(|stream| crate::Body::h2(stream, content_length));
let fut = H2Stream::new(service.call(req), respond);
exec.execute_h2stream(fut);
},
}
Some(Err(e)) => {
return Poll::Ready(Err(crate::Error::new_h2(e)));
},
}
None => {
// no more incoming streams...
trace!("incoming connection complete");
return Poll::Ready(Ok(()));
},
}
}
}
}
debug_assert!(self.closing.is_some(), "poll_server broke loop without closing");
debug_assert!(
self.closing.is_some(),
"poll_server broke loop without closing"
);
ready!(self.conn.poll_closed(cx).map_err(crate::Error::new_h2))?;
@@ -230,7 +232,7 @@ where
}
macro_rules! reply {
($me:expr, $res:expr, $eos:expr) => ({
($me:expr, $res:expr, $eos:expr) => {{
match $me.reply.send_response($res, $eos) {
Ok(tx) => tx,
Err(e) => {
@@ -239,7 +241,7 @@ macro_rules! reply {
return Poll::Ready(Err(crate::Error::new_h2(e)));
}
}
})
}};
}
impl<F, B, E> H2Stream<F, B>
@@ -261,8 +263,10 @@ where
Poll::Pending => {
// Response is not yet ready, so we want to check if the client has sent a
// RST_STREAM frame which would cancel the current request.
if let Poll::Ready(reason) =
me.reply.poll_reset(cx).map_err(|e| crate::Error::new_h2(e))?
if let Poll::Ready(reason) = me
.reply
.poll_reset(cx)
.map_err(|e| crate::Error::new_h2(e))?
{
debug!("stream received RST_STREAM: {:?}", reason);
return Poll::Ready(Err(crate::Error::new_h2(reason.into())));
@@ -274,7 +278,7 @@ where
warn!("http2 service errored: {}", err);
me.reply.send_reset(err.h2_reason());
return Poll::Ready(Err(err));
},
}
};
let (head, body) = res.into_parts();
@@ -282,13 +286,10 @@ where
super::strip_connection_headers(res.headers_mut(), false);
// set Date header if it isn't already set...
res
.headers_mut()
res.headers_mut()
.entry(::http::header::DATE)
.or_insert_with(crate::proto::h1::date::update_and_header_value);
// automatically set Content-Length from body...
if let Some(len) = body.size_hint().exact() {
headers::set_content_length_if_missing(res.headers_mut(), len);
@@ -301,7 +302,7 @@ where
reply!(me, res, true);
return Poll::Ready(Ok(()));
}
},
}
H2StreamState::Body(ref mut pipe) => {
return Pin::new(pipe).poll(cx);
}