Skip to content

Commit ba224a1

Browse files
committed
restore the parse_magnet_uri overload that amends an add_torrent_params object
1 parent 9e0a3ae commit ba224a1

File tree

6 files changed

+42
-88
lines changed

6 files changed

+42
-88
lines changed

docs/tutorial.rst

+1-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ For example:
3939
}
4040
lt::session ses;
4141

42-
lt::add_torrent_params atp;
43-
lt::error_code ec;
44-
lt::parse_magnet_uri(argv[1], atp, ec);
42+
lt::add_torrent_params atp = lt::parse_magnet_uri(argv[1]);
4543
atp.save_path = "."; // save in current dir
4644
lt::torrent_handle h = ses.add_torrent(atp);
4745

examples/bt-get.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,15 @@ POSSIBILITY OF SUCH DAMAGE.
4040
#include <libtorrent/alert_types.hpp>
4141
#include <libtorrent/magnet_uri.hpp>
4242

43-
int main(int argc, char const* argv[])
43+
int main(int argc, char const* argv[]) try
4444
{
4545
if (argc != 2) {
4646
std::cerr << "usage: " << argv[0] << " <magnet-url>" << std::endl;
4747
return 1;
4848
}
4949
lt::session ses;
5050

51-
lt::error_code ec;
52-
lt::add_torrent_params atp = lt::parse_magnet_uri(argv[1], ec);
53-
if (ec) {
54-
std::cerr << "invalid magnet URI: " << ec.message() << std::endl;
55-
return 1;
56-
}
51+
lt::add_torrent_params atp = lt::parse_magnet_uri(argv[1]);
5752
atp.save_path = "."; // save in current dir
5853
lt::torrent_handle h = ses.add_torrent(std::move(atp));
5954

@@ -76,4 +71,8 @@ int main(int argc, char const* argv[])
7671
done:
7772
std::cout << "done, shutting down" << std::endl;
7873
}
74+
catch (std::exception& e)
75+
{
76+
std::cerr << "Error: " << e.what() << std::endl;
77+
}
7978

examples/bt-get2.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ char const* state(lt::torrent_status::state_t s)
6363
}
6464
}
6565

66-
int main(int argc, char const* argv[])
66+
int main(int argc, char const* argv[]) try
6767
{
6868
if (argc != 2) {
6969
std::cerr << "usage: " << argv[0] << " <magnet-url>" << std::endl;
@@ -91,14 +91,10 @@ int main(int argc, char const* argv[])
9191
std::cerr << "failed to read resume data: " << ec.message() << std::endl;
9292
return 1;
9393
}
94-
lt::add_torrent_params magnet = lt::parse_magnet_uri(argv[1], ec);
94+
lt::add_torrent_params magnet = lt::parse_magnet_uri(argv[1]);
9595
if (atp.info_hash != magnet.info_hash) {
9696
atp = std::move(magnet);
9797
}
98-
if (ec) {
99-
std::cerr << "invalid magnet URI: " << ec.message() << std::endl;
100-
return 1;
101-
}
10298
atp.save_path = "."; // save in current dir
10399
ses.async_add_torrent(std::move(atp));
104100

@@ -162,4 +158,8 @@ int main(int argc, char const* argv[])
162158
done:
163159
std::cout << "\ndone, shutting down" << std::endl;
164160
}
161+
catch (std::exception& e)
162+
{
163+
std::cerr << "Error: " << e.what() << std::endl;
164+
}
165165

include/libtorrent/magnet_uri.hpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,17 @@ namespace libtorrent {
7373
TORRENT_DEPRECATED_EXPORT
7474
torrent_handle add_magnet_uri(session& ses, std::string const& uri
7575
, add_torrent_params p, error_code& ec);
76-
77-
// deprecated in 1.2
78-
TORRENT_DEPRECATED_EXPORT void parse_magnet_uri(string_view uri, add_torrent_params& p, error_code& ec);
7976
#endif
8077

78+
8179
// This function parses out information from the magnet link and populates the
8280
// add_torrent_params object. The overload that does not take an
8381
// ``error_code`` reference will throw a system_error on error
82+
// The overload taking an ``add_torrent_params`` reference will fill in the
83+
// fields specified in the magnet URI.
8484
TORRENT_EXPORT add_torrent_params parse_magnet_uri(string_view uri, error_code& ec);
8585
TORRENT_EXPORT add_torrent_params parse_magnet_uri(string_view uri);
86+
TORRENT_EXPORT void parse_magnet_uri(string_view uri, add_torrent_params& p, error_code& ec);
8687
}
8788

8889
#endif

src/magnet_uri.cpp

+13-29
Original file line numberDiff line numberDiff line change
@@ -165,33 +165,17 @@ namespace libtorrent {
165165
return ret;
166166
}
167167
#endif // BOOST_NO_EXCEPTIONS
168+
#endif // TORRENT_NO_DEPRECATE
168169

169-
void parse_magnet_uri(string_view uri, add_torrent_params& p, error_code& ec)
170+
add_torrent_params parse_magnet_uri(string_view uri, error_code& ec)
170171
{
171-
add_torrent_params tmp = parse_magnet_uri(uri, ec);
172-
if (!tmp.name.empty()) p.name = std::move(tmp.name);
173-
if (!tmp.trackers.empty())
174-
{
175-
int const tier = p.tracker_tiers.empty() ? 0 : p.tracker_tiers.back();
176-
p.tracker_tiers.resize(p.trackers.size(), tier);
177-
p.trackers.insert(p.trackers.end(), tmp.trackers.begin(), tmp.trackers.end());
178-
p.tracker_tiers.insert(p.tracker_tiers.end(), tmp.tracker_tiers.begin(), tmp.tracker_tiers.end());
179-
}
180-
p.url_seeds.insert(p.url_seeds.end(), tmp.url_seeds.begin(), tmp.url_seeds.end());
181-
p.info_hash = tmp.info_hash;
182-
183-
p.peers.insert(p.peers.end(), tmp.peers.begin(), tmp.peers.end());
184-
185-
#ifndef TORRENT_DISABLE_DHT
186-
p.dht_nodes.insert(p.dht_nodes.end(), tmp.dht_nodes.begin(), tmp.dht_nodes.end());
187-
#endif
172+
add_torrent_params ret;
173+
parse_magnet_uri(uri, ret, ec);
174+
return ret;
188175
}
189176

190-
#endif // TORRENT_NO_DEPRECATE
191-
192-
add_torrent_params parse_magnet_uri(string_view uri, error_code& ec)
177+
void parse_magnet_uri(string_view uri, add_torrent_params& p, error_code& ec)
193178
{
194-
add_torrent_params p;
195179
ec.clear();
196180
std::string name;
197181

@@ -244,18 +228,18 @@ namespace libtorrent {
244228
if (btih.empty())
245229
{
246230
ec = errors::missing_info_hash_in_uri;
247-
return p;
231+
return;
248232
}
249233
if (btih.find('%') != string_view::npos)
250234
{
251235
unescaped_btih = unescape_string(btih, ec);
252-
if (ec) return p;
236+
if (ec) return;
253237
btih = unescaped_btih;
254238
}
255239
if (btih.substr(0, 9) != "urn:btih:")
256240
{
257241
ec = errors::missing_info_hash_in_uri;
258-
return p;
242+
return;
259243
}
260244

261245
auto select_pos = std::string::npos;
@@ -360,25 +344,25 @@ namespace libtorrent {
360344
if (ih.size() != 20)
361345
{
362346
ec = errors::invalid_info_hash;
363-
return p;
347+
return;
364348
}
365349
info_hash.assign(ih);
366350
}
367351
else
368352
{
369353
ec = errors::invalid_info_hash;
370-
return p;
354+
return;
371355
}
372356

373357
p.info_hash = info_hash;
374358
if (!name.empty()) p.name = name;
375-
return p;
376359
}
377360

378361
add_torrent_params parse_magnet_uri(string_view uri)
379362
{
380363
error_code ec;
381-
add_torrent_params ret = parse_magnet_uri(uri, ec);
364+
add_torrent_params ret;
365+
parse_magnet_uri(uri, ret, ec);
382366
if (ec) aux::throw_ex<system_error>(ec);
383367
return ret;
384368
}

test/test_magnet.cpp

+12-40
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,19 @@ TORRENT_TEST(magnet)
110110
s->save_state(session_state);
111111

112112
// test magnet link parsing
113-
error_code ec;
114113
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
115114
"&tr=http://1"
116115
"&tr=http://2"
117116
"&tr=http://3"
118117
"&tr=http://3"
119118
"&dn=foo"
120-
"&dht=127.0.0.1:43", ec);
119+
"&dht=127.0.0.1:43");
121120

122121
p.flags &= ~torrent_flags::paused;
123122
p.flags &= ~torrent_flags::auto_managed;
124123
p.save_path = ".";
125124

126-
TEST_CHECK(!ec);
125+
error_code ec;
127126
torrent_handle t = s->add_torrent(p, ec);
128127
TEST_CHECK(!ec);
129128
if (ec) std::printf("%s\n", ec.message().c_str());
@@ -144,8 +143,7 @@ TORRENT_TEST(magnet)
144143
"&tr=http://2"
145144
"&dn=foo"
146145
"&dht=127.0.0.1:43"
147-
"&xt=urn:btih:c352cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd", ec);
148-
TEST_CHECK(!ec);
146+
"&xt=urn:btih:c352cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd");
149147
p.flags &= ~torrent_flags::paused;
150148
p.flags &= ~torrent_flags::auto_managed;
151149
p.save_path = ".";
@@ -163,8 +161,7 @@ TORRENT_TEST(magnet)
163161
"&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80"
164162
"&tr=udp%3A%2F%2Ftracker.ccc.de%3A80"
165163
"&xt=urn:btih:a38d02c287893842a32825aa866e00828a318f07"
166-
"&dn=Ubuntu+11.04+%28Final%29", ec);
167-
TEST_CHECK(!ec);
164+
"&dn=Ubuntu+11.04+%28Final%29");
168165
p.flags &= ~torrent_flags::paused;
169166
p.flags &= ~torrent_flags::auto_managed;
170167
p.save_path = ".";
@@ -229,17 +226,13 @@ TORRENT_TEST(magnet)
229226

230227
TORRENT_TEST(parse_escaped_hash_parameter)
231228
{
232-
error_code ec;
233-
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn%3Abtih%3Acdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd", ec);
234-
TEST_CHECK(!ec);
229+
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn%3Abtih%3Acdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd");
235230
TEST_EQUAL(aux::to_hex(p.info_hash), "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd");
236231
}
237232

238233
TORRENT_TEST(parse_escaped_hash_parameter_in_hex)
239234
{
240-
error_code ec;
241-
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdc%64", ec);
242-
TEST_CHECK(!ec);
235+
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdc%64");
243236
TEST_EQUAL(aux::to_hex(p.info_hash), "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd");
244237
}
245238

@@ -261,70 +254,57 @@ TORRENT_TEST(parse_missing_hash)
261254
error_code ec;
262255
add_torrent_params p = parse_magnet_uri("magnet:?dn=foo&dht=127.0.0.1:43", ec);
263256
TEST_EQUAL(ec, error_code(errors::missing_info_hash_in_uri));
264-
ec.clear();
265257
}
266258

267259
TORRENT_TEST(parse_base32_hash)
268260
{
269261
// parse_magnet_uri
270-
error_code ec;
271-
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:MFRGCYTBMJQWEYLCMFRGCYTBMJQWEYLC", ec);
272-
TEST_CHECK(!ec);
262+
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:MFRGCYTBMJQWEYLCMFRGCYTBMJQWEYLC");
273263
TEST_EQUAL(p.info_hash, sha1_hash("abababababababababab"));
274-
ec.clear();
275264
}
276265

277266
TORRENT_TEST(parse_web_seeds)
278267
{
279268
// parse_magnet_uri
280-
error_code ec;
281269
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
282-
"&ws=http://foo.com/bar&ws=http://bar.com/foo", ec);
283-
TEST_CHECK(!ec);
270+
"&ws=http://foo.com/bar&ws=http://bar.com/foo");
284271
TEST_EQUAL(p.url_seeds.size(), 2);
285272
TEST_EQUAL(p.url_seeds[0], "http://foo.com/bar");
286273
TEST_EQUAL(p.url_seeds[1], "http://bar.com/foo");
287-
ec.clear();
288274
}
289275

290276
TORRENT_TEST(parse_missing_hash2)
291277
{
292278
error_code ec;
293279
add_torrent_params p = parse_magnet_uri("magnet:?xt=blah&dn=foo&dht=127.0.0.1:43", ec);
294280
TEST_EQUAL(ec, error_code(errors::missing_info_hash_in_uri));
295-
ec.clear();
296281
}
297282

298283
TORRENT_TEST(parse_short_hash)
299284
{
300285
error_code ec;
301286
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:abababab", ec);
302287
TEST_EQUAL(ec, error_code(errors::invalid_info_hash));
303-
ec.clear();
304288
}
305289

306290
TORRENT_TEST(parse_long_hash)
307291
{
308292
error_code ec;
309293
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:ababababababababababab", ec);
310294
TEST_EQUAL(ec, error_code(errors::invalid_info_hash));
311-
ec.clear();
312295
}
313296

314297
TORRENT_TEST(parse_space_hash)
315298
{
316299
error_code ec;
317300
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih: abababababababababab", ec);
318301
TEST_EQUAL(ec, error_code(errors::invalid_info_hash));
319-
ec.clear();
320302
}
321303

322304
TORRENT_TEST(parse_peer)
323305
{
324-
error_code ec;
325306
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
326-
"&dn=foo&x.pe=127.0.0.1:43&x.pe=<invalid1>&x.pe=<invalid2>:100&x.pe=[::1]:45", ec);
327-
TEST_CHECK(!ec);
307+
"&dn=foo&x.pe=127.0.0.1:43&x.pe=<invalid1>&x.pe=<invalid2>:100&x.pe=[::1]:45");
328308
#if TORRENT_USE_IPV6
329309
TEST_EQUAL(p.peers.size(), 2);
330310
TEST_EQUAL(p.peers[0], ep("127.0.0.1", 43));
@@ -333,18 +313,13 @@ TORRENT_TEST(parse_peer)
333313
TEST_EQUAL(p.peers.size(), 1);
334314
TEST_EQUAL(p.peers[0], ep("127.0.0.1", 43));
335315
#endif
336-
ec.clear();
337316
}
338317

339318
#ifndef TORRENT_DISABLE_DHT
340319
TORRENT_TEST(parse_dht_node)
341320
{
342-
error_code ec;
343321
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
344-
"&dn=foo&dht=127.0.0.1:43&dht=10.0.0.1:1337", ec);
345-
TEST_CHECK(!ec);
346-
if (ec) std::printf("%s\n", ec.message().c_str());
347-
ec.clear();
322+
"&dn=foo&dht=127.0.0.1:43&dht=10.0.0.1:1337");
348323

349324
TEST_EQUAL(p.dht_nodes.size(), 2);
350325
TEST_EQUAL(p.dht_nodes[0].first, "127.0.0.1");
@@ -452,9 +427,8 @@ TORRENT_TEST(trailing_whitespace)
452427
TEST_THROW(ses.add_torrent(p));
453428

454429
ec.clear();
455-
p = parse_magnet_uri("magnet:?xt=urn:btih:abaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", ec);
430+
p = parse_magnet_uri("magnet:?xt=urn:btih:abaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
456431
p.save_path = ".";
457-
TEST_CHECK(!ec);
458432
// now it's valid, because there's no trailing whitespace
459433
torrent_handle h = ses.add_torrent(p);
460434
TEST_CHECK(h.is_valid());
@@ -481,9 +455,7 @@ auto const no = dont_download;
481455

482456
void test_select_only(string_view uri, std::vector<download_priority_t> expected)
483457
{
484-
error_code ec;
485-
add_torrent_params p = parse_magnet_uri(uri, ec);
486-
TEST_CHECK(!ec);
458+
add_torrent_params p = parse_magnet_uri(uri);
487459
TEST_CHECK(p.file_priorities == expected);
488460
}
489461

0 commit comments

Comments
 (0)