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