Skip to content

Commit 4ed7c93

Browse files
committed
remove manual header methods
1 parent a47a9b1 commit 4ed7c93

36 files changed

+368
-572
lines changed

src/auth/authorization.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::headers::{Header, HeaderName, HeaderValue, Headers, AUTHORIZATION};
2121
/// let authz = Authorization::new(scheme, credentials.into());
2222
///
2323
/// let mut res = Response::new(200);
24-
/// authz.apply(&mut res);
24+
/// authz.apply_header(&mut res);
2525
///
2626
/// let authz = Authorization::from_headers(res)?.unwrap();
2727
///
@@ -71,24 +71,6 @@ impl Authorization {
7171
}))
7272
}
7373

74-
/// Sets the header.
75-
pub fn apply(&self, mut headers: impl AsMut<Headers>) {
76-
headers.as_mut().insert(self.name(), self.value());
77-
}
78-
79-
/// Get the `HeaderName`.
80-
pub fn name(&self) -> HeaderName {
81-
self.header_name()
82-
}
83-
84-
/// Get the `HeaderValue`.
85-
pub fn value(&self) -> HeaderValue {
86-
let output = format!("{} {}", self.scheme, self.credentials);
87-
88-
// SAFETY: the internal string is validated to be ASCII.
89-
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
90-
}
91-
9274
/// Get the authorization scheme.
9375
pub fn scheme(&self) -> AuthenticationScheme {
9476
self.scheme
@@ -116,7 +98,10 @@ impl Header for Authorization {
11698
}
11799

118100
fn header_value(&self) -> HeaderValue {
119-
self.value()
101+
let output = format!("{} {}", self.scheme, self.credentials);
102+
103+
// SAFETY: the internal string is validated to be ASCII.
104+
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
120105
}
121106
}
122107

@@ -132,7 +117,7 @@ mod test {
132117
let authz = Authorization::new(scheme, credentials.into());
133118

134119
let mut headers = Headers::new();
135-
authz.apply(&mut headers);
120+
authz.apply_header(&mut headers);
136121

137122
let authz = Authorization::from_headers(headers)?.unwrap();
138123

src/auth/basic_auth.rs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
use crate::auth::{AuthenticationScheme, Authorization};
21
use crate::headers::{HeaderName, HeaderValue, Headers, AUTHORIZATION};
32
use crate::Status;
3+
use crate::{
4+
auth::{AuthenticationScheme, Authorization},
5+
headers::Header,
6+
};
47
use crate::{bail_status as bail, ensure_status as ensure};
58

69
/// HTTP Basic authorization.
@@ -22,7 +25,7 @@ use crate::{bail_status as bail, ensure_status as ensure};
2225
/// let authz = BasicAuth::new(username, password);
2326
///
2427
/// let mut res = Response::new(200);
25-
/// authz.apply(&mut res);
28+
/// authz.apply_header(&mut res);
2629
///
2730
/// let authz = BasicAuth::from_headers(res)?.unwrap();
2831
///
@@ -84,24 +87,6 @@ impl BasicAuth {
8487
Ok(Self { username, password })
8588
}
8689

87-
/// Sets the header.
88-
pub fn apply(&self, mut headers: impl AsMut<Headers>) {
89-
headers.as_mut().insert(self.name(), self.value());
90-
}
91-
92-
/// Get the `HeaderName`.
93-
pub fn name(&self) -> HeaderName {
94-
AUTHORIZATION
95-
}
96-
97-
/// Get the `HeaderValue`.
98-
pub fn value(&self) -> HeaderValue {
99-
let scheme = AuthenticationScheme::Basic;
100-
let credentials = base64::encode(format!("{}:{}", self.username, self.password));
101-
let auth = Authorization::new(scheme, credentials);
102-
auth.value()
103-
}
104-
10590
/// Get the username.
10691
pub fn username(&self) -> &str {
10792
self.username.as_str()
@@ -113,13 +98,16 @@ impl BasicAuth {
11398
}
11499
}
115100

116-
impl crate::headers::Header for BasicAuth {
101+
impl Header for BasicAuth {
117102
fn header_name(&self) -> HeaderName {
118103
AUTHORIZATION
119104
}
120105

121106
fn header_value(&self) -> HeaderValue {
122-
self.value()
107+
let scheme = AuthenticationScheme::Basic;
108+
let credentials = base64::encode(format!("{}:{}", self.username, self.password));
109+
let auth = Authorization::new(scheme, credentials);
110+
auth.header_value()
123111
}
124112
}
125113

@@ -135,7 +123,7 @@ mod test {
135123
let authz = BasicAuth::new(username, password);
136124

137125
let mut headers = Headers::new();
138-
authz.apply(&mut headers);
126+
authz.apply_header(&mut headers);
139127

140128
let authz = BasicAuth::from_headers(headers)?.unwrap();
141129

src/auth/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! let authz = BasicAuth::new(username, password);
1414
//!
1515
//! let mut res = Response::new(200);
16-
//! authz.apply(&mut res);
16+
//! authz.apply_header(&mut res);
1717
//!
1818
//! let authz = BasicAuth::from_headers(res)?.unwrap();
1919
//!

src/auth/www_authenticate.rs

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::auth::AuthenticationScheme;
21
use crate::bail_status as bail;
32
use crate::headers::{HeaderName, HeaderValue, Headers, WWW_AUTHENTICATE};
3+
use crate::{auth::AuthenticationScheme, headers::Header};
44

55
/// Define the authentication method that should be used to gain access to a
66
/// resource.
@@ -27,7 +27,7 @@ use crate::headers::{HeaderName, HeaderValue, Headers, WWW_AUTHENTICATE};
2727
/// let authz = WwwAuthenticate::new(scheme, realm.into());
2828
///
2929
/// let mut res = Response::new(200);
30-
/// authz.apply(&mut res);
30+
/// authz.apply_header(&mut res);
3131
///
3232
/// let authz = WwwAuthenticate::from_headers(res)?.unwrap();
3333
///
@@ -93,24 +93,6 @@ impl WwwAuthenticate {
9393
Ok(Some(Self { scheme, realm }))
9494
}
9595

96-
/// Sets the header.
97-
pub fn apply(&self, mut headers: impl AsMut<Headers>) {
98-
headers.as_mut().insert(self.name(), self.value());
99-
}
100-
101-
/// Get the `HeaderName`.
102-
pub fn name(&self) -> HeaderName {
103-
WWW_AUTHENTICATE
104-
}
105-
106-
/// Get the `HeaderValue`.
107-
pub fn value(&self) -> HeaderValue {
108-
let output = format!(r#"{} realm="{}", charset="UTF-8""#, self.scheme, self.realm);
109-
110-
// SAFETY: the internal string is validated to be ASCII.
111-
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
112-
}
113-
11496
/// Get the authorization scheme.
11597
pub fn scheme(&self) -> AuthenticationScheme {
11698
self.scheme
@@ -132,13 +114,16 @@ impl WwwAuthenticate {
132114
}
133115
}
134116

135-
impl crate::headers::Header for WwwAuthenticate {
117+
impl Header for WwwAuthenticate {
136118
fn header_name(&self) -> HeaderName {
137119
WWW_AUTHENTICATE
138120
}
139121

140122
fn header_value(&self) -> HeaderValue {
141-
self.value()
123+
let output = format!(r#"{} realm="{}", charset="UTF-8""#, self.scheme, self.realm);
124+
125+
// SAFETY: the internal string is validated to be ASCII.
126+
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
142127
}
143128
}
144129

@@ -154,7 +139,7 @@ mod test {
154139
let authz = WwwAuthenticate::new(scheme, realm.into());
155140

156141
let mut headers = Headers::new();
157-
authz.apply(&mut headers);
142+
authz.apply_header(&mut headers);
158143

159144
assert_eq!(
160145
headers["WWW-Authenticate"],

src/cache/age.rs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::headers::{HeaderName, HeaderValue, Headers, ToHeaderValues, AGE};
1+
use crate::headers::{Header, HeaderName, HeaderValue, Headers, ToHeaderValues, AGE};
22
use crate::Status;
33

44
use std::fmt::Debug;
@@ -22,7 +22,7 @@ use std::time::Duration;
2222
/// let age = Age::from_secs(12);
2323
///
2424
/// let mut res = Response::new(200);
25-
/// age.apply(&mut res);
25+
/// age.apply_header(&mut res);
2626
///
2727
/// let age = Age::from_headers(res)?.unwrap();
2828
/// assert_eq!(age, Age::from_secs(12));
@@ -67,41 +67,26 @@ impl Age {
6767

6868
Ok(Some(Self { dur }))
6969
}
70-
71-
/// Insert a `HeaderName` + `HeaderValue` pair into a `Headers` instance.
72-
pub fn apply(&self, mut headers: impl AsMut<Headers>) {
73-
headers.as_mut().insert(AGE, self.value());
74-
}
75-
76-
/// Get the `HeaderName`.
77-
pub fn name(&self) -> HeaderName {
78-
AGE
79-
}
80-
81-
/// Get the `HeaderValue`.
82-
pub fn value(&self) -> HeaderValue {
83-
let output = self.dur.as_secs().to_string();
84-
85-
// SAFETY: the internal string is validated to be ASCII.
86-
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
87-
}
8870
}
8971

9072
impl ToHeaderValues for Age {
9173
type Iter = option::IntoIter<HeaderValue>;
9274
fn to_header_values(&self) -> crate::Result<Self::Iter> {
9375
// A HeaderValue will always convert into itself.
94-
Ok(self.value().to_header_values().unwrap())
76+
Ok(self.header_value().to_header_values().unwrap())
9577
}
9678
}
9779

98-
impl crate::headers::Header for Age {
80+
impl Header for Age {
9981
fn header_name(&self) -> HeaderName {
10082
AGE
10183
}
10284

10385
fn header_value(&self) -> HeaderValue {
104-
self.value()
86+
let output = self.dur.as_secs().to_string();
87+
88+
// SAFETY: the internal string is validated to be ASCII.
89+
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
10590
}
10691
}
10792

@@ -115,7 +100,7 @@ mod test {
115100
let age = Age::new(Duration::from_secs(12));
116101

117102
let mut headers = Headers::new();
118-
age.apply(&mut headers);
103+
age.apply_header(&mut headers);
119104

120105
let age = Age::from_headers(headers)?.unwrap();
121106
assert_eq!(age, Age::new(Duration::from_secs(12)));

src/cache/cache_control/cache_control.rs

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
use crate::cache::CacheDirective;
1+
use headers::Header;
2+
23
use crate::headers::{HeaderName, HeaderValue, Headers, ToHeaderValues, CACHE_CONTROL};
4+
use crate::{cache::CacheDirective, headers};
35

46
use std::fmt::{self, Debug, Write};
57
use std::iter::Iterator;
@@ -20,7 +22,7 @@ use std::slice;
2022
/// entries.push(CacheDirective::NoStore);
2123
///
2224
/// let mut res = Response::new(200);
23-
/// entries.apply(&mut res);
25+
/// entries.apply_header(&mut res);
2426
///
2527
/// let entries = CacheControl::from_headers(res)?.unwrap();
2628
/// let mut entries = entries.iter();
@@ -59,31 +61,6 @@ impl CacheControl {
5961

6062
Ok(Some(Self { entries }))
6163
}
62-
63-
/// Sets the `Server-Timing` header.
64-
pub fn apply(&self, mut headers: impl AsMut<Headers>) {
65-
headers.as_mut().insert(CACHE_CONTROL, self.value());
66-
}
67-
68-
/// Get the `HeaderName`.
69-
pub fn name(&self) -> HeaderName {
70-
CACHE_CONTROL
71-
}
72-
73-
/// Get the `HeaderValue`.
74-
pub fn value(&self) -> HeaderValue {
75-
let mut output = String::new();
76-
for (n, directive) in self.entries.iter().enumerate() {
77-
let directive: HeaderValue = directive.clone().into();
78-
match n {
79-
0 => write!(output, "{}", directive).unwrap(),
80-
_ => write!(output, ", {}", directive).unwrap(),
81-
};
82-
}
83-
84-
// SAFETY: the internal string is validated to be ASCII.
85-
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
86-
}
8764
/// Push a directive into the list of entries.
8865
pub fn push(&mut self, directive: CacheDirective) {
8966
self.entries.push(directive);
@@ -104,12 +81,22 @@ impl CacheControl {
10481
}
10582
}
10683

107-
impl crate::headers::Header for CacheControl {
84+
impl Header for CacheControl {
10885
fn header_name(&self) -> HeaderName {
10986
CACHE_CONTROL
11087
}
11188
fn header_value(&self) -> HeaderValue {
112-
self.value()
89+
let mut output = String::new();
90+
for (n, directive) in self.entries.iter().enumerate() {
91+
let directive: HeaderValue = directive.clone().into();
92+
match n {
93+
0 => write!(output, "{}", directive).unwrap(),
94+
_ => write!(output, ", {}", directive).unwrap(),
95+
};
96+
}
97+
98+
// SAFETY: the internal string is validated to be ASCII.
99+
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
113100
}
114101
}
115102

@@ -206,7 +193,7 @@ impl ToHeaderValues for CacheControl {
206193
type Iter = option::IntoIter<HeaderValue>;
207194
fn to_header_values(&self) -> crate::Result<Self::Iter> {
208195
// A HeaderValue will always convert into itself.
209-
Ok(self.value().to_header_values().unwrap())
196+
Ok(self.header_value().to_header_values().unwrap())
210197
}
211198
}
212199

src/cache/cache_control/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub use cache_directive::CacheDirective;
1616
#[cfg(test)]
1717
mod test {
1818
use super::*;
19-
use crate::headers::{Headers, CACHE_CONTROL};
19+
use crate::headers::{Header, Headers, CACHE_CONTROL};
2020

2121
#[test]
2222
fn smoke() -> crate::Result<()> {
@@ -25,7 +25,7 @@ mod test {
2525
entries.push(CacheDirective::NoStore);
2626

2727
let mut headers = Headers::new();
28-
entries.apply(&mut headers);
28+
entries.apply_header(&mut headers);
2929

3030
let entries = CacheControl::from_headers(headers)?.unwrap();
3131
let mut entries = entries.iter();

0 commit comments

Comments
 (0)