Skip to content
Closed
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
17 changes: 14 additions & 3 deletions src/common/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ use bytes::{Buf, BufMut, Bytes, BytesMut};

pub(crate) struct BufList<T> {
bufs: VecDeque<T>,
remaining: usize,
}

impl<T: Buf> BufList<T> {
pub(crate) fn new() -> BufList<T> {
BufList {
bufs: VecDeque::new(),
remaining: 0,
}
}

#[inline]
pub(crate) fn push(&mut self, buf: T) {
debug_assert!(buf.has_remaining());
self.remaining += buf.remaining();
self.bufs.push_back(buf);
}

Expand All @@ -29,7 +32,7 @@ impl<T: Buf> BufList<T> {
impl<T: Buf> Buf for BufList<T> {
#[inline]
fn remaining(&self) -> usize {
self.bufs.iter().map(|buf| buf.remaining()).sum()
self.remaining
}

#[inline]
Expand All @@ -39,6 +42,7 @@ impl<T: Buf> Buf for BufList<T> {

#[inline]
fn advance(&mut self, mut cnt: usize) {
self.remaining -= cnt;
while cnt > 0 {
{
let front = &mut self.bufs[0];
Expand Down Expand Up @@ -78,12 +82,18 @@ impl<T: Buf> Buf for BufList<T> {
Some(front) if front.remaining() == len => {
let b = front.copy_to_bytes(len);
self.bufs.pop_front();
self.remaining -= len;
b
}
Some(front) if front.remaining() > len => front.copy_to_bytes(len),
Some(front) if front.remaining() > len => {
self.remaining -= len;
front.copy_to_bytes(len)
}
_ => {
assert!(len <= self.remaining(), "`len` greater than remaining");
assert!(len <= self.remaining, "`len` greater than remaining");
let mut bm = BytesMut::with_capacity(len);
// Note: `self.take(len)` calls `self.advance()` internally,
// which already decrements `self.remaining`.
bm.put(self.take(len));
bm.freeze()
}
Expand All @@ -100,6 +110,7 @@ mod tests {
fn hello_world_buf() -> BufList<Bytes> {
BufList {
bufs: vec![Bytes::from("Hello"), Bytes::from(" "), Bytes::from("World")].into(),
remaining: 11,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/proto/h1/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ where
ParseContext {
cached_headers: &mut self.state.cached_headers,
req_method: &mut self.state.method,
h1_parser_config: self.state.h1_parser_config.clone(),
h1_parser_config: &self.state.h1_parser_config,
h1_max_headers: self.state.h1_max_headers,
preserve_header_case: self.state.preserve_header_case,
#[cfg(feature = "ffi")]
Expand Down
6 changes: 3 additions & 3 deletions src/proto/h1/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ where
io,
partial_len: None,
read_blocked: false,
read_buf: BytesMut::with_capacity(0),
read_buf: BytesMut::with_capacity(INIT_BUFFER_SIZE),
read_buf_strategy: ReadStrategy::default(),
write_buf,
}
Expand Down Expand Up @@ -182,7 +182,7 @@ where
ParseContext {
cached_headers: parse_ctx.cached_headers,
req_method: parse_ctx.req_method,
h1_parser_config: parse_ctx.h1_parser_config.clone(),
h1_parser_config: parse_ctx.h1_parser_config,
h1_max_headers: parse_ctx.h1_max_headers,
preserve_header_case: parse_ctx.preserve_header_case,
#[cfg(feature = "ffi")]
Expand Down Expand Up @@ -704,7 +704,7 @@ mod tests {
let parse_ctx = ParseContext {
cached_headers: &mut None,
req_method: &mut None,
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: None,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand Down
2 changes: 1 addition & 1 deletion src/proto/h1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub(crate) struct ParsedMessage<T> {
pub(crate) struct ParseContext<'a> {
cached_headers: &'a mut Option<HeaderMap>,
req_method: &'a mut Option<Method>,
h1_parser_config: ParserConfig,
h1_parser_config: &'a ParserConfig,
h1_max_headers: Option<usize>,
preserve_header_case: bool,
#[cfg(feature = "ffi")]
Expand Down
40 changes: 20 additions & 20 deletions src/proto/h1/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,7 @@ mod tests {
ParseContext {
cached_headers: &mut None,
req_method: &mut method,
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: None,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand Down Expand Up @@ -1702,7 +1702,7 @@ mod tests {
let ctx = ParseContext {
cached_headers: &mut None,
req_method: &mut Some(crate::Method::GET),
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: None,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand All @@ -1726,7 +1726,7 @@ mod tests {
let ctx = ParseContext {
cached_headers: &mut None,
req_method: &mut None,
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: None,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand All @@ -1747,7 +1747,7 @@ mod tests {
let ctx = ParseContext {
cached_headers: &mut None,
req_method: &mut Some(crate::Method::GET),
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: None,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand All @@ -1770,7 +1770,7 @@ mod tests {
let ctx = ParseContext {
cached_headers: &mut None,
req_method: &mut Some(crate::Method::GET),
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: None,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand All @@ -1797,7 +1797,7 @@ mod tests {
let ctx = ParseContext {
cached_headers: &mut None,
req_method: &mut Some(crate::Method::GET),
h1_parser_config,
h1_parser_config: &h1_parser_config,
h1_max_headers: None,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand All @@ -1821,7 +1821,7 @@ mod tests {
let ctx = ParseContext {
cached_headers: &mut None,
req_method: &mut Some(crate::Method::GET),
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: None,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand Down Expand Up @@ -1849,7 +1849,7 @@ mod tests {
let ctx = ParseContext {
cached_headers: &mut None,
req_method: &mut method,
h1_parser_config,
h1_parser_config: &h1_parser_config,
h1_max_headers: None,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand All @@ -1876,7 +1876,7 @@ mod tests {
let ctx = ParseContext {
cached_headers: &mut None,
req_method: &mut None,
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: None,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand All @@ -1896,7 +1896,7 @@ mod tests {
let ctx = ParseContext {
cached_headers: &mut None,
req_method: &mut None,
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: None,
preserve_header_case: true,
#[cfg(feature = "ffi")]
Expand Down Expand Up @@ -1935,7 +1935,7 @@ mod tests {
ParseContext {
cached_headers: &mut None,
req_method: &mut None,
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: None,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand All @@ -1956,7 +1956,7 @@ mod tests {
ParseContext {
cached_headers: &mut None,
req_method: &mut None,
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: None,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand Down Expand Up @@ -2186,7 +2186,7 @@ mod tests {
ParseContext {
cached_headers: &mut None,
req_method: &mut Some(Method::GET),
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: None,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand All @@ -2207,7 +2207,7 @@ mod tests {
ParseContext {
cached_headers: &mut None,
req_method: &mut Some(m),
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: None,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand All @@ -2228,7 +2228,7 @@ mod tests {
ParseContext {
cached_headers: &mut None,
req_method: &mut Some(Method::GET),
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: None,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand Down Expand Up @@ -2798,7 +2798,7 @@ mod tests {
ParseContext {
cached_headers: &mut None,
req_method: &mut Some(Method::GET),
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: None,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand Down Expand Up @@ -2842,7 +2842,7 @@ mod tests {
ParseContext {
cached_headers: &mut None,
req_method: &mut None,
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: max_headers,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand All @@ -2866,7 +2866,7 @@ mod tests {
ParseContext {
cached_headers: &mut None,
req_method: &mut None,
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: max_headers,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand Down Expand Up @@ -3035,7 +3035,7 @@ mod tests {
ParseContext {
cached_headers: &mut headers,
req_method: &mut None,
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: None,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand Down Expand Up @@ -3080,7 +3080,7 @@ mod tests {
ParseContext {
cached_headers: &mut headers,
req_method: &mut None,
h1_parser_config: Default::default(),
h1_parser_config: &Default::default(),
h1_max_headers: None,
preserve_header_case: false,
#[cfg(feature = "ffi")]
Expand Down