1 unstable release

new 0.1.1 Mar 3, 2025

#51 in #site

MulanPSL-2.0

11KB
60 lines

header_host : x-forwarded-host or host from http HeaderMap

#[derive(Debug)]
pub struct HeaderNoHost;

impl std::fmt::Display for HeaderNoHost {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(f, "no host")
  }
}

impl std::error::Error for HeaderNoHost {}

pub fn header_host(headers: &http::HeaderMap) -> Result<&str, HeaderNoHost> {
  let host = if let Some(host) = headers.get("x-forwarded-host") {
    host
  } else if let Some(host) = headers.get("host") {
    host
  } else {
    Err(HeaderNoHost)?;
    unreachable!();
  }
  .to_str();
  match host {
    Ok(host) => Ok(host),
    Err(_) => Err(HeaderNoHost),
  }
}

About

This project is an open-source component of i18n.site ⋅ Internationalization Solution.

关于

本项目为 i18n.site ⋅ 国际化解决方案 的开源组件。

Dependencies

~785KB
~13K SLoC