Skip to content

Commit 19d92d1

Browse files
authored
refactor(client): properly set legacy client feature attributes (#21)
1 parent 95bb0d9 commit 19d92d1

File tree

2 files changed

+96
-38
lines changed

2 files changed

+96
-38
lines changed

src/client/legacy.rs

Lines changed: 95 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ where
453453
}
454454
}
455455

456+
#[cfg(any(feature = "http1", feature = "http2"))]
456457
fn connect_to(
457458
&self,
458459
pool_key: PoolKey,
@@ -513,37 +514,46 @@ where
513514

514515
Either::Left(Box::pin(async move {
515516
let tx = if is_h2 {
516-
let (mut tx, conn) =
517-
h2_builder.handshake(io).await.map_err(Error::tx)?;
518-
519-
trace!(
520-
"http2 handshake complete, spawning background dispatcher task"
521-
);
522-
executor.execute(
523-
conn.map_err(|e| debug!("client connection error: {}", e))
524-
.map(|_| ()),
525-
);
526-
527-
// Wait for 'conn' to ready up before we
528-
// declare this tx as usable
529-
tx.ready().await.map_err(Error::tx)?;
530-
PoolTx::Http2(tx)
517+
#[cfg(feature = "http2")] {
518+
let (mut tx, conn) =
519+
h2_builder.handshake(io).await.map_err(Error::tx)?;
520+
521+
trace!(
522+
"http2 handshake complete, spawning background dispatcher task"
523+
);
524+
executor.execute(
525+
conn.map_err(|e| debug!("client connection error: {}", e))
526+
.map(|_| ()),
527+
);
528+
529+
// Wait for 'conn' to ready up before we
530+
// declare this tx as usable
531+
tx.ready().await.map_err(Error::tx)?;
532+
PoolTx::Http2(tx)
533+
}
534+
#[cfg(not(feature = "http2"))]
535+
panic!("http2 feature is not enabled");
531536
} else {
532-
let (mut tx, conn) =
533-
h1_builder.handshake(io).await.map_err(Error::tx)?;
534-
535-
trace!(
536-
"http1 handshake complete, spawning background dispatcher task"
537-
);
538-
executor.execute(
539-
conn.map_err(|e| debug!("client connection error: {}", e))
540-
.map(|_| ()),
541-
);
542-
543-
// Wait for 'conn' to ready up before we
544-
// declare this tx as usable
545-
tx.ready().await.map_err(Error::tx)?;
546-
PoolTx::Http1(tx)
537+
#[cfg(feature = "http1")] {
538+
let (mut tx, conn) =
539+
h1_builder.handshake(io).await.map_err(Error::tx)?;
540+
541+
trace!(
542+
"http1 handshake complete, spawning background dispatcher task"
543+
);
544+
executor.execute(
545+
conn.map_err(|e| debug!("client connection error: {}", e))
546+
.map(|_| ()),
547+
);
548+
549+
// Wait for 'conn' to ready up before we
550+
// declare this tx as usable
551+
tx.ready().await.map_err(Error::tx)?;
552+
PoolTx::Http1(tx)
553+
}
554+
#[cfg(not(feature = "http1"))] {
555+
panic!("http1 feature is not enabled");
556+
}
547557
};
548558

549559
Ok(pool.pooled(
@@ -605,7 +615,9 @@ impl<C: Clone, B> Clone for Client<C, B> {
605615
Client {
606616
config: self.config.clone(),
607617
exec: self.exec.clone(),
618+
#[cfg(feature = "http1")]
608619
h1_builder: self.h1_builder.clone(),
620+
#[cfg(feature = "http2")]
609621
h2_builder: self.h2_builder.clone(),
610622
connector: self.connector.clone(),
611623
pool: self.pool.clone(),
@@ -661,14 +673,19 @@ struct PoolClient<B> {
661673
}
662674

663675
enum PoolTx<B> {
676+
#[cfg(feature = "http1")]
664677
Http1(hyper::client::conn::http1::SendRequest<B>),
665678
#[cfg(feature = "http2")]
666679
Http2(hyper::client::conn::http2::SendRequest<B>),
667680
}
668681

669682
impl<B> PoolClient<B> {
670-
fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll<Result<(), Error>> {
683+
fn poll_ready(
684+
&mut self,
685+
#[allow(unused_variables)] cx: &mut task::Context<'_>,
686+
) -> Poll<Result<(), Error>> {
671687
match self.tx {
688+
#[cfg(feature = "http1")]
672689
PoolTx::Http1(ref mut tx) => tx.poll_ready(cx).map_err(|_| todo!()),
673690
#[cfg(feature = "http2")]
674691
PoolTx::Http2(_) => Poll::Ready(Ok(())),
@@ -681,6 +698,7 @@ impl<B> PoolClient<B> {
681698

682699
fn is_http2(&self) -> bool {
683700
match self.tx {
701+
#[cfg(feature = "http1")]
684702
PoolTx::Http1(_) => false,
685703
#[cfg(feature = "http2")]
686704
PoolTx::Http2(_) => true,
@@ -689,6 +707,7 @@ impl<B> PoolClient<B> {
689707

690708
fn is_ready(&self) -> bool {
691709
match self.tx {
710+
#[cfg(feature = "http1")]
692711
PoolTx::Http1(ref tx) => tx.is_ready(),
693712
#[cfg(feature = "http2")]
694713
PoolTx::Http2(ref tx) => tx.is_ready(),
@@ -697,6 +716,7 @@ impl<B> PoolClient<B> {
697716

698717
fn is_closed(&self) -> bool {
699718
match self.tx {
719+
#[cfg(feature = "http1")]
700720
PoolTx::Http1(ref tx) => tx.is_closed(),
701721
#[cfg(feature = "http2")]
702722
PoolTx::Http2(ref tx) => tx.is_closed(),
@@ -712,15 +732,30 @@ impl<B: Body + 'static> PoolClient<B> {
712732
where
713733
B: Send,
714734
{
715-
match self.tx {
716-
#[cfg(not(feature = "http2"))]
717-
PoolTx::Http1(ref mut tx) => tx.send_request(req),
718-
#[cfg(feature = "http2")]
735+
#[cfg(all(feature = "http1", feature = "http2"))]
736+
return match self.tx {
737+
#[cfg(feature = "http1")]
719738
PoolTx::Http1(ref mut tx) => Either::Left(tx.send_request(req)),
720739
#[cfg(feature = "http2")]
721740
PoolTx::Http2(ref mut tx) => Either::Right(tx.send_request(req)),
722741
}
723-
.map_err(|_| todo!())
742+
.map_err(|_| todo!());
743+
744+
#[cfg(feature = "http1")]
745+
#[cfg(not(feature = "http2"))]
746+
return match self.tx {
747+
#[cfg(feature = "http1")]
748+
PoolTx::Http1(ref mut tx) => tx.send_request(req),
749+
}
750+
.map_err(|_| todo!());
751+
752+
#[cfg(not(feature = "http1"))]
753+
#[cfg(feature = "http2")]
754+
return match self.tx {
755+
#[cfg(feature = "http2")]
756+
PoolTx::Http2(ref mut tx) => tx.send_request(req),
757+
}
758+
.map_err(|_| todo!());
724759
}
725760
/*
726761
//TODO: can we re-introduce this somehow? Or must people use tower::retry?
@@ -734,7 +769,7 @@ impl<B: Body + 'static> PoolClient<B> {
734769
match self.tx {
735770
#[cfg(not(feature = "http2"))]
736771
PoolTx::Http1(ref mut tx) => tx.send_request_retryable(req),
737-
#[cfg(feature = "http2")]
772+
#[cfg(feature = "http1")]
738773
PoolTx::Http1(ref mut tx) => Either::Left(tx.send_request_retryable(req)),
739774
#[cfg(feature = "http2")]
740775
PoolTx::Http2(ref mut tx) => Either::Right(tx.send_request_retryable(req)),
@@ -753,6 +788,7 @@ where
753788

754789
fn reserve(self) -> pool::Reservation<Self> {
755790
match self.tx {
791+
#[cfg(feature = "http1")]
756792
PoolTx::Http1(tx) => pool::Reservation::Unique(PoolClient {
757793
conn_info: self.conn_info,
758794
tx: PoolTx::Http1(tx),
@@ -915,7 +951,9 @@ fn is_schema_secure(uri: &Uri) -> bool {
915951
pub struct Builder {
916952
client_config: Config,
917953
exec: Exec,
954+
#[cfg(feature = "http1")]
918955
h1_builder: hyper::client::conn::http1::Builder,
956+
#[cfg(feature = "http2")]
919957
h2_builder: hyper::client::conn::http2::Builder,
920958
pool_config: pool::Config,
921959
}
@@ -934,7 +972,9 @@ impl Builder {
934972
ver: Ver::Auto,
935973
},
936974
exec,
975+
#[cfg(feature = "http1")]
937976
h1_builder: hyper::client::conn::http1::Builder::new(),
977+
#[cfg(feature = "http2")]
938978
h2_builder: hyper::client::conn::http2::Builder::new(executor),
939979
pool_config: pool::Config {
940980
idle_timeout: Some(Duration::from_secs(90)),
@@ -977,6 +1017,8 @@ impl Builder {
9771017
/// Note that setting this option unsets the `http1_max_buf_size` option.
9781018
///
9791019
/// Default is an adaptive read buffer.
1020+
#[cfg(feature = "http1")]
1021+
#[cfg_attr(docsrs, doc(cfg(feature = "http1")))]
9801022
pub fn http1_read_buf_exact_size(&mut self, sz: usize) -> &mut Self {
9811023
self.h1_builder.read_buf_exact_size(Some(sz));
9821024
self
@@ -1020,6 +1062,8 @@ impl Builder {
10201062
/// Default is false.
10211063
///
10221064
/// [RFC 7230 Section 3.2.4.]: https://tools.ietf.org/html/rfc7230#section-3.2.4
1065+
#[cfg(feature = "http1")]
1066+
#[cfg_attr(docsrs, doc(cfg(feature = "http1")))]
10231067
pub fn http1_allow_spaces_after_header_name_in_responses(&mut self, val: bool) -> &mut Self {
10241068
self.h1_builder
10251069
.allow_spaces_after_header_name_in_responses(val);
@@ -1057,6 +1101,8 @@ impl Builder {
10571101
/// Default is false.
10581102
///
10591103
/// [RFC 7230 Section 3.2.4.]: https://tools.ietf.org/html/rfc7230#section-3.2.4
1104+
#[cfg(feature = "http1")]
1105+
#[cfg_attr(docsrs, doc(cfg(feature = "http1")))]
10601106
pub fn http1_allow_obsolete_multiline_headers_in_responses(&mut self, val: bool) -> &mut Self {
10611107
self.h1_builder
10621108
.allow_obsolete_multiline_headers_in_responses(val);
@@ -1087,6 +1133,8 @@ impl Builder {
10871133
/// line in the input to resume parsing the rest of the headers. An error
10881134
/// will be emitted nonetheless if it finds `\0` or a lone `\r` while
10891135
/// looking for the next line.
1136+
#[cfg(feature = "http1")]
1137+
#[cfg_attr(docsrs, doc(cfg(feature = "http1")))]
10901138
pub fn http1_ignore_invalid_headers_in_responses(&mut self, val: bool) -> &mut Builder {
10911139
self.h1_builder.ignore_invalid_headers_in_responses(val);
10921140
self
@@ -1104,6 +1152,8 @@ impl Builder {
11041152
///
11051153
/// Default is `auto`. In this mode hyper will try to guess which
11061154
/// mode to use
1155+
#[cfg(feature = "http1")]
1156+
#[cfg_attr(docsrs, doc(cfg(feature = "http1")))]
11071157
pub fn http1_writev(&mut self, enabled: bool) -> &mut Builder {
11081158
self.h1_builder.writev(enabled);
11091159
self
@@ -1115,6 +1165,8 @@ impl Builder {
11151165
/// Note that this setting does not affect HTTP/2.
11161166
///
11171167
/// Default is false.
1168+
#[cfg(feature = "http1")]
1169+
#[cfg_attr(docsrs, doc(cfg(feature = "http1")))]
11181170
pub fn http1_title_case_headers(&mut self, val: bool) -> &mut Self {
11191171
self.h1_builder.title_case_headers(val);
11201172
self
@@ -1133,6 +1185,8 @@ impl Builder {
11331185
/// Note that this setting does not affect HTTP/2.
11341186
///
11351187
/// Default is false.
1188+
#[cfg(feature = "http1")]
1189+
#[cfg_attr(docsrs, doc(cfg(feature = "http1")))]
11361190
pub fn http1_preserve_header_case(&mut self, val: bool) -> &mut Self {
11371191
self.h1_builder.preserve_header_case(val);
11381192
self
@@ -1141,6 +1195,8 @@ impl Builder {
11411195
/// Set whether HTTP/0.9 responses should be tolerated.
11421196
///
11431197
/// Default is false.
1198+
#[cfg(feature = "http1")]
1199+
#[cfg_attr(docsrs, doc(cfg(feature = "http1")))]
11441200
pub fn http09_responses(&mut self, val: bool) -> &mut Self {
11451201
self.h1_builder.http09_responses(val);
11461202
self
@@ -1358,7 +1414,9 @@ impl Builder {
13581414
Client {
13591415
config: self.client_config,
13601416
exec: self.exec.clone(),
1417+
#[cfg(feature = "http1")]
13611418
h1_builder: self.h1_builder.clone(),
1419+
#[cfg(feature = "http2")]
13621420
h2_builder: self.h2_builder.clone(),
13631421
connector,
13641422
pool: pool::Pool::new(self.pool_config, &self.exec),

src/client/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
//mod client;
44
pub mod connect;
5-
#[cfg(all(feature = "http1", feature = "http2"))]
5+
#[cfg(any(feature = "http1", feature = "http2"))]
66
pub mod legacy;
77
#[doc(hidden)]
88
pub mod pool;

0 commit comments

Comments
 (0)