Skip to content

Commit 9f3a100

Browse files
committed
fix all bugs actually
1 parent 095693f commit 9f3a100

File tree

5 files changed

+67
-977
lines changed

5 files changed

+67
-977
lines changed

examples/server.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ async fn accept(addr: String, stream: TcpStream) -> http_types::Result<()> {
3232
async_h1::accept(&addr, stream.clone(), |_req| async move {
3333
let mut res = Response::new(StatusCode::Ok);
3434
res.insert_header("Content-Type", "text/plain")?;
35-
let body = Body::from_reader(BufReader::new(File::open("src/date.rs").await?), None);
35+
let body = Body::from_reader(
36+
BufReader::new(File::open("tests/fixtures/zeros.txt").await?),
37+
None,
38+
);
3639
res.set_body(body);
3740
Ok(res)
3841
})

src/server/encode.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::pin::Pin;
44

55
use async_std::io;
66
use async_std::io::prelude::*;
7-
use async_std::task::{ready, Context, Poll};
7+
use async_std::task::{Context, Poll};
88
use http_types::Response;
99

1010
use crate::date::fmt_http_date;
@@ -201,15 +201,26 @@ impl Encoder {
201201
cx: &mut Context<'_>,
202202
buf: &mut [u8],
203203
) -> Poll<io::Result<usize>> {
204-
// Get bytes from the underlying stream. If a zero-length buffer is
205-
// returned we're done.
206-
let src = ready!(Pin::new(&mut self.res).poll_fill_buf(cx))?;
204+
// Get bytes from the underlying stream. If the stream is not ready yet,
205+
// return the header bytes if we have any.
206+
let src = match Pin::new(&mut self.res).poll_fill_buf(cx) {
207+
Poll::Ready(Ok(n)) => n,
208+
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
209+
Poll::Pending => match self.bytes_read {
210+
0 => return Poll::Pending,
211+
n => return Poll::Ready(Ok(n)),
212+
},
213+
};
214+
215+
// If the stream doesn't have any more bytes left to read we're done.
207216
if src.len() == 0 {
208-
// Finalize the chunk with a final CRLF.
217+
// Write out the final empty chunk
209218
let idx = self.bytes_read;
210219
buf[idx] = b'0';
211220
buf[idx + 1] = CR;
212221
buf[idx + 2] = LF;
222+
223+
// Write the final CRLF
213224
buf[idx + 3] = CR;
214225
buf[idx + 4] = LF;
215226
self.bytes_read += 5;
@@ -224,9 +235,10 @@ impl Encoder {
224235
// buffer to read all that.
225236
let buf_len = buf.len().checked_sub(self.bytes_read).unwrap_or(0);
226237
let amt = src.len().min(buf_len);
227-
let len_prefix = format!("{}", amt).into_bytes();
238+
let len_prefix = format!("{:X}", amt).into_bytes();
228239
let buf_upper = buf_len.checked_sub(len_prefix.len() + 4).unwrap_or(0);
229240
let amt = amt.min(buf_upper);
241+
let len_prefix = format!("{:X}", amt).into_bytes();
230242

231243
// Write our frame header to the buffer.
232244
let lower = self.bytes_read;

0 commit comments

Comments
 (0)