From 1b7884b2205a5310feb02f05aeb3fa7321de51aa Mon Sep 17 00:00:00 2001 From: arvidn Date: Fri, 15 Feb 2019 14:34:27 +0100 Subject: [PATCH] support numeric suffixes to magnet link parameter names --- ChangeLog | 1 + src/magnet_uri.cpp | 10 ++++++++++ test/test_magnet.cpp | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/ChangeLog b/ChangeLog index 7412717c0..b4d715ed0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ + * support magnet link parameters with number siffixes * consistently use "lt" namespace in examples and documentation * fix Mingw build to use native cryptoAPI diff --git a/src/magnet_uri.cpp b/src/magnet_uri.cpp index aca7875a5..d9888c42e 100644 --- a/src/magnet_uri.cpp +++ b/src/magnet_uri.cpp @@ -185,6 +185,16 @@ namespace libtorrent { string_view value; std::tie(value, sv) = split_string(sv, '&'); + // parameter names are allowed to have a .-suffix. + // the number has no meaning, just strip it + // if the characters after the period are not digits, don't strip + // anything + string_view number; + string_view stripped_name; + std::tie(stripped_name, number) = split_string(name, '.'); + if (std::all_of(number.begin(), number.end(), [](char const c) { return is_digit(c); } )) + name = stripped_name; + if (name == "dn"_sv) // display name { error_code e; diff --git a/test/test_magnet.cpp b/test/test_magnet.cpp index 93a9af558..26e534c14 100644 --- a/test/test_magnet.cpp +++ b/test/test_magnet.cpp @@ -538,3 +538,23 @@ TORRENT_TEST(parse_magnet_select_only_invalid_quotes) test_select_only("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd" "&dn=foo&so=\"1,2\"", {}); } + +TORRENT_TEST(magnet_tr_x_uri) +{ + add_torrent_params p = parse_magnet_uri("magnet:" + "?tr.0=udp://1" + "&tr.1=http://2" + "&tr=http://3" + "&xt=urn:btih:c352cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"); + TEST_CHECK((p.trackers == std::vector{ + "udp://1", "http://2", "http://3"})); + + TEST_CHECK((p.tracker_tiers == std::vector{0, 1, 2 })); + + p = parse_magnet_uri("magnet:" + "?tr.a=udp://1" + "&tr.1=http://2" + "&xt=urn:btih:c352cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"); + TEST_CHECK((p.trackers == std::vector{"http://2" })); + TEST_CHECK((p.tracker_tiers == std::vector{0})); +}