Skip to content

Commit f96c0ee

Browse files
committed
correctly encode the response body
Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent 95d47ad commit f96c0ee

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/server.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,6 @@ impl Encoder {
121121
std::io::Write::write_fmt(&mut head, format_args!("Content-Length: {}\r\n", len))?;
122122
} else {
123123
std::io::Write::write_fmt(&mut head, format_args!("Transfer-Encoding: chunked\r\n"))?;
124-
panic!("chunked encoding is not implemented yet");
125-
// See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding
126-
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Trailer
127124
}
128125

129126
for (header, value) in self.res.headers().iter() {
@@ -150,7 +147,8 @@ impl Read for Encoder {
150147

151148
// Send the headers. As long as the headers aren't fully sent yet we
152149
// keep sending more of the headers.
153-
let mut bytes_read = 0;
150+
let mut head_bytes_read = 0;
151+
let mut body_bytes_read = 0;
154152

155153
// Read from the serialized headers, url and methods.
156154
if !self.head_done {
@@ -163,19 +161,39 @@ impl Read for Encoder {
163161
if self.cursor == head_len {
164162
self.head_done = true;
165163
}
166-
bytes_read += len;
164+
head_bytes_read += len;
167165
}
168166

169167
// Read from the AsyncRead impl on the inner Response struct.
170168
if !self.body_done {
171-
let n = ready!(Pin::new(&mut self.res).poll_read(cx, &mut buf[bytes_read..]))?;
172-
bytes_read += n;
169+
// figure out how many bytes we can read. If a len was set, we need
170+
// to make sure we don't read more than that.
171+
let upper_bound = match self.res.len() {
172+
Some(len) => (head_bytes_read + len - self.body_bytes_read).min(buf.len()),
173+
None => buf.len(),
174+
};
175+
176+
// Read bytes, and update internal tracking stuff.
177+
let n = ready!(Pin::new(&mut self.res).poll_read(cx, &mut buf[head_bytes_read..upper_bound]))?;
178+
body_bytes_read += n;
173179
self.body_bytes_read += n;
174-
if bytes_read == 0 {
180+
181+
// If our stream no longer gives bytes, end.
182+
if body_bytes_read == 0 {
175183
self.body_done = true;
176184
}
185+
186+
// If we know we've read all bytes, end.
187+
if let Some(len) = self.res.len() {
188+
if len == self.body_bytes_read {
189+
self.body_done = true;
190+
}
191+
debug_assert!(self.body_bytes_read <= len, "Too many bytes read. Expected: {}, read: {}", len, self.body_bytes_read);
192+
}
177193
}
178194

195+
// Return the total amount of bytes read.
196+
let bytes_read = head_bytes_read + body_bytes_read;
179197
Poll::Ready(Ok(bytes_read as usize))
180198
}
181199
}
@@ -211,7 +229,6 @@ where
211229
// Convert our header buf into an httparse instance, and validate.
212230
let status = httparse_req.parse(&buf)?;
213231
if status.is_partial() {
214-
dbg!(String::from_utf8(buf).unwrap());
215232
return Err("Malformed HTTP head".into());
216233
}
217234

0 commit comments

Comments
 (0)