restore the parse_magnet_uri overload that amends an add_torrent_params object

This commit is contained in:
arvidn 2017-12-13 17:42:55 +01:00 committed by Arvid Norberg
parent 9e0a3aead1
commit ba224a1577
6 changed files with 43 additions and 89 deletions

View File

@ -39,9 +39,7 @@ For example:
} }
lt::session ses; lt::session ses;
lt::add_torrent_params atp; lt::add_torrent_params atp = lt::parse_magnet_uri(argv[1]);
lt::error_code ec;
lt::parse_magnet_uri(argv[1], atp, ec);
atp.save_path = "."; // save in current dir atp.save_path = "."; // save in current dir
lt::torrent_handle h = ses.add_torrent(atp); lt::torrent_handle h = ses.add_torrent(atp);

View File

@ -40,7 +40,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <libtorrent/alert_types.hpp> #include <libtorrent/alert_types.hpp>
#include <libtorrent/magnet_uri.hpp> #include <libtorrent/magnet_uri.hpp>
int main(int argc, char const* argv[]) int main(int argc, char const* argv[]) try
{ {
if (argc != 2) { if (argc != 2) {
std::cerr << "usage: " << argv[0] << " <magnet-url>" << std::endl; std::cerr << "usage: " << argv[0] << " <magnet-url>" << std::endl;
@ -48,12 +48,7 @@ int main(int argc, char const* argv[])
} }
lt::session ses; lt::session ses;
lt::error_code ec; lt::add_torrent_params atp = lt::parse_magnet_uri(argv[1]);
lt::add_torrent_params atp = lt::parse_magnet_uri(argv[1], ec);
if (ec) {
std::cerr << "invalid magnet URI: " << ec.message() << std::endl;
return 1;
}
atp.save_path = "."; // save in current dir atp.save_path = "."; // save in current dir
lt::torrent_handle h = ses.add_torrent(std::move(atp)); lt::torrent_handle h = ses.add_torrent(std::move(atp));
@ -76,4 +71,8 @@ int main(int argc, char const* argv[])
done: done:
std::cout << "done, shutting down" << std::endl; std::cout << "done, shutting down" << std::endl;
} }
catch (std::exception& e)
{
std::cerr << "Error: " << e.what() << std::endl;
}

View File

@ -63,7 +63,7 @@ char const* state(lt::torrent_status::state_t s)
} }
} }
int main(int argc, char const* argv[]) int main(int argc, char const* argv[]) try
{ {
if (argc != 2) { if (argc != 2) {
std::cerr << "usage: " << argv[0] << " <magnet-url>" << std::endl; std::cerr << "usage: " << argv[0] << " <magnet-url>" << std::endl;
@ -91,14 +91,10 @@ int main(int argc, char const* argv[])
std::cerr << "failed to read resume data: " << ec.message() << std::endl; std::cerr << "failed to read resume data: " << ec.message() << std::endl;
return 1; return 1;
} }
lt::add_torrent_params magnet = lt::parse_magnet_uri(argv[1], ec); lt::add_torrent_params magnet = lt::parse_magnet_uri(argv[1]);
if (atp.info_hash != magnet.info_hash) { if (atp.info_hash != magnet.info_hash) {
atp = std::move(magnet); atp = std::move(magnet);
} }
if (ec) {
std::cerr << "invalid magnet URI: " << ec.message() << std::endl;
return 1;
}
atp.save_path = "."; // save in current dir atp.save_path = "."; // save in current dir
ses.async_add_torrent(std::move(atp)); ses.async_add_torrent(std::move(atp));
@ -162,4 +158,8 @@ int main(int argc, char const* argv[])
done: done:
std::cout << "\ndone, shutting down" << std::endl; std::cout << "\ndone, shutting down" << std::endl;
} }
catch (std::exception& e)
{
std::cerr << "Error: " << e.what() << std::endl;
}

View File

@ -73,16 +73,17 @@ namespace libtorrent {
TORRENT_DEPRECATED_EXPORT TORRENT_DEPRECATED_EXPORT
torrent_handle add_magnet_uri(session& ses, std::string const& uri torrent_handle add_magnet_uri(session& ses, std::string const& uri
, add_torrent_params p, error_code& ec); , add_torrent_params p, error_code& ec);
// deprecated in 1.2
TORRENT_DEPRECATED_EXPORT void parse_magnet_uri(string_view uri, add_torrent_params& p, error_code& ec);
#endif #endif
// This function parses out information from the magnet link and populates the // This function parses out information from the magnet link and populates the
// add_torrent_params object. The overload that does not take an // add_torrent_params object. The overload that does not take an
// ``error_code`` reference will throw a system_error on error // ``error_code`` reference will throw a system_error on error
// The overload taking an ``add_torrent_params`` reference will fill in the
// fields specified in the magnet URI.
TORRENT_EXPORT add_torrent_params parse_magnet_uri(string_view uri, error_code& ec); TORRENT_EXPORT add_torrent_params parse_magnet_uri(string_view uri, error_code& ec);
TORRENT_EXPORT add_torrent_params parse_magnet_uri(string_view uri); TORRENT_EXPORT add_torrent_params parse_magnet_uri(string_view uri);
TORRENT_EXPORT void parse_magnet_uri(string_view uri, add_torrent_params& p, error_code& ec);
} }
#endif #endif

View File

@ -165,33 +165,17 @@ namespace libtorrent {
return ret; return ret;
} }
#endif // BOOST_NO_EXCEPTIONS #endif // BOOST_NO_EXCEPTIONS
void parse_magnet_uri(string_view uri, add_torrent_params& p, error_code& ec)
{
add_torrent_params tmp = parse_magnet_uri(uri, ec);
if (!tmp.name.empty()) p.name = std::move(tmp.name);
if (!tmp.trackers.empty())
{
int const tier = p.tracker_tiers.empty() ? 0 : p.tracker_tiers.back();
p.tracker_tiers.resize(p.trackers.size(), tier);
p.trackers.insert(p.trackers.end(), tmp.trackers.begin(), tmp.trackers.end());
p.tracker_tiers.insert(p.tracker_tiers.end(), tmp.tracker_tiers.begin(), tmp.tracker_tiers.end());
}
p.url_seeds.insert(p.url_seeds.end(), tmp.url_seeds.begin(), tmp.url_seeds.end());
p.info_hash = tmp.info_hash;
p.peers.insert(p.peers.end(), tmp.peers.begin(), tmp.peers.end());
#ifndef TORRENT_DISABLE_DHT
p.dht_nodes.insert(p.dht_nodes.end(), tmp.dht_nodes.begin(), tmp.dht_nodes.end());
#endif
}
#endif // TORRENT_NO_DEPRECATE #endif // TORRENT_NO_DEPRECATE
add_torrent_params parse_magnet_uri(string_view uri, error_code& ec) add_torrent_params parse_magnet_uri(string_view uri, error_code& ec)
{ {
add_torrent_params p; add_torrent_params ret;
parse_magnet_uri(uri, ret, ec);
return ret;
}
void parse_magnet_uri(string_view uri, add_torrent_params& p, error_code& ec)
{
ec.clear(); ec.clear();
std::string name; std::string name;
@ -244,18 +228,18 @@ namespace libtorrent {
if (btih.empty()) if (btih.empty())
{ {
ec = errors::missing_info_hash_in_uri; ec = errors::missing_info_hash_in_uri;
return p; return;
} }
if (btih.find('%') != string_view::npos) if (btih.find('%') != string_view::npos)
{ {
unescaped_btih = unescape_string(btih, ec); unescaped_btih = unescape_string(btih, ec);
if (ec) return p; if (ec) return;
btih = unescaped_btih; btih = unescaped_btih;
} }
if (btih.substr(0, 9) != "urn:btih:") if (btih.substr(0, 9) != "urn:btih:")
{ {
ec = errors::missing_info_hash_in_uri; ec = errors::missing_info_hash_in_uri;
return p; return;
} }
auto select_pos = std::string::npos; auto select_pos = std::string::npos;
@ -360,25 +344,25 @@ namespace libtorrent {
if (ih.size() != 20) if (ih.size() != 20)
{ {
ec = errors::invalid_info_hash; ec = errors::invalid_info_hash;
return p; return;
} }
info_hash.assign(ih); info_hash.assign(ih);
} }
else else
{ {
ec = errors::invalid_info_hash; ec = errors::invalid_info_hash;
return p; return;
} }
p.info_hash = info_hash; p.info_hash = info_hash;
if (!name.empty()) p.name = name; if (!name.empty()) p.name = name;
return p;
} }
add_torrent_params parse_magnet_uri(string_view uri) add_torrent_params parse_magnet_uri(string_view uri)
{ {
error_code ec; error_code ec;
add_torrent_params ret = parse_magnet_uri(uri, ec); add_torrent_params ret;
parse_magnet_uri(uri, ret, ec);
if (ec) aux::throw_ex<system_error>(ec); if (ec) aux::throw_ex<system_error>(ec);
return ret; return ret;
} }

View File

@ -110,20 +110,19 @@ TORRENT_TEST(magnet)
s->save_state(session_state); s->save_state(session_state);
// test magnet link parsing // test magnet link parsing
error_code ec;
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd" add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
"&tr=http://1" "&tr=http://1"
"&tr=http://2" "&tr=http://2"
"&tr=http://3" "&tr=http://3"
"&tr=http://3" "&tr=http://3"
"&dn=foo" "&dn=foo"
"&dht=127.0.0.1:43", ec); "&dht=127.0.0.1:43");
p.flags &= ~torrent_flags::paused; p.flags &= ~torrent_flags::paused;
p.flags &= ~torrent_flags::auto_managed; p.flags &= ~torrent_flags::auto_managed;
p.save_path = "."; p.save_path = ".";
TEST_CHECK(!ec); error_code ec;
torrent_handle t = s->add_torrent(p, ec); torrent_handle t = s->add_torrent(p, ec);
TEST_CHECK(!ec); TEST_CHECK(!ec);
if (ec) std::printf("%s\n", ec.message().c_str()); if (ec) std::printf("%s\n", ec.message().c_str());
@ -144,8 +143,7 @@ TORRENT_TEST(magnet)
"&tr=http://2" "&tr=http://2"
"&dn=foo" "&dn=foo"
"&dht=127.0.0.1:43" "&dht=127.0.0.1:43"
"&xt=urn:btih:c352cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd", ec); "&xt=urn:btih:c352cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd");
TEST_CHECK(!ec);
p.flags &= ~torrent_flags::paused; p.flags &= ~torrent_flags::paused;
p.flags &= ~torrent_flags::auto_managed; p.flags &= ~torrent_flags::auto_managed;
p.save_path = "."; p.save_path = ".";
@ -163,8 +161,7 @@ TORRENT_TEST(magnet)
"&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80" "&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80"
"&tr=udp%3A%2F%2Ftracker.ccc.de%3A80" "&tr=udp%3A%2F%2Ftracker.ccc.de%3A80"
"&xt=urn:btih:a38d02c287893842a32825aa866e00828a318f07" "&xt=urn:btih:a38d02c287893842a32825aa866e00828a318f07"
"&dn=Ubuntu+11.04+%28Final%29", ec); "&dn=Ubuntu+11.04+%28Final%29");
TEST_CHECK(!ec);
p.flags &= ~torrent_flags::paused; p.flags &= ~torrent_flags::paused;
p.flags &= ~torrent_flags::auto_managed; p.flags &= ~torrent_flags::auto_managed;
p.save_path = "."; p.save_path = ".";
@ -229,17 +226,13 @@ TORRENT_TEST(magnet)
TORRENT_TEST(parse_escaped_hash_parameter) TORRENT_TEST(parse_escaped_hash_parameter)
{ {
error_code ec; add_torrent_params p = parse_magnet_uri("magnet:?xt=urn%3Abtih%3Acdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd");
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn%3Abtih%3Acdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd", ec);
TEST_CHECK(!ec);
TEST_EQUAL(aux::to_hex(p.info_hash), "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"); TEST_EQUAL(aux::to_hex(p.info_hash), "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd");
} }
TORRENT_TEST(parse_escaped_hash_parameter_in_hex) TORRENT_TEST(parse_escaped_hash_parameter_in_hex)
{ {
error_code ec; add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdc%64");
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdc%64", ec);
TEST_CHECK(!ec);
TEST_EQUAL(aux::to_hex(p.info_hash), "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"); TEST_EQUAL(aux::to_hex(p.info_hash), "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd");
} }
@ -261,30 +254,23 @@ TORRENT_TEST(parse_missing_hash)
error_code ec; error_code ec;
add_torrent_params p = parse_magnet_uri("magnet:?dn=foo&dht=127.0.0.1:43", ec); add_torrent_params p = parse_magnet_uri("magnet:?dn=foo&dht=127.0.0.1:43", ec);
TEST_EQUAL(ec, error_code(errors::missing_info_hash_in_uri)); TEST_EQUAL(ec, error_code(errors::missing_info_hash_in_uri));
ec.clear();
} }
TORRENT_TEST(parse_base32_hash) TORRENT_TEST(parse_base32_hash)
{ {
// parse_magnet_uri // parse_magnet_uri
error_code ec; add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:MFRGCYTBMJQWEYLCMFRGCYTBMJQWEYLC");
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:MFRGCYTBMJQWEYLCMFRGCYTBMJQWEYLC", ec);
TEST_CHECK(!ec);
TEST_EQUAL(p.info_hash, sha1_hash("abababababababababab")); TEST_EQUAL(p.info_hash, sha1_hash("abababababababababab"));
ec.clear();
} }
TORRENT_TEST(parse_web_seeds) TORRENT_TEST(parse_web_seeds)
{ {
// parse_magnet_uri // parse_magnet_uri
error_code ec;
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd" add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
"&ws=http://foo.com/bar&ws=http://bar.com/foo", ec); "&ws=http://foo.com/bar&ws=http://bar.com/foo");
TEST_CHECK(!ec);
TEST_EQUAL(p.url_seeds.size(), 2); TEST_EQUAL(p.url_seeds.size(), 2);
TEST_EQUAL(p.url_seeds[0], "http://foo.com/bar"); TEST_EQUAL(p.url_seeds[0], "http://foo.com/bar");
TEST_EQUAL(p.url_seeds[1], "http://bar.com/foo"); TEST_EQUAL(p.url_seeds[1], "http://bar.com/foo");
ec.clear();
} }
TORRENT_TEST(parse_missing_hash2) TORRENT_TEST(parse_missing_hash2)
@ -292,7 +278,6 @@ TORRENT_TEST(parse_missing_hash2)
error_code ec; error_code ec;
add_torrent_params p = parse_magnet_uri("magnet:?xt=blah&dn=foo&dht=127.0.0.1:43", ec); add_torrent_params p = parse_magnet_uri("magnet:?xt=blah&dn=foo&dht=127.0.0.1:43", ec);
TEST_EQUAL(ec, error_code(errors::missing_info_hash_in_uri)); TEST_EQUAL(ec, error_code(errors::missing_info_hash_in_uri));
ec.clear();
} }
TORRENT_TEST(parse_short_hash) TORRENT_TEST(parse_short_hash)
@ -300,7 +285,6 @@ TORRENT_TEST(parse_short_hash)
error_code ec; error_code ec;
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:abababab", ec); add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:abababab", ec);
TEST_EQUAL(ec, error_code(errors::invalid_info_hash)); TEST_EQUAL(ec, error_code(errors::invalid_info_hash));
ec.clear();
} }
TORRENT_TEST(parse_long_hash) TORRENT_TEST(parse_long_hash)
@ -308,7 +292,6 @@ TORRENT_TEST(parse_long_hash)
error_code ec; error_code ec;
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:ababababababababababab", ec); add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:ababababababababababab", ec);
TEST_EQUAL(ec, error_code(errors::invalid_info_hash)); TEST_EQUAL(ec, error_code(errors::invalid_info_hash));
ec.clear();
} }
TORRENT_TEST(parse_space_hash) TORRENT_TEST(parse_space_hash)
@ -316,15 +299,12 @@ TORRENT_TEST(parse_space_hash)
error_code ec; error_code ec;
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih: abababababababababab", ec); add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih: abababababababababab", ec);
TEST_EQUAL(ec, error_code(errors::invalid_info_hash)); TEST_EQUAL(ec, error_code(errors::invalid_info_hash));
ec.clear();
} }
TORRENT_TEST(parse_peer) TORRENT_TEST(parse_peer)
{ {
error_code ec;
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd" add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
"&dn=foo&x.pe=127.0.0.1:43&x.pe=<invalid1>&x.pe=<invalid2>:100&x.pe=[::1]:45", ec); "&dn=foo&x.pe=127.0.0.1:43&x.pe=<invalid1>&x.pe=<invalid2>:100&x.pe=[::1]:45");
TEST_CHECK(!ec);
#if TORRENT_USE_IPV6 #if TORRENT_USE_IPV6
TEST_EQUAL(p.peers.size(), 2); TEST_EQUAL(p.peers.size(), 2);
TEST_EQUAL(p.peers[0], ep("127.0.0.1", 43)); TEST_EQUAL(p.peers[0], ep("127.0.0.1", 43));
@ -333,18 +313,13 @@ TORRENT_TEST(parse_peer)
TEST_EQUAL(p.peers.size(), 1); TEST_EQUAL(p.peers.size(), 1);
TEST_EQUAL(p.peers[0], ep("127.0.0.1", 43)); TEST_EQUAL(p.peers[0], ep("127.0.0.1", 43));
#endif #endif
ec.clear();
} }
#ifndef TORRENT_DISABLE_DHT #ifndef TORRENT_DISABLE_DHT
TORRENT_TEST(parse_dht_node) TORRENT_TEST(parse_dht_node)
{ {
error_code ec;
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd" add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
"&dn=foo&dht=127.0.0.1:43&dht=10.0.0.1:1337", ec); "&dn=foo&dht=127.0.0.1:43&dht=10.0.0.1:1337");
TEST_CHECK(!ec);
if (ec) std::printf("%s\n", ec.message().c_str());
ec.clear();
TEST_EQUAL(p.dht_nodes.size(), 2); TEST_EQUAL(p.dht_nodes.size(), 2);
TEST_EQUAL(p.dht_nodes[0].first, "127.0.0.1"); TEST_EQUAL(p.dht_nodes[0].first, "127.0.0.1");
@ -452,9 +427,8 @@ TORRENT_TEST(trailing_whitespace)
TEST_THROW(ses.add_torrent(p)); TEST_THROW(ses.add_torrent(p));
ec.clear(); ec.clear();
p = parse_magnet_uri("magnet:?xt=urn:btih:abaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", ec); p = parse_magnet_uri("magnet:?xt=urn:btih:abaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
p.save_path = "."; p.save_path = ".";
TEST_CHECK(!ec);
// now it's valid, because there's no trailing whitespace // now it's valid, because there's no trailing whitespace
torrent_handle h = ses.add_torrent(p); torrent_handle h = ses.add_torrent(p);
TEST_CHECK(h.is_valid()); TEST_CHECK(h.is_valid());
@ -481,9 +455,7 @@ auto const no = dont_download;
void test_select_only(string_view uri, std::vector<download_priority_t> expected) void test_select_only(string_view uri, std::vector<download_priority_t> expected)
{ {
error_code ec; add_torrent_params p = parse_magnet_uri(uri);
add_torrent_params p = parse_magnet_uri(uri, ec);
TEST_CHECK(!ec);
TEST_CHECK(p.file_priorities == expected); TEST_CHECK(p.file_priorities == expected);
} }