Skip to content

An open-source cross-platform alternative to AirDrop

fix: transfers fail with special characters in PIN (#3493)

Shlomo82abda323d7c
diff --git a/packages/core/src/http/client/url.rs b/packages/core/src/http/client/url.rs
index 135d817e..61e0fc89 100644
--- a/packages/core/src/http/client/url.rs
+++ b/packages/core/src/http/client/url.rs
@@ -8,8 +8,7 @@ pub struct TargetUrl<'a> {
pub port: u16,
pub path: &'static str,
- /// Query parameters as key-value pairs.
- /// Note: It is expected that the caller will URL-encode the values if necessary.
+ /// Query parameters as raw key-value pairs; encoded when building the URL.
pub params: &'a [(&'static str, &'a str)],
}
@@ -42,12 +41,9 @@ impl<'a> TargetUrl<'a> {
if self.params.is_empty() {
base
} else {
- let query = self
- .params
- .iter()
- .map(|(k, v)| format!("{}={}", k, v))
- .collect::<Vec<_>>()
- .join("&");
+ let query = form_urlencoded::Serializer::new(String::new())
+ .extend_pairs(self.params.iter().copied())
+ .finish();
format!("{}?{}", base, query)
}
}
@@ -57,6 +53,33 @@ impl<'a> TargetUrl<'a> {
mod tests {
use super::{ApiVersion, TargetUrl};
+ #[test]
+ fn test_pin_survives_query_round_trip() {
+ for pin in ["1234", "a+b", "a&b", "a#b", "%41", "a b", "קוד", ""] {
+ let url = TargetUrl {
+ version: ApiVersion::V2,
+ protocol: "https",
+ host: "192.168.1.1".to_string(),
+ port: 53317,
+ path: "/prepare-download",
+ params: &[("pin", pin), ("sessionId", "session-1")],
+ }
+ .to_string();
+ let url = reqwest::Url::parse(&url).unwrap();
+ let params: Vec<_> = form_urlencoded::parse(url.query().unwrap().as_bytes())
+ .into_owned()
+ .collect();
+ assert_eq!(
+ params,
+ vec![
+ ("pin".to_string(), pin.to_string()),
+ ("sessionId".to_string(), "session-1".to_string())
+ ],
+ "PIN: {pin:?}"
+ );
+ }
+ }
+
#[test]
fn test_build_url_ipv4() {
let url = TargetUrl {