Skip to content

Commit 340e6ce

Browse files
committed
Switch over to http types
1 parent bef59cc commit 340e6ce

File tree

8 files changed

+242
-251
lines changed

8 files changed

+242
-251
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ readme = "README.md"
1212
edition = "2018"
1313

1414
[dependencies]
15+
url = "2.1.0"
1516
httparse = "1.3.3"
16-
http = "0.1.17"
1717
futures-io = "0.3.0"
1818
async-std = { version = "0.99.12", features = ["unstable"] }
1919
futures-core-preview = "0.3.0-alpha.18"
20+
http-types = { path = '../http-types' }
2021

2122
[dev-dependencies]
2223
futures-util = "0.3.0"

examples/client.rs

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,85 @@
1-
use async_h1::{client, Body};
1+
use async_h1::client;
22
use async_std::{io, net, task};
3+
use http_types::{HttpVersion, Method, Request, Url};
34

45
fn main() -> Result<(), async_h1::Exception> {
56
task::block_on(async {
67
let tcp_stream = net::TcpStream::connect("127.0.0.1:8080").await?;
78
println!("connecting to {}", tcp_stream.peer_addr()?);
89

10+
let stream = Stream::new(tcp_stream);
911
for i in 0usize..2 {
10-
let (tcp_reader, tcp_writer) = &mut (&tcp_stream, &tcp_stream);
11-
1212
println!("making request {}/2", i + 1);
1313

14-
let body = Body::from_string("hello chashu".to_owned());
15-
let mut req = client::encode(http::Request::new(body)).await?;
16-
io::copy(&mut req, tcp_writer).await?;
14+
let mut req = client::encode(Request::new(
15+
HttpVersion::HTTP1_1,
16+
Method::Get,
17+
Url::parse("/foo").unwrap(),
18+
))
19+
.await?;
20+
io::copy(&mut req, &mut stream.clone()).await?;
1721

1822
// read the response
19-
let res = client::decode(tcp_reader).await?;
23+
let res = client::decode(stream.clone()).await?;
2024
println!("{:?}", res);
2125
}
2226
Ok(())
2327
})
2428
}
29+
30+
use async_std::{
31+
io::{Read, Write},
32+
net::TcpStream,
33+
task::{Context, Poll},
34+
};
35+
use std::{
36+
pin::Pin,
37+
sync::{Arc, Mutex},
38+
};
39+
40+
struct Stream {
41+
internal: Arc<Mutex<TcpStream>>,
42+
}
43+
44+
impl Stream {
45+
fn new(internal: TcpStream) -> Self {
46+
Stream {
47+
internal: Arc::new(Mutex::new(internal)),
48+
}
49+
}
50+
}
51+
52+
impl Clone for Stream {
53+
fn clone(&self) -> Self {
54+
Stream {
55+
internal: self.internal.clone(),
56+
}
57+
}
58+
}
59+
60+
impl Read for Stream {
61+
fn poll_read(
62+
mut self: Pin<&mut Self>,
63+
cx: &mut Context,
64+
buf: &mut [u8],
65+
) -> Poll<io::Result<usize>> {
66+
<TcpStream as Read>::poll_read(Pin::new(&mut self.internal.lock().unwrap()), cx, buf)
67+
}
68+
}
69+
impl Write for Stream {
70+
fn poll_write(
71+
mut self: Pin<&mut Self>,
72+
cx: &mut Context,
73+
buf: &[u8],
74+
) -> Poll<io::Result<usize>> {
75+
<TcpStream as Write>::poll_write(Pin::new(&mut self.internal.lock().unwrap()), cx, buf)
76+
}
77+
78+
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
79+
<TcpStream as Write>::poll_flush(Pin::new(&mut self.internal.lock().unwrap()), cx)
80+
}
81+
82+
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
83+
<TcpStream as Write>::poll_close(Pin::new(&mut self.internal.lock().unwrap()), cx)
84+
}
85+
}

examples/server.rs

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use async_h1::{server, Body};
1+
use async_h1::server;
22
use async_std::net;
33
use async_std::prelude::*;
44
use async_std::task;
5+
use http_types::{HttpVersion, Response, StatusCode};
56

67
fn main() -> Result<(), async_h1::Exception> {
78
task::block_on(async {
@@ -14,16 +15,70 @@ fn main() -> Result<(), async_h1::Exception> {
1415
let stream = stream?;
1516
println!("starting new connection from {}", stream.peer_addr()?);
1617

17-
let (reader, writer) = &mut (&stream, &stream);
18-
server::connect(reader, writer, |_| {
19-
async {
20-
let body = Body::from_string("hello chashu".to_owned());
21-
Ok(http::Response::new(body))
22-
}
18+
let stream = Stream::new(stream);
19+
server::connect(stream.clone(), stream, |_| {
20+
async { Ok(Response::new(HttpVersion::HTTP1_1, StatusCode::Ok)) }
2321
})
2422
.await
2523
});
2624
}
2725
Ok(())
2826
})
2927
}
28+
29+
use async_std::{
30+
io::{self, Read, Write},
31+
net::TcpStream,
32+
task::{Context, Poll},
33+
};
34+
use std::{
35+
pin::Pin,
36+
sync::{Arc, Mutex},
37+
};
38+
39+
struct Stream {
40+
internal: Arc<Mutex<TcpStream>>,
41+
}
42+
43+
impl Stream {
44+
fn new(internal: TcpStream) -> Self {
45+
Stream {
46+
internal: Arc::new(Mutex::new(internal)),
47+
}
48+
}
49+
}
50+
51+
impl Clone for Stream {
52+
fn clone(&self) -> Self {
53+
Stream {
54+
internal: self.internal.clone(),
55+
}
56+
}
57+
}
58+
59+
impl Read for Stream {
60+
fn poll_read(
61+
mut self: Pin<&mut Self>,
62+
cx: &mut Context,
63+
buf: &mut [u8],
64+
) -> Poll<io::Result<usize>> {
65+
<TcpStream as Read>::poll_read(Pin::new(&mut self.internal.lock().unwrap()), cx, buf)
66+
}
67+
}
68+
impl Write for Stream {
69+
fn poll_write(
70+
mut self: Pin<&mut Self>,
71+
cx: &mut Context,
72+
buf: &[u8],
73+
) -> Poll<io::Result<usize>> {
74+
<TcpStream as Write>::poll_write(Pin::new(&mut self.internal.lock().unwrap()), cx, buf)
75+
}
76+
77+
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
78+
<TcpStream as Write>::poll_flush(Pin::new(&mut self.internal.lock().unwrap()), cx)
79+
}
80+
81+
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
82+
<TcpStream as Write>::poll_close(Pin::new(&mut self.internal.lock().unwrap()), cx)
83+
}
84+
}

src/body.rs

Lines changed: 0 additions & 94 deletions
This file was deleted.

0 commit comments

Comments
 (0)