Skip to main content

composable_tower_http/authorize/header/impls/
default_header_extractor.rs

1use std::borrow::Cow;
2
3use http::{header::ToStrError, HeaderMap};
4
5use crate::authorize::header::header_extractor::HeaderExtractor;
6
7#[derive(Debug)]
8pub struct DefaultHeaderExtractor {
9    header_name: Cow<'static, str>,
10}
11
12impl DefaultHeaderExtractor {
13    pub fn new(header_name: impl Into<Cow<'static, str>>) -> Self {
14        Self {
15            header_name: header_name.into(),
16        }
17    }
18}
19
20impl HeaderExtractor for DefaultHeaderExtractor {
21    type Error = DefaultHeaderError;
22
23    fn extract_header<'a>(&self, headers: &'a HeaderMap) -> Result<&'a str, Self::Error> {
24        let header = headers
25            .get(self.header_name.as_ref())
26            .ok_or(DefaultHeaderError::Missing)?
27            .to_str()
28            .map_err(DefaultHeaderError::Ascii)?;
29
30        Ok(header)
31    }
32}
33
34#[derive(Debug, thiserror::Error)]
35pub enum DefaultHeaderError {
36    #[error("Header not found")]
37    Missing,
38    #[error("Header ascii error: {0}")]
39    Ascii(ToStrError),
40}