Skip to content

Commit 287faa8

Browse files
committed
Send correct content-length header
1 parent d078b74 commit 287faa8

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

src/body.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ pub struct Body<R: AsyncRead> {
1313

1414
impl<R: AsyncRead> fmt::Debug for Body<R> {
1515
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16-
f.debug_struct("Body").finish()
16+
f.debug_struct("Body")
17+
.field("length", &self.length)
18+
.finish()
1719
}
1820
}
1921

src/client.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ pub async fn encode<R: AsyncRead>(req: Request<Body<R>>) -> Result<Encoder<R>, s
5656

5757
// If the body isn't streaming, we can set the content-length ahead of time. Else we need to
5858
// send all items in chunks.
59-
// if let Some(len) = res.body().len() {
60-
// write!(&mut buf, "Content-Length: {}\r\n", len)?;
61-
// } else {
62-
// write!(&mut buf, "Transfer-Encoding: chunked\r\n")?;
63-
// panic!("chunked encoding is not implemented yet");
64-
// // See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding
65-
// // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Trailer
66-
// }
59+
if let Some(len) = req.body().len() {
60+
write!(&mut buf, "Content-Length: {}\r\n", len)?;
61+
} else {
62+
// write!(&mut buf, "Transfer-Encoding: chunked\r\n")?;
63+
panic!("chunked encoding is not implemented yet");
64+
// See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding
65+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Trailer
66+
}
6767

6868
for (header, value) in req.headers() {
6969
write!(
@@ -88,7 +88,7 @@ where
8888
let mut reader = BufReader::new(reader);
8989
let mut buf = Vec::new();
9090
let mut headers = [httparse::EMPTY_HEADER; MAX_HEADERS];
91-
let mut httparse_resp = httparse::Response::new(&mut headers);
91+
let mut httparse_res = httparse::Response::new(&mut headers);
9292

9393
// Keep reading bytes from the stream until we hit the end of the stream.
9494
loop {
@@ -106,26 +106,26 @@ where
106106
}
107107

108108
// Convert our header buf into an httparse instance, and validate.
109-
let status = httparse_resp.parse(&buf)?;
109+
let status = httparse_res.parse(&buf)?;
110110
if status.is_partial() {
111111
dbg!(String::from_utf8(buf).unwrap());
112112
return Err("Malformed HTTP head".into());
113113
}
114114

115115
// Convert httparse headers + body into a `http::Response` type.
116-
let mut resp = Response::builder();
117-
for header in httparse_resp.headers.iter() {
118-
resp.header(header.name, header.value);
116+
let mut res = Response::builder();
117+
for header in httparse_res.headers.iter() {
118+
res.header(header.name, header.value);
119119
}
120-
if let Some(version) = httparse_resp.version {
121-
resp.version(match version {
120+
if let Some(version) = httparse_res.version {
121+
res.version(match version {
122122
1 => Version::HTTP_11,
123123
_ => return Err("Unsupported HTTP version".into()),
124124
});
125125
}
126126

127127
// Process the body if `Content-Length` was passed.
128-
let body = match httparse_resp
128+
let body = match httparse_res
129129
.headers
130130
.iter()
131131
.find(|h| h.name == "Content-Length")
@@ -135,7 +135,7 @@ where
135135
};
136136

137137
// Return the response.
138-
Ok(resp.body(body)?)
138+
Ok(res.body(body)?)
139139
}
140140

141141
impl <R: AsyncRead + Unpin> AsyncRead for Encoder<R> {

0 commit comments

Comments
 (0)