Skip to main content

rustlavel_http/
request.rs

1use crate::cookie;
2use crate::headers::Headers;
3use crate::method::Method;
4use crate::url;
5use rustlavel_core::{Config, Context, Json};
6use std::any::{Any, TypeId};
7use std::collections::{BTreeMap, HashMap};
8use std::net::SocketAddr;
9
10/// An incoming request, already parsed and matched against a route.
11pub struct Request {
12    pub(crate) method: Method,
13    pub(crate) target: String,
14    pub(crate) path: String,
15    pub(crate) query: Vec<(String, String)>,
16    pub(crate) headers: Headers,
17    pub(crate) body: Vec<u8>,
18    pub(crate) params: BTreeMap<String, String>,
19    pub(crate) context: Context,
20    pub(crate) peer: Option<SocketAddr>,
21    pub(crate) route: Option<String>,
22    /// Values attached by middleware — the authenticated user, a request id.
23    extensions: HashMap<TypeId, Box<dyn Any + Send + Sync>>,
24    /// Parsed lazily on first access, since most requests never read a body.
25    parsed_body: Option<ParsedBody>,
26}
27
28enum ParsedBody {
29    Json(Json),
30    Form(Vec<(String, String)>),
31    None,
32}
33
34impl Request {
35    /// Build a request directly. This is what the test client and the server
36    /// parser both go through.
37    pub fn new(method: Method, target: impl Into<String>) -> Self {
38        let target = target.into();
39        let (path, query) = url::split_target(&target);
40        Request {
41            method,
42            path: path.to_string(),
43            query: url::parse_query(query),
44            target,
45            headers: Headers::new(),
46            body: Vec::new(),
47            params: BTreeMap::new(),
48            context: Context::default(),
49            peer: None,
50            route: None,
51            extensions: HashMap::new(),
52            parsed_body: None,
53        }
54    }
55
56    pub fn method(&self) -> Method {
57        self.method
58    }
59
60    /// The path with no query string: `/users/7`.
61    pub fn path(&self) -> &str {
62        &self.path
63    }
64
65    /// The raw request target, query string included.
66    pub fn target(&self) -> &str {
67        &self.target
68    }
69
70    /// The pattern this request matched: `/users/{id}`. Useful for metrics
71    /// that must not explode into one series per id.
72    pub fn route(&self) -> Option<&str> {
73        self.route.as_deref()
74    }
75
76    pub fn headers(&self) -> &Headers {
77        &self.headers
78    }
79
80    pub fn headers_mut(&mut self) -> &mut Headers {
81        &mut self.headers
82    }
83
84    pub fn header(&self, name: &str) -> Option<&str> {
85        self.headers.get(name)
86    }
87
88    pub fn body(&self) -> &[u8] {
89        &self.body
90    }
91
92    pub fn body_string(&self) -> String {
93        String::from_utf8_lossy(&self.body).into_owned()
94    }
95
96    pub fn context(&self) -> &Context {
97        &self.context
98    }
99
100    pub fn config(&self) -> &Config {
101        self.context.config()
102    }
103
104    /// A service registered on the application: `req.state::<Database>()`.
105    pub fn state<T: Send + Sync + 'static>(&self) -> Option<&T> {
106        self.context.state::<T>()
107    }
108
109    pub fn peer_addr(&self) -> Option<SocketAddr> {
110        self.peer
111    }
112
113    /// The client IP, honouring `X-Forwarded-For` when behind a proxy.
114    pub fn ip(&self) -> Option<String> {
115        if let Some(forwarded) = self.headers.get("x-forwarded-for")
116            && let Some(first) = forwarded.split(',').next() {
117                return Some(first.trim().to_string());
118            }
119        self.peer.map(|addr| addr.ip().to_string())
120    }
121
122    /// A route parameter: for `/users/{id}` matching `/users/7`, `param("id")`
123    /// is `"7"`.
124    pub fn param(&self, name: &str) -> Option<&str> {
125        self.params.get(name).map(String::as_str)
126    }
127
128    /// A route parameter parsed into a type, so a handler can ask for an id as
129    /// a number without unwrapping twice.
130    pub fn param_as<T: std::str::FromStr>(&self, name: &str) -> Option<T> {
131        self.param(name)?.parse().ok()
132    }
133
134    pub fn params(&self) -> &BTreeMap<String, String> {
135        &self.params
136    }
137
138    pub fn query(&self, name: &str) -> Option<&str> {
139        self.query.iter().find(|(key, _)| key == name).map(|(_, value)| value.as_str())
140    }
141
142    /// Every value for a repeated query key: `?tag=a&tag=b`.
143    pub fn query_all(&self, name: &str) -> Vec<&str> {
144        self.query
145            .iter()
146            .filter(|(key, _)| key == name)
147            .map(|(_, value)| value.as_str())
148            .collect()
149    }
150
151    pub fn query_pairs(&self) -> &[(String, String)] {
152        &self.query
153    }
154
155    pub fn content_type(&self) -> Option<&str> {
156        self.headers.content_type()
157    }
158
159    pub fn is_json(&self) -> bool {
160        self.content_type().is_some_and(|ct| ct.ends_with("json"))
161    }
162
163    /// Whether the client wants JSON back — an API client or a fetch() call.
164    pub fn wants_json(&self) -> bool {
165        self.is_json()
166            || self.headers.get("accept").is_some_and(|a| a.contains("application/json"))
167            || self.headers.get("x-requested-with").is_some_and(|x| x == "XMLHttpRequest")
168    }
169
170    /// The body parsed as JSON, or `None` if it is absent or malformed.
171    pub fn json(&mut self) -> Option<&Json> {
172        self.parse_body();
173        match self.parsed_body.as_ref()? {
174            ParsedBody::Json(value) => Some(value),
175            _ => None,
176        }
177    }
178
179    /// One input value, looked up in the JSON body, then the form body, then
180    /// the query string — the resolution order of Laravel's `$request->input()`.
181    pub fn input(&mut self, name: &str) -> Option<String> {
182        self.parse_body();
183        match self.parsed_body.as_ref() {
184            Some(ParsedBody::Json(value)) => {
185                if let Some(found) = value.get(name) {
186                    return Some(match found {
187                        Json::String(s) => s.clone(),
188                        Json::Null => String::new(),
189                        other => other.to_string(),
190                    });
191                }
192            }
193            Some(ParsedBody::Form(pairs)) => {
194                if let Some((_, value)) = pairs.iter().find(|(key, _)| key == name) {
195                    return Some(value.clone());
196                }
197            }
198            _ => {}
199        }
200        self.query(name).map(str::to_string)
201    }
202
203    /// All decoded form fields of a `application/x-www-form-urlencoded` body.
204    pub fn form(&mut self) -> &[(String, String)] {
205        self.parse_body();
206        match self.parsed_body.as_ref() {
207            Some(ParsedBody::Form(pairs)) => pairs,
208            _ => &[],
209        }
210    }
211
212    fn parse_body(&mut self) {
213        if self.parsed_body.is_some() {
214            return;
215        }
216        let parsed = match self.headers.content_type() {
217            _ if self.body.is_empty() => ParsedBody::None,
218            Some(ct) if ct.ends_with("json") => match std::str::from_utf8(&self.body) {
219                Ok(text) => Json::parse(text).map_or(ParsedBody::None, ParsedBody::Json),
220                Err(_) => ParsedBody::None,
221            },
222            Some("application/x-www-form-urlencoded") => {
223                ParsedBody::Form(url::parse_query(&String::from_utf8_lossy(&self.body)))
224            }
225            _ => ParsedBody::None,
226        };
227        self.parsed_body = Some(parsed);
228    }
229
230    pub fn cookies(&self) -> BTreeMap<String, String> {
231        self.headers.get("cookie").map(cookie::parse_header).unwrap_or_default()
232    }
233
234    pub fn cookie(&self, name: &str) -> Option<String> {
235        self.cookies().remove(name)
236    }
237
238    /// Attach a value for later middleware or the handler to read.
239    pub fn extend<T: Send + Sync + 'static>(&mut self, value: T) {
240        self.extensions.insert(TypeId::of::<T>(), Box::new(value));
241    }
242
243    /// The API version this request is for — from the route's
244    /// [`Router::version`](crate::Router::version) group, or from the
245    /// [`VersionHeader`](crate::versioning::VersionHeader) middleware.
246    pub fn api_version(&self) -> Option<&str> {
247        self.extension::<crate::versioning::ApiVersion>().map(|v| v.0.as_str())
248    }
249
250    /// The identifier the [`RequestId`](crate::request_id::RequestId)
251    /// middleware assigned, for log lines and error reports.
252    pub fn request_id(&self) -> Option<&str> {
253        self.extension::<crate::request_id::Assigned>().map(|id| id.0.as_str())
254    }
255
256    /// Read a value attached by earlier middleware.
257    pub fn extension<T: Send + Sync + 'static>(&self) -> Option<&T> {
258        self.extensions.get(&TypeId::of::<T>()).and_then(|value| value.downcast_ref::<T>())
259    }
260
261    // --- Builders, used by the server, the router, and the test client. ---
262
263    pub fn with_header(mut self, name: &str, value: impl Into<String>) -> Self {
264        self.headers.set(name, value);
265        self
266    }
267
268    pub fn with_body(mut self, body: impl Into<Vec<u8>>) -> Self {
269        self.body = body.into();
270        self.parsed_body = None;
271        self
272    }
273
274    pub fn with_json(self, value: Json) -> Self {
275        self.with_header("content-type", "application/json").with_body(value.to_string())
276    }
277
278    pub fn with_form(self, fields: &[(&str, &str)]) -> Self {
279        let encoded = fields
280            .iter()
281            .map(|(key, value)| format!("{}={}", url::encode(key), url::encode(value)))
282            .collect::<Vec<_>>()
283            .join("&");
284        self.with_header("content-type", "application/x-www-form-urlencoded").with_body(encoded)
285    }
286
287    pub fn with_context(mut self, context: Context) -> Self {
288        self.context = context;
289        self
290    }
291
292    pub(crate) fn set_params(&mut self, params: BTreeMap<String, String>) {
293        self.params = params;
294    }
295}
296
297impl std::fmt::Debug for Request {
298    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
299        f.debug_struct("Request")
300            .field("method", &self.method)
301            .field("target", &self.target)
302            .field("headers", &self.headers)
303            .field("body_len", &self.body.len())
304            .finish()
305    }
306}
307
308#[cfg(test)]
309mod tests {
310    use super::*;
311
312    #[test]
313    fn splits_path_and_query() {
314        let request = Request::new(Method::Get, "/users?page=2&tag=a&tag=b");
315
316        assert_eq!(request.path(), "/users");
317        assert_eq!(request.query("page"), Some("2"));
318        assert_eq!(request.query_all("tag"), ["a", "b"]);
319        assert_eq!(request.query("missing"), None);
320    }
321
322    #[test]
323    fn input_prefers_the_body_over_the_query() {
324        let mut request = Request::new(Method::Post, "/users?name=from-query")
325            .with_json(Json::object([("name", "from-body".into())]));
326
327        assert_eq!(request.input("name").as_deref(), Some("from-body"));
328        // A key absent from the body still falls through to the query string.
329        assert_eq!(request.input("missing"), None);
330    }
331
332    #[test]
333    fn reads_urlencoded_form_bodies() {
334        let mut request =
335            Request::new(Method::Post, "/login").with_form(&[("email", "a@b.com"), ("password", "s e c")]);
336
337        assert_eq!(request.input("email").as_deref(), Some("a@b.com"));
338        assert_eq!(request.input("password").as_deref(), Some("s e c"));
339        assert_eq!(request.form().len(), 2);
340    }
341
342    #[test]
343    fn parses_cookies_from_the_header() {
344        let request = Request::new(Method::Get, "/").with_header("cookie", "session=abc; theme=dark");
345
346        assert_eq!(request.cookie("session").as_deref(), Some("abc"));
347        assert_eq!(request.cookies().len(), 2);
348    }
349
350    #[test]
351    fn extensions_round_trip_through_middleware() {
352        struct User(&'static str);
353        let mut request = Request::new(Method::Get, "/");
354        request.extend(User("ada"));
355
356        assert_eq!(request.extension::<User>().unwrap().0, "ada");
357    }
358
359    #[test]
360    fn forwarded_header_wins_over_socket_address() {
361        let request = Request::new(Method::Get, "/").with_header("x-forwarded-for", "203.0.113.9, 10.0.0.1");
362        assert_eq!(request.ip().as_deref(), Some("203.0.113.9"));
363    }
364
365    #[test]
366    fn detects_clients_that_want_json() {
367        let api = Request::new(Method::Get, "/").with_header("accept", "application/json");
368        let browser = Request::new(Method::Get, "/").with_header("accept", "text/html");
369
370        assert!(api.wants_json());
371        assert!(!browser.wants_json());
372    }
373}