3
Copyright (c) 2007, Arvid Norberg
6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions
10
* Redistributions of source code must retain the above copyright
11
notice, this list of conditions and the following disclaimer.
12
* Redistributions in binary form must reproduce the above copyright
13
notice, this list of conditions and the following disclaimer in
14
the documentation and/or other materials provided with the distribution.
15
* Neither the name of the author nor the names of its
16
contributors may be used to endorse or promote products derived
17
from this software without specific prior written permission.
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
POSSIBILITY OF SUCH DAMAGE.
33
#include "libtorrent/magnet_uri.hpp"
34
#include "libtorrent/session.hpp"
35
#include "libtorrent/torrent_handle.hpp"
36
#include "libtorrent/escape_string.hpp"
40
#include <boost/lexical_cast.hpp>
44
std::string make_magnet_uri(torrent_handle const& handle)
46
std::stringstream ret;
47
if (!handle.is_valid()) return ret.str();
49
std::string name = handle.name();
51
ret << "magnet:?xt=urn:btih:" << base32encode(
52
std::string((char*)handle.info_hash().begin(), 20));
54
ret << "&dn=" << escape_string(name.c_str(), name.length());
55
torrent_status st = handle.status();
56
if (!st.current_tracker.empty())
58
ret << "&tr=" << escape_string(st.current_tracker.c_str()
59
, st.current_tracker.length());
63
std::vector<announce_entry> const& tr = handle.trackers();
66
ret << "&tr=" << escape_string(tr[0].url.c_str()
67
, tr[0].url.length());
73
std::string make_magnet_uri(torrent_info const& info)
75
std::stringstream ret;
76
if (!info.is_valid()) return ret.str();
78
std::string name = info.name();
80
ret << "magnet:?xt=urn:btih:" << base32encode(
81
std::string((char*)info.info_hash().begin(), 20));
83
ret << "&dn=" << escape_string(name.c_str(), name.length());
84
std::vector<announce_entry> const& tr = info.trackers();
87
ret << "&tr=" << escape_string(tr[0].url.c_str()
88
, tr[0].url.length());
93
#ifndef TORRENT_NO_DEPRECATE
94
torrent_handle add_magnet_uri(session& ses, std::string const& uri
95
, fs::path const& save_path
96
, storage_mode_t storage_mode
98
, storage_constructor_type sc
104
boost::optional<std::string> display_name = url_has_argument(uri, "dn");
105
if (display_name) name = unescape_string(display_name->c_str());
106
boost::optional<std::string> tracker_string = url_has_argument(uri, "tr");
107
if (tracker_string) tracker = unescape_string(tracker_string->c_str());
109
boost::optional<std::string> btih = url_has_argument(uri, "xt");
110
if (!btih) return torrent_handle();
112
if (btih->compare(0, 9, "urn:btih:") != 0) return torrent_handle();
115
if (btih->size() == 40 + 9) info_hash = boost::lexical_cast<sha1_hash>(btih->substr(9));
116
else info_hash.assign(base32decode(btih->substr(9)));
118
return ses.add_torrent(tracker.empty() ? 0 : tracker.c_str(), info_hash
119
, name.empty() ? 0 : name.c_str(), save_path, entry()
120
, storage_mode, paused, sc, userdata);
124
torrent_handle add_magnet_uri(session& ses, std::string const& uri
125
, add_torrent_params p)
130
boost::optional<std::string> display_name = url_has_argument(uri, "dn");
131
if (display_name) name = unescape_string(display_name->c_str());
132
boost::optional<std::string> tracker_string = url_has_argument(uri, "tr");
133
if (tracker_string) tracker = unescape_string(tracker_string->c_str());
135
boost::optional<std::string> btih = url_has_argument(uri, "xt");
136
if (!btih) return torrent_handle();
138
if (btih->compare(0, 9, "urn:btih:") != 0) return torrent_handle();
141
if (btih->size() == 40 + 9) info_hash = boost::lexical_cast<sha1_hash>(btih->substr(9));
142
else info_hash.assign(base32decode(btih->substr(9)));
144
if (!tracker.empty()) p.tracker_url = tracker.c_str();
145
p.info_hash = info_hash;
146
if (!name.empty()) p.name = name.c_str();
147
return ses.add_torrent(p);