-
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.
Close #298
- Loading branch information
Showing
4 changed files
with
88 additions
and
93 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
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 |
---|---|---|
@@ -1,43 +1,73 @@ | ||
use std::borrow::Cow; | ||
use std::ops::{Deref, DerefMut}; | ||
|
||
use serde::Deserialize; | ||
use url::Url; | ||
|
||
pub struct PjUrl { | ||
url: Url, | ||
ohttp: Option<String>, | ||
use crate::OhttpKeys; | ||
#[derive(Clone, Debug, Deserialize)] | ||
pub struct PjUrl(pub Url); | ||
|
||
impl Deref for PjUrl { | ||
type Target = Url; | ||
|
||
fn deref(&self) -> &Self::Target { &self.0 } | ||
} | ||
|
||
impl PjUrl { | ||
pub fn new(url: Url) -> Self { | ||
let (url, ohttp) = Self::extract_ohttp(url); | ||
PjUrl { url, ohttp } | ||
} | ||
impl DerefMut for PjUrl { | ||
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } | ||
} | ||
|
||
fn extract_ohttp(mut url: Url) -> (Url, Option<String>) { | ||
let fragment = &mut url.fragment().and_then(|f| { | ||
impl PjUrl { | ||
pub fn ohttp(&self) -> Option<OhttpKeys> { | ||
self.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())) | ||
let base64_config = Cow::try_from(parts[1]).ok()?; | ||
let config_bytes = | ||
bitcoin::base64::decode_config(&*base64_config, bitcoin::base64::URL_SAFE) | ||
.ok()?; | ||
OhttpKeys::decode(&config_bytes).ok() | ||
} else { | ||
None | ||
} | ||
}); | ||
}) | ||
} | ||
|
||
if let Some((remaining_fragment, ohttp)) = fragment { | ||
url.set_fragment(Some(remaining_fragment)); | ||
(url, Some(ohttp)) | ||
pub fn set_ohttp(&mut self, ohttp: Option<OhttpKeys>) { | ||
if let Some(ohttp) = ohttp { | ||
let new_ohttp = format!("ohttp={}", ohttp.to_string()); | ||
let mut fragment = self.fragment().unwrap_or("").to_string(); | ||
if let Some(start) = fragment.find("ohttp=") { | ||
let end = fragment[start..].find('&').map_or(fragment.len(), |i| start + i); | ||
fragment.replace_range(start..end, &new_ohttp); | ||
} else { | ||
if !fragment.is_empty() { | ||
fragment.push('&'); | ||
} | ||
fragment.push_str(&new_ohttp); | ||
} | ||
self.set_fragment(Some(&fragment)); | ||
} else { | ||
(url, None) | ||
self.set_fragment(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 | ||
#[cfg(test)] | ||
mod test { | ||
use url::Url; | ||
|
||
use super::PjUrl; | ||
|
||
#[test] | ||
fn test_pj_url() { | ||
let url = PjUrl( | ||
Url::parse( | ||
"https://example.com#ohttp=AQAg3WpRjS0aqAxQUoLvpas2VYjT2oIg6-3XSiB-QiYI1BAABAABAAM", | ||
) | ||
.unwrap(), | ||
); | ||
assert!(url.ohttp().is_some()); | ||
} | ||
} |
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