Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions src/magnet_link.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::*;
use {crate::common::*, url::form_urlencoded::byte_serialize as urlencode};

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct MagnetLink {
Expand Down Expand Up @@ -62,7 +62,9 @@ impl MagnetLink {

for tracker in &self.trackers {
query.push_str("&tr=");
query.push_str(tracker.as_str());
for part in urlencode(tracker.as_str().as_bytes()) {
query.push_str(part);
}
}

for peer in &self.peers {
Expand Down Expand Up @@ -210,7 +212,7 @@ mod tests {
link.add_tracker(Url::parse("http://foo.com/announce").unwrap());
assert_eq!(
link.to_url().as_str(),
"magnet:?xt=urn:btih:da39a3ee5e6b4b0d3255bfef95601890afd80709&tr=http://foo.com/announce"
"magnet:?xt=urn:btih:da39a3ee5e6b4b0d3255bfef95601890afd80709&tr=http%3A%2F%2Ffoo.com%2Fannounce"
);
}

Expand Down Expand Up @@ -240,8 +242,8 @@ mod tests {
concat!(
"magnet:?xt=urn:btih:da39a3ee5e6b4b0d3255bfef95601890afd80709",
"&dn=foo",
"&tr=http://foo.com/announce",
"&tr=http://bar.net/announce",
"&tr=http%3A%2F%2Ffoo.com%2Fannounce",
"&tr=http%3A%2F%2Fbar.net%2Fannounce",
"&x.pe=foo.com:1337",
"&x.pe=bar.net:666",
),
Expand All @@ -263,6 +265,37 @@ mod tests {
assert_eq!(link_to, link_from);
}

#[test]
fn link_from_str_tracker_round_trip() {
let magnet_str = concat!(
"magnet:?xt=urn:btih:da39a3ee5e6b4b0d3255bfef95601890afd80709",
"&dn=foo",
"&tr=http%3A%2F%2Ffoo.com%2Fannounce",
"&tr=http%3A%2F%2Fbar.net%2Fannounce"
);

let link_from = MagnetLink::from_str(magnet_str).unwrap();
let link_roundtripped = MagnetLink::from_str(&link_from.to_string()).unwrap();
assert_eq!(link_from, link_roundtripped,);
}

#[test]
fn link_from_str_tracker_urlencoding() {
let magnet_str = concat!(
"magnet:?xt=urn:btih:da39a3ee5e6b4b0d3255bfef95601890afd80709",
"&dn=foo",
"&tr=http%3A%2F%2Ffoo.com%2Fannounce",
);

let link_from = MagnetLink::from_str(magnet_str).unwrap();
let tracker_url = link_from.trackers.first().unwrap();

assert_eq!(
tracker_url,
&"http://foo.com/announce".parse::<Url>().unwrap(),
);
}

#[test]
fn link_from_str_url_error() {
let link = "%imdl.io";
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/torrent/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2593,7 +2593,7 @@ Content Size 9 bytes
"magnet:\
?xt=urn:btih:516735f4b80f2b5487eed5f226075bdcde33a54e\
&dn=foo\
&tr=http://foo.com/announce\n"
&tr=http%3A%2F%2Ffoo.com%2Fannounce\n"
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/subcommand/torrent/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ mod tests {
assert_eq!(
env.out(),
format!(
"magnet:?xt=urn:btih:{}&dn=foo&tr=https://foo.com/announce\n",
"magnet:?xt=urn:btih:{}&dn=foo&tr=https%3A%2F%2Ffoo.com%2Fannounce\n",
infohash
),
);
Expand Down Expand Up @@ -266,7 +266,7 @@ mod tests {
assert_eq!(
env.out(),
format!(
"magnet:?xt=urn:btih:{}&dn=foo&tr=https://foo.com/announce&tr=https://bar.com/announce\n",
"magnet:?xt=urn:btih:{}&dn=foo&tr=https%3A%2F%2Ffoo.com%2Fannounce&tr=https%3A%2F%2Fbar.com%2Fannounce\n",
infohash
),
);
Expand Down Expand Up @@ -300,7 +300,7 @@ mod tests {
assert_eq!(
env.out(),
format!(
"magnet:?xt=urn:btih:{}&dn=foo&tr=https://foo.com/announce&x.pe=foo.com:1337\n",
"magnet:?xt=urn:btih:{}&dn=foo&tr=https%3A%2F%2Ffoo.com%2Fannounce&x.pe=foo.com:1337\n",
infohash
),
);
Expand Down Expand Up @@ -336,7 +336,7 @@ mod tests {
assert_eq!(
env.out(),
format!(
"magnet:?xt=urn:btih:{}&dn=foo&tr=https://foo.com/announce&so=2,4,6\n",
"magnet:?xt=urn:btih:{}&dn=foo&tr=https%3A%2F%2Ffoo.com%2Fannounce&so=2,4,6\n",
infohash
),
);
Expand Down