headers/common/
proxy_authorization.rs

1use http::{HeaderName, HeaderValue};
2
3use super::authorization::{Authorization, Credentials};
4use crate::{Error, Header};
5
6/// `Proxy-Authorization` header, defined in [RFC7235](https://tools.ietf.org/html/rfc7235#section-4.4)
7///
8/// The `Proxy-Authorization` header field allows a user agent to authenticate
9/// itself with an HTTP proxy -- usually, but not necessarily, after
10/// receiving a 407 (Proxy Authentication Required) response and the
11/// `Proxy-Authenticate` header. Its value consists of credentials containing
12/// the authentication information of the user agent for the realm of the
13/// resource being requested.
14///
15/// # ABNF
16///
17/// ```text
18/// Proxy-Authorization = credentials
19/// ```
20///
21/// # Example values
22/// * `Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==`
23/// * `Bearer fpKL54jvWmEGVoRdCNjG`
24///
25/// # Examples
26///
27#[derive(Clone, PartialEq, Debug)]
28pub struct ProxyAuthorization<C: Credentials>(pub C);
29
30impl<C: Credentials> Header for ProxyAuthorization<C> {
31    fn name() -> &'static HeaderName {
32        &::http::header::PROXY_AUTHORIZATION
33    }
34
35    fn decode<'i, I: Iterator<Item = &'i HeaderValue>>(values: &mut I) -> Result<Self, Error> {
36        Authorization::decode(values).map(|auth| ProxyAuthorization(auth.0))
37    }
38
39    fn encode<E: Extend<HeaderValue>>(&self, values: &mut E) {
40        let value = self.0.encode();
41        debug_assert!(
42            value.as_bytes().starts_with(C::SCHEME.as_bytes()),
43            "Credentials::encode should include its scheme: scheme = {:?}, encoded = {:?}",
44            C::SCHEME,
45            value,
46        );
47
48        values.extend(::std::iter::once(value));
49    }
50}