Skip to content

Commit 7ca8f9a

Browse files
committed
addr not required
1 parent 704256b commit 7ca8f9a

File tree

8 files changed

+27
-34
lines changed

8 files changed

+27
-34
lines changed

examples/server.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ async fn main() -> http_types::Result<()> {
1414
let mut incoming = listener.incoming();
1515
while let Some(stream) = incoming.next().await {
1616
let stream = stream?;
17-
let addr = addr.clone();
1817
task::spawn(async {
19-
if let Err(err) = accept(addr, stream).await {
18+
if let Err(err) = accept(stream).await {
2019
eprintln!("{}", err);
2120
}
2221
});
@@ -25,9 +24,9 @@ async fn main() -> http_types::Result<()> {
2524
}
2625

2726
// Take a TCP stream, and convert it into sequential HTTP request / response pairs.
28-
async fn accept(addr: String, stream: TcpStream) -> http_types::Result<()> {
27+
async fn accept(stream: TcpStream) -> http_types::Result<()> {
2928
println!("starting new connection from {}", stream.peer_addr()?);
30-
async_h1::accept(&addr, stream.clone(), |_req| async move {
29+
async_h1::accept(stream.clone(), |_req| async move {
3130
let mut res = Response::new(StatusCode::Ok);
3231
res.insert_header("Content-Type", "text/plain")?;
3332
res.set_body("Hello world");

src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@
6767
//! let mut incoming = listener.incoming();
6868
//! while let Some(stream) = incoming.next().await {
6969
//! let stream = stream?;
70-
//! let addr = addr.clone();
7170
//! task::spawn(async {
72-
//! if let Err(err) = accept(addr, stream).await {
71+
//! if let Err(err) = accept(stream).await {
7372
//! eprintln!("{}", err);
7473
//! }
7574
//! });
@@ -78,9 +77,9 @@
7877
//! }
7978
//!
8079
//! // Take a TCP stream, and convert it into sequential HTTP request / response pairs.
81-
//! async fn accept(addr: String, stream: TcpStream) -> http_types::Result<()> {
80+
//! async fn accept(stream: TcpStream) -> http_types::Result<()> {
8281
//! println!("starting new connection from {}", stream.peer_addr()?);
83-
//! async_h1::accept(&addr, stream.clone(), |_req| async move {
82+
//! async_h1::accept(stream.clone(), |_req| async move {
8483
//! let mut res = Response::new(StatusCode::Ok);
8584
//! res.insert_header("Content-Type", "text/plain")?;
8685
//! res.set_body("Hello");

src/server/decode.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const LF: u8 = b'\n';
1818
const HTTP_1_1_VERSION: u8 = 1;
1919

2020
/// Decode an HTTP request on the server.
21-
pub(crate) async fn decode<R>(addr: &str, reader: R) -> http_types::Result<Option<Request>>
21+
pub(crate) async fn decode<R>(reader: R) -> http_types::Result<Option<Request>>
2222
where
2323
R: Read + Unpin + Send + Sync + 'static,
2424
{
@@ -59,24 +59,25 @@ where
5959

6060
let path = httparse_req.path;
6161
let path = path.ok_or_else(|| format_err!("No uri found"))?;
62-
let uri = url::Url::parse(&format!("{}{}", addr, path))?;
6362

6463
let version = httparse_req.version;
6564
let version = version.ok_or_else(|| format_err!("No version found"))?;
6665
ensure_eq!(version, HTTP_1_1_VERSION, "Unsupported HTTP version");
6766

68-
let mut req = Request::new(Method::from_str(method)?, uri);
67+
let mut req = Request::new(Method::from_str(method)?, url::Url::parse("x:").unwrap());
68+
6969
for header in httparse_req.headers.iter() {
7070
let name = HeaderName::from_str(header.name)?;
7171
let value = HeaderValue::from_str(std::str::from_utf8(header.value)?)?;
7272
req.insert_header(name, value)?;
7373
}
7474

75-
if let Some(host) = req.header(&HOST).cloned() {
76-
if let Some(host) = host.last() {
77-
*req.url_mut() = url::Url::parse(&format!("http://{}{}", host, path))?;
78-
}
79-
}
75+
let host = req
76+
.header(&HOST)
77+
.and_then(|header| header.last())
78+
.ok_or_else(|| format_err!("Mandatory host header missing"))?;
79+
80+
*req.url_mut() = url::Url::parse(&format!("http://{}", host.as_str()))?.join(path)?;
8081

8182
let content_length = req.header(&CONTENT_LENGTH);
8283
let transfer_encoding = req.header(&TRANSFER_ENCODING);

src/server/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,19 @@ impl Default for ServerOptions {
3131
/// Accept a new incoming HTTP/1.1 connection.
3232
///
3333
/// Supports `KeepAlive` requests by default.
34-
pub async fn accept<RW, F, Fut>(addr: &str, io: RW, endpoint: F) -> http_types::Result<()>
34+
pub async fn accept<RW, F, Fut>(io: RW, endpoint: F) -> http_types::Result<()>
3535
where
3636
RW: Read + Write + Clone + Send + Sync + Unpin + 'static,
3737
F: Fn(Request) -> Fut,
3838
Fut: Future<Output = http_types::Result<Response>>,
3939
{
40-
accept_with_opts(addr, io, endpoint, Default::default()).await
40+
accept_with_opts(io, endpoint, Default::default()).await
4141
}
4242

4343
/// Accept a new incoming HTTP/1.1 connection.
4444
///
4545
/// Supports `KeepAlive` requests by default.
4646
pub async fn accept_with_opts<RW, F, Fut>(
47-
addr: &str,
4847
mut io: RW,
4948
endpoint: F,
5049
opts: ServerOptions,
@@ -56,7 +55,7 @@ where
5655
{
5756
loop {
5857
// Decode a new request, timing out if this takes longer than the timeout duration.
59-
let fut = decode(addr, io.clone());
58+
let fut = decode(io.clone());
6059

6160
let req = if let Some(timeout_duration) = opts.headers_timeout {
6261
match timeout(timeout_duration, fut).await {

tests/fixtures/request-unexpected-eof.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
POST / HTTP/1.1
2+
host: example.com
23
content-type: text/plain
34
content-length: 11
45

tests/server-chunked-encode-large.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ const RESPONSE: &'static str = concat![
145145
#[async_std::test]
146146
async fn server_chunked_large() {
147147
let case = TestCase::new(REQUEST, "").await;
148-
async_h1::accept("http://example.com", case.clone(), |_| async {
148+
async_h1::accept(case.clone(), |_| async {
149149
let mut res = Response::new(StatusCode::Ok);
150150
let body = Cursor::new(TEXT.to_owned());
151151
res.set_body(Body::from_reader(body, None));

tests/server-chunked-encode-small.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const RESPONSE: &'static str = concat![
4141
#[async_std::test]
4242
async fn server_chunked_large() {
4343
let case = TestCase::new(REQUEST, "").await;
44-
async_h1::accept("http://example.com", case.clone(), |_| async {
44+
async_h1::accept(case.clone(), |_| async {
4545
let mut res = Response::new(StatusCode::Ok);
4646
let body = Cursor::new(TEXT.to_owned());
4747
res.set_body(Body::from_reader(body, None));

tests/server.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ async fn test_basic_request() {
1212
"fixtures/response-add-date.txt",
1313
)
1414
.await;
15-
let addr = "http://example.com";
1615

17-
async_h1::accept(addr, case.clone(), |_req| async {
16+
async_h1::accept(case.clone(), |_req| async {
1817
let mut res = Response::new(StatusCode::Ok);
1918
res.set_body("");
2019
Ok(res)
@@ -32,9 +31,8 @@ async fn test_host() {
3231
"fixtures/response-with-host.txt",
3332
)
3433
.await;
35-
let addr = "http://127.0.0.1:8000";
3634

37-
async_h1::accept(addr, case.clone(), |req| async move {
35+
async_h1::accept(case.clone(), |req| async move {
3836
let mut res = Response::new(StatusCode::Ok);
3937
res.set_body(req.url().as_str());
4038
Ok(res)
@@ -52,9 +50,8 @@ async fn test_chunked_basic() {
5250
"fixtures/response-chunked-basic.txt",
5351
)
5452
.await;
55-
let addr = "http://example.com";
5653

57-
async_h1::accept(addr, case.clone(), |_req| async {
54+
async_h1::accept(case.clone(), |_req| async {
5855
let mut res = Response::new(StatusCode::Ok);
5956
res.set_body(Body::from_reader(
6057
Cursor::new(b"Mozilla")
@@ -79,8 +76,7 @@ async fn test_chunked_echo() {
7976
)
8077
.await;
8178

82-
let addr = "http://example.com";
83-
async_h1::accept(addr, case.clone(), |req| async {
79+
async_h1::accept(case.clone(), |req| async {
8480
let ct = req.content_type();
8581
let body: Body = req.into();
8682

@@ -106,9 +102,8 @@ async fn test_unexpected_eof() {
106102
"fixtures/response-unexpected-eof.txt",
107103
)
108104
.await;
109-
let addr = "http://example.com";
110105

111-
async_h1::accept(addr, case.clone(), |req| async {
106+
async_h1::accept(case.clone(), |req| async {
112107
let mut res = Response::new(StatusCode::Ok);
113108
let ct = req.content_type();
114109
let body: Body = req.into();
@@ -132,9 +127,8 @@ async fn test_invalid_trailer() {
132127
"fixtures/response-invalid-trailer.txt",
133128
)
134129
.await;
135-
let addr = "http://example.com";
136130

137-
async_h1::accept(addr, case.clone(), |req| async {
131+
async_h1::accept(case.clone(), |req| async {
138132
let mut res = Response::new(StatusCode::Ok);
139133
let ct = req.content_type();
140134
let body: Body = req.into();

0 commit comments

Comments
 (0)