Skip to content

Commit 11c96cc

Browse files
authored
Merge pull request #42 from yoshuawuyts/fix-client
updates the client impl to work with new http-types
2 parents 33429ef + 1562881 commit 11c96cc

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

examples/client.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ use http_types::{Method, Request, Url};
1010
fn main() -> Result<(), async_h1::Exception> {
1111
task::block_on(async {
1212
let stream = net::TcpStream::connect("127.0.0.1:8080").await?;
13-
println!("connecting to {}", stream.peer_addr()?);
13+
let peer_addr = stream.peer_addr()?;
14+
println!("connecting to {}", peer_addr);
1415

1516
// TODO: Delete this line when we implement `Clone` for `TcpStream`.
1617
let stream = Stream(Arc::new(stream));
1718

1819
for i in 0usize..2 {
1920
println!("making request {}/2", i + 1);
20-
21-
let mut req =
22-
client::encode(Request::new(Method::Get, Url::parse("/foo").unwrap())).await?;
21+
let url = Url::parse(&format!("http://{}/foo", peer_addr)).unwrap();
22+
let req = Request::new(Method::Get, dbg!(url));
23+
let mut req = client::encode(req).await?;
2324
io::copy(&mut req, &mut stream.clone()).await?;
2425

2526
// read the response

src/client.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,17 @@ impl Encoder {
5050
pub async fn encode(req: Request) -> Result<Encoder, std::io::Error> {
5151
let mut buf: Vec<u8> = vec![];
5252

53-
write!(&mut buf, "{} {} HTTP/1.1\r\n", req.method(), req.url(),).await?;
53+
let mut url = req.url().path().to_owned();
54+
if let Some(fragment) = req.url().fragment() {
55+
url.push('#');
56+
url.push_str(fragment);
57+
}
58+
if let Some(query) = req.url().query() {
59+
url.push('?');
60+
url.push_str(query);
61+
}
62+
63+
write!(&mut buf, "{} {} HTTP/1.1\r\n", req.method(), url).await?;
5464

5565
// If the body isn't streaming, we can set the content-length ahead of time. Else we need to
5666
// send all items in chunks.

0 commit comments

Comments
 (0)