http_type/stream/impl.rs
1use crate::*;
2
3/// Implementation of `From` trait for converting `usize` address into `&Stream`.
4impl From<usize> for &'static Stream {
5 /// Converts a memory address into a reference to `Stream`.
6 ///
7 /// # Arguments
8 ///
9 /// - `usize` - The memory address of the `Stream` instance.
10 ///
11 /// # Returns
12 ///
13 /// - `&'static Stream` - A reference to the `Stream` at the given address.
14 ///
15 /// # Safety
16 ///
17 /// - The address is guaranteed to be a valid `Stream` instance
18 /// that was previously converted from a reference and is managed by the runtime.
19 #[inline(always)]
20 fn from(address: usize) -> &'static Stream {
21 unsafe { &*(address as *const Stream) }
22 }
23}
24
25/// Implementation of `From` trait for converting `usize` address into `&mut Stream`.
26impl<'a> From<usize> for &'a mut Stream {
27 /// Converts a memory address into a mutable reference to `Stream`.
28 ///
29 /// # Arguments
30 ///
31 /// - `usize` - The memory address of the `Stream` instance.
32 ///
33 /// # Returns
34 ///
35 /// - `&mut Stream` - A mutable reference to the `Stream` at the given address.
36 ///
37 /// # Safety
38 ///
39 /// - The address is guaranteed to be a valid `Stream` instance
40 /// that was previously converted from a reference and is managed by the runtime.
41 #[inline(always)]
42 fn from(address: usize) -> &'a mut Stream {
43 unsafe { &mut *(address as *mut Stream) }
44 }
45}
46
47/// Implementation of `From` trait for converting `&Stream` into `usize` address.
48impl From<&Stream> for usize {
49 /// Converts a reference to `Stream` into its memory address.
50 ///
51 /// # Arguments
52 ///
53 /// - `&Stream` - The reference to the `Stream` instance.
54 ///
55 /// # Returns
56 ///
57 /// - `usize` - The memory address of the `Stream` instance.
58 #[inline(always)]
59 fn from(stream: &Stream) -> Self {
60 stream as *const Stream as usize
61 }
62}
63
64/// Implementation of `From` trait for converting `&mut Stream` into `usize` address.
65impl From<&mut Stream> for usize {
66 /// Converts a mutable reference to `Stream` into its memory address.
67 ///
68 /// # Arguments
69 ///
70 /// - `&mut Stream` - The mutable reference to the `Stream` instance.
71 ///
72 /// # Returns
73 ///
74 /// - `usize` - The memory address of the `Stream` instance.
75 #[inline(always)]
76 fn from(stream: &mut Stream) -> Self {
77 stream as *mut Stream as usize
78 }
79}
80
81/// Implementation of `AsRef` trait for `Stream`.
82impl AsRef<Stream> for Stream {
83 /// Converts `&Stream` to `&Stream` via memory address conversion.
84 ///
85 /// # Returns
86 ///
87 /// - `&Stream` - A reference to the `Stream` instance.
88 #[inline(always)]
89 fn as_ref(&self) -> &Self {
90 let address: usize = self.into();
91 address.into()
92 }
93}
94
95/// Implementation of `AsMut` trait for `Stream`.
96impl AsMut<Stream> for Stream {
97 /// Converts `&mut Stream` to `&mut Stream` via memory address conversion.
98 ///
99 /// # Returns
100 ///
101 /// - `&mut Stream` - A mutable reference to the `Stream` instance.
102 #[inline(always)]
103 fn as_mut(&mut self) -> &mut Self {
104 let address: usize = self.into();
105 address.into()
106 }
107}
108
109/// Implementation of `Lifetime` trait for `Stream`.
110impl Lifetime for Stream {
111 /// Converts a reference to the stream into a `'static` reference.
112 ///
113 /// # Returns
114 ///
115 /// - `&'static Self`: A reference to the stream with a `'static` lifetime.
116 ///
117 /// # Safety
118 ///
119 /// - The address is guaranteed to be a valid `Self` instance
120 /// that was previously converted from a reference and is managed by the runtime.
121 #[inline(always)]
122 unsafe fn leak(&self) -> &'static Self {
123 let address: usize = self.into();
124 address.into()
125 }
126
127 /// Converts a reference to the stream into a `'static` mutable reference.
128 ///
129 /// # Returns
130 ///
131 /// - `&'static mut Self`: A mutable reference to the stream with a `'static` lifetime.
132 ///
133 /// # Safety
134 ///
135 /// - The address is guaranteed to be a valid `Self` instance
136 /// that was previously converted from a reference and is managed by the runtime.
137 #[inline(always)]
138 unsafe fn leak_mut(&self) -> &'static mut Self {
139 let address: usize = self.into();
140 address.into()
141 }
142}
143
144impl Stream {
145 /// Checks if the connection should be kept alive.
146 ///
147 /// This method evaluates whether the connection should remain open based on
148 /// the closed state and the keep_alive parameter.
149 ///
150 /// # Arguments
151 ///
152 /// - `bool` - Whether keep-alive is enabled for the request.
153 ///
154 /// # Returns
155 ///
156 /// - `bool` - True if the connection should be kept alive, otherwise false.
157 #[inline(always)]
158 pub fn is_keep_alive(&self, keep_alive: bool) -> bool {
159 !self.get_closed() && keep_alive
160 }
161
162 /// Free the context.
163 ///
164 /// # Safety
165 ///
166 /// - The address is guaranteed to be a valid `Self` instance
167 /// that was previously converted from a reference and is managed by the runtime.
168 #[inline(always)]
169 pub unsafe fn free(&mut self) {
170 let _ = unsafe { Box::from_raw(self) };
171 }
172
173 /// Parses the HTTP request content from the stream.
174 ///
175 /// This is an internal helper function that performs the actual parsing.
176 ///
177 /// # Returns
178 ///
179 /// - `Result<Request, RequestError>`: The parsed request or an error.
180 async fn get_http_from_stream(&mut self) -> Result<Request, RequestError> {
181 let config: RequestConfig = *self.get_request_config();
182 let buffer_size: usize = config.get_buffer_size();
183 let max_path_size: usize = config.get_max_path_size();
184 let reader: &mut BufReader<&mut TcpStream> =
185 &mut BufReader::with_capacity(buffer_size, self.get_mut_stream());
186 let mut line: String = String::with_capacity(buffer_size);
187 AsyncBufReadExt::read_line(reader, &mut line).await?;
188 let (method, path, version): (RequestMethod, &str, RequestVersion) =
189 Request::get_http_first_line(&line)?;
190 Request::check_http_path_size(path, max_path_size)?;
191 let hash_index: Option<usize> = path.find(HASH);
192 let query_index: Option<usize> = path.find(QUERY);
193 let query: &str = Request::get_http_query(path, query_index, hash_index);
194 let querys: RequestQuerys = Request::get_http_querys(query);
195 let path: RequestPath = Request::get_http_path(path, query_index, hash_index);
196 let (headers, host, content_size): (RequestHeaders, RequestHost, usize) =
197 Request::get_http_headers(reader, &config).await?;
198 let body: RequestBody = Request::get_http_body(reader, content_size).await?;
199 Ok(Request {
200 method,
201 host,
202 version,
203 path,
204 querys,
205 headers,
206 body,
207 })
208 }
209
210 /// Parses an HTTP request from a TCP stream.
211 ///
212 /// Wraps the stream in a buffered reader and delegates to `http_from_reader`.
213 /// If the timeout is DEFAULT_LOW_SECURITY_READ_TIMEOUT_MS, no timeout is applied.
214 ///
215 /// # Returns
216 ///
217 /// - `Result<Request, RequestError>` - The parsed request or an error.
218 pub async fn try_get_http_request(&mut self) -> Result<Request, RequestError> {
219 let timeout_ms: u64 = self.get_request_config().get_read_timeout_ms();
220 if timeout_ms == DEFAULT_LOW_SECURITY_READ_TIMEOUT_MS {
221 return self.get_http_from_stream().await;
222 }
223 let duration: Duration = Duration::from_millis(timeout_ms);
224 timeout(duration, self.get_http_from_stream()).await?
225 }
226
227 /// Parses a WebSocket request from a TCP stream.
228 ///
229 /// Wraps the stream in a buffered reader and delegates to `ws_from_reader`.
230 /// If the timeout is DEFAULT_LOW_SECURITY_READ_TIMEOUT_MS, no timeout is applied.
231 ///
232 /// # Returns
233 ///
234 /// - `Result<Request, RequestError>`: The parsed WebSocket request or an error.
235 pub async fn try_get_websocket_request(&mut self) -> Result<RequestBody, RequestError> {
236 let config: RequestConfig = *self.get_request_config();
237 let buffer_size: usize = config.get_buffer_size();
238 let read_timeout_ms: u64 = config.get_read_timeout_ms();
239 let mut dynamic_buffer: Vec<u8> = Vec::with_capacity(buffer_size);
240 let mut temp_buffer: Vec<u8> = vec![0; buffer_size];
241 let mut full_frame: Vec<u8> = Vec::new();
242 let mut is_client_response: bool = false;
243 let duration_opt: Option<Duration> =
244 if read_timeout_ms == DEFAULT_LOW_SECURITY_READ_TIMEOUT_MS {
245 None
246 } else {
247 let adjusted_timeout_ms: u64 = (read_timeout_ms >> 1) + (read_timeout_ms & 1);
248 Some(Duration::from_millis(adjusted_timeout_ms))
249 };
250 loop {
251 let len: usize = match self
252 .get_websocket_from_stream(&mut temp_buffer, duration_opt, &mut is_client_response)
253 .await
254 {
255 Ok(Some(len)) => len,
256 Ok(None) => continue,
257 Err(error) => return Err(error),
258 };
259 if len == 0 {
260 return Err(RequestError::IncompleteWebSocketFrame(
261 HttpStatus::BadRequest,
262 ));
263 }
264 dynamic_buffer.extend_from_slice(&temp_buffer[..len]);
265 while let Some((frame, consumed)) = WebSocketFrame::decode_ws_frame(&dynamic_buffer) {
266 is_client_response = true;
267 dynamic_buffer.drain(0..consumed);
268 match frame.get_opcode() {
269 WebSocketOpcode::Close => {
270 return Err(RequestError::ClientClosedConnection(HttpStatus::BadRequest));
271 }
272 WebSocketOpcode::Ping | WebSocketOpcode::Pong => continue,
273 WebSocketOpcode::Text | WebSocketOpcode::Binary => {
274 match frame.build_full_frame(&mut full_frame) {
275 Ok(Some(result)) => return Ok(result),
276 Ok(None) => continue,
277 Err(error) => return Err(error),
278 }
279 }
280 _ => {
281 return Err(RequestError::WebSocketOpcodeUnsupported(
282 HttpStatus::NotImplemented,
283 ));
284 }
285 }
286 }
287 }
288 }
289
290 /// Reads data from the stream with optional timeout handling.
291 ///
292 /// # Arguments
293 ///
294 /// - `&mut [u8]`: The buffer to read data into.
295 /// - `Option<Duration>`: The optional timeout duration. If Some, timeout is applied; if None, no timeout.
296 /// - `&mut bool`: Mutable reference to track if we got a client response.
297 ///
298 /// # Returns
299 ///
300 /// - `Result<Option<usize>, RequestError>`: The number of bytes read, None for timeout/ping, or an error.
301 pub(crate) async fn get_websocket_from_stream(
302 &mut self,
303 buffer: &mut [u8],
304 duration_opt: Option<Duration>,
305 is_client_response: &mut bool,
306 ) -> Result<Option<usize>, RequestError> {
307 let stream: &mut TcpStream = self.get_mut_stream();
308 if let Some(duration) = duration_opt {
309 return match timeout(duration, stream.read(buffer)).await {
310 Ok(result) => match result {
311 Ok(len) => Ok(Some(len)),
312 Err(error) => Err(error.into()),
313 },
314 Err(error) => {
315 if !*is_client_response {
316 return Err(error.into());
317 }
318 *is_client_response = false;
319 self.try_send(&PING_FRAME).await?;
320 Ok(None)
321 }
322 };
323 }
324 match stream.read(buffer).await {
325 Ok(len) => Ok(Some(len)),
326 Err(error) => Err(error.into()),
327 }
328 }
329
330 /// Sends data over the stream.
331 ///
332 /// # Arguments
333 ///
334 /// - `AsRef<[u8]>` - The data to send (must implement AsRef<[u8]>).
335 ///
336 /// # Returns
337 ///
338 /// - `Result<(), ResponseError>` - Result indicating success or failure.
339 pub async fn try_send<D>(&mut self, data: D) -> Result<(), ResponseError>
340 where
341 D: AsRef<[u8]>,
342 {
343 Ok(self.get_mut_stream().write_all(data.as_ref()).await?)
344 }
345
346 /// Sends data over the stream.
347 ///
348 /// # Arguments
349 ///
350 /// - `AsRef<[u8]>` - The data to send (must implement AsRef<[u8]>).
351 ///
352 /// # Panics
353 ///
354 /// Panics if the write operation fails.
355 pub async fn send<D>(&mut self, data: D)
356 where
357 D: AsRef<[u8]>,
358 {
359 self.try_send(data).await.unwrap();
360 }
361
362 /// Sends multiple data.
363 ///
364 /// # Arguments
365 ///
366 /// - `IntoIterator<Item = AsRef<[u8]>>` - The data list to send.
367 ///
368 /// # Returns
369 ///
370 /// - `Result<(), ResponseError>` - Result indicating success or failure.
371 pub async fn try_send_list<I, D>(&mut self, data_iter: I) -> Result<(), ResponseError>
372 where
373 I: IntoIterator<Item = D>,
374 D: AsRef<[u8]>,
375 {
376 let stream: &mut TcpStream = self.get_mut_stream();
377 for data in data_iter {
378 stream.write_all(data.as_ref()).await?;
379 }
380 Ok(())
381 }
382
383 /// Sends multiple data.
384 ///
385 /// # Arguments
386 ///
387 /// - `IntoIterator<Item = AsRef<[u8]>>` - The data list to send.
388 ///
389 /// # Panics
390 ///
391 /// Panics if any write operation fails.
392 pub async fn send_list<I, D>(&mut self, data_iter: I)
393 where
394 I: IntoIterator<Item = D>,
395 D: AsRef<[u8]>,
396 {
397 self.try_send_list(data_iter).await.unwrap();
398 }
399
400 /// Flushes all buffered data to the stream.
401 ///
402 /// # Returns
403 ///
404 /// - `Result<(), ResponseError>` - Result indicating success or failure.
405 pub async fn try_flush(&mut self) -> Result<(), ResponseError> {
406 Ok(self.get_mut_stream().flush().await?)
407 }
408
409 /// Flushes all buffered data to the stream.
410 ///
411 /// # Panics
412 ///
413 /// Panics if the flush operation fails.
414 pub async fn flush(&mut self) {
415 self.try_flush().await.unwrap();
416 }
417}