-
Notifications
You must be signed in to change notification settings - Fork 40
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
Showing
3 changed files
with
93 additions
and
46 deletions.
There are no files selected for viewing
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,40 @@ | ||
#[cfg(feature = "v2")] | ||
use crate::v2::ParseOhttpKeysError; | ||
|
||
#[derive(Debug)] | ||
pub struct PjParseError(InternalPjParseError); | ||
|
||
#[derive(Debug)] | ||
pub(crate) enum InternalPjParseError { | ||
BadPjOs, | ||
MultipleParams(&'static str), | ||
MissingEndpoint, | ||
NotUtf8, | ||
BadEndpoint, | ||
#[cfg(feature = "v2")] | ||
BadOhttpKeys(ParseOhttpKeysError), | ||
UnsecureEndpoint, | ||
} | ||
|
||
impl From<InternalPjParseError> for PjParseError { | ||
fn from(value: InternalPjParseError) -> Self { PjParseError(value) } | ||
} | ||
|
||
impl std::fmt::Display for PjParseError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match &self.0 { | ||
InternalPjParseError::BadPjOs => write!(f, "Bad pjos parameter"), | ||
InternalPjParseError::MultipleParams(param) => { | ||
write!(f, "Multiple instances of parameter '{}'", param) | ||
} | ||
InternalPjParseError::MissingEndpoint => write!(f, "Missing payjoin endpoint"), | ||
InternalPjParseError::NotUtf8 => write!(f, "Endpoint is not valid UTF-8"), | ||
InternalPjParseError::BadEndpoint => write!(f, "Endpoint is not valid"), | ||
#[cfg(feature = "v2")] | ||
InternalPjParseError::BadOhttpKeys(e) => write!(f, "OHTTP keys are not valid: {}", e), | ||
InternalPjParseError::UnsecureEndpoint => { | ||
write!(f, "Endpoint scheme is not secure (https or onion)") | ||
} | ||
} | ||
} | ||
} |
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,43 @@ | ||
use url::Url; | ||
|
||
pub struct PjUrl { | ||
url: Url, | ||
ohttp: Option<String>, | ||
} | ||
|
||
impl PjUrl { | ||
pub fn new(url: Url) -> Self { | ||
let (url, ohttp) = Self::extract_ohttp(url); | ||
PjUrl { url, ohttp } | ||
} | ||
|
||
fn extract_ohttp(mut url: Url) -> (Url, Option<String>) { | ||
let fragment = &mut url.fragment().and_then(|f| { | ||
let parts: Vec<&str> = f.splitn(2, "ohttp=").collect(); | ||
if parts.len() == 2 { | ||
Some((parts[0].trim_end_matches('&'), parts[1].to_string())) | ||
} else { | ||
None | ||
} | ||
}); | ||
|
||
if let Some((remaining_fragment, ohttp)) = fragment { | ||
url.set_fragment(Some(remaining_fragment)); | ||
(url, Some(ohttp)) | ||
} else { | ||
(url, None) | ||
} | ||
} | ||
|
||
pub fn into_url(self) -> Url { | ||
let mut url = self.url; | ||
if let Some(ohttp) = self.ohttp { | ||
let fragment = url | ||
.fragment() | ||
.map(|f| format!("{}&ohttp={}", f, ohttp)) | ||
.unwrap_or_else(|| format!("ohttp={}", ohttp)); | ||
url.set_fragment(Some(&fragment)); | ||
} | ||
url | ||
} | ||
} |