Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/io/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,18 @@ fn encode_body(body: &mut Body, writer: &mut impl Write, must_include_body: bool
if let Some(length) = body.len() {
if must_include_body || length > 0 {
write!(writer, "content-length: {length}\r\n\r\n")?;
copy(body, writer)?;
if length > 0 {
copy(body, writer)?;
}
} else {
write!(writer, "\r\n")?;
}
} else {
write!(writer, "transfer-encoding: chunked\r\n\r\n")?;
let mut buffer = vec![b'\0'; 4096];
let mut buffer = [0; 4096];
loop {
let mut read = 0;
while read < 1024 {
while read < buffer.len() {
// We try to avoid too small chunks
let new_read = body.read(&mut buffer[read..])?;
if new_read == 0 {
Expand Down
2 changes: 1 addition & 1 deletion src/model/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Body {
Self(BodyAlt::Sized {
total_len: len,
consumed_len: 0,
content: Box::new(read.take(len)),
content: Box::new(read),
})
}

Expand Down
10 changes: 9 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ fn read_body_and_build_response(
Ok(mut request) => {
let response = on_request(&mut request);
// We make sure to finish reading the body
if let Err(error) = copy(request.body_mut(), &mut sink()) {
if let Err(error) = drain_body(request.body_mut()) {
(build_error(error), ConnectionState::Close) // TODO: ignore?
} else {
let connection_state = request
Expand Down Expand Up @@ -305,6 +305,14 @@ fn build_text_response(status: StatusCode, text: String) -> Response<Body> {
.unwrap()
}

fn drain_body(body: &mut Body) -> Result<()> {
if body.len() == Some(0) {
return Ok(()); // Nothing to drain
}
copy(body, &mut sink())?;
Ok(())
}

/// Dumb semaphore allowing to overflow capacity
#[derive(Clone)]
struct Semaphore {
Expand Down