From db1dc19c0a6c10dece18716f59c8ced8a2218bd2 Mon Sep 17 00:00:00 2001 From: Thomas Zahner Date: Wed, 13 Mar 2024 08:35:19 +0100 Subject: [PATCH] Implement Chainable directly for BasicAuthCredentials --- lychee-lib/src/chain/mod.rs | 25 +------------------ lychee-lib/src/client.rs | 4 +-- .../src/types/basic_auth/credentials.rs | 17 +++++++++++++ 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/lychee-lib/src/chain/mod.rs b/lychee-lib/src/chain/mod.rs index 8ea86b9..97d534f 100644 --- a/lychee-lib/src/chain/mod.rs +++ b/lychee-lib/src/chain/mod.rs @@ -1,7 +1,5 @@ -use crate::{BasicAuthCredentials, Response}; +use crate::Response; use core::fmt::Debug; -use headers::authorization::Credentials; -use http::header::AUTHORIZATION; use reqwest::Request; #[derive(Debug, PartialEq)] @@ -43,27 +41,6 @@ pub(crate) trait Chainable: Debug { fn handle(&mut self, input: T) -> ChainResult; } -#[derive(Debug)] -pub(crate) struct BasicAuth { - credentials: BasicAuthCredentials, -} - -impl BasicAuth { - pub(crate) fn new(credentials: BasicAuthCredentials) -> Self { - Self { credentials } - } -} - -impl Chainable for BasicAuth { - fn handle(&mut self, mut request: Request) -> ChainResult { - request.headers_mut().append( - AUTHORIZATION, - self.credentials.to_authorization().0.encode(), - ); - ChainResult::Chained(request) - } -} - mod test { use super::{ChainResult, ChainResult::*, Chainable}; diff --git a/lychee-lib/src/client.rs b/lychee-lib/src/client.rs index 3e2d3e3..48e4c74 100644 --- a/lychee-lib/src/client.rs +++ b/lychee-lib/src/client.rs @@ -31,7 +31,7 @@ use typed_builder::TypedBuilder; use crate::{ chain::ChainResult::*, - chain::{BasicAuth, Chain, RequestChain}, + chain::{Chain, RequestChain}, filter::{Excludes, Filter, Includes}, quirks::Quirks, remap::Remaps, @@ -482,7 +482,7 @@ impl Client { let mut request_chain: RequestChain = Chain::new(vec![Box::new(quirks)]); if let Some(c) = credentials { - request_chain.push(Box::new(BasicAuth::new(c.clone()))); + request_chain.push(Box::new(c.clone())); } let status = match uri.scheme() { diff --git a/lychee-lib/src/types/basic_auth/credentials.rs b/lychee-lib/src/types/basic_auth/credentials.rs index 2452fb9..bfbc519 100644 --- a/lychee-lib/src/types/basic_auth/credentials.rs +++ b/lychee-lib/src/types/basic_auth/credentials.rs @@ -1,9 +1,17 @@ use std::str::FromStr; +use headers::authorization::Credentials; use headers::{authorization::Basic, Authorization}; +use http::header::AUTHORIZATION; +use reqwest::Request; use serde::Deserialize; use thiserror::Error; +use crate::{ + chain::{ChainResult, Chainable}, + Response, +}; + #[derive(Copy, Clone, Debug, Error, PartialEq)] pub enum BasicAuthCredentialsParseError { #[error("Invalid basic auth credentials syntax")] @@ -66,3 +74,12 @@ impl BasicAuthCredentials { Authorization::basic(&self.username, &self.password) } } + +impl Chainable for BasicAuthCredentials { + fn handle(&mut self, mut request: Request) -> ChainResult { + request + .headers_mut() + .append(AUTHORIZATION, self.to_authorization().0.encode()); + ChainResult::Chained(request) + } +}