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
//! Middleware for setting headers on HTTP responses.
//!
//! This module provides middleware for setting one or more headers on HTTP responses, either with fixed values or values determined dynamically from the response.
//!
//! # Single Header
//!
//! Use [`SetResponseHeaderLayer`] and [`SetResponseHeader`] to set a single header. The header value can be a fixed value or computed dynamically using a closure. See [`crate::set_header::MakeHeaderValue`] for details.
//!
//! ## Example: Fixed Value
//!
//! ```
//! use http::{Request, Response, header::{self, HeaderValue}};
//! use tower::{Service, ServiceExt, ServiceBuilder};
//! use tower_http::set_header::SetResponseHeaderLayer;
//! use http_body_util::Full;
//! use bytes::Bytes;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let render_html = tower::service_fn(|request: Request<Full<Bytes>>| async move {
//! # Ok::<_, std::convert::Infallible>(Response::new(request.into_body()))
//! # });
//!
//! let mut svc = ServiceBuilder::new()
//! .layer(
//! SetResponseHeaderLayer::if_not_present(
//! header::CONTENT_TYPE,
//! HeaderValue::from_static("text/html"),
//! )
//! )
//! .service(render_html);
//!
//! let request = Request::new(Full::default());
//!
//! let response = svc.ready().await?.call(request).await?;
//!
//! assert_eq!(response.headers()["content-type"], "text/html");
//! # Ok(())
//! # }
//! ```
//!
//! ## Example: Dynamic Value
//!
//! ```
//! use http::{Request, Response, header::{self, HeaderValue}};
//! use tower::{Service, ServiceExt, ServiceBuilder};
//! use tower_http::set_header::SetResponseHeaderLayer;
//! use bytes::Bytes;
//! use http_body_util::Full;
//! use http_body::Body as _; // for `Body::size_hint`
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let render_html = tower::service_fn(|request: Request<Full<Bytes>>| async move {
//! # Ok::<_, std::convert::Infallible>(Response::new(Full::from("1234567890")))
//! # });
//!
//! let mut svc = ServiceBuilder::new()
//! .layer(
//! SetResponseHeaderLayer::overriding(
//! header::CONTENT_LENGTH,
//! |response: &Response<Full<Bytes>>| {
//! if let Some(size) = response.body().size_hint().exact() {
//! Some(HeaderValue::from_str(&size.to_string()).unwrap())
//! } else {
//! None
//! }
//! }
//! )
//! )
//! .service(render_html);
//!
//! let request = Request::new(Full::default());
//!
//! let response = svc.ready().await?.call(request).await?;
//!
//! assert_eq!(response.headers()["content-length"], "10");
//! # Ok(())
//! # }
//! ```
//!
//! # Multiple Headers
//!
//! Use [`SetMultipleResponseHeadersLayer`] and [`SetMultipleResponseHeader`] to set multiple headers at once. Each header can have a fixed value or be computed dynamically.
//!
//! Note: this layer uses boxing (allocation + dynamic dispatch) to support mixed producer
//! types in a single vec. Stacking multiple [`SetResponseHeaderLayer`] avoids this at the
//! cost of a more complex composed service type.
//!
//! ## Example: Multiple Fixed Values
//!
//! ```
//! use http::{Request, Response, header::{self, HeaderValue}};
//! use tower::{Service, ServiceExt, ServiceBuilder};
//! use tower_http::set_header::{HeaderMetadata, response::{SetMultipleResponseHeadersLayer}};
//! use http_body_util::Full;
//! use bytes::Bytes;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let render_html = tower::service_fn(|request: Request<Full<Bytes>>| async move {
//! # Ok::<_, std::convert::Infallible>(Response::new(request.into_body()))
//! # });
//!
//! let mut svc = ServiceBuilder::new()
//! .layer(
//! SetMultipleResponseHeadersLayer::overriding(vec![
//! (header::CONTENT_TYPE, HeaderValue::from_static("text/html")).into(),
//! (header::CACHE_CONTROL, HeaderValue::from_static("no-cache")).into(),
//! ])
//! )
//! .service(render_html);
//!
//! let request = Request::new(Full::default());
//!
//! let response = svc.ready().await?.call(request).await?;
//!
//! assert_eq!(response.headers()["content-type"], "text/html");
//! assert_eq!(response.headers()["cache-control"], "no-cache");
//! # Ok(())
//! # }
//! ```
//!
//! ## Example: Multiple Dynamic Values
//!
//! ```
//! use http::{Request, Response, header::{self, HeaderValue}};
//! use tower::{Service, ServiceExt, ServiceBuilder};
//! use tower_http::set_header::{HeaderMetadata, response::{SetMultipleResponseHeadersLayer}};
//! use bytes::Bytes;
//! use http_body_util::Full;
//! use http_body::Body as _; // for `Body::size_hint`
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let render_html = tower::service_fn(|request: Request<Full<Bytes>>| async move {
//! # Ok::<_, std::convert::Infallible>(Response::new(Full::from("1234567890")))
//! # });
//!
//! let mut svc = ServiceBuilder::new()
//! .layer(
//! SetMultipleResponseHeadersLayer::overriding(vec![
//! (header::CONTENT_LENGTH, |response: &Response<Full<Bytes>>| {
//! if let Some(size) = response.body().size_hint().exact() {
//! Some(HeaderValue::from_str(&size.to_string()).unwrap())
//! } else {
//! None
//! }
//! }).into(),
//! ])
//! )
//! .service(render_html);
//!
//! let request = Request::new(Full::default());
//!
//! let response = svc.ready().await?.call(request).await?;
//!
//! assert_eq!(response.headers()["content-length"], "10");
//! # Ok(())
//! # }
//! ```
//!
//! # Modes
//!
//! - `overriding`: If a previous value exists for the same header, it is removed and replaced with the new value.
//! - `appending`: The new header is always added, preserving any existing values. If previous values exist, the header will have multiple values.
//! - `if_not_present`: If a previous value exists for the header, the new value is not inserted.
//!
//! See [`SetResponseHeaderLayer`], [`SetResponseHeader`], [`SetMultipleResponseHeadersLayer`], and [`SetMultipleResponseHeader`] for more details.
pub use ;
pub use ;