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
//! Middleware next-handler for chaining middleware functions in the request/response pipeline.
//!
//! This module provides the [`Next`] struct which allows middleware functions to
//! pass control to the next middleware in the chain. It also manages task-local
//! storage for pending headers and cookies that are collected during middleware
//! execution and applied to the final response.
use crate::;
use RefCell;
task_local!
/// A marker type for calling the next middleware in the chain.
///
/// `Next` is passed to middleware functions to allow them to invoke the
/// next middleware and/or the final route handler. When invoked, it collects
/// any headers and cookies from the response and stores them in task-local
/// storage for later application to the final HTTP response.
///
/// # Construction
///
/// `Next` can be constructed via `Next::default()` or `Default::default()`:
///
/// ```ignore
/// let next = Next::default();
/// let next = Default::default();
/// ```
///
/// # Example
///
/// ```ignore
/// async fn logging_middleware(
/// req: HttpRequest,
/// res: HttpResponse,
/// next: Next,
/// ) -> (HttpRequest, Option<HttpResponse>) {
/// println!("Request: {}", req.path());
/// let (req, res) = next.call(req, res).await;
/// println!("Response status: {:?}", res.as_ref().map(|r| r.status()));
/// (req, res)
/// }
/// ```
;