Skip to content

Commit 276f225

Browse files
committed
init connect
1 parent 7d64ffc commit 276f225

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

examples/client.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ fn main() -> Result<(), async_h1::Exception> {
2020
println!("making request {}/2", i + 1);
2121
let url = Url::parse(&format!("http://{}/foo", peer_addr)).unwrap();
2222
let req = Request::new(Method::Get, dbg!(url));
23-
let mut req = client::encode(req).await?;
24-
io::copy(&mut req, &mut stream.clone()).await?;
25-
26-
// read the response
27-
let res = client::decode(stream.clone()).await?;
23+
let res = client::connect(stream.clone(), req).await?;
2824
println!("{:?}", res);
2925
}
3026
Ok(())

src/client.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Process HTTP connections on the client.
22
3-
use async_std::io::{self, BufReader, Read};
3+
use async_std::io::{self, BufReader, Read, Write};
44
use async_std::prelude::*;
55
use async_std::task::{Context, Poll};
66
use futures_core::ready;
@@ -48,8 +48,19 @@ impl Encoder {
4848
}
4949
}
5050

51+
/// Send an HTTP request over a stream.
52+
pub async fn connect<RW>(stream: RW, req: Request) -> Result<Response, std::io::Error>
53+
where
54+
RW: Read + Write + Clone + Send + Sync + Unpin + 'static,
55+
{
56+
let mut req = encode(req).await?;
57+
io::copy(&mut req, &mut stream.clone()).await?;
58+
let res = decode(stream.clone()).await.unwrap(); // todo: convert to http_types::Error
59+
Ok(res)
60+
}
61+
5162
/// Encode an HTTP request on the client.
52-
pub async fn encode(req: Request) -> Result<Encoder, std::io::Error> {
63+
async fn encode(req: Request) -> Result<Encoder, std::io::Error> {
5364
let mut buf: Vec<u8> = vec![];
5465

5566
let mut url = req.url().path().to_owned();
@@ -98,7 +109,7 @@ pub async fn encode(req: Request) -> Result<Encoder, std::io::Error> {
98109
}
99110

100111
/// Decode an HTTP response on the client.
101-
pub async fn decode<R>(reader: R) -> Result<Response, Exception>
112+
async fn decode<R>(reader: R) -> Result<Response, Exception>
102113
where
103114
R: Read + Unpin + Send + Sync + 'static,
104115
{

0 commit comments

Comments
 (0)