Skip to content

Commit 6a4a4e8

Browse files
updates client tests to explicitly specify http version
1 parent 90d5834 commit 6a4a4e8

File tree

5 files changed

+142
-66
lines changed

5 files changed

+142
-66
lines changed

src/client/conn/http1.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::error::Error as StdError;
44
use std::fmt;
55
use std::sync::Arc;
66

7+
use bytes::Bytes;
78
use http::{Request, Response};
89
use httparse::ParserConfig;
910
use tokio::io::{AsyncRead, AsyncWrite};
@@ -27,6 +28,27 @@ pub struct SendRequest<B> {
2728
dispatch: dispatch::Sender<Request<B>, Response<Body>>,
2829
}
2930

31+
/// Deconstructed parts of a `Connection`.
32+
///
33+
/// This allows taking apart a `Connection` at a later time, in order to
34+
/// reclaim the IO object, and additional related pieces.
35+
#[derive(Debug)]
36+
pub struct Parts<T> {
37+
/// The original IO object used in the handshake.
38+
pub io: T,
39+
/// A buffer of bytes that have been read but not processed as HTTP.
40+
///
41+
/// For instance, if the `Connection` is used for an HTTP upgrade request,
42+
/// it is possible the server sent back the first bytes of the new protocol
43+
/// along with the response upgrade.
44+
///
45+
/// You will want to check for any existing bytes if you plan to continue
46+
/// communicating on the IO object.
47+
pub read_buf: Bytes,
48+
_inner: (),
49+
}
50+
51+
3052
/// A future that processes all HTTP state for the IO object.
3153
///
3254
/// In most cases, this should just be spawned into an executor, so that it
@@ -40,6 +62,44 @@ where
4062
inner: Option<Dispatcher<T, B>>,
4163
}
4264

65+
impl<T, B> Connection<T, B>
66+
where
67+
T: AsyncRead + AsyncWrite + Send + Unpin + 'static,
68+
B: HttpBody + 'static,
69+
B::Error: Into<Box<dyn StdError + Send + Sync>>,
70+
{
71+
72+
73+
/// Return the inner IO object, and additional information.
74+
///
75+
/// Only works for HTTP/1 connections. HTTP/2 connections will panic.
76+
pub fn into_parts(self) -> Parts<T> {
77+
78+
let (io, read_buf, _) = self.inner.expect("already upgraded").into_inner();
79+
Parts {
80+
io,
81+
read_buf,
82+
_inner: (),
83+
}
84+
}
85+
86+
/// Poll the connection for completion, but without calling `shutdown`
87+
/// on the underlying IO.
88+
///
89+
/// This is useful to allow running a connection while doing an HTTP
90+
/// upgrade. Once the upgrade is completed, the connection would be "done",
91+
/// but it is not desired to actually shutdown the IO object. Instead you
92+
/// would take it back using `into_parts`.
93+
///
94+
/// Use [`poll_fn`](https://docs.rs/futures/0.1.25/futures/future/fn.poll_fn.html)
95+
/// and [`try_ready!`](https://docs.rs/futures/0.1.25/futures/macro.try_ready.html)
96+
/// to work with this function; or use the `without_shutdown` wrapper.
97+
pub fn poll_without_shutdown(&mut self, _cx: &mut task::Context<'_>) -> Poll<crate::Result<()>> {
98+
todo!()
99+
// self.inner.as_mut().expect("algready upgraded").poll_without_shutdown(cx)
100+
}
101+
}
102+
43103
/// A builder to configure an HTTP connection.
44104
///
45105
/// After setting options, the builder is used to create a handshake future.

src/proto/h2/client.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,14 @@ where
205205
req_rx: ClientRx<B>,
206206
}
207207

208-
impl<B> ClientTask<B>
209-
where
210-
B: HttpBody + 'static,
211-
{
212-
pub(crate) fn is_extended_connect_protocol_enabled(&self) -> bool {
213-
self.h2_tx.is_extended_connect_protocol_enabled()
214-
}
215-
}
208+
// impl<B> ClientTask<B>
209+
// where
210+
// B: HttpBody + 'static,
211+
// {
212+
// pub(crate) fn is_extended_connect_protocol_enabled(&self) -> bool {
213+
// self.h2_tx.is_extended_connect_protocol_enabled()
214+
// }
215+
// }
216216

217217
impl<B> Future for ClientTask<B>
218218
where

tests/client.rs

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ macro_rules! test {
232232
// Wrapper around hyper::client::conn::Builder with set_host field to mimic
233233
// hyper::client::Builder.
234234
struct Builder {
235-
inner: hyper::client::conn::Builder,
235+
inner: hyper::client::conn::http1::Builder,
236236
set_host: bool,
237237
http09_responses: bool,
238238
http2_only: bool,
@@ -241,7 +241,7 @@ macro_rules! test {
241241
impl Builder {
242242
fn new() -> Self {
243243
Self {
244-
inner: hyper::client::conn::Builder::new(),
244+
inner: hyper::client::conn::http1::Builder::new(),
245245
set_host: true,
246246
http09_responses: false,
247247
http2_only: false,
@@ -261,16 +261,16 @@ macro_rules! test {
261261
self
262262
}
263263

264-
#[allow(unused)]
265-
fn http2_only(&mut self, val: bool) -> &mut Self {
266-
self.http2_only = val;
267-
self.inner.http2_only(val);
268-
self
269-
}
264+
// #[allow(unused)]
265+
// fn http2_only(&mut self, val: bool) -> &mut Self {
266+
// self.http2_only = val;
267+
// self.inner.http2_only(val);
268+
// self
269+
// }
270270
}
271271

272272
impl std::ops::Deref for Builder {
273-
type Target = hyper::client::conn::Builder;
273+
type Target = hyper::client::conn::http1::Builder;
274274

275275
fn deref(&self) -> &Self::Target {
276276
&self.inner
@@ -1371,7 +1371,7 @@ mod conn {
13711371

13721372
let client = async move {
13731373
let tcp = tcp_connect(&addr).await.expect("connect");
1374-
let (mut client, conn) = conn::handshake(tcp).await.expect("handshake");
1374+
let (mut client, conn) = conn::http1::handshake(tcp).await.expect("handshake");
13751375

13761376
tokio::task::spawn(async move {
13771377
conn.await.expect("http conn");
@@ -1415,7 +1415,7 @@ mod conn {
14151415

14161416
let client = async move {
14171417
let tcp = tcp_connect(&addr).await.expect("connect");
1418-
let (mut client, conn) = conn::handshake(tcp).await.expect("handshake");
1418+
let (mut client, conn) = conn::http1::handshake(tcp).await.expect("handshake");
14191419

14201420
tokio::task::spawn(async move {
14211421
conn.await.expect("http conn");
@@ -1473,7 +1473,7 @@ mod conn {
14731473

14741474
let tcp = rt.block_on(tcp_connect(&addr)).unwrap();
14751475

1476-
let (mut client, conn) = rt.block_on(conn::handshake(tcp)).unwrap();
1476+
let (mut client, conn) = rt.block_on(conn::http1::handshake(tcp)).unwrap();
14771477

14781478
rt.spawn(conn.map_err(|e| panic!("conn error: {}", e)).map(|_| ()));
14791479

@@ -1519,7 +1519,7 @@ mod conn {
15191519

15201520
let tcp = rt.block_on(tcp_connect(&addr)).unwrap();
15211521

1522-
let (mut client, conn) = rt.block_on(conn::handshake(tcp)).unwrap();
1522+
let (mut client, conn) = rt.block_on(conn::http1::handshake(tcp)).unwrap();
15231523

15241524
rt.spawn(conn.map_err(|e| panic!("conn error: {}", e)).map(|_| ()));
15251525

@@ -1570,7 +1570,7 @@ mod conn {
15701570

15711571
let tcp = rt.block_on(tcp_connect(&addr)).unwrap();
15721572

1573-
let (mut client, conn) = rt.block_on(conn::handshake(tcp)).unwrap();
1573+
let (mut client, conn) = rt.block_on(conn::http1::handshake(tcp)).unwrap();
15741574

15751575
rt.spawn(conn.map_err(|e| panic!("conn error: {}", e)).map(|_| ()));
15761576

@@ -1615,7 +1615,7 @@ mod conn {
16151615

16161616
let tcp = rt.block_on(tcp_connect(&addr)).unwrap();
16171617

1618-
let (mut client, conn) = rt.block_on(conn::handshake(tcp)).unwrap();
1618+
let (mut client, conn) = rt.block_on(conn::http2::handshake(tcp)).unwrap();
16191619

16201620
rt.spawn(conn.map_err(|e| panic!("conn error: {}", e)).map(|_| ()));
16211621

@@ -1657,7 +1657,7 @@ mod conn {
16571657

16581658
let tcp = rt.block_on(tcp_connect(&addr)).unwrap();
16591659

1660-
let (mut client, conn) = rt.block_on(conn::handshake(tcp)).unwrap();
1660+
let (mut client, conn) = rt.block_on(conn::http2::handshake(tcp)).unwrap();
16611661

16621662
rt.spawn(conn.map_err(|e| panic!("conn error: {}", e)).map(|_| ()));
16631663

@@ -1727,7 +1727,7 @@ mod conn {
17271727
shutdown_called: false,
17281728
};
17291729

1730-
let (mut client, mut conn) = rt.block_on(conn::handshake(io)).unwrap();
1730+
let (mut client, mut conn) = rt.block_on(conn::http1::handshake(io)).unwrap();
17311731

17321732
{
17331733
let until_upgrade = poll_fn(|ctx| conn.poll_without_shutdown(ctx));
@@ -1813,7 +1813,7 @@ mod conn {
18131813
shutdown_called: false,
18141814
};
18151815

1816-
let (mut client, mut conn) = rt.block_on(conn::handshake(io)).unwrap();
1816+
let (mut client, mut conn) = rt.block_on(conn::http1::handshake(io)).unwrap();
18171817

18181818
{
18191819
let until_tunneled = poll_fn(|ctx| conn.poll_without_shutdown(ctx));
@@ -1911,8 +1911,7 @@ mod conn {
19111911
});
19121912

19131913
let io = tcp_connect(&addr).await.expect("tcp connect");
1914-
let (mut client, conn) = conn::Builder::new()
1915-
.http2_only(true)
1914+
let (mut client, conn) = conn::http2::Builder::new()
19161915
.handshake::<_, Body>(io)
19171916
.await
19181917
.expect("http handshake");
@@ -1973,8 +1972,7 @@ mod conn {
19731972
});
19741973

19751974
let io = tcp_connect(&addr).await.expect("tcp connect");
1976-
let (_client, conn) = conn::Builder::new()
1977-
.http2_only(true)
1975+
let (_client, conn) = conn::http2::Builder::new()
19781976
.http2_keep_alive_interval(Duration::from_secs(1))
19791977
.http2_keep_alive_timeout(Duration::from_secs(1))
19801978
// enable while idle since we aren't sending requests
@@ -2006,8 +2004,7 @@ mod conn {
20062004
});
20072005

20082006
let io = tcp_connect(&addr).await.expect("tcp connect");
2009-
let (mut client, conn) = conn::Builder::new()
2010-
.http2_only(true)
2007+
let (mut client, conn) = conn::http2::Builder::new()
20112008
.http2_keep_alive_interval(Duration::from_secs(1))
20122009
.http2_keep_alive_timeout(Duration::from_secs(1))
20132010
.handshake::<_, Body>(io)
@@ -2042,8 +2039,7 @@ mod conn {
20422039
});
20432040

20442041
let io = tcp_connect(&addr).await.expect("tcp connect");
2045-
let (mut client, conn) = conn::Builder::new()
2046-
.http2_only(true)
2042+
let (mut client, conn) = conn::http2::Builder::new()
20472043
.http2_keep_alive_interval(Duration::from_secs(1))
20482044
.http2_keep_alive_timeout(Duration::from_secs(1))
20492045
.handshake::<_, Body>(io)
@@ -2106,8 +2102,7 @@ mod conn {
21062102
});
21072103

21082104
let io = tcp_connect(&addr).await.expect("tcp connect");
2109-
let (mut client, conn) = conn::Builder::new()
2110-
.http2_only(true)
2105+
let (mut client, conn) = conn::http2::Builder::new()
21112106
.http2_keep_alive_interval(Duration::from_secs(1))
21122107
.http2_keep_alive_timeout(Duration::from_secs(1))
21132108
.handshake::<_, Body>(io)
@@ -2165,8 +2160,7 @@ mod conn {
21652160
});
21662161

21672162
let io = tcp_connect(&addr).await.expect("tcp connect");
2168-
let (mut client, conn) = conn::Builder::new()
2169-
.http2_only(true)
2163+
let (mut client, conn) = conn::http2::Builder::new()
21702164
.handshake::<_, Body>(io)
21712165
.await
21722166
.expect("http handshake");
@@ -2221,8 +2215,7 @@ mod conn {
22212215
});
22222216

22232217
let io = tcp_connect(&addr).await.expect("tcp connect");
2224-
let (mut client, conn) = conn::Builder::new()
2225-
.http2_only(true)
2218+
let (mut client, conn) = conn::http2::Builder::new()
22262219
.handshake::<_, Body>(io)
22272220
.await
22282221
.expect("http handshake");

tests/server.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2516,8 +2516,7 @@ async fn http2_keep_alive_with_responsive_client() {
25162516
});
25172517

25182518
let tcp = connect_async(addr).await;
2519-
let (mut client, conn) = hyper::client::conn::Builder::new()
2520-
.http2_only(true)
2519+
let (mut client, conn) = hyper::client::conn::http2::Builder::new()
25212520
.handshake::<_, Body>(tcp)
25222521
.await
25232522
.expect("http handshake");
@@ -3142,8 +3141,7 @@ impl TestClient {
31423141
let host = req.uri().host().expect("uri has no host");
31433142
let port = req.uri().port_u16().expect("uri has no port");
31443143

3145-
let mut builder = hyper::client::conn::Builder::new();
3146-
builder.http2_only(self.http2_only);
3144+
let builder = hyper::client::conn::http2::Builder::new();
31473145

31483146
let stream = TkTcpStream::connect(format!("{}:{}", host, port))
31493147
.await

0 commit comments

Comments
 (0)