1
1
//! HTTP Security Headers.
2
2
//!
3
- // // ! ## Example
4
- // // !
5
- // // ! ```
6
- // // ! use http_types::Response;
7
- // // !
8
- // // ! let mut res = Response::new(StatusCode::Ok);
9
- // // ! http_types::security::default(&mut res);
3
+ //! ## Example
4
+ //!
5
+ //! ```
6
+ //! use http_types::Response;
7
+ //!
8
+ //! let mut res = Response::new(StatusCode::Ok);
9
+ //! http_types::security::default(&mut res);
10
10
// //! assert_eq!(res["X-Content-Type-Options"], "nosniff");
11
11
// //! assert_eq!(res["X-XSS-Protection"], "1; mode=block");
12
- // //! ```
13
- use crate :: headers:: { HeaderName , Headers } ;
12
+ //! ```
13
+
14
+ use crate :: headers:: { HeaderName , HeaderValue , Headers } ;
14
15
pub use csp:: { ContentSecurityPolicy , ReportTo , ReportToEndpoint , Source } ;
15
16
16
17
mod csp;
@@ -30,7 +31,7 @@ pub fn default(mut headers: impl AsMut<Headers>) {
30
31
dns_prefetch_control ( & mut headers) ;
31
32
nosniff ( & mut headers) ;
32
33
frameguard ( & mut headers, None ) ;
33
- hide_powered_by ( & mut headers) ;
34
+ powered_by ( & mut headers, None ) ;
34
35
hsts ( & mut headers) ;
35
36
xss_filter ( & mut headers) ;
36
37
}
@@ -100,10 +101,16 @@ pub fn frameguard(mut headers: impl AsMut<Headers>, guard: Option<FrameOptions>)
100
101
// /// assert_eq!(headers.get("X-Powered-By"), None);
101
102
// /// ```
102
103
#[ inline]
103
- pub fn hide_powered_by ( mut headers : impl AsMut < Headers > ) {
104
- headers
105
- . as_mut ( )
106
- . remove ( & HeaderName :: from_lowercase_str ( "X-Powered-By" ) ) ;
104
+ pub fn powered_by ( mut headers : impl AsMut < Headers > , value : Option < HeaderValue > ) {
105
+ let name = HeaderName :: from_lowercase_str ( "X-Powered-By" ) ;
106
+ match value {
107
+ Some ( value) => {
108
+ headers. as_mut ( ) . insert ( name, value) . unwrap ( ) ;
109
+ }
110
+ None => {
111
+ headers. as_mut ( ) . remove ( & name) ;
112
+ }
113
+ } ;
107
114
}
108
115
109
116
/// Sets the `Strict-Transport-Security` header to keep your users on `HTTPS`.
0 commit comments