Browse Source

Added the other headers needed

master
Julio Biason 2 years ago
parent
commit
fa614757f6
  1. 1
      axumtest/src/header/mod.rs
  2. 39
      axumtest/src/headers/cipwd.rs
  3. 39
      axumtest/src/headers/cirole.rs
  4. 0
      axumtest/src/headers/ciusr.rs
  5. 3
      axumtest/src/headers/mod.rs
  6. 12
      axumtest/src/main.rs

1
axumtest/src/header/mod.rs

@ -1 +0,0 @@
pub mod ciusr;

39
axumtest/src/headers/cipwd.rs

@ -0,0 +1,39 @@
//! Header parsing for X-CIPWD
use axum::headers::Header;
use axum::headers::HeaderName;
use axum::headers::HeaderValue;
static NAME: HeaderName = HeaderName::from_static("x-cipwd");
pub struct CiPwd(String);
impl Header for CiPwd {
fn name() -> &'static HeaderName {
&NAME
}
fn decode<'i, I>(values: &mut I) -> Result<Self, axum::headers::Error>
where
Self: Sized,
I: Iterator<Item = &'i axum::http::HeaderValue>,
{
let value = values.next().ok_or_else(axum::headers::Error::invalid)?;
Ok(CiPwd(
value
.to_str()
.or(Err(axum::headers::Error::invalid()))?
.into(),
))
}
fn encode<E: Extend<axum::http::HeaderValue>>(&self, values: &mut E) {
values.extend(std::iter::once(HeaderValue::from_str(&self.0).unwrap()))
}
}
impl std::fmt::Display for CiPwd {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

39
axumtest/src/headers/cirole.rs

@ -0,0 +1,39 @@
//! Header parsing for X-CIROLE
use axum::headers::Header;
use axum::headers::HeaderName;
use axum::headers::HeaderValue;
static NAME: HeaderName = HeaderName::from_static("x-cirole");
pub struct CiRole(String);
impl Header for CiRole {
fn name() -> &'static HeaderName {
&NAME
}
fn decode<'i, I>(values: &mut I) -> Result<Self, axum::headers::Error>
where
Self: Sized,
I: Iterator<Item = &'i axum::http::HeaderValue>,
{
let value = values.next().ok_or_else(axum::headers::Error::invalid)?;
Ok(CiRole(
value
.to_str()
.or(Err(axum::headers::Error::invalid()))?
.into(),
))
}
fn encode<E: Extend<axum::http::HeaderValue>>(&self, values: &mut E) {
values.extend(std::iter::once(HeaderValue::from_str(&self.0).unwrap()))
}
}
impl std::fmt::Display for CiRole {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

0
axumtest/src/header/ciusr.rs → axumtest/src/headers/ciusr.rs

3
axumtest/src/headers/mod.rs

@ -0,0 +1,3 @@
pub mod cipwd;
pub mod cirole;
pub mod ciusr;

12
axumtest/src/main.rs

@ -1,9 +1,11 @@
mod header; mod headers;
use axum::routing::get; use axum::routing::get;
use axum::routing::Router; use axum::routing::Router;
use axum::TypedHeader; use axum::TypedHeader;
use header::ciusr::CiUsr; use headers::cipwd::CiPwd;
use headers::cirole::CiRole;
use headers::ciusr::CiUsr;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
@ -14,6 +16,10 @@ async fn main() {
.unwrap(); .unwrap();
} }
async fn index(TypedHeader(usr): TypedHeader<CiUsr>) -> String { async fn index(
TypedHeader(usr): TypedHeader<CiUsr>,
TypedHeader(pwd): TypedHeader<CiPwd>,
TypedHeader(role): TypedHeader<CiRole>,
) -> String {
format!("Hellow {}", usr) format!("Hellow {}", usr)
} }

Loading…
Cancel
Save