From cd6af662a48caf5834d899dae25bb3388cfe183e Mon Sep 17 00:00:00 2001 From: tottoto Date: Fri, 13 Oct 2023 04:01:02 +0900 Subject: [PATCH] refactor(lib): resolve unused warning (#3344) In CI, check unused_imports and dead_code --- .github/workflows/CI.yml | 2 + src/body/length.rs | 4 +- src/body/mod.rs | 2 +- src/client/dispatch.rs | 17 +++-- src/common/buf.rs | 1 - src/common/io/mod.rs | 4 +- src/common/mod.rs | 9 ++- src/common/task.rs | 6 +- src/common/time.rs | 9 ++- src/error.rs | 142 +++++++++++++++++++++++++----------- src/ext/h1_reason_phrase.rs | 1 + src/ext/mod.rs | 14 +++- src/headers.rs | 19 +++-- src/proto/h1/conn.rs | 4 +- src/proto/h1/dispatch.rs | 4 +- src/proto/h1/role.rs | 18 ++++- src/proto/mod.rs | 2 + src/rt/io.rs | 4 + src/server/conn/http1.rs | 3 +- src/server/conn/http2.rs | 3 +- src/upgrade.rs | 22 ++++-- 21 files changed, 199 insertions(+), 91 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 43891ee66d..922a27121a 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -146,6 +146,8 @@ jobs: - name: check --feature-powerset run: cargo hack --no-dev-deps check --feature-powerset --depth 2 --skip ffi,tracing + env: + RUSTFLAGS: "-D dead_code -D unused_imports" ffi: name: Test C API (FFI) diff --git a/src/body/length.rs b/src/body/length.rs index 2e46e4b309..54c6dcd3e5 100644 --- a/src/body/length.rs +++ b/src/body/length.rs @@ -33,7 +33,7 @@ impl DecodedLength { /// Should only be called if previously confirmed this isn't /// CLOSE_DELIMITED or CHUNKED. #[inline] - #[cfg(feature = "http1")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] pub(crate) fn danger_len(self) -> u64 { debug_assert!(self.0 < Self::CHUNKED.0); self.0 @@ -72,7 +72,7 @@ impl DecodedLength { /// This includes 0, which of course is an exact known length. /// /// It would return false if "chunked" or otherwise size-unknown. - #[cfg(feature = "http2")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] pub(crate) fn is_exact(&self) -> bool { self.0 <= MAX_LEN } diff --git a/src/body/mod.rs b/src/body/mod.rs index 54f85c173c..4b0f1ed7b1 100644 --- a/src/body/mod.rs +++ b/src/body/mod.rs @@ -26,7 +26,7 @@ pub use http_body::SizeHint; pub use self::incoming::Incoming; -#[cfg(feature = "http1")] +#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] pub(crate) use self::incoming::Sender; pub(crate) use self::length::DecodedLength; diff --git a/src/client/dispatch.rs b/src/client/dispatch.rs index ef9bce181c..c4347d0614 100644 --- a/src/client/dispatch.rs +++ b/src/client/dispatch.rs @@ -1,17 +1,17 @@ #[cfg(feature = "http2")] use std::future::Future; +#[cfg(feature = "http2")] use http::{Request, Response}; +#[cfg(feature = "http2")] use http_body::Body; +#[cfg(feature = "http2")] use pin_project_lite::pin_project; use tokio::sync::{mpsc, oneshot}; -use crate::{ - body::Incoming, - common::{task, Poll}, -}; +use crate::common::{task, Poll}; #[cfg(feature = "http2")] -use crate::{common::Pin, proto::h2::client::ResponseFutMap}; +use crate::{body::Incoming, common::Pin, proto::h2::client::ResponseFutMap}; #[cfg(test)] pub(crate) type RetryPromise = oneshot::Receiver)>>; @@ -21,6 +21,7 @@ pub(crate) fn channel() -> (Sender, Receiver) { let (tx, rx) = mpsc::unbounded_channel(); let (giver, taker) = want::new(); let tx = Sender { + #[cfg(feature = "http1")] buffered_once: false, giver, inner: tx, @@ -37,6 +38,7 @@ pub(crate) struct Sender { /// One message is always allowed, even if the Receiver hasn't asked /// for it yet. This boolean keeps track of whether we've sent one /// without notice. + #[cfg(feature = "http1")] buffered_once: bool, /// The Giver helps watch that the the Receiver side has been polled /// when the queue is empty. This helps us know when a request and @@ -59,20 +61,24 @@ pub(crate) struct UnboundedSender { } impl Sender { + #[cfg(feature = "http1")] pub(crate) fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll> { self.giver .poll_want(cx) .map_err(|_| crate::Error::new_closed()) } + #[cfg(feature = "http1")] pub(crate) fn is_ready(&self) -> bool { self.giver.is_wanting() } + #[cfg(feature = "http1")] pub(crate) fn is_closed(&self) -> bool { self.giver.is_canceled() } + #[cfg(feature = "http1")] fn can_send(&mut self) -> bool { if self.giver.give() || !self.buffered_once { // If the receiver is ready *now*, then of course we can send. @@ -98,6 +104,7 @@ impl Sender { .map_err(|mut e| (e.0).0.take().expect("envelope not dropped").0) } + #[cfg(feature = "http1")] pub(crate) fn send(&mut self, val: T) -> Result, T> { if !self.can_send() { return Err(val); diff --git a/src/common/buf.rs b/src/common/buf.rs index 64e9333ead..d00071551b 100644 --- a/src/common/buf.rs +++ b/src/common/buf.rs @@ -21,7 +21,6 @@ impl BufList { } #[inline] - #[cfg(feature = "http1")] pub(crate) fn bufs_cnt(&self) -> usize { self.bufs.len() } diff --git a/src/common/io/mod.rs b/src/common/io/mod.rs index 6ad07bb771..92f71c4ee6 100644 --- a/src/common/io/mod.rs +++ b/src/common/io/mod.rs @@ -1,7 +1,7 @@ -#[cfg(any(feature = "http2", test))] +#[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] mod compat; mod rewind; -#[cfg(any(feature = "http2", test))] +#[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] pub(crate) use self::compat::{compat, Compat}; pub(crate) use self::rewind::Rewind; diff --git a/src/common/mod.rs b/src/common/mod.rs index 632b363e2b..d57d059a1c 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -7,6 +7,7 @@ macro_rules! ready { }; } +#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] pub(crate) mod buf; #[cfg(all(feature = "server", any(feature = "http1", feature = "http2")))] pub(crate) mod date; @@ -14,14 +15,14 @@ pub(crate) mod date; pub(crate) mod exec; pub(crate) mod io; pub(crate) mod task; -#[cfg(any(feature = "http1", feature = "http2", feature = "server"))] +#[cfg(any( + all(feature = "server", feature = "http1"), + all(any(feature = "client", feature = "server"), feature = "http2"), +))] pub(crate) mod time; pub(crate) mod watch; pub(crate) use self::task::Poll; // group up types normally needed for `Future` -cfg_proto! { - pub(crate) use std::marker::Unpin; -} pub(crate) use std::{future::Future, pin::Pin}; diff --git a/src/common/task.rs b/src/common/task.rs index 10a7802bdd..9a296c4cfc 100644 --- a/src/common/task.rs +++ b/src/common/task.rs @@ -1,12 +1,10 @@ -#[cfg(feature = "http1")] -use std::convert::Infallible; pub(crate) use std::task::{Context, Poll}; /// A function to help "yield" a future, such that it is re-scheduled immediately. /// /// Useful for spin counts, so a future doesn't hog too much time. -#[cfg(feature = "http1")] -pub(crate) fn yield_now(cx: &mut Context<'_>) -> Poll { +#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] +pub(crate) fn yield_now(cx: &mut Context<'_>) -> Poll { cx.waker().wake_by_ref(); Poll::Pending } diff --git a/src/common/time.rs b/src/common/time.rs index 09cb0ae95d..9dff12534d 100644 --- a/src/common/time.rs +++ b/src/common/time.rs @@ -1,8 +1,7 @@ +#[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] +use std::time::Duration; use std::{fmt, sync::Arc}; -use std::{ - pin::Pin, - time::{Duration, Instant}, -}; +use std::{pin::Pin, time::Instant}; use crate::rt::Sleep; use crate::rt::Timer; @@ -55,6 +54,7 @@ impl Future for HyperTimeout where F: Future { */ impl Time { + #[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] pub(crate) fn sleep(&self, duration: Duration) -> Pin> { match *self { Time::Empty => { @@ -64,6 +64,7 @@ impl Time { } } + #[cfg(feature = "http1")] pub(crate) fn sleep_until(&self, deadline: Instant) -> Pin> { match *self { Time::Empty => { diff --git a/src/error.rs b/src/error.rs index a51916359d..719bd2a304 100644 --- a/src/error.rs +++ b/src/error.rs @@ -45,43 +45,55 @@ pub(super) enum Kind { #[allow(unused)] IncompleteMessage, /// A connection received a message (or bytes) when not waiting for one. - #[cfg(feature = "http1")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] UnexpectedMessage, /// A pending item was dropped before ever being processed. Canceled, /// Indicates a channel (client or body sender) is closed. ChannelClosed, /// An `io::Error` that occurred while trying to read or write to a network stream. - #[cfg(any(feature = "http1", feature = "http2"))] + #[cfg(all( + any(feature = "client", feature = "server"), + any(feature = "http1", feature = "http2") + ))] Io, /// User took too long to send headers #[cfg(all(feature = "http1", feature = "server"))] HeaderTimeout, /// Error while reading a body from connection. - #[cfg(any(feature = "http1", feature = "http2"))] + #[cfg(all( + any(feature = "client", feature = "server"), + any(feature = "http1", feature = "http2") + ))] Body, /// Error while writing a body to connection. - #[cfg(any(feature = "http1", feature = "http2"))] + #[cfg(all( + any(feature = "client", feature = "server"), + any(feature = "http1", feature = "http2") + ))] BodyWrite, /// Error calling AsyncWrite::shutdown() - #[cfg(feature = "http1")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] Shutdown, /// A general error from h2. - #[cfg(feature = "http2")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] Http2, } #[derive(Debug)] pub(super) enum Parse { Method, - Version, #[cfg(feature = "http1")] + Version, + #[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] VersionH2, Uri, - #[cfg_attr(not(all(feature = "http1", feature = "server")), allow(unused))] + #[cfg(all(feature = "http1", feature = "server"))] UriTooLong, + #[cfg(feature = "http1")] Header(Header), + #[cfg(any(feature = "http1", feature = "http2"))] TooLarge, Status, #[cfg_attr(debug_assertions, allow(unused))] @@ -89,25 +101,32 @@ pub(super) enum Parse { } #[derive(Debug)] +#[cfg(feature = "http1")] pub(super) enum Header { Token, - #[cfg(feature = "http1")] + #[cfg(all(any(feature = "client", feature = "server")))] ContentLengthInvalid, - #[cfg(all(feature = "http1", feature = "server"))] + #[cfg(feature = "server")] TransferEncodingInvalid, - #[cfg(feature = "http1")] + #[cfg(any(feature = "client", feature = "server"))] TransferEncodingUnexpected, } #[derive(Debug)] pub(super) enum User { /// Error calling user's Body::poll_data(). - #[cfg(any(feature = "http1", feature = "http2"))] + #[cfg(all( + any(feature = "client", feature = "server"), + any(feature = "http1", feature = "http2") + ))] Body, /// The user aborted writing of the outgoing body. BodyWriteAborted, /// Error from future of user's Service. - #[cfg(any(feature = "http1", feature = "http2"))] + #[cfg(any( + all(any(feature = "client", feature = "server"), feature = "http1"), + all(feature = "server", feature = "http2") + ))] Service, /// User tried to send a certain header in an unexpected context. /// @@ -124,11 +143,11 @@ pub(super) enum User { NoUpgrade, /// User polled for an upgrade, but low-level API is not using upgrades. - #[cfg(feature = "http1")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] ManualUpgrade, /// The dispatch task is gone. - #[cfg(feature = "client")] + #[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))] DispatchGone, /// User aborted in an FFI callback. @@ -147,6 +166,7 @@ impl Error { } /// Returns true if this was an HTTP parse error caused by a message that was too large. + #[cfg(all(feature = "http1", feature = "server"))] pub fn is_parse_too_large(&self) -> bool { matches!( self.inner.kind, @@ -219,7 +239,7 @@ impl Error { None } - #[cfg(feature = "http2")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] pub(super) fn h2_reason(&self) -> h2::Reason { // Find an h2::Reason somewhere in the cause stack, if it exists, // otherwise assume an INTERNAL_ERROR. @@ -232,27 +252,30 @@ impl Error { Error::new(Kind::Canceled) } - #[cfg(feature = "http1")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] pub(super) fn new_incomplete() -> Error { Error::new(Kind::IncompleteMessage) } - #[cfg(feature = "http1")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] pub(super) fn new_too_large() -> Error { Error::new(Kind::Parse(Parse::TooLarge)) } - #[cfg(feature = "http1")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] pub(super) fn new_version_h2() -> Error { Error::new(Kind::Parse(Parse::VersionH2)) } - #[cfg(feature = "http1")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] pub(super) fn new_unexpected_message() -> Error { Error::new(Kind::UnexpectedMessage) } - #[cfg(any(feature = "http1", feature = "http2"))] + #[cfg(all( + any(feature = "client", feature = "server"), + any(feature = "http1", feature = "http2") + ))] pub(super) fn new_io(cause: std::io::Error) -> Error { Error::new(Kind::Io).with(cause) } @@ -261,12 +284,18 @@ impl Error { Error::new(Kind::ChannelClosed) } - #[cfg(any(feature = "http1", feature = "http2"))] + #[cfg(all( + any(feature = "client", feature = "server"), + any(feature = "http1", feature = "http2") + ))] pub(super) fn new_body>(cause: E) -> Error { Error::new(Kind::Body).with(cause) } - #[cfg(any(feature = "http1", feature = "http2"))] + #[cfg(all( + any(feature = "client", feature = "server"), + any(feature = "http1", feature = "http2") + ))] pub(super) fn new_body_write>(cause: E) -> Error { Error::new(Kind::BodyWrite).with(cause) } @@ -300,22 +329,28 @@ impl Error { Error::new_user(User::NoUpgrade) } - #[cfg(feature = "http1")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] pub(super) fn new_user_manual_upgrade() -> Error { Error::new_user(User::ManualUpgrade) } - #[cfg(any(feature = "http1", feature = "http2"))] + #[cfg(any( + all(any(feature = "client", feature = "server"), feature = "http1"), + all(feature = "server", feature = "http2") + ))] pub(super) fn new_user_service>(cause: E) -> Error { Error::new_user(User::Service).with(cause) } - #[cfg(any(feature = "http1", feature = "http2"))] + #[cfg(all( + any(feature = "client", feature = "server"), + any(feature = "http1", feature = "http2") + ))] pub(super) fn new_user_body>(cause: E) -> Error { Error::new_user(User::Body).with(cause) } - #[cfg(feature = "http1")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] pub(super) fn new_shutdown(cause: std::io::Error) -> Error { Error::new(Kind::Shutdown).with(cause) } @@ -325,12 +360,12 @@ impl Error { Error::new_user(User::AbortedByCallback) } - #[cfg(feature = "client")] + #[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))] pub(super) fn new_user_dispatch_gone() -> Error { Error::new(Kind::User(User::DispatchGone)) } - #[cfg(feature = "http2")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] pub(super) fn new_h2(cause: ::h2::Error) -> Error { if cause.is_io() { Error::new_io(cause.into_io().expect("h2::Error::is_io")) @@ -342,13 +377,16 @@ impl Error { fn description(&self) -> &str { match self.inner.kind { Kind::Parse(Parse::Method) => "invalid HTTP method parsed", - Kind::Parse(Parse::Version) => "invalid HTTP version parsed", #[cfg(feature = "http1")] + Kind::Parse(Parse::Version) => "invalid HTTP version parsed", + #[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] Kind::Parse(Parse::VersionH2) => "invalid HTTP version parsed (found HTTP2 preface)", Kind::Parse(Parse::Uri) => "invalid URI", + #[cfg(all(feature = "http1", feature = "server"))] Kind::Parse(Parse::UriTooLong) => "URI too long", - Kind::Parse(Parse::Header(Header::Token)) => "invalid HTTP header parsed", #[cfg(feature = "http1")] + Kind::Parse(Parse::Header(Header::Token)) => "invalid HTTP header parsed", + #[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] Kind::Parse(Parse::Header(Header::ContentLengthInvalid)) => { "invalid content-length parsed" } @@ -356,37 +394,53 @@ impl Error { Kind::Parse(Parse::Header(Header::TransferEncodingInvalid)) => { "invalid transfer-encoding parsed" } - #[cfg(feature = "http1")] + #[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] Kind::Parse(Parse::Header(Header::TransferEncodingUnexpected)) => { "unexpected transfer-encoding parsed" } + #[cfg(any(feature = "http1", feature = "http2"))] Kind::Parse(Parse::TooLarge) => "message head is too large", Kind::Parse(Parse::Status) => "invalid HTTP status-code parsed", Kind::Parse(Parse::Internal) => { "internal error inside Hyper and/or its dependencies, please report" } Kind::IncompleteMessage => "connection closed before message completed", - #[cfg(feature = "http1")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] Kind::UnexpectedMessage => "received unexpected message from connection", Kind::ChannelClosed => "channel closed", Kind::Canceled => "operation was canceled", #[cfg(all(feature = "http1", feature = "server"))] Kind::HeaderTimeout => "read header from client timeout", - #[cfg(any(feature = "http1", feature = "http2"))] + #[cfg(all( + any(feature = "client", feature = "server"), + any(feature = "http1", feature = "http2") + ))] Kind::Body => "error reading a body from connection", - #[cfg(any(feature = "http1", feature = "http2"))] + #[cfg(all( + any(feature = "client", feature = "server"), + any(feature = "http1", feature = "http2") + ))] Kind::BodyWrite => "error writing a body to connection", - #[cfg(feature = "http1")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] Kind::Shutdown => "error shutting down connection", - #[cfg(feature = "http2")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] Kind::Http2 => "http2 error", - #[cfg(any(feature = "http1", feature = "http2"))] + #[cfg(all( + any(feature = "client", feature = "server"), + any(feature = "http1", feature = "http2") + ))] Kind::Io => "connection error", - #[cfg(any(feature = "http1", feature = "http2"))] + #[cfg(all( + any(feature = "client", feature = "server"), + any(feature = "http1", feature = "http2") + ))] Kind::User(User::Body) => "error from user's Body stream", Kind::User(User::BodyWriteAborted) => "user body write aborted", - #[cfg(any(feature = "http1", feature = "http2"))] + #[cfg(any( + all(any(feature = "client", feature = "server"), feature = "http1"), + all(feature = "server", feature = "http2") + ))] Kind::User(User::Service) => "error from user's Service", #[cfg(any(feature = "http1", feature = "http2"))] #[cfg(feature = "server")] @@ -397,9 +451,9 @@ impl Error { "response has 1xx status code, not supported by server" } Kind::User(User::NoUpgrade) => "no upgrade available", - #[cfg(feature = "http1")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] Kind::User(User::ManualUpgrade) => "upgrade expected but low level API in use", - #[cfg(feature = "client")] + #[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))] Kind::User(User::DispatchGone) => "dispatch task is gone", #[cfg(feature = "ffi")] Kind::User(User::AbortedByCallback) => "operation aborted by an application callback", @@ -442,15 +496,17 @@ impl From for Error { #[cfg(feature = "http1")] impl Parse { + #[cfg(any(feature = "client", feature = "server"))] pub(crate) fn content_length_invalid() -> Self { Parse::Header(Header::ContentLengthInvalid) } - #[cfg(all(feature = "http1", feature = "server"))] + #[cfg(feature = "server")] pub(crate) fn transfer_encoding_invalid() -> Self { Parse::Header(Header::TransferEncodingInvalid) } + #[cfg(any(feature = "client", feature = "server"))] pub(crate) fn transfer_encoding_unexpected() -> Self { Parse::Header(Header::TransferEncodingUnexpected) } diff --git a/src/ext/h1_reason_phrase.rs b/src/ext/h1_reason_phrase.rs index 62173baa72..06265c9a3f 100644 --- a/src/ext/h1_reason_phrase.rs +++ b/src/ext/h1_reason_phrase.rs @@ -55,6 +55,7 @@ impl ReasonPhrase { /// /// Use with care; invalid bytes in a reason phrase can cause serious security problems if /// emitted in a response. + #[cfg(feature = "client")] pub(crate) fn from_bytes_unchecked(reason: Bytes) -> Self { Self(reason) } diff --git a/src/ext/mod.rs b/src/ext/mod.rs index a87b115576..8c717e4a49 100644 --- a/src/ext/mod.rs +++ b/src/ext/mod.rs @@ -1,9 +1,12 @@ //! HTTP extensions. use bytes::Bytes; -#[cfg(any(feature = "http1", feature = "ffi"))] +#[cfg(any( + all(any(feature = "client", feature = "server"), feature = "http1"), + feature = "ffi" +))] use http::header::HeaderName; -#[cfg(feature = "http1")] +#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] use http::header::{IntoHeaderName, ValueIter}; use http::HeaderMap; #[cfg(feature = "ffi")] @@ -45,6 +48,7 @@ impl Protocol { Self { inner } } + #[cfg(all(feature = "client", feature = "http2"))] pub(crate) fn into_inner(self) -> h2::ext::Protocol { self.inner } @@ -97,10 +101,11 @@ impl fmt::Debug for Protocol { #[derive(Clone, Debug)] pub(crate) struct HeaderCaseMap(HeaderMap); -#[cfg(feature = "http1")] +#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] impl HeaderCaseMap { /// Returns a view of all spellings associated with that header name, /// in the order they were found. + #[cfg(feature = "client")] pub(crate) fn get_all<'a>( &'a self, name: &HeaderName, @@ -110,10 +115,12 @@ impl HeaderCaseMap { /// Returns a view of all spellings associated with that header name, /// in the order they were found. + #[cfg(any(feature = "client", feature = "server"))] pub(crate) fn get_all_internal<'a>(&'a self, name: &HeaderName) -> ValueIter<'_, Bytes> { self.0.get_all(name).into_iter() } + #[cfg(any(feature = "client", feature = "server"))] pub(crate) fn default() -> Self { Self(Default::default()) } @@ -123,6 +130,7 @@ impl HeaderCaseMap { self.0.insert(name, orig); } + #[cfg(any(feature = "client", feature = "server"))] pub(crate) fn append(&mut self, name: N, orig: Bytes) where N: IntoHeaderName, diff --git a/src/headers.rs b/src/headers.rs index 6fe672de01..5960471bbc 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -1,10 +1,13 @@ -#[cfg(feature = "http1")] +#[cfg(all(feature = "client", feature = "http1"))] use bytes::BytesMut; -use http::header::CONTENT_LENGTH; -use http::header::{HeaderValue, ValueIter}; -use http::HeaderMap; +use http::header::HeaderValue; #[cfg(all(feature = "http2", feature = "client"))] use http::Method; +#[cfg(any(feature = "client", all(feature = "server", feature = "http2")))] +use http::{ + header::{ValueIter, CONTENT_LENGTH}, + HeaderMap, +}; #[cfg(feature = "http1")] pub(super) fn connection_keep_alive(value: &HeaderValue) -> bool { @@ -33,10 +36,12 @@ pub(super) fn content_length_parse(value: &HeaderValue) -> Option { from_digits(value.as_bytes()) } +#[cfg(any(feature = "client", all(feature = "server", feature = "http2")))] pub(super) fn content_length_parse_all(headers: &HeaderMap) -> Option { content_length_parse_all_values(headers.get_all(CONTENT_LENGTH).into_iter()) } +#[cfg(any(feature = "client", all(feature = "server", feature = "http2")))] pub(super) fn content_length_parse_all_values(values: ValueIter<'_, HeaderValue>) -> Option { // If multiple Content-Length headers were sent, everything can still // be alright if they all contain the same value, and all parse @@ -106,12 +111,12 @@ pub(super) fn set_content_length_if_missing(headers: &mut HeaderMap, len: u64) { .or_insert_with(|| HeaderValue::from(len)); } -#[cfg(feature = "http1")] +#[cfg(all(feature = "client", feature = "http1"))] pub(super) fn transfer_encoding_is_chunked(headers: &HeaderMap) -> bool { is_chunked(headers.get_all(http::header::TRANSFER_ENCODING).into_iter()) } -#[cfg(feature = "http1")] +#[cfg(all(feature = "client", feature = "http1"))] pub(super) fn is_chunked(mut encodings: ValueIter<'_, HeaderValue>) -> bool { // chunked must always be the last encoding, according to spec if let Some(line) = encodings.next_back() { @@ -133,7 +138,7 @@ pub(super) fn is_chunked_(value: &HeaderValue) -> bool { false } -#[cfg(feature = "http1")] +#[cfg(all(feature = "client", feature = "http1"))] pub(super) fn add_chunked(mut entry: http::header::OccupiedEntry<'_, HeaderValue>) { const CHUNKED: &str = "chunked"; diff --git a/src/proto/h1/conn.rs b/src/proto/h1/conn.rs index ce7c41a366..4885f9c0dc 100644 --- a/src/proto/h1/conn.rs +++ b/src/proto/h1/conn.rs @@ -1,6 +1,6 @@ use std::fmt; use std::io; -use std::marker::PhantomData; +use std::marker::{PhantomData, Unpin}; #[cfg(feature = "server")] use std::time::Duration; @@ -15,7 +15,7 @@ use super::{Decoder, Encode, EncodedBuf, Encoder, Http1Transaction, ParseContext use crate::body::DecodedLength; #[cfg(feature = "server")] use crate::common::time::Time; -use crate::common::{task, Pin, Poll, Unpin}; +use crate::common::{task, Pin, Poll}; use crate::headers::connection_keep_alive; use crate::proto::{BodyLength, MessageHead}; #[cfg(feature = "server")] diff --git a/src/proto/h1/dispatch.rs b/src/proto/h1/dispatch.rs index 705bc77a4a..7083c46217 100644 --- a/src/proto/h1/dispatch.rs +++ b/src/proto/h1/dispatch.rs @@ -1,4 +1,4 @@ -use std::error::Error as StdError; +use std::{error::Error as StdError, marker::Unpin}; use crate::rt::{Read, Write}; use bytes::{Buf, Bytes}; @@ -6,7 +6,7 @@ use http::Request; use super::{Http1Transaction, Wants}; use crate::body::{Body, DecodedLength, Incoming as IncomingBody}; -use crate::common::{task, Future, Pin, Poll, Unpin}; +use crate::common::{task, Future, Pin, Poll}; use crate::proto::{BodyLength, Conn, Dispatched, MessageHead, RequestHead}; use crate::upgrade::OnUpgrade; diff --git a/src/proto/h1/role.rs b/src/proto/h1/role.rs index f964d2138f..aa958fdc3b 100644 --- a/src/proto/h1/role.rs +++ b/src/proto/h1/role.rs @@ -1,13 +1,17 @@ -use std::fmt::{self, Write}; -use std::mem::MaybeUninit; +use std::{fmt, mem::MaybeUninit}; + +#[cfg(feature = "client")] +use std::fmt::Write; #[cfg(feature = "server")] use std::time::Instant; use bytes::Bytes; use bytes::BytesMut; +#[cfg(feature = "client")] +use http::header::Entry; #[cfg(feature = "server")] use http::header::ValueIter; -use http::header::{self, Entry, HeaderName, HeaderValue}; +use http::header::{self, HeaderName, HeaderValue}; use http::{HeaderMap, Method, StatusCode, Version}; use crate::body::DecodedLength; @@ -21,7 +25,9 @@ use crate::headers; use crate::proto::h1::{ Encode, Encoder, Http1Transaction, ParseContext, ParseResult, ParsedMessage, }; -use crate::proto::{BodyLength, MessageHead, RequestHead, RequestLine}; +#[cfg(feature = "client")] +use crate::proto::RequestHead; +use crate::proto::{BodyLength, MessageHead, RequestLine}; const MAX_HEADERS: usize = 100; const AVERAGE_HEADER_SIZE: usize = 30; // totally scientific @@ -1392,6 +1398,7 @@ impl Client { } } +#[cfg(feature = "client")] fn set_content_length(headers: &mut HeaderMap, len: u64) -> Encoder { // At this point, there should not be a valid Content-Length // header. However, since we'll be indexing in anyways, we can @@ -1478,6 +1485,7 @@ fn title_case(dst: &mut Vec, name: &[u8]) { } } +#[cfg(feature = "client")] fn write_headers_title_case(headers: &HeaderMap, dst: &mut Vec) { for (name, value) in headers { title_case(dst, name.as_str().as_bytes()); @@ -1487,6 +1495,7 @@ fn write_headers_title_case(headers: &HeaderMap, dst: &mut Vec) { } } +#[cfg(feature = "client")] fn write_headers(headers: &HeaderMap, dst: &mut Vec) { for (name, value) in headers { extend(dst, name.as_str().as_bytes()); @@ -1497,6 +1506,7 @@ fn write_headers(headers: &HeaderMap, dst: &mut Vec) { } #[cold] +#[cfg(feature = "client")] fn write_headers_original_case( headers: &HeaderMap, orig_case: &HeaderCaseMap, diff --git a/src/proto/mod.rs b/src/proto/mod.rs index 3628576dc1..714a0d9c21 100644 --- a/src/proto/mod.rs +++ b/src/proto/mod.rs @@ -17,6 +17,7 @@ cfg_feature! { pub(crate) mod h2; /// An Incoming Message head. Includes request/status line, and headers. +#[cfg(any(feature = "http1"))] #[derive(Debug, Default)] pub(crate) struct MessageHead { /// HTTP version of the message. @@ -59,6 +60,7 @@ pub(crate) enum Dispatched { Upgrade(crate::upgrade::Pending), } +#[cfg(all(feature = "client", feature = "http1"))] impl MessageHead { fn into_response(self, body: B) -> http::Response { let mut res = http::Response::new(body); diff --git a/src/rt/io.rs b/src/rt/io.rs index c39e1e098d..bf045c3714 100644 --- a/src/rt/io.rs +++ b/src/rt/io.rs @@ -176,21 +176,25 @@ impl<'data> ReadBuf<'data> { } #[inline] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] pub(crate) unsafe fn set_init(&mut self, n: usize) { self.init = self.init.max(n); } #[inline] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] pub(crate) unsafe fn set_filled(&mut self, n: usize) { self.filled = self.filled.max(n); } #[inline] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] pub(crate) fn len(&self) -> usize { self.filled } #[inline] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] pub(crate) fn init_len(&self) -> usize { self.init } diff --git a/src/server/conn/http1.rs b/src/server/conn/http1.rs index 50629cf4fe..ca7d919e81 100644 --- a/src/server/conn/http1.rs +++ b/src/server/conn/http1.rs @@ -2,6 +2,7 @@ use std::error::Error as StdError; use std::fmt; +use std::marker::Unpin; use std::sync::Arc; use std::time::Duration; @@ -9,7 +10,7 @@ use crate::rt::{Read, Write}; use bytes::Bytes; use crate::body::{Body, Incoming as IncomingBody}; -use crate::common::{task, Future, Pin, Poll, Unpin}; +use crate::common::{task, Future, Pin, Poll}; use crate::proto; use crate::service::HttpService; use crate::{common::time::Time, rt::Timer}; diff --git a/src/server/conn/http2.rs b/src/server/conn/http2.rs index 1cc351134b..acb7247eb4 100644 --- a/src/server/conn/http2.rs +++ b/src/server/conn/http2.rs @@ -2,6 +2,7 @@ use std::error::Error as StdError; use std::fmt; +use std::marker::Unpin; use std::sync::Arc; use std::time::Duration; @@ -9,7 +10,7 @@ use crate::rt::{Read, Write}; use pin_project_lite::pin_project; use crate::body::{Body, Incoming as IncomingBody}; -use crate::common::{task, Future, Pin, Poll, Unpin}; +use crate::common::{task, Future, Pin, Poll}; use crate::proto; use crate::rt::bounds::Http2ConnExec; use crate::service::HttpService; diff --git a/src/upgrade.rs b/src/upgrade.rs index 03a56af6c5..2e5d25959f 100644 --- a/src/upgrade.rs +++ b/src/upgrade.rs @@ -103,12 +103,18 @@ pub fn on(msg: T) -> OnUpgrade { msg.on_upgrade() } -#[cfg(any(feature = "http1", feature = "http2"))] +#[cfg(all( + any(feature = "client", feature = "server"), + any(feature = "http1", feature = "http2"), +))] pub(super) struct Pending { tx: oneshot::Sender>, } -#[cfg(any(feature = "http1", feature = "http2"))] +#[cfg(all( + any(feature = "client", feature = "server"), + any(feature = "http1", feature = "http2"), +))] pub(super) fn pending() -> (Pending, OnUpgrade) { let (tx, rx) = oneshot::channel(); (Pending { tx }, OnUpgrade { rx: Some(rx) }) @@ -117,7 +123,10 @@ pub(super) fn pending() -> (Pending, OnUpgrade) { // ===== impl Upgraded ===== impl Upgraded { - #[cfg(any(feature = "http1", feature = "http2", test))] + #[cfg(all( + any(feature = "client", feature = "server"), + any(feature = "http1", feature = "http2") + ))] pub(super) fn new(io: T, read_buf: Bytes) -> Self where T: Read + Write + Unpin + Send + 'static, @@ -199,7 +208,7 @@ impl OnUpgrade { OnUpgrade { rx: None } } - #[cfg(feature = "http1")] + #[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] pub(super) fn is_none(&self) -> bool { self.rx.is_none() } @@ -228,7 +237,10 @@ impl fmt::Debug for OnUpgrade { // ===== impl Pending ===== -#[cfg(any(feature = "http1", feature = "http2"))] +#[cfg(all( + any(feature = "client", feature = "server"), + any(feature = "http1", feature = "http2") +))] impl Pending { pub(super) fn fulfill(self, upgraded: Upgraded) { trace!("pending upgrade fulfill");