Skip to main content

rama_http_headers/common/
proxy_authorization.rs

1use rama_http_types::{HeaderName, HeaderValue};
2
3use super::authorization::{Authorization, Credentials};
4use crate::{Error, HeaderDecode, HeaderEncode, TypedHeader};
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> TypedHeader for ProxyAuthorization<C> {
31    fn name() -> &'static HeaderName {
32        &::rama_http_types::header::PROXY_AUTHORIZATION
33    }
34}
35
36impl<C: Credentials> HeaderDecode for ProxyAuthorization<C> {
37    fn decode<'i, I: Iterator<Item = &'i HeaderValue>>(values: &mut I) -> Result<Self, Error> {
38        Authorization::decode(values).map(|auth| Self(auth.0))
39    }
40}
41
42impl<C: Credentials> HeaderEncode for ProxyAuthorization<C> {
43    fn encode<E: Extend<HeaderValue>>(&self, values: &mut E) {
44        values.extend(self.0.encode().map(|value| {
45            debug_assert!(
46                value.as_bytes().starts_with(C::SCHEME.as_bytes()),
47                "Credentials::encode should include its scheme: scheme = {:?}, encoded = {:?}",
48                C::SCHEME,
49                value,
50            );
51            value
52        }));
53    }
54}