Skip to main content

http_type/stream/
struct.rs

1use super::*;
2
3#[derive(CustomDebug, Data, DisplayDebug, New)]
4pub struct Stream {
5    #[get_mut(pub(super))]
6    #[set(pub(super))]
7    pub(super) stream: TcpStream,
8    #[get_mut(pub(super))]
9    #[set(pub(super))]
10    pub(super) request_config: RequestConfig,
11    #[get(type(copy))]
12    #[get_mut(pub(super))]
13    pub(super) closed: bool,
14    /// Bytes read ahead of the current request, already consumed from the
15    /// socket but not yet parsed into a request.
16    ///
17    /// A single socket read can deliver bytes belonging to the next pipelined
18    /// request. Those bytes are carried here between requests so
19    /// HTTP pipelining is not silently dropped.
20    #[get_mut(pub(super))]
21    #[set(pub(super))]
22    #[new(skip)]
23    pub(super) leftover: Vec<u8>,
24    /// Offset of the first unconsumed byte in `leftover`.
25    #[get(type(copy))]
26    #[get_mut(pub(super))]
27    #[set(pub(super))]
28    #[new(skip)]
29    pub(super) leftover_start: usize,
30    /// Offset one past the last valid byte in `leftover`.
31    #[get(type(copy))]
32    #[get_mut(pub(super))]
33    #[set(pub(super))]
34    #[new(skip)]
35    pub(super) leftover_end: usize,
36}
37
38/// A buffered reader over a `TcpStream` with a reusable read buffer.
39///
40/// This reader batches socket reads into an internal buffer to reduce
41/// syscall count, and returns its buffer to a thread-local pool on drop
42/// so keep-alive requests avoid repeated buffer allocation.
43#[derive(Data, New)]
44pub(crate) struct PooledReader<'a> {
45    /// The underlying TCP stream being read.
46    #[get(pub(super))]
47    #[get_mut(pub(super))]
48    #[set(pub(super))]
49    pub(super) stream: &'a mut TcpStream,
50    /// The reusable read buffer holding unconsumed bytes in `start..end`.
51    #[get(pub(super))]
52    #[get_mut(pub(super))]
53    #[set(pub(super))]
54    pub(super) buffer: Vec<u8>,
55    /// The index of the first unconsumed byte in the buffer.
56    #[get(pub(super))]
57    #[get_mut(pub(super))]
58    #[set(pub(super))]
59    #[new(skip)]
60    pub(super) start: usize,
61    /// The index one past the last valid byte in the buffer.
62    #[get(pub(super))]
63    #[get_mut(pub(super))]
64    #[set(pub(super))]
65    #[new(skip)]
66    pub(super) end: usize,
67}