Skip to content

Commit

Permalink
simplify http1 body type
Browse files Browse the repository at this point in the history
  • Loading branch information
zh-jq-b committed May 14, 2024
1 parent dddc1c0 commit c98e1c9
Show file tree
Hide file tree
Showing 19 changed files with 120 additions and 272 deletions.
10 changes: 5 additions & 5 deletions lib/g3-http/src/body/chunked_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ where
ChunkedNoTrailerEncodeTransfer::new(reader, writer, copy_config.yield_size());
ChunkedTransferState::Encode(encoder)
}
HttpBodyType::ChunkedWithoutTrailer | HttpBodyType::ChunkedWithTrailer => {
HttpBodyType::Chunked => {
let body_reader = HttpBodyReader::new(reader, body_type, body_line_max_len);
let copy = ROwnedLimitedCopy::new(body_reader, writer, copy_config);
ChunkedTransferState::Copy(copy)
Expand Down Expand Up @@ -136,7 +136,7 @@ where
ChunkedNoTrailerEncodeTransfer::new(reader, writer, copy_config.yield_size());
ChunkedTransferState::Encode(encoder)
}
HttpBodyType::ChunkedWithoutTrailer | HttpBodyType::ChunkedWithTrailer => {
HttpBodyType::Chunked => {
let next_chunk_size = preview_state.chunked_next_size;
if next_chunk_size > 0 {
let head = format!("{next_chunk_size:x}\r\n");
Expand Down Expand Up @@ -411,7 +411,7 @@ mod test {
let mut body_transfer = ChunkedTransfer::new(
&mut buf_stream,
&mut write_buf,
HttpBodyType::ChunkedWithoutTrailer,
HttpBodyType::Chunked,
1024,
Default::default(),
);
Expand Down Expand Up @@ -441,7 +441,7 @@ mod test {
let mut body_transfer = ChunkedTransfer::new(
&mut buf_stream,
&mut write_buf,
HttpBodyType::ChunkedWithoutTrailer,
HttpBodyType::Chunked,
1024,
Default::default(),
);
Expand All @@ -465,7 +465,7 @@ mod test {
let mut body_transfer = ChunkedTransfer::new(
&mut buf_stream,
&mut write_buf,
HttpBodyType::ChunkedWithoutTrailer,
HttpBodyType::Chunked,
1024,
Default::default(),
);
Expand Down
3 changes: 1 addition & 2 deletions lib/g3-http/src/body/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum HttpBodyType {
ContentLength(u64),
ChunkedWithoutTrailer,
ChunkedWithTrailer,
Chunked,
ReadUntilEnd,
}

Expand Down
132 changes: 46 additions & 86 deletions lib/g3-http/src/body/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,14 @@ fn push_preview_data(
chunked_next_size: 0,
})
}
HttpBodyType::ChunkedWithoutTrailer => push_chunked_preview_data(header, limit, buf, false),
HttpBodyType::ChunkedWithTrailer => push_chunked_preview_data(header, limit, buf, true),
HttpBodyType::Chunked => push_chunked_preview_data(header, limit, buf),
}
}

fn push_chunked_preview_data(
header: &mut Vec<u8>,
limit: usize,
buf: &[u8],
has_trailer: bool,
) -> Result<PreviewDataState, PreviewError> {
let mut consume_size = 0;
let mut preview_size = 0;
Expand All @@ -164,17 +162,6 @@ fn push_chunked_preview_data(
}

if chunk_size == 0 {
if has_trailer {
// do not consume the ending chunk, so we can pass Trailer along with the ending chunk in the continue request
header.put_slice(b"0\r\n\r\n");
return Ok(PreviewDataState {
consume_size,
preview_size,
preview_eof: false,
chunked_next_size: 0,
});
}

let end = &left[p + 1..];
let end_len = end.len();
if end_len >= 1 {
Expand All @@ -200,7 +187,15 @@ fn push_chunked_preview_data(
chunked_next_size: 0,
});
}
return Err(PreviewError::InvalidChunkedBody);

// do not consume the ending chunk, so we can pass Trailer along with the ending chunk in the continue request
header.put_slice(b"0\r\n\r\n");
return Ok(PreviewDataState {
consume_size,
preview_size,
preview_eof: false,
chunked_next_size: 0,
});
} else {
// do not consume the ending chunk, send them in the continue request
header.put_slice(b"0\r\n\r\n");
Expand Down Expand Up @@ -395,55 +390,32 @@ mod tests {
fn preview_data_chunked() {
let mut headers = Vec::with_capacity(256);

let s = push_preview_data(
&mut headers,
HttpBodyType::ChunkedWithoutTrailer,
4,
b"1\r\n",
)
.unwrap();
let s = push_preview_data(&mut headers, HttpBodyType::Chunked, 4, b"1\r\n").unwrap();
assert_eq!(s.consume_size, 0);
assert_eq!(s.preview_size, 0);
assert!(!s.preview_eof);
assert_eq!(s.chunked_next_size, 0);
assert_eq!(headers.as_slice(), b"0\r\n\r\n");

headers.clear();
let s = push_preview_data(
&mut headers,
HttpBodyType::ChunkedWithoutTrailer,
4,
b"1\r\na",
)
.unwrap();
let s = push_preview_data(&mut headers, HttpBodyType::Chunked, 4, b"1\r\na").unwrap();
assert_eq!(s.consume_size, 0);
assert_eq!(s.preview_size, 0);
assert!(!s.preview_eof);
assert_eq!(s.chunked_next_size, 0);
assert_eq!(headers.len(), 0);

headers.clear();
let s = push_preview_data(
&mut headers,
HttpBodyType::ChunkedWithoutTrailer,
4,
b"1\r\na\r\n",
)
.unwrap();
let s = push_preview_data(&mut headers, HttpBodyType::Chunked, 4, b"1\r\na\r\n").unwrap();
assert_eq!(s.consume_size, 6);
assert_eq!(s.preview_size, 1);
assert!(!s.preview_eof);
assert_eq!(s.chunked_next_size, 0);
assert_eq!(headers.as_slice(), b"1\r\na\r\n0\r\n\r\n");

headers.clear();
let s = push_preview_data(
&mut headers,
HttpBodyType::ChunkedWithoutTrailer,
4,
b"1\r\na\r\n1\r\n",
)
.unwrap();
let s =
push_preview_data(&mut headers, HttpBodyType::Chunked, 4, b"1\r\na\r\n1\r\n").unwrap();
assert_eq!(s.consume_size, 6);
assert_eq!(s.preview_size, 1);
assert!(!s.preview_eof);
Expand All @@ -453,7 +425,7 @@ mod tests {
headers.clear();
let s = push_preview_data(
&mut headers,
HttpBodyType::ChunkedWithoutTrailer,
HttpBodyType::Chunked,
4,
b"1\r\na\r\n1\r\nb\r\n",
)
Expand All @@ -467,7 +439,7 @@ mod tests {
headers.clear();
let s = push_preview_data(
&mut headers,
HttpBodyType::ChunkedWithoutTrailer,
HttpBodyType::Chunked,
4,
b"1\r\na\r\n3\r\nbcd\r\n",
)
Expand All @@ -479,41 +451,25 @@ mod tests {
assert_eq!(headers.as_slice(), b"1\r\na\r\n3\r\nbcd\r\n0\r\n\r\n");

headers.clear();
let s = push_preview_data(
&mut headers,
HttpBodyType::ChunkedWithoutTrailer,
4,
b"2\r\nab\r\n",
)
.unwrap();
let s = push_preview_data(&mut headers, HttpBodyType::Chunked, 4, b"2\r\nab\r\n").unwrap();
assert_eq!(s.consume_size, 7);
assert_eq!(s.preview_size, 2);
assert!(!s.preview_eof);
assert_eq!(s.chunked_next_size, 0);
assert_eq!(headers.as_slice(), b"2\r\nab\r\n0\r\n\r\n");

headers.clear();
let s = push_preview_data(
&mut headers,
HttpBodyType::ChunkedWithoutTrailer,
4,
b"4\r\nabcd\r\n",
)
.unwrap();
let s =
push_preview_data(&mut headers, HttpBodyType::Chunked, 4, b"4\r\nabcd\r\n").unwrap();
assert_eq!(s.consume_size, 9);
assert_eq!(s.preview_size, 4);
assert!(!s.preview_eof);
assert_eq!(s.chunked_next_size, 0);
assert_eq!(headers.as_slice(), b"4\r\nabcd\r\n0\r\n\r\n");

headers.clear();
let s = push_preview_data(
&mut headers,
HttpBodyType::ChunkedWithoutTrailer,
4,
b"5\r\nabcde\r\n",
)
.unwrap();
let s =
push_preview_data(&mut headers, HttpBodyType::Chunked, 4, b"5\r\nabcde\r\n").unwrap();
assert_eq!(s.consume_size, 7);
assert_eq!(s.preview_size, 4);
assert!(!s.preview_eof);
Expand All @@ -523,7 +479,7 @@ mod tests {
headers.clear();
let s = push_preview_data(
&mut headers,
HttpBodyType::ChunkedWithoutTrailer,
HttpBodyType::Chunked,
4,
b"1\r\na\r\n4\r\nbcde\r\n",
)
Expand All @@ -535,27 +491,17 @@ mod tests {
assert_eq!(headers.as_slice(), b"1\r\na\r\n3\r\nbcd\r\n0\r\n\r\n");

headers.clear();
let s = push_preview_data(
&mut headers,
HttpBodyType::ChunkedWithoutTrailer,
4,
b"3\r\nabc\r\n0",
)
.unwrap();
let s =
push_preview_data(&mut headers, HttpBodyType::Chunked, 4, b"3\r\nabc\r\n0").unwrap();
assert_eq!(s.consume_size, 8);
assert_eq!(s.preview_size, 3);
assert!(!s.preview_eof);
assert_eq!(s.chunked_next_size, 0);
assert_eq!(headers.as_slice(), b"3\r\nabc\r\n0\r\n\r\n");

headers.clear();
let s = push_preview_data(
&mut headers,
HttpBodyType::ChunkedWithoutTrailer,
4,
b"4\r\nabcd\r\n0",
)
.unwrap();
let s =
push_preview_data(&mut headers, HttpBodyType::Chunked, 4, b"4\r\nabcd\r\n0").unwrap();
assert_eq!(s.consume_size, 9);
assert_eq!(s.preview_size, 4);
assert!(!s.preview_eof);
Expand All @@ -565,7 +511,7 @@ mod tests {
headers.clear();
let s = push_preview_data(
&mut headers,
HttpBodyType::ChunkedWithoutTrailer,
HttpBodyType::Chunked,
4,
b"4\r\nabcd\r\n0\r\n",
)
Expand All @@ -579,7 +525,7 @@ mod tests {
headers.clear();
let s = push_preview_data(
&mut headers,
HttpBodyType::ChunkedWithoutTrailer,
HttpBodyType::Chunked,
4,
b"3\r\nabc\r\n0\r\n\r\n",
)
Expand All @@ -593,7 +539,21 @@ mod tests {
headers.clear();
let s = push_preview_data(
&mut headers,
HttpBodyType::ChunkedWithoutTrailer,
HttpBodyType::Chunked,
4,
b"3\r\nabc\r\n0\r\nA: B\r\n\r\n",
)
.unwrap();
assert_eq!(s.consume_size, 8);
assert_eq!(s.preview_size, 3);
assert!(!s.preview_eof);
assert_eq!(s.chunked_next_size, 0);
assert_eq!(headers.as_slice(), b"3\r\nabc\r\n0\r\n\r\n");

headers.clear();
let s = push_preview_data(
&mut headers,
HttpBodyType::Chunked,
4,
b"4\r\nabcd\r\n0\r\n\r\n",
)
Expand All @@ -607,9 +567,9 @@ mod tests {
headers.clear();
let s = push_preview_data(
&mut headers,
HttpBodyType::ChunkedWithTrailer,
HttpBodyType::Chunked,
4,
b"4\r\nabcd\r\n0\r\n\r\n",
b"4\r\nabcd\r\n0\r\nA: B\r\n\r\n",
)
.unwrap();
assert_eq!(s.consume_size, 9);
Expand Down
Loading

0 comments on commit c98e1c9

Please sign in to comment.