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::add_torrent_params atp;
lt::error_code ec;
lt::parse_magnet_uri(argv[1], atp, ec);
lt::add_torrent_params atp = lt::parse_magnet_uri(argv[1]);
atp.save_path = "."; // save in current dir
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/magnet_uri.hpp>
int main(int argc, char const* argv[])
int main(int argc, char const* argv[]) try
{
if (argc != 2) {
std::cerr << "usage: " << argv[0] << " <magnet-url>" << std::endl;
@ -48,12 +48,7 @@ int main(int argc, char const* argv[])
}
lt::session ses;
lt::error_code ec;
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;
}
lt::add_torrent_params atp = lt::parse_magnet_uri(argv[1]);
atp.save_path = "."; // save in current dir
lt::torrent_handle h = ses.add_torrent(std::move(atp));
@ -76,4 +71,8 @@ int main(int argc, char const* argv[])
done:
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) {
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;
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) {
atp = std::move(magnet);
}
if (ec) {
std::cerr << "invalid magnet URI: " << ec.message() << std::endl;
return 1;
}
atp.save_path = "."; // save in current dir
ses.async_add_torrent(std::move(atp));
@ -162,4 +158,8 @@ int main(int argc, char const* argv[])
done:
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_handle add_magnet_uri(session& ses, std::string const& uri
, 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
// This function parses out information from the magnet link and populates the
// add_torrent_params object. The overload that does not take an
// ``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);
TORRENT_EXPORT void parse_magnet_uri(string_view uri, add_torrent_params& p, error_code& ec);
}
#endif

View File

@ -165,33 +165,17 @@ namespace libtorrent {
return ret;
}
#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
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();
std::string name;
@ -244,18 +228,18 @@ namespace libtorrent {
if (btih.empty())
{
ec = errors::missing_info_hash_in_uri;
return p;
return;
}
if (btih.find('%') != string_view::npos)
{
unescaped_btih = unescape_string(btih, ec);
if (ec) return p;
if (ec) return;
btih = unescaped_btih;
}
if (btih.substr(0, 9) != "urn:btih:")
{
ec = errors::missing_info_hash_in_uri;
return p;
return;
}
auto select_pos = std::string::npos;
@ -360,25 +344,25 @@ namespace libtorrent {
if (ih.size() != 20)
{
ec = errors::invalid_info_hash;
return p;
return;
}
info_hash.assign(ih);
}
else
{
ec = errors::invalid_info_hash;
return p;
return;
}
p.info_hash = info_hash;
if (!name.empty()) p.name = name;
return p;
}
add_torrent_params parse_magnet_uri(string_view uri)
{
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);
return ret;
}

View File

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