Skip to content

Commit 507cccb

Browse files
authored
Merge pull request #17 from yoshuawuyts/http-types
Switch over to http-types
2 parents 94f59e8 + cd5dc61 commit 507cccb

File tree

8 files changed

+306
-259
lines changed

8 files changed

+306
-259
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: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,81 @@
1-
use async_h1::{client, Body};
1+
use async_h1::client;
22
use async_std::{io, net, task};
3+
use http_types::{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 =
15+
client::encode(Request::new(Method::Get, Url::parse("/foo").unwrap())).await?;
16+
io::copy(&mut req, &mut stream.clone()).await?;
1717

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

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::{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(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)