Skip to content

Commit f3238db

Browse files
committed
Update http-types
1 parent 7c7a176 commit f3238db

File tree

5 files changed

+58
-33
lines changed

5 files changed

+58
-33
lines changed

examples/server.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
use std::pin::Pin;
2+
use std::str::FromStr;
23
use std::sync::Arc;
34

45
use async_h1::server;
56
use async_std::io::{self, Read, Write};
67
use async_std::net::{self, TcpStream};
78
use async_std::prelude::*;
89
use async_std::task::{self, Context, Poll};
9-
use http_types::{Response, StatusCode};
10+
use http_types::{
11+
headers::{HeaderName, HeaderValue},
12+
Response, StatusCode,
13+
};
1014

1115
async fn accept(addr: String, stream: TcpStream) -> Result<(), async_h1::Exception> {
1216
// println!("starting new connection from {}", stream.peer_addr()?);
@@ -16,9 +20,12 @@ async fn accept(addr: String, stream: TcpStream) -> Result<(), async_h1::Excepti
1620

1721
server::accept(&addr, stream.clone(), stream, |_| {
1822
async {
19-
let resp = Response::new(StatusCode::Ok)
20-
.set_header("Content-Type", "text/plain")?
21-
.set_body_string("Hello".into())?;
23+
let mut resp = Response::new(StatusCode::Ok);
24+
resp.insert_header(
25+
HeaderName::from_str("Content-Type")?,
26+
HeaderValue::from_str("text/plain")?,
27+
)?;
28+
resp.set_body("Hello");
2229
// To try chunked encoding, replace `set_body_string` with the following method call
2330
// .set_body(io::Cursor::new(vec![
2431
// 0x48u8, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x21,

src/client.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ use async_std::prelude::*;
55
use async_std::task::{Context, Poll};
66
use futures_core::ready;
77
use futures_io::AsyncRead;
8-
use http_types::{Request, Response, StatusCode};
8+
use http_types::{
9+
headers::{HeaderName, HeaderValue, CONTENT_LENGTH},
10+
Body, Request, Response, StatusCode,
11+
};
912

1013
use std::pin::Pin;
14+
use std::str::FromStr;
1115

1216
use crate::{Exception, MAX_HEADERS};
1317

@@ -58,8 +62,10 @@ pub async fn encode(req: Request) -> Result<Encoder, std::io::Error> {
5862
// See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding
5963
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Trailer
6064
}
61-
for (header, value) in req.headers().iter() {
62-
write!(&mut buf, "{}: {}\r\n", header, value).await?;
65+
for (header, values) in req.iter() {
66+
for value in values.iter() {
67+
write!(&mut buf, "{}: {}\r\n", header, value).await?;
68+
}
6369
}
6470

6571
write!(&mut buf, "\r\n").await?;
@@ -107,22 +113,24 @@ where
107113
use std::convert::TryFrom;
108114
let mut res = Response::new(StatusCode::try_from(code)?);
109115
for header in httparse_res.headers.iter() {
110-
res = res.set_header(header.name, std::str::from_utf8(header.value)?)?;
116+
res.insert_header(
117+
HeaderName::from_str(header.name)?,
118+
HeaderValue::from_str(std::str::from_utf8(header.value)?)?,
119+
)?;
111120
}
112121

113122
// Process the body if `Content-Length` was passed.
114-
if let Some(content_length) = httparse_res
115-
.headers
116-
.iter()
117-
.find(|h| h.name.eq_ignore_ascii_case("Content-Length"))
118-
{
119-
let length = std::str::from_utf8(content_length.value)
120-
.ok()
121-
.and_then(|s| s.parse::<usize>().ok());
122-
123-
if let Some(_len) = length {
124-
// TODO: set size
125-
res = res.set_body(reader);
123+
if let Some(content_length) = res.header(&CONTENT_LENGTH) {
124+
let length = content_length
125+
.last()
126+
.unwrap()
127+
.as_str()
128+
.parse::<usize>()
129+
.ok();
130+
131+
if let Some(len) = length {
132+
res.set_body(Body::from_reader(reader));
133+
res.set_len(len);
126134
} else {
127135
return Err("Invalid value for Content-Length".into());
128136
}

src/server.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use async_std::io::{Read, Write};
66
use async_std::prelude::*;
77
use async_std::task::{Context, Poll};
88
use futures_core::ready;
9-
use http_types::{Method, Request, Response};
9+
use http_types::{
10+
headers::{HeaderName, HeaderValue, CONTENT_TYPE},
11+
Body, Method, Request, Response,
12+
};
1013
use std::fmt;
1114
use std::str::FromStr;
1215
use std::time::Duration;
@@ -140,11 +143,10 @@ impl Encoder {
140143
let date = fmt_http_date(std::time::SystemTime::now());
141144
std::io::Write::write_fmt(&mut head, format_args!("Date: {}\r\n", date))?;
142145

143-
for (header, value) in self.res.headers().iter() {
144-
std::io::Write::write_fmt(
145-
&mut head,
146-
format_args!("{}: {}\r\n", header.as_str(), value),
147-
)?
146+
for (header, values) in self.res.iter() {
147+
for value in values.iter() {
148+
std::io::Write::write_fmt(&mut head, format_args!("{}: {}\r\n", header, value))?
149+
}
148150
}
149151

150152
std::io::Write::write_fmt(&mut head, format_args!("\r\n"))?;
@@ -390,16 +392,20 @@ where
390392
}
391393
let mut req = Request::new(Method::from_str(method)?, uri);
392394
for header in httparse_req.headers.iter() {
393-
req = req.set_header(header.name, std::str::from_utf8(header.value)?)?;
395+
req.insert_header(
396+
HeaderName::from_str(header.name)?,
397+
HeaderValue::from_str(std::str::from_utf8(header.value)?)?,
398+
)?;
394399
}
395400

396401
// Check for content-length, that determines determines whether we can parse
397402
// it with a known length, or need to use chunked encoding.
398-
let len = match req.header("Content-Length") {
399-
Some(len) => len.parse::<usize>()?,
403+
let len = match req.header(&CONTENT_TYPE) {
404+
Some(len) => len.last().unwrap().as_str().parse::<usize>()?,
400405
None => return Ok(Some(DecodedRequest::WithoutBody(req, Box::new(reader)))),
401406
};
402-
req = req.set_body_reader(reader).set_len(len);
407+
req.set_body(Body::from_reader(reader));
408+
req.set_len(len);
403409
Ok(Some(DecodedRequest::WithBody(req)))
404410
}
405411

@@ -436,7 +442,7 @@ impl DecodedRequest {
436442
/// When it does not, the underlying body has been passed alongside the request.
437443
fn into_reader(self) -> Box<dyn BufRead + Unpin + Send + 'static> {
438444
match self {
439-
DecodedRequest::WithBody(r) => r.into_body_reader(),
445+
DecodedRequest::WithBody(r) => r.into_body().into_reader(),
440446
DecodedRequest::WithoutBody(_, s) => s,
441447
}
442448
}

tests/fixtures/response1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
HTTP/1.1 200 OK
22
Content-Length: 0
33
Date: {DATE}
4-
Content-Type: text/plain; charset=utf-8
4+
content-type: text/plain; charset=utf-8
55

tests/server.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ fn test_basic_request() {
1616
actual,
1717
expected,
1818
server::accept(addr, Cursor::new(request), &mut actual, |_req| {
19-
async { Ok(Response::new(StatusCode::Ok).set_body_string("".to_owned())?) }
19+
async {
20+
let mut resp = Response::new(StatusCode::Ok);
21+
resp.set_body("");
22+
Ok(resp)
23+
}
2024
})
2125
);
2226
}

0 commit comments

Comments
 (0)