Skip to content

Commit 1ecbcbb

Browse files
authored
fix(http1): return error if user body ends prematurely
- update proto::h1::end_body to return Result<()> - update Encoder::end to return Error(NotEof) only when there's Content-length left to be addressed Closes #2263
1 parent 3de81c8 commit 1ecbcbb

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

src/proto/h1/conn.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use super::{Decoder, Encode, EncodedBuf, Encoder, Http1Transaction, ParseContext
1212
use crate::common::{task, Pin, Poll, Unpin};
1313
use crate::headers::connection_keep_alive;
1414
use crate::proto::{BodyLength, DecodedLength, MessageHead};
15+
use crate::Result;
1516

1617
const H2_PREFACE: &[u8] = b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
1718

@@ -584,7 +585,7 @@ where
584585
self.state.writing = state;
585586
}
586587

587-
pub fn end_body(&mut self) {
588+
pub fn end_body(&mut self) -> Result<()> {
588589
debug_assert!(self.can_write_body());
589590

590591
let state = match self.state.writing {
@@ -601,13 +602,18 @@ where
601602
Writing::KeepAlive
602603
}
603604
}
604-
Err(_not_eof) => Writing::Closed,
605+
Err(_not_eof) => {
606+
return Err(crate::Error::new_user_body(
607+
crate::Error::new_body_write_aborted(),
608+
))
609+
}
605610
}
606611
}
607-
_ => return,
612+
_ => return Ok(()),
608613
};
609614

610615
self.state.writing = state;
616+
Ok(())
611617
}
612618

613619
// When we get a parse error, depending on what side we are, we might be able

src/proto/h1/dispatch.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ where
338338
*clear_body = true;
339339
if chunk.remaining() == 0 {
340340
trace!("discarding empty chunk");
341-
self.conn.end_body();
341+
self.conn.end_body()?;
342342
} else {
343343
self.conn.write_body_and_end(chunk);
344344
}
@@ -351,7 +351,7 @@ where
351351
}
352352
} else {
353353
*clear_body = true;
354-
self.conn.end_body();
354+
self.conn.end_body()?;
355355
}
356356
} else {
357357
return Poll::Pending;

src/proto/h1/encode.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ impl Encoder {
8787
Kind::Chunked => Ok(Some(EncodedBuf {
8888
kind: BufKind::ChunkedEnd(b"0\r\n\r\n"),
8989
})),
90-
_ => Err(NotEof),
90+
Kind::CloseDelimited => Ok(None),
91+
Kind::Length(_) => Err(NotEof),
9192
}
9293
}
9394

@@ -405,14 +406,14 @@ mod tests {
405406

406407
assert_eq!(dst, b"foo bar");
407408
assert!(!encoder.is_eof());
408-
encoder.end::<()>().unwrap_err();
409+
encoder.end::<()>().unwrap();
409410

410411
let msg2 = b"baz".as_ref();
411412
let buf2 = encoder.encode(msg2);
412413
dst.put(buf2);
413414

414415
assert_eq!(dst, b"foo barbaz");
415416
assert!(!encoder.is_eof());
416-
encoder.end::<()>().unwrap_err();
417+
encoder.end::<()>().unwrap();
417418
}
418419
}

0 commit comments

Comments
 (0)