Skip to content

Commit

Permalink
make chunk encoder code more clear
Browse files Browse the repository at this point in the history
  • Loading branch information
zh-jq-b committed May 14, 2024
1 parent 7da19cc commit 373e64d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
20 changes: 11 additions & 9 deletions lib/g3-http/src/body/chunked_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use tokio::io::{AsyncBufRead, AsyncWrite};

use g3_io_ext::LimitedCopyError;

struct ChunkedEncodeTransferInternal {
struct ChunkedNoTrailerEncodeTransferInternal {
yield_size: usize,
this_chunk_size: usize,
left_chunk_size: usize,
Expand All @@ -34,9 +34,9 @@ struct ChunkedEncodeTransferInternal {
active: bool,
}

impl ChunkedEncodeTransferInternal {
impl ChunkedNoTrailerEncodeTransferInternal {
fn new(yield_size: usize) -> Self {
ChunkedEncodeTransferInternal {
ChunkedNoTrailerEncodeTransferInternal {
yield_size,
this_chunk_size: 0,
left_chunk_size: 0,
Expand Down Expand Up @@ -69,8 +69,10 @@ impl ChunkedEncodeTransferInternal {
if chunk_size == 0 {
self.read_finished = true;
if self.total_write == 0 {
// no trailer
let _ = write!(&mut self.static_header, "0\r\n\r\n");
} else {
// no trailer
let _ = write!(&mut self.static_header, "\r\n0\r\n\r\n");
}
} else if self.total_write == 0 {
Expand Down Expand Up @@ -145,18 +147,18 @@ impl ChunkedEncodeTransferInternal {
}
}

pub struct ChunkedEncodeTransfer<'a, R, W> {
pub struct ChunkedNoTrailerEncodeTransfer<'a, R, W> {
reader: &'a mut R,
writer: &'a mut W,
internal: ChunkedEncodeTransferInternal,
internal: ChunkedNoTrailerEncodeTransferInternal,
}

impl<'a, R, W> ChunkedEncodeTransfer<'a, R, W> {
impl<'a, R, W> ChunkedNoTrailerEncodeTransfer<'a, R, W> {
pub fn new(reader: &'a mut R, writer: &'a mut W, yield_size: usize) -> Self {
ChunkedEncodeTransfer {
ChunkedNoTrailerEncodeTransfer {
reader,
writer,
internal: ChunkedEncodeTransferInternal::new(yield_size),
internal: ChunkedNoTrailerEncodeTransferInternal::new(yield_size),
}
}

Expand All @@ -181,7 +183,7 @@ impl<'a, R, W> ChunkedEncodeTransfer<'a, R, W> {
}
}

impl<'a, R, W> Future for ChunkedEncodeTransfer<'a, R, W>
impl<'a, R, W> Future for ChunkedNoTrailerEncodeTransfer<'a, R, W>
where
R: AsyncBufRead + Unpin,
W: AsyncWrite + Unpin,
Expand Down
28 changes: 17 additions & 11 deletions lib/g3-http/src/body/chunked_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ use tokio::io::{AsyncBufRead, AsyncBufReadExt, AsyncWrite};

use g3_io_ext::{LimitedCopyConfig, LimitedCopyError, ROwnedLimitedCopy};

use super::{ChunkedEncodeTransfer, HttpBodyReader, HttpBodyType, PreviewDataState};
use super::{ChunkedNoTrailerEncodeTransfer, HttpBodyReader, HttpBodyType, PreviewDataState};

const END_BUFFER: &[u8] = b"\r\n0\r\n\r\n";
const NO_TRAILER_END_BUFFER: &[u8] = b"\r\n0\r\n\r\n";

pub struct ChunkedTransfer<'a, R, W> {
body_type: HttpBodyType,
Expand All @@ -49,8 +49,8 @@ struct SendEnd<'a, W> {
enum ChunkedTransferState<'a, R, W> {
SendHead(SendHead<'a, R, W>),
Copy(ROwnedLimitedCopy<'a, HttpBodyReader<'a, R>, W>),
SendEnd(SendEnd<'a, W>),
Encode(ChunkedEncodeTransfer<'a, R, W>),
SendNoTrailerEnd(SendEnd<'a, W>),
Encode(ChunkedNoTrailerEncodeTransfer<'a, R, W>),
End,
}

Expand All @@ -67,6 +67,10 @@ where
copy_config: LimitedCopyConfig,
) -> ChunkedTransfer<'a, R, W> {
let state = match body_type {
HttpBodyType::ContentLength(0) => {
// just send 0 chunk size and empty trailer end
ChunkedTransferState::SendNoTrailerEnd(SendEnd { offset: 2, writer })
}
HttpBodyType::ContentLength(len) => {
let head = format!("{len:x}\r\n");
let body_reader = HttpBodyReader::new(
Expand All @@ -82,7 +86,8 @@ where
})
}
HttpBodyType::ReadUntilEnd => {
let encoder = ChunkedEncodeTransfer::new(reader, writer, copy_config.yield_size());
let encoder =
ChunkedNoTrailerEncodeTransfer::new(reader, writer, copy_config.yield_size());
ChunkedTransferState::Encode(encoder)
}
HttpBodyType::ChunkedWithoutTrailer | HttpBodyType::ChunkedWithTrailer => {
Expand Down Expand Up @@ -127,7 +132,8 @@ where
}
HttpBodyType::ReadUntilEnd => {
reader.consume(preview_state.consume_size);
let encoder = ChunkedEncodeTransfer::new(reader, writer, copy_config.yield_size());
let encoder =
ChunkedNoTrailerEncodeTransfer::new(reader, writer, copy_config.yield_size());
ChunkedTransferState::Encode(encoder)
}
HttpBodyType::ChunkedWithoutTrailer | HttpBodyType::ChunkedWithTrailer => {
Expand Down Expand Up @@ -173,7 +179,7 @@ where

pub fn no_cached_data(&self) -> bool {
match &self.state {
ChunkedTransferState::SendHead(_) | ChunkedTransferState::SendEnd(_) => false,
ChunkedTransferState::SendHead(_) | ChunkedTransferState::SendNoTrailerEnd(_) => false,
ChunkedTransferState::Copy(copy) => copy.no_cached_data(),
ChunkedTransferState::Encode(encode) => encode.no_cached_data(),
ChunkedTransferState::End => true,
Expand Down Expand Up @@ -239,7 +245,7 @@ where
let ChunkedTransferState::Copy(copy) = old_state else {
unreachable!()
};
self.state = ChunkedTransferState::SendEnd(SendEnd {
self.state = ChunkedTransferState::SendNoTrailerEnd(SendEnd {
offset: 0,
writer: copy.writer(),
});
Expand All @@ -249,9 +255,9 @@ where
Poll::Ready(Ok(()))
}
}
ChunkedTransferState::SendEnd(send_end) => {
while send_end.offset < END_BUFFER.len() {
let buf = &END_BUFFER[send_end.offset..];
ChunkedTransferState::SendNoTrailerEnd(send_end) => {
while send_end.offset < NO_TRAILER_END_BUFFER.len() {
let buf = &NO_TRAILER_END_BUFFER[send_end.offset..];
let nw = ready!(Pin::new(&mut send_end.writer).poll_write(cx, buf))
.map_err(LimitedCopyError::WriteFailed)?;
send_end.offset += nw;
Expand Down
2 changes: 1 addition & 1 deletion lib/g3-http/src/body/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mod chunked_transfer;
pub use chunked_transfer::ChunkedTransfer;

mod chunked_encoder;
pub use chunked_encoder::ChunkedEncodeTransfer;
pub use chunked_encoder::ChunkedNoTrailerEncodeTransfer;

mod chunked_decoder;
pub use chunked_decoder::ChunkedDecodeReader;
Expand Down
4 changes: 2 additions & 2 deletions lib/g3-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub use parse::{

mod body;
pub use body::{
ChunkedDecodeReader, ChunkedEncodeTransfer, ChunkedTransfer, HttpBodyReader, HttpBodyType,
PreviewData, PreviewDataState, PreviewError, TrailerReadError, TrailerReader,
ChunkedDecodeReader, ChunkedNoTrailerEncodeTransfer, ChunkedTransfer, HttpBodyReader,
HttpBodyType, PreviewData, PreviewDataState, PreviewError, TrailerReadError, TrailerReader,
};

pub mod client;
Expand Down

0 comments on commit 373e64d

Please sign in to comment.