seanmonstar / httparse

A push parser for the HTTP 1.x protocol in Rust.
https://docs.rs/httparse
Apache License 2.0
567 stars 111 forks source link

Invalid end of chunk (parse_chunk_size) #149

Open caobug opened 8 months ago

caobug commented 8 months ago

Hello, the end of the chunk should be ended by two sets of \r\n. but parse_chunk_size considers that one set is enough, which results in the inability to know the completion of message transmission in the TCP stream.

for example:

correct:

let buf = b"0\r\n\r\n";
assert_eq!(httparse::parse_chunk_size(buf), Ok(httparse::Status::Complete((3, 0))));

unexp:

let buf = b"0\r\n";
assert_eq!(httparse::parse_chunk_size(buf), Ok(httparse::Status::Partial));

let buf = b"0\r\n\r";
assert_eq!(httparse::parse_chunk_size(buf), Ok(httparse::Status::Partial));
seanmonstar commented 8 months ago

The unexpected ones look correct to me. Those are partial ends.

The top example looks wrong, though. It should be complete, and report that the end is at byte 5, no?

caobug commented 8 months ago

No. The function parse_chunk_size reads \r, then \n, then breaks and returns Complete.

Therefore correct only reads 3 bytes. The unexpected below will always read only the first 3 bytes.

https://github.com/seanmonstar/httparse/blob/a3c10fd3281d3b70becc1aa54a995b617f502045/src/lib.rs#L1300C36-L1300C36