Skip to content

Commit aec60c6

Browse files
authored
Merge pull request #23 from yoshuawuyts/fix-server
Fix URL parsing in server code
2 parents f7f2cde + 07bac49 commit aec60c6

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ edition = "2018"
1515
url = "2.1.0"
1616
httparse = "1.3.3"
1717
futures-io = "0.3.0"
18-
async-std = { version = "0.99.12", features = ["unstable"] }
18+
async-std = { version = "1", features = ["unstable"] }
1919
futures-core-preview = "0.3.0-alpha.18"
2020
http-types = { path = '../http-types' }
2121

examples/server.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,37 @@ use async_std::prelude::*;
88
use async_std::task::{self, Context, Poll};
99
use http_types::{Response, StatusCode};
1010

11+
async fn accept(addr: String, stream: TcpStream) -> Result<(), async_h1::Exception> {
12+
// println!("starting new connection from {}", stream.peer_addr()?);
13+
14+
// TODO: Delete this line when we implement `Clone` for `TcpStream`.
15+
let stream = Stream(Arc::new(stream));
16+
17+
server::connect(&addr, stream.clone(), stream, |_| {
18+
async {
19+
let resp = Response::new(StatusCode::Ok)
20+
.set_header("Content-Type", "text/plain")?
21+
.set_body_string("Hello, World!".to_string())?;
22+
Ok(resp)
23+
}
24+
})
25+
.await
26+
}
27+
1128
fn main() -> Result<(), async_h1::Exception> {
1229
task::block_on(async {
1330
let listener = net::TcpListener::bind(("127.0.0.1", 8080)).await?;
14-
println!("listening on {}", listener.local_addr()?);
31+
let addr = format!("http://{}", listener.local_addr()?);
32+
println!("listening on {}", addr);
1533
let mut incoming = listener.incoming();
1634

1735
while let Some(stream) = incoming.next().await {
36+
let stream = stream?;
37+
let addr = addr.clone();
1838
task::spawn(async {
19-
let stream = stream?;
20-
println!("starting new connection from {}", stream.peer_addr()?);
21-
22-
// TODO: Delete this line when we implement `Clone` for `TcpStream`.
23-
let stream = Stream(Arc::new(stream));
24-
25-
server::connect(stream.clone(), stream, |_| {
26-
async { Ok(Response::new(StatusCode::Ok)) }
27-
})
28-
.await
39+
if let Err(err) = accept(addr, stream).await {
40+
eprintln!("{}", err);
41+
}
2942
});
3043
}
3144
Ok(())

src/date.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const SECONDS_IN_HOUR: u64 = 3600;
1313

1414
/// Format using the `Display` trait.
1515
/// Convert timestamp into/from `SytemTime` to use.
16-
/// Supports comparsion and sorting.
16+
/// Supports comparison and sorting.
1717
#[derive(Copy, Clone, Debug, Eq, Ord)]
1818
pub struct HttpDate {
1919
/// 0...59

src/server.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use std::pin::Pin;
1515

1616
use crate::{Exception, MAX_HEADERS};
1717

18-
pub async fn connect<'a, R, W, F, Fut>(
18+
pub async fn connect<R, W, F, Fut>(
19+
addr: &str,
1920
reader: R,
2021
mut writer: W,
2122
callback: F,
@@ -33,7 +34,7 @@ where
3334

3435
// Decode a request. This may be the first of many since
3536
// the connection is Keep-Alive by default
36-
let decoded = decode(reader).await?;
37+
let decoded = decode(addr, reader).await?;
3738
// Decode returns one of three things;
3839
// * A request with its body reader set to the underlying TCP stream
3940
// * A request with an empty body AND the underlying stream
@@ -60,7 +61,7 @@ where
6061

6162
// Decode a new request, timing out if this takes longer than the
6263
// timeout duration.
63-
decoded = match timeout(timeout_duration, decode(to_decode)).await {
64+
decoded = match timeout(timeout_duration, decode(addr, to_decode)).await {
6465
Ok(Ok(Some(r))) => r,
6566
Ok(Ok(None)) | Err(TimeoutError { .. }) => break, /* EOF or timeout */
6667
Ok(Err(e)) => return Err(e),
@@ -170,7 +171,7 @@ pub async fn encode(res: Response) -> io::Result<Encoder> {
170171
const HTTP_1_1_VERSION: u8 = 1;
171172

172173
/// Decode an HTTP request on the server.
173-
pub async fn decode<R>(reader: R) -> Result<Option<DecodedRequest>, Exception>
174+
pub async fn decode<R>(addr: &str, reader: R) -> Result<Option<DecodedRequest>, Exception>
174175
where
175176
R: Read + Unpin + Send + 'static,
176177
{
@@ -204,7 +205,7 @@ where
204205
// Convert httparse headers + body into a `http::Request` type.
205206
let method = httparse_req.method.ok_or_else(|| "No method found")?;
206207
let uri = httparse_req.path.ok_or_else(|| "No uri found")?;
207-
let uri = url::Url::parse(uri)?;
208+
let uri = url::Url::parse(&format!("{}{}", addr, uri))?;
208209
let version = httparse_req.version.ok_or_else(|| "No version found")?;
209210
if version != HTTP_1_1_VERSION {
210211
return Err("Unsupported HTTP version".into());

0 commit comments

Comments
 (0)