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(crate) 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 /// The buffer the request line is read into.
38 ///
39 /// It lives with the connection rather than with the request, so a
40 /// keep-alive connection reads its second, third and later request lines
41 /// into the same allocation instead of a fresh one per request.
42 #[get_mut(pub(super))]
43 #[set(pub(super))]
44 pub(super) line_buffer: String,
45 /// The buffer the header lines are read into, kept for the same reason as
46 /// the request line buffer.
47 #[get_mut(pub(super))]
48 #[set(pub(super))]
49 pub(super) header_line_buffer: String,
50 /// Lazily allocated WebSocket parsing state retained across cancelled reads.
51 #[get(pub(super), type(ref))]
52 #[get_mut(pub(super))]
53 #[set(pub(super))]
54 pub(super) websocket_state: Option<Box<WebSocketReadState>>,
55 /// Reusable read-timeout sleep timer kept armed across keep-alive requests.
56 #[get(skip)]
57 #[get_mut(skip)]
58 #[set(skip)]
59 pub(super) read_timer: Option<Pin<Box<Sleep>>>,
60}
61
62/// Partial WebSocket input that must survive selection of an outbound message.
63#[derive(Clone, Data, Debug)]
64pub(super) struct WebSocketReadState {
65 /// Wire bytes not yet consumed by a complete frame.
66 #[get(pub(super), type(ref))]
67 #[get_mut(pub(super))]
68 #[set(pub(super))]
69 pub(super) buffer: Vec<u8>,
70 /// Start offset of unconsumed wire bytes within `buffer`.
71 #[get(pub(super), type(copy))]
72 #[get_mut(pub(super))]
73 #[set(pub(super))]
74 pub(super) cursor: usize,
75 /// Payload accumulated from a message spanning multiple frames.
76 #[get(pub(super))]
77 #[get_mut(pub(super))]
78 #[set(pub(super))]
79 pub(super) full_frame: Vec<u8>,
80 /// Data opcode of the currently active fragmented message, if any.
81 #[get(pub(super), type(ref))]
82 #[get_mut(pub(super))]
83 #[set(pub(super))]
84 pub(super) fragment_opcode: Option<WebSocketOpcode>,
85 /// Whether a peer response has arrived since the last heartbeat.
86 #[get(pub(super), type(copy))]
87 #[get_mut(pub(super))]
88 #[set(pub(super))]
89 pub(super) is_client_response: bool,
90}
91
92/// A buffered reader over a transport with a reusable read buffer.
93///
94/// This reader batches reads into an internal buffer to reduce syscall count,
95/// and returns drained buffers to a thread-local pool while waiting for more
96/// input so idle keep-alive connections do not each retain a read buffer.
97#[derive(Data, New)]
98pub(crate) struct PooledReader<'a> {
99 /// The underlying transport being read.
100 #[get(pub(super))]
101 #[get_mut(pub(super))]
102 #[set(pub(super))]
103 pub(super) stream: &'a mut Box<dyn Transport>,
104 /// The reusable read buffer holding unconsumed bytes in `start..end`.
105 #[get(pub(super))]
106 #[get_mut(pub(super))]
107 #[set(pub(super))]
108 pub(super) buffer: Vec<u8>,
109 /// The required buffer length when the transport becomes readable.
110 #[get(pub(super), type(copy))]
111 #[get_mut(pub(super))]
112 #[set(pub(super))]
113 pub(super) capacity: usize,
114 /// The index of the first unconsumed byte in the buffer.
115 #[get(pub(super), type(copy))]
116 #[get_mut(pub(super))]
117 #[set(pub(super))]
118 #[new(skip)]
119 pub(super) start: usize,
120 /// The index one past the last valid byte in the buffer.
121 #[get(pub(super), type(copy))]
122 #[get_mut(pub(super))]
123 #[set(pub(super))]
124 #[new(skip)]
125 pub(super) end: usize,
126}