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
use std::any::Any;
use std::fmt;
use std::str::{FromStr, from_utf8};
use std::ops::{Deref, DerefMut};
use header::{Header, Raw, Scheme};
#[derive(Clone, PartialEq, Debug)]
pub struct ProxyAuthorization<S: Scheme>(pub S);
impl<S: Scheme> Deref for ProxyAuthorization<S> {
type Target = S;
fn deref(&self) -> &S {
&self.0
}
}
impl<S: Scheme> DerefMut for ProxyAuthorization<S> {
fn deref_mut(&mut self) -> &mut S {
&mut self.0
}
}
impl<S: Scheme + Any> Header for ProxyAuthorization<S> where <S as FromStr>::Err: 'static {
fn header_name() -> &'static str {
static NAME: &'static str = "Proxy-Authorization";
NAME
}
fn parse_header(raw: &Raw) -> ::Result<ProxyAuthorization<S>> {
if let Some(line) = raw.one() {
let header = try!(from_utf8(line));
if let Some(scheme) = <S as Scheme>::scheme() {
if header.starts_with(scheme) && header.len() > scheme.len() + 1 {
match header[scheme.len() + 1..].parse::<S>().map(ProxyAuthorization) {
Ok(h) => Ok(h),
Err(_) => Err(::Error::Header)
}
} else {
Err(::Error::Header)
}
} else {
match header.parse::<S>().map(ProxyAuthorization) {
Ok(h) => Ok(h),
Err(_) => Err(::Error::Header)
}
}
} else {
Err(::Error::Header)
}
}
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
f.fmt_line(self)
}
}
impl<S: Scheme> fmt::Display for ProxyAuthorization<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(scheme) = <S as Scheme>::scheme() {
try!(write!(f, "{} ", scheme))
};
self.0.fmt_scheme(f)
}
}
#[cfg(test)]
mod tests {
use super::ProxyAuthorization;
use super::super::super::{Headers, Header, Basic, Bearer};
#[test]
fn test_raw_auth() {
let mut headers = Headers::new();
headers.set(ProxyAuthorization("foo bar baz".to_owned()));
assert_eq!(headers.to_string(), "Proxy-Authorization: foo bar baz\r\n".to_owned());
}
#[test]
fn test_raw_auth_parse() {
let header: ProxyAuthorization<String> = Header::parse_header(&b"foo bar baz".as_ref().into()).unwrap();
assert_eq!(header.0, "foo bar baz");
}
#[test]
fn test_basic_auth() {
let mut headers = Headers::new();
headers.set(ProxyAuthorization(
Basic { username: "Aladdin".to_owned(), password: Some("open sesame".to_owned()) }));
assert_eq!(
headers.to_string(),
"Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==\r\n".to_owned());
}
#[test]
fn test_basic_auth_no_password() {
let mut headers = Headers::new();
headers.set(ProxyAuthorization(Basic { username: "Aladdin".to_owned(), password: None }));
assert_eq!(headers.to_string(), "Proxy-Authorization: Basic QWxhZGRpbjo=\r\n".to_owned());
}
#[test]
fn test_basic_auth_parse() {
let auth: ProxyAuthorization<Basic> = Header::parse_header(
&b"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==".as_ref().into()).unwrap();
assert_eq!(auth.0.username, "Aladdin");
assert_eq!(auth.0.password, Some("open sesame".to_owned()));
}
#[test]
fn test_basic_auth_parse_no_password() {
let auth: ProxyAuthorization<Basic> = Header::parse_header(
&b"Basic QWxhZGRpbjo=".as_ref().into()).unwrap();
assert_eq!(auth.0.username, "Aladdin");
assert_eq!(auth.0.password, Some("".to_owned()));
}
#[test]
fn test_bearer_auth() {
let mut headers = Headers::new();
headers.set(ProxyAuthorization(
Bearer { token: "fpKL54jvWmEGVoRdCNjG".to_owned() }));
assert_eq!(
headers.to_string(),
"Proxy-Authorization: Bearer fpKL54jvWmEGVoRdCNjG\r\n".to_owned());
}
#[test]
fn test_bearer_auth_parse() {
let auth: ProxyAuthorization<Bearer> = Header::parse_header(
&b"Bearer fpKL54jvWmEGVoRdCNjG".as_ref().into()).unwrap();
assert_eq!(auth.0.token, "fpKL54jvWmEGVoRdCNjG");
}
}
#[cfg(test)]
#[cfg(feature = "nightly")]
mod benches {
use super::ProxyAuthorization;
use ::header::{Basic, Bearer};
bench_header!(raw, ProxyAuthorization<String>, { vec![b"foo bar baz".to_vec()] });
bench_header!(basic, ProxyAuthorization<Basic>, { vec![b"Basic QWxhZGRpbjpuIHNlc2FtZQ==".to_vec()] });
bench_header!(bearer, ProxyAuthorization<Bearer>, { vec![b"Bearer fpKL54jvWmEGVoRdCNjG".to_vec()] });
}