-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bac3b2c
commit ea34b63
Showing
23 changed files
with
597 additions
and
274 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//! Provides serialization and deserialization functions for [`HeaderValue`]. | ||
use serde::{Deserialize, ser::{Serializer, Error as _}, de::{Deserializer, Error as _}}; | ||
#[allow(unused_imports)] // [`HeaderValue`] is imported for [`serialize`]'s documentation. | ||
use reqwest::header::HeaderValue; | ||
|
||
/// Deserializes a [`HeaderValue`] | ||
/// # Errors | ||
/// If one of the keys or values aren't a valid header key or value, this function errors. | ||
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<HeaderValue, D::Error> { | ||
let temp: String = Deserialize::deserialize(d)?; | ||
temp.try_into().map_err(D::Error::custom) | ||
} | ||
|
||
/// Serializes [`HeaderValue`]. | ||
/// # Errors | ||
/// When the call to [`HeaderValue::to_str`] returns an error, that error is returned. | ||
pub fn serialize<S: Serializer>(x: &HeaderValue, s: S) -> Result<S::Ok, S::Error> { | ||
s.serialize_str(x.to_str().map_err(S::Error::custom)?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//! Proxy support for HTTP and HTTPS requests. | ||
use serde::{Serialize, Deserialize}; | ||
use url::Url; | ||
use reqwest::header::HeaderValue; | ||
use reqwest::Proxy; | ||
|
||
// Used for doc links. | ||
#[allow(unused_imports)] | ||
use crate::types::HttpClientConfig; | ||
|
||
/// Used by [`HttpClientConfig`] to detail how a [`reqwest::Proxy`] should be made. | ||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
pub struct ProxyConfig { | ||
/// The URL to proxy traffic to. Not the URL whose traffic to proxy. | ||
pub url: Url, | ||
/// The type of requests to proxy. Defaults to [`ProxyMode::All`] which proxies HTTP and HTTPS requests. | ||
#[serde(default)] | ||
pub mode: ProxyMode, | ||
/// Authentication for the proxy server. Defaults to [`None`]. | ||
#[serde(default)] | ||
pub auth: Option<ProxyAuth> | ||
} | ||
|
||
/// The types of traffic to proxy. Defaults to [`Self::All`]. | ||
#[derive(Debug, Clone, Copy, PartialEq, Default, Eq, Serialize, Deserialize)] | ||
pub enum ProxyMode { | ||
/// [`reqwest::Proxy::all`]. | ||
#[default] | ||
All, | ||
/// [`reqwest::Proxy::https`]. | ||
Https, | ||
/// [`reqwest::Proxy::http`]. | ||
Http | ||
} | ||
|
||
/// Authentication for the proxy server. | ||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
pub enum ProxyAuth { | ||
/// [`reqwest::Proxy::basic_auth`]. | ||
Basic { | ||
/// The username to use. | ||
username: String, | ||
/// The password to use. | ||
password: String | ||
}, | ||
/// [`reqwest::Proxy::custom_http_auth`]. | ||
Custom(#[serde(with = "crate::glue::headervalue")] HeaderValue) | ||
} | ||
|
||
impl ProxyConfig { | ||
/// Create a [`reqwest::Proxy`]. | ||
/// # Errors | ||
/// If the call to [`reqwest::Proxy::all`], [`reqwest::Proxy::https`], or [`reqwest::Proxy::http`] return an error, that error is returned. | ||
pub fn make(&self) -> reqwest::Result<reqwest::Proxy> { | ||
let temp = match self.mode { | ||
ProxyMode::All => Proxy::all (self.url.clone()), | ||
ProxyMode::Https => Proxy::https(self.url.clone()), | ||
ProxyMode::Http => Proxy::http (self.url.clone()) | ||
}?; | ||
Ok(match &self.auth { | ||
None => temp, | ||
Some(ProxyAuth::Basic {username, password}) => temp.basic_auth(username, password), | ||
Some(ProxyAuth::Custom(value)) => temp.custom_http_auth(value.clone()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.