Skip to content

Commit e1c1b24

Browse files
committed
Chunked is working with one chunk
1 parent 1f9cb09 commit e1c1b24

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

examples/server.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ async fn accept(addr: String, stream: TcpStream) -> Result<(), async_h1::Excepti
1818
async {
1919
let resp = Response::new(StatusCode::Ok)
2020
.set_header("Content-Type", "text/plain")?
21-
.set_body_string("Hello, World!".to_string())?;
21+
.set_body(io::Cursor::new(vec![
22+
0x48u8, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x21,
23+
]));
2224
Ok(resp)
2325
}
2426
})

src/server.rs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,12 @@ impl Read for Encoder {
188188
None => buf.len() - 2,
189189
};
190190

191-
// Read bytes, and update internal tracking stuff.
192-
body_bytes_read = ready!(
193-
Pin::new(&mut self.res).poll_read(cx, &mut buf[head_bytes_read..upper_bound])
194-
)?;
195-
self.body_bytes_read += body_bytes_read; // total body bytes read on all polls
196-
197191
match self.res.len() {
198192
Some(len) => {
193+
// Read bytes, and update internal tracking stuff.
194+
body_bytes_read = ready!(Pin::new(&mut self.res)
195+
.poll_read(cx, &mut buf[head_bytes_read..upper_bound]))?;
196+
199197
debug_assert!(
200198
self.body_bytes_read <= len,
201199
"Too many bytes read. Expected: {}, read: {}",
@@ -208,22 +206,46 @@ impl Read for Encoder {
208206
}
209207
}
210208
None => {
209+
let mut chunk_buf = vec![0; buf.len()];
210+
// Read bytes from body reader
211+
let chunk_length = ready!(Pin::new(&mut self.res)
212+
.poll_read(cx, &mut chunk_buf[0..buf.len() - head_bytes_read]))?;
213+
214+
// serialize chunk length as hex
215+
let chunk_length_string = format!("{:X}", chunk_length);
216+
let chunk_length_bytes = chunk_length_string.as_bytes();
217+
let chunk_length_bytes_len = chunk_length_bytes.len();
218+
body_bytes_read += chunk_length_bytes_len;
219+
buf[head_bytes_read..head_bytes_read + body_bytes_read]
220+
.copy_from_slice(chunk_length_bytes);
221+
222+
// follow chunk length with CRLF
223+
buf[head_bytes_read + body_bytes_read] = b'\r';
224+
buf[head_bytes_read + body_bytes_read + 1] = b'\n';
225+
body_bytes_read += 2;
226+
227+
// copy chunk into buf
228+
buf[head_bytes_read + body_bytes_read
229+
..head_bytes_read + body_bytes_read + chunk_length]
230+
.copy_from_slice(&chunk_buf[..chunk_length]);
231+
body_bytes_read += chunk_length;
232+
211233
// TODO: relax this constraint at some point by adding extra state
212234
let bytes_read = head_bytes_read + body_bytes_read;
213-
assert!(buf.len() >= bytes_read + 4, "Buffers should have room for the head, the body, and 4 extra bytes when using chunked encoding");
235+
assert!(buf.len() >= bytes_read + 7, "Buffers should have room for the head, the chunk length, 2 bytes, the chunk, and 4 extra bytes when using chunked encoding");
214236

215-
buf[bytes_read] = b'\r';
216-
buf[bytes_read + 1] = b'\n';
237+
buf[bytes_read..bytes_read + 7].copy_from_slice(b"\r\n0\r\n\r\n");
217238

218-
if body_bytes_read == 0 {
219-
self.body_done = true;
220-
buf[bytes_read + 2] = b'\r';
221-
buf[bytes_read + 3] = b'\n';
222-
}
239+
// if body_bytes_read == 0 {
240+
self.body_done = true;
241+
body_bytes_read += 7;
242+
// }
223243
}
224244
}
225245
}
226246

247+
self.body_bytes_read += body_bytes_read; // total body bytes read on all polls
248+
227249
// Return the total amount of bytes read.
228250
let bytes_read = head_bytes_read + body_bytes_read;
229251
Poll::Ready(Ok(bytes_read as usize))

0 commit comments

Comments
 (0)