Skip to main content

base_http/
request.rs

1// Released under the MIT License.
2// Copyright, 2026, by Samuel Williams.
3
4use crate::{Authority, Fields, Method, Path, Protocol, Scheme, Version, body::EmptyBody};
5use std::ops::{Deref, DerefMut};
6
7/// Protocol-independent request metadata. Components are preserved as supplied,
8/// except that schemes and authorities are stored lowercase; drivers validate
9/// target combinations and wire syntax before transmission.
10#[derive(Clone, Debug)]
11pub struct RequestMetadata {
12    pub method: Method,
13    pub scheme: Option<Scheme>,
14    pub authority: Option<Authority>,
15    /// Path and optional query, or the supplied request-target form. `None`
16    /// represents a request such as traditional CONNECT with no path.
17    pub path: Option<Path>,
18    pub version: Option<Version>,
19    pub fields: Fields,
20    /// Declared representation length, independent of whether this message has
21    /// readable data (for example HEAD and 304 responses).
22    pub content_length: Option<u64>,
23    /// Outgoing connection reuse preference. Received hop-specific decisions
24    /// are retained by the protocol exchange, rather than forwarded as fields.
25    pub persistent: Option<bool>,
26    pub protocol: Option<Protocol>,
27}
28
29impl RequestMetadata {
30    pub fn new(method: Method) -> Self {
31        Self {
32            method,
33            scheme: None,
34            authority: None,
35            path: None,
36            version: None,
37            fields: Fields::new(),
38            content_length: None,
39            persistent: None,
40            protocol: None,
41        }
42    }
43}
44
45/// A semantic request with an arbitrary body owner. The metadata is also accessible
46/// through dereferencing, e.g. `request.fields` and `request.method`.
47#[derive(Clone, Debug)]
48pub struct Request<B = EmptyBody> {
49    pub metadata: RequestMetadata,
50    pub body: B,
51}
52
53impl<B> Request<B> {
54    pub fn new(method: Method, path: impl Into<Path>, body: B) -> Self {
55        let mut metadata = RequestMetadata::new(method);
56        metadata.path = Some(path.into());
57        Self { metadata, body }
58    }
59
60    pub fn from_parts(metadata: RequestMetadata, body: B) -> Self {
61        Self { metadata, body }
62    }
63    pub fn into_parts(self) -> (RequestMetadata, B) {
64        (self.metadata, self.body)
65    }
66    /// Move the body through `map` without cloning or rebuilding the metadata.
67    pub fn map_body<T>(self, map: impl FnOnce(B) -> T) -> Request<T> {
68        Request {
69            metadata: self.metadata,
70            body: map(self.body),
71        }
72    }
73    /// Replace and drop the previous body. This does not drain it or establish
74    /// asynchronous exchange cleanup; an I/O driver retains that responsibility.
75    pub fn with_body<T>(self, body: T) -> Request<T> {
76        self.map_body(|_| body)
77    }
78    pub fn is_head(&self) -> bool {
79        self.method == Method::HEAD
80    }
81    pub fn is_connect(&self) -> bool {
82        self.method == Method::CONNECT
83    }
84}
85
86impl Request<EmptyBody> {
87    pub fn get(path: impl Into<Path>) -> Self {
88        Self::new(Method::GET, path, EmptyBody)
89    }
90}
91
92impl<B> Deref for Request<B> {
93    type Target = RequestMetadata;
94    fn deref(&self) -> &Self::Target {
95        &self.metadata
96    }
97}
98impl<B> DerefMut for Request<B> {
99    fn deref_mut(&mut self) -> &mut Self::Target {
100        &mut self.metadata
101    }
102}
103
104impl<B: crate::body::ReadableBody> Request<B> {
105    /// Read an owned body frame. Received fields stay in the frame; the caller
106    /// can forward them or explicitly append them with `HttpHeaders::append_trailers`.
107    pub async fn read(
108        &mut self,
109        cancellation: &base_scheduler::Cancellation,
110    ) -> Result<crate::body::BodyFrame<B::Data>, B::Error> {
111        self.body.read(cancellation).await
112    }
113}