1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//! HTTP request type and helper methods.
//!
//! Cot uses the [`Request`](http::Request) type from the [`http`] crate
//! to represent incoming HTTP requests. However, it also provides a
//! [`RequestExt`] trait that contains various helper methods for working with
//! HTTP requests. These methods are used to access the application context,
//! project configuration, path parameters, and more. You probably want to have
//! a `use` statement for [`RequestExt`] in your code most of the time to be
//! able to use these functions:
//!
//! ```
//! use cot::request::RequestExt;
//! ```
use std::future::Future;
use std::sync::Arc;
use cot_core::request::{AppName, InvalidContentType, RouteName};
#[doc(inline)]
pub use cot_core::request::{PathParams, PathParamsDeserializerError, Request, RequestHead};
use http::Extensions;
use crate::Result;
use crate::request::extractors::FromRequestHead;
use crate::router::Router;
pub mod extractors;
mod private {
pub trait Sealed {}
}
/// Extension trait for [`http::Request`] that provides helper methods for
/// working with HTTP requests.
///
/// # Sealed
///
/// This trait is sealed since it doesn't make sense to be implemented for types
/// outside the context of Cot.
pub trait RequestExt: private::Sealed {
/// Runs an extractor implementing [`FromRequestHead`] on the request.
///
/// # Examples
///
/// ```
/// use cot::request::extractors::Path;
/// use cot::request::{Request, RequestExt};
/// use cot::response::Response;
///
/// async fn my_handler(mut request: Request) -> cot::Result<Response> {
/// let path_params = request.extract_from_head::<Path<String>>().await?;
/// // ...
/// # unimplemented!()
/// }
/// ```
fn extract_from_head<E>(&mut self) -> impl Future<Output = Result<E>> + Send
where
E: FromRequestHead + 'static;
/// Get the application context.
///
/// # Examples
///
/// ```
/// use cot::request::{Request, RequestExt};
/// use cot::response::Response;
///
/// async fn my_handler(mut request: Request) -> cot::Result<Response> {
/// let context = request.context();
/// // ... do something with the context
/// # unimplemented!()
/// }
/// ```
#[must_use]
fn context(&self) -> &crate::ProjectContext;
/// Get the project configuration.
///
/// # Examples
///
/// ```
/// use cot::request::{Request, RequestExt};
/// use cot::response::Response;
///
/// async fn my_handler(mut request: Request) -> cot::Result<Response> {
/// let config = request.project_config();
/// // ... do something with the config
/// # unimplemented!()
/// }
/// ```
#[must_use]
fn project_config(&self) -> &crate::config::ProjectConfig;
/// Get the router.
///
/// # Examples
///
/// ```
/// use cot::request::{Request, RequestExt};
/// use cot::response::Response;
///
/// async fn my_handler(mut request: Request) -> cot::Result<Response> {
/// let router = request.router();
/// // ... do something with the router
/// # unimplemented!()
/// }
/// ```
#[must_use]
fn router(&self) -> &Arc<Router>;
/// Returns the name of the app that the current route belongs to, or
/// [`None`] if the request is not routed.
///
/// This is mainly useful for providing context to reverse redirects, where
/// you want to redirect to a route in the same app.
///
/// # Examples
///
/// ```
/// use cot::request::{Request, RequestExt};
/// use cot::response::Response;
///
/// async fn my_handler(mut request: Request) -> cot::Result<Response> {
/// let app_name = request.app_name();
/// // ... do something with the app name
/// # unimplemented!()
/// }
/// ```
fn app_name(&self) -> Option<&str>;
/// Returns the name of the current route.
///
/// This returns [`None`] if:
/// - the request is not routed, or
/// - the route has no name.
///
/// This is mainly useful for use in templates, where you want to know which
/// route is being rendered, for instance to mark the active tab.
///
/// # Examples
///
/// ```
/// use cot::request::{Request, RequestExt};
/// use cot::response::Response;
///
/// async fn my_handler(mut request: Request) -> cot::Result<Response> {
/// let route_name = request.route_name();
/// // ... do something with the route name
/// # unimplemented!()
/// }
/// ```
#[must_use]
fn route_name(&self) -> Option<&str>;
/// Get the path parameters.
///
/// # Examples
///
/// ```
/// use cot::request::{Request, RequestExt};
/// use cot::response::Response;
///
/// async fn my_handler(mut request: Request) -> cot::Result<Response> {
/// let path_params = request.path_params();
/// // ... do something with the path params
/// # unimplemented!()
/// }
/// ```
#[must_use]
fn path_params(&self) -> &PathParams;
/// Get the path parameters mutably.
///
/// # Examples
///
/// ```
/// use cot::request::{Request, RequestExt};
/// use cot::response::Response;
///
/// async fn my_handler(mut request: Request) -> cot::Result<Response> {
/// let path_params = request.path_params_mut();
/// // ... do something with the path params
/// # unimplemented!()
/// }
/// ```
#[must_use]
fn path_params_mut(&mut self) -> &mut PathParams;
/// Get the content type of the request.
///
/// # Examples
///
/// ```
/// use cot::request::{Request, RequestExt};
/// use cot::response::Response;
///
/// async fn my_handler(mut request: Request) -> cot::Result<Response> {
/// let content_type = request.content_type();
/// // ... do something with the content type
/// # unimplemented!()
/// }
/// ```
#[must_use]
fn content_type(&self) -> Option<&http::HeaderValue>;
/// Expect the content type of the request to be the given value.
///
/// # Errors
///
/// Returns an error if the content type is not the expected value.
///
/// # Examples
///
/// ```
/// use cot::request::{Request, RequestExt};
/// use cot::response::Response;
///
/// async fn my_handler(mut request: Request) -> cot::Result<Response> {
/// request.expect_content_type("application/json")?;
/// // ...
/// # unimplemented!()
/// }
/// ```
fn expect_content_type(&mut self, expected: &'static str) -> Result<()> {
let content_type = self
.content_type()
.map_or("".into(), |value| String::from_utf8_lossy(value.as_bytes()));
if content_type == expected {
Ok(())
} else {
Err(InvalidContentType {
expected,
actual: content_type.into_owned(),
}
.into())
}
}
#[doc(hidden)]
fn extensions(&self) -> &Extensions;
}
impl private::Sealed for Request {}
impl RequestExt for Request {
async fn extract_from_head<E>(&mut self) -> Result<E>
where
E: FromRequestHead + 'static,
{
let request = std::mem::take(self);
let (head, body) = request.into_parts();
let result = E::from_request_head(&head).await;
*self = Request::from_parts(head, body);
result
}
#[track_caller]
fn context(&self) -> &crate::ProjectContext {
self.extensions()
.get::<Arc<crate::ProjectContext>>()
.expect("AppContext extension missing")
}
fn project_config(&self) -> &crate::config::ProjectConfig {
self.context().config()
}
fn router(&self) -> &Arc<Router> {
self.context().router()
}
fn app_name(&self) -> Option<&str> {
self.extensions()
.get::<AppName>()
.map(|AppName(name)| name.as_str())
}
fn route_name(&self) -> Option<&str> {
self.extensions()
.get::<RouteName>()
.map(|RouteName(name)| name.as_str())
}
#[track_caller]
fn path_params(&self) -> &PathParams {
self.extensions()
.get::<PathParams>()
.expect("PathParams extension missing")
}
fn path_params_mut(&mut self) -> &mut PathParams {
self.extensions_mut().get_or_insert_default::<PathParams>()
}
fn content_type(&self) -> Option<&http::HeaderValue> {
self.headers().get(http::header::CONTENT_TYPE)
}
fn extensions(&self) -> &Extensions {
self.extensions()
}
}
impl private::Sealed for RequestHead {}
impl RequestExt for RequestHead {
async fn extract_from_head<E>(&mut self) -> Result<E>
where
E: FromRequestHead + 'static,
{
E::from_request_head(self).await
}
fn context(&self) -> &crate::ProjectContext {
self.extensions
.get::<Arc<crate::ProjectContext>>()
.expect("AppContext extension missing")
}
fn project_config(&self) -> &crate::config::ProjectConfig {
self.context().config()
}
fn router(&self) -> &Arc<Router> {
self.context().router()
}
fn app_name(&self) -> Option<&str> {
self.extensions
.get::<AppName>()
.map(|AppName(name)| name.as_str())
}
fn route_name(&self) -> Option<&str> {
self.extensions
.get::<RouteName>()
.map(|RouteName(name)| name.as_str())
}
fn path_params(&self) -> &PathParams {
self.extensions
.get::<PathParams>()
.expect("PathParams extension missing")
}
fn path_params_mut(&mut self) -> &mut PathParams {
self.extensions.get_or_insert_default::<PathParams>()
}
fn content_type(&self) -> Option<&http::HeaderValue> {
self.headers.get(http::header::CONTENT_TYPE)
}
fn extensions(&self) -> &Extensions {
&self.extensions
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Body;
use crate::request::extractors::Path;
use crate::response::Response;
use crate::router::{Route, Router};
use crate::test::TestRequestBuilder;
#[test]
fn request_ext_app_name() {
let mut request = TestRequestBuilder::get("/").build();
assert_eq!(request.app_name(), None);
request
.extensions_mut()
.insert(AppName("test_app".to_string()));
assert_eq!(request.app_name(), Some("test_app"));
}
#[test]
fn request_ext_route_name() {
let mut request = TestRequestBuilder::get("/").build();
assert_eq!(request.route_name(), None);
request
.extensions_mut()
.insert(RouteName("test_route".to_string()));
assert_eq!(request.route_name(), Some("test_route"));
}
#[test]
fn request_ext_parts_route_name() {
let request = TestRequestBuilder::get("/").build();
let (mut head, _body) = request.into_parts();
assert_eq!(head.route_name(), None);
head.extensions.insert(RouteName("test_route".to_string()));
assert_eq!(head.route_name(), Some("test_route"));
}
#[test]
fn request_ext_path_params() {
let mut request = TestRequestBuilder::get("/").build();
let mut params = PathParams::new();
params.insert("id".to_string(), "42".to_string());
request.extensions_mut().insert(params);
assert_eq!(request.path_params().get("id"), Some("42"));
}
#[test]
fn request_ext_path_params_mut() {
let mut request = TestRequestBuilder::get("/").build();
request
.path_params_mut()
.insert("id".to_string(), "42".to_string());
assert_eq!(request.path_params().get("id"), Some("42"));
}
#[test]
fn request_ext_content_type() {
let mut request = TestRequestBuilder::get("/").build();
assert_eq!(request.content_type(), None);
request.headers_mut().insert(
http::header::CONTENT_TYPE,
http::HeaderValue::from_static("text/plain"),
);
assert_eq!(
request.content_type(),
Some(&http::HeaderValue::from_static("text/plain"))
);
}
#[test]
fn request_ext_expect_content_type() {
let mut request = TestRequestBuilder::get("/").build();
// Should fail with no content type
assert!(request.expect_content_type("text/plain").is_err());
request.headers_mut().insert(
http::header::CONTENT_TYPE,
http::HeaderValue::from_static("text/plain"),
);
// Should succeed with matching content type
assert!(request.expect_content_type("text/plain").is_ok());
// Should fail with non-matching content type
assert!(request.expect_content_type("application/json").is_err());
}
#[cot::test]
async fn request_ext_extract_from_head() {
async fn handler(mut request: Request) -> Result<Response> {
let Path(id): Path<String> = request.extract_from_head().await?;
assert_eq!(id, "42");
Ok(Response::new(Body::empty()))
}
let router = Router::with_urls([Route::with_handler("/{id}/", handler)]);
let request = TestRequestBuilder::get("/42/")
.router(router.clone())
.build();
router.handle(request).await.unwrap();
}
#[test]
fn parts_ext_path_params() {
let (mut head, _) = Request::new(Body::empty()).into_parts();
let mut params = PathParams::new();
params.insert("id".to_string(), "42".to_string());
head.extensions.insert(params);
assert_eq!(head.path_params().get("id"), Some("42"));
}
#[test]
fn parts_ext_mutating_path_params() {
let (mut head, _) = Request::new(Body::empty()).into_parts();
head.path_params_mut()
.insert("page".to_string(), "1".to_string());
assert_eq!(head.path_params().get("page"), Some("1"));
}
#[test]
fn parts_ext_app_name() {
let (mut head, _) = Request::new(Body::empty()).into_parts();
head.extensions.insert(AppName("test_app".to_string()));
assert_eq!(head.app_name(), Some("test_app"));
}
#[test]
fn parts_ext_route_name() {
let (mut head, _) = Request::new(Body::empty()).into_parts();
head.extensions.insert(RouteName("test_route".to_string()));
assert_eq!(head.route_name(), Some("test_route"));
}
#[test]
fn parts_ext_content_type() {
let (mut head, _) = Request::new(Body::empty()).into_parts();
head.headers.insert(
http::header::CONTENT_TYPE,
http::HeaderValue::from_static("text/plain"),
);
assert_eq!(
head.content_type(),
Some(&http::HeaderValue::from_static("text/plain"))
);
}
#[cot::test]
async fn path_extract_from_head() {
let (mut head, _) = Request::new(Body::empty()).into_parts();
let mut params = PathParams::new();
params.insert("id".to_string(), "42".to_string());
head.extensions.insert(params);
let Path(id): Path<String> = head.extract_from_head().await.unwrap();
assert_eq!(id, "42");
}
}