2007-12-03 07:03:16 +01:00
|
|
|
/*
|
|
|
|
|
2014-02-23 20:12:25 +01:00
|
|
|
Copyright (c) 2007-2014, Arvid Norberg
|
2007-12-03 07:03:16 +01:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libtorrent/magnet_uri.hpp"
|
|
|
|
#include "libtorrent/session.hpp"
|
|
|
|
#include "libtorrent/torrent_handle.hpp"
|
2015-03-15 00:10:20 +01:00
|
|
|
#include "libtorrent/aux_/escape_string.hpp"
|
2012-06-09 06:48:53 +02:00
|
|
|
#include "libtorrent/error_code.hpp"
|
2007-12-03 07:03:16 +01:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
std::string make_magnet_uri(torrent_handle const& handle)
|
|
|
|
{
|
2009-04-13 06:22:03 +02:00
|
|
|
if (!handle.is_valid()) return "";
|
|
|
|
|
2013-04-28 00:35:06 +02:00
|
|
|
std::string ret;
|
2009-11-24 19:42:58 +01:00
|
|
|
sha1_hash const& ih = handle.info_hash();
|
2013-04-28 00:35:06 +02:00
|
|
|
ret += "magnet:?xt=urn:btih:";
|
|
|
|
ret += to_hex(ih.to_string());
|
2007-12-03 07:03:16 +01:00
|
|
|
|
2013-03-04 04:24:53 +01:00
|
|
|
torrent_status st = handle.status(torrent_handle::query_name);
|
2013-04-28 00:35:06 +02:00
|
|
|
if (!st.name.empty())
|
|
|
|
{
|
|
|
|
ret += "&dn=";
|
|
|
|
ret += escape_string(st.name.c_str(), st.name.length());
|
|
|
|
}
|
2009-04-13 06:22:03 +02:00
|
|
|
|
2012-11-23 17:10:26 +01:00
|
|
|
std::vector<announce_entry> const& tr = handle.trackers();
|
|
|
|
for (std::vector<announce_entry>::const_iterator i = tr.begin(), end(tr.end()); i != end; ++i)
|
2007-12-03 07:03:16 +01:00
|
|
|
{
|
2013-04-28 00:35:06 +02:00
|
|
|
ret += "&tr=";
|
|
|
|
ret += escape_string(i->url.c_str(), i->url.length());
|
2012-11-23 17:10:26 +01:00
|
|
|
}
|
2009-04-13 06:22:03 +02:00
|
|
|
|
2014-04-15 11:31:28 +02:00
|
|
|
std::set<std::string> seeds = handle.url_seeds();
|
|
|
|
for (std::set<std::string>::iterator i = seeds.begin()
|
|
|
|
, end(seeds.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
ret += "&ws=";
|
|
|
|
ret += escape_string(i->c_str(), i->length());
|
|
|
|
}
|
|
|
|
|
2009-04-13 06:22:03 +02:00
|
|
|
return ret;
|
2007-12-03 07:03:16 +01:00
|
|
|
}
|
|
|
|
|
2008-08-27 20:44:35 +02:00
|
|
|
std::string make_magnet_uri(torrent_info const& info)
|
|
|
|
{
|
2013-04-28 00:35:06 +02:00
|
|
|
std::string ret;
|
2009-11-24 19:42:58 +01:00
|
|
|
sha1_hash const& ih = info.info_hash();
|
2013-04-28 00:35:06 +02:00
|
|
|
ret += "magnet:?xt=urn:btih:";
|
|
|
|
ret += to_hex(ih.to_string());
|
2008-08-27 20:44:35 +02:00
|
|
|
|
2009-04-13 06:22:03 +02:00
|
|
|
std::string const& name = info.name();
|
2008-08-27 20:44:35 +02:00
|
|
|
|
2013-04-28 00:35:06 +02:00
|
|
|
if (!name.empty())
|
|
|
|
{
|
|
|
|
ret += "&dn=";
|
|
|
|
ret += escape_string(name.c_str(), name.length());
|
|
|
|
}
|
2009-04-13 06:22:03 +02:00
|
|
|
|
2008-08-27 20:44:35 +02:00
|
|
|
std::vector<announce_entry> const& tr = info.trackers();
|
2013-04-28 00:35:06 +02:00
|
|
|
|
2012-11-23 17:10:26 +01:00
|
|
|
for (std::vector<announce_entry>::const_iterator i = tr.begin(), end(tr.end()); i != end; ++i)
|
2008-08-27 20:44:35 +02:00
|
|
|
{
|
2013-04-28 00:35:06 +02:00
|
|
|
ret += "&tr=";
|
|
|
|
ret += escape_string(i->url.c_str(), i->url.length());
|
2008-08-27 20:44:35 +02:00
|
|
|
}
|
2009-04-13 06:22:03 +02:00
|
|
|
|
2014-04-15 11:31:28 +02:00
|
|
|
std::vector<web_seed_entry> const& seeds = info.web_seeds();
|
|
|
|
for (std::vector<web_seed_entry>::const_iterator i = seeds.begin()
|
|
|
|
, end(seeds.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->type != web_seed_entry::url_seed) continue;
|
|
|
|
|
|
|
|
ret += "&ws=";
|
|
|
|
ret += escape_string(i->url.c_str(), i->url.length());
|
|
|
|
}
|
|
|
|
|
2009-04-13 06:22:03 +02:00
|
|
|
return ret;
|
2008-08-27 20:44:35 +02:00
|
|
|
}
|
|
|
|
|
2012-06-09 06:48:53 +02:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2014-07-05 16:10:25 +02:00
|
|
|
|
|
|
|
torrent_handle add_magnet_uri_deprecated(session& ses, std::string const& uri
|
|
|
|
, add_torrent_params p, error_code& ec)
|
|
|
|
{
|
|
|
|
parse_magnet_uri(uri, p, ec);
|
|
|
|
if (ec) return torrent_handle();
|
|
|
|
return ses.add_torrent(p, ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
torrent_handle add_magnet_uri(session& ses, std::string const& uri
|
|
|
|
, add_torrent_params p, error_code& ec)
|
|
|
|
{
|
|
|
|
return add_magnet_uri_deprecated(ses, uri, p, ec);
|
|
|
|
}
|
|
|
|
|
2012-06-14 17:16:59 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2007-12-03 07:03:16 +01:00
|
|
|
torrent_handle add_magnet_uri(session& ses, std::string const& uri
|
2009-10-26 02:29:39 +01:00
|
|
|
, std::string const& save_path
|
2007-12-03 07:03:16 +01:00
|
|
|
, storage_mode_t storage_mode
|
|
|
|
, bool paused
|
|
|
|
, storage_constructor_type sc
|
|
|
|
, void* userdata)
|
|
|
|
{
|
2014-07-05 16:10:25 +02:00
|
|
|
add_torrent_params params(sc);
|
|
|
|
params.storage_mode = storage_mode;
|
|
|
|
params.userdata = userdata;
|
|
|
|
params.save_path = save_path;
|
|
|
|
|
|
|
|
if (paused) params.flags |= add_torrent_params::flag_paused;
|
|
|
|
else params.flags &= ~add_torrent_params::flag_paused;
|
2007-12-03 07:03:16 +01:00
|
|
|
|
2009-02-23 02:21:19 +01:00
|
|
|
error_code ec;
|
2009-11-23 09:38:50 +01:00
|
|
|
std::string display_name = url_has_argument(uri, "dn");
|
2014-07-05 16:10:25 +02:00
|
|
|
if (!display_name.empty()) params.name = unescape_string(display_name.c_str(), ec);
|
2009-11-23 09:38:50 +01:00
|
|
|
std::string tracker_string = url_has_argument(uri, "tr");
|
2014-07-05 16:10:25 +02:00
|
|
|
if (!tracker_string.empty()) params.trackers.push_back(unescape_string(tracker_string.c_str(), ec));
|
2007-12-03 07:03:16 +01:00
|
|
|
|
2009-11-23 09:38:50 +01:00
|
|
|
std::string btih = url_has_argument(uri, "xt");
|
|
|
|
if (btih.empty()) return torrent_handle();
|
2007-12-03 07:03:16 +01:00
|
|
|
|
2009-11-23 09:38:50 +01:00
|
|
|
if (btih.compare(0, 9, "urn:btih:") != 0) return torrent_handle();
|
2007-12-03 07:03:16 +01:00
|
|
|
|
2014-07-05 16:10:25 +02:00
|
|
|
if (btih.size() == 40 + 9) from_hex(&btih[9], 40, (char*)¶ms.info_hash[0]);
|
|
|
|
else params.info_hash.assign(base32decode(btih.substr(9)));
|
2007-12-03 07:03:16 +01:00
|
|
|
|
2014-07-05 16:10:25 +02:00
|
|
|
return ses.add_torrent(params);
|
2007-12-03 07:03:16 +01:00
|
|
|
}
|
2008-08-03 17:14:08 +02:00
|
|
|
|
|
|
|
torrent_handle add_magnet_uri(session& ses, std::string const& uri
|
|
|
|
, add_torrent_params p)
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
|
|
|
error_code ec;
|
2014-07-05 16:10:25 +02:00
|
|
|
torrent_handle ret = add_magnet_uri_deprecated(ses, uri, p, ec);
|
2009-02-27 08:03:00 +01:00
|
|
|
if (ec) throw libtorrent_exception(ec);
|
2009-02-26 08:09:56 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2012-06-14 17:16:59 +02:00
|
|
|
#endif // BOOST_NO_EXCEPTIONS
|
|
|
|
#endif // TORRENT_NO_DEPRECATE
|
|
|
|
|
2012-03-08 10:54:44 +01:00
|
|
|
void parse_magnet_uri(std::string const& uri, add_torrent_params& p, error_code& ec)
|
2008-08-03 17:14:08 +02:00
|
|
|
{
|
2013-02-28 05:31:55 +01:00
|
|
|
ec.clear();
|
2008-08-03 17:14:08 +02:00
|
|
|
std::string name;
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
error_code e;
|
2009-11-23 09:38:50 +01:00
|
|
|
std::string display_name = url_has_argument(uri, "dn");
|
|
|
|
if (!display_name.empty()) name = unescape_string(display_name.c_str(), e);
|
2012-03-08 10:54:44 +01:00
|
|
|
|
|
|
|
// parse trackers out of the magnet link
|
2010-03-06 04:57:48 +01:00
|
|
|
std::string::size_type pos = std::string::npos;
|
2012-03-21 17:27:50 +01:00
|
|
|
std::string url = url_has_argument(uri, "tr", &pos);
|
|
|
|
while (pos != std::string::npos)
|
2012-03-08 10:54:44 +01:00
|
|
|
{
|
|
|
|
error_code e;
|
2012-03-21 17:27:50 +01:00
|
|
|
url = unescape_string(url, e);
|
2012-03-08 10:54:44 +01:00
|
|
|
if (e) continue;
|
|
|
|
p.trackers.push_back(url);
|
2012-03-21 17:27:50 +01:00
|
|
|
pos = uri.find("&tr=", pos);
|
|
|
|
if (pos == std::string::npos) break;
|
|
|
|
pos += 4;
|
|
|
|
url = uri.substr(pos, uri.find('&', pos) - pos);
|
2012-03-08 10:54:44 +01:00
|
|
|
}
|
2014-04-15 11:31:28 +02:00
|
|
|
|
|
|
|
// parse web seeds out of the magnet link
|
|
|
|
pos = std::string::npos;
|
|
|
|
url = url_has_argument(uri, "ws", &pos);
|
|
|
|
while (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
error_code e;
|
|
|
|
url = unescape_string(url, e);
|
|
|
|
if (e) continue;
|
|
|
|
p.url_seeds.push_back(url);
|
|
|
|
pos = uri.find("&ws=", pos);
|
|
|
|
if (pos == std::string::npos) break;
|
|
|
|
pos += 4;
|
|
|
|
url = uri.substr(pos, uri.find('&', pos) - pos);
|
|
|
|
}
|
|
|
|
|
2009-11-23 09:38:50 +01:00
|
|
|
std::string btih = url_has_argument(uri, "xt");
|
|
|
|
if (btih.empty())
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
ec = errors::missing_info_hash_in_uri;
|
2012-03-08 10:54:44 +01:00
|
|
|
return;
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2008-08-03 17:14:08 +02:00
|
|
|
|
2009-11-23 09:38:50 +01:00
|
|
|
if (btih.compare(0, 9, "urn:btih:") != 0)
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
ec = errors::missing_info_hash_in_uri;
|
2012-03-08 10:54:44 +01:00
|
|
|
return;
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2008-08-03 17:14:08 +02:00
|
|
|
|
2010-11-07 20:18:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
std::string::size_type node_pos = std::string::npos;
|
|
|
|
std::string node = url_has_argument(uri, "dht", &node_pos);
|
|
|
|
while (!node.empty())
|
|
|
|
{
|
|
|
|
std::string::size_type divider = node.find_last_of(':');
|
|
|
|
if (divider != std::string::npos)
|
|
|
|
{
|
|
|
|
int port = atoi(node.c_str()+divider+1);
|
|
|
|
if (port != 0)
|
2012-03-08 10:54:44 +01:00
|
|
|
p.dht_nodes.push_back(std::make_pair(node.substr(0, divider), port));
|
2010-11-07 20:18:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
node_pos = uri.find("&dht=", node_pos);
|
|
|
|
if (node_pos == std::string::npos) break;
|
|
|
|
node_pos += 5;
|
|
|
|
node = uri.substr(node_pos, uri.find('&', node_pos) - node_pos);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-05-02 20:36:51 +02:00
|
|
|
sha1_hash info_hash;
|
2009-11-23 09:38:50 +01:00
|
|
|
if (btih.size() == 40 + 9) from_hex(&btih[9], 40, (char*)&info_hash[0]);
|
|
|
|
else info_hash.assign(base32decode(btih.substr(9)));
|
2008-08-03 17:14:08 +02:00
|
|
|
|
|
|
|
p.info_hash = info_hash;
|
2012-03-08 10:54:44 +01:00
|
|
|
if (!name.empty()) p.name = name;
|
2008-08-03 17:14:08 +02:00
|
|
|
}
|
2007-12-03 07:03:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|