-
Notifications
You must be signed in to change notification settings - Fork 778
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
e014bfb
commit 7c440dd
Showing
18 changed files
with
216 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use url::Url; | ||
|
||
/// A trusted host, which could be a host or a host-port pair. | ||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
pub enum TrustedHost { | ||
Host(String), | ||
HostPort(String, u16), | ||
} | ||
|
||
impl TrustedHost { | ||
pub fn matches(&self, url: &Url) -> bool { | ||
match self { | ||
Self::Host(host) => url.host_str() == Some(host.as_str()), | ||
Self::HostPort(host, port) => { | ||
url.host_str() == Some(host.as_str()) && url.port() == Some(*port) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum TrustedHostError { | ||
#[error("missing host for `--trusted-host`: `{0}`")] | ||
MissingHost(String), | ||
#[error("invalid port for `--trusted-host`: `{0}`")] | ||
InvalidPort(String), | ||
} | ||
|
||
impl std::str::FromStr for TrustedHost { | ||
type Err = TrustedHostError; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let mut parts = s.splitn(2, ':'); | ||
let host = parts | ||
.next() | ||
.ok_or_else(|| TrustedHostError::MissingHost(s.to_string()))?; | ||
let port = parts | ||
.next() | ||
.map(str::parse) | ||
.transpose() | ||
.map_err(|_| TrustedHostError::InvalidPort(s.to_string()))?; | ||
|
||
match port { | ||
Some(port) => Ok(TrustedHost::HostPort(host.to_string(), port)), | ||
None => Ok(TrustedHost::Host(host.to_string())), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "schemars")] | ||
impl schemars::JsonSchema for TrustedHost { | ||
fn schema_name() -> String { | ||
"TrustedHost".to_string() | ||
} | ||
|
||
fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { | ||
schemars::schema::SchemaObject { | ||
instance_type: Some(schemars::schema::InstanceType::String.into()), | ||
metadata: Some(Box::new(schemars::schema::Metadata { | ||
description: Some("A host or host-port pair.".to_string()), | ||
..schemars::schema::Metadata::default() | ||
})), | ||
..schemars::schema::SchemaObject::default() | ||
} | ||
.into() | ||
} | ||
} |
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.