added some precautionary checks in bdecoder
[libtorrent.git] / src / magnet_uri.cpp
blobaf54de9e0cc29d8840034673072005003e90ff73
1 /*
3 Copyright (c) 2007, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
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"
38 #include <string>
39 #include <sstream>
41 namespace libtorrent
43 std::string make_magnet_uri(torrent_handle const& handle)
45 std::stringstream ret;
46 if (!handle.is_valid()) return ret.str();
48 std::string name = handle.name();
50 ret << "magnet:?xt=urn:btih:" << base32encode(
51 std::string((char*)handle.info_hash().begin(), 20));
52 if (!name.empty())
53 ret << "&dn=" << escape_string(name.c_str(), name.length());
54 torrent_status st = handle.status();
55 if (!st.current_tracker.empty())
57 ret << "&tr=" << escape_string(st.current_tracker.c_str()
58 , st.current_tracker.length());
60 else
62 std::vector<announce_entry> const& tr = handle.trackers();
63 if (!tr.empty())
65 ret << "&tr=" << escape_string(tr[0].url.c_str()
66 , tr[0].url.length());
69 return ret.str();
72 #ifndef TORRENT_NO_DEPRECATE
73 torrent_handle add_magnet_uri(session& ses, std::string const& uri
74 , fs::path const& save_path
75 , storage_mode_t storage_mode
76 , bool paused
77 , storage_constructor_type sc
78 , void* userdata)
80 std::string name;
81 std::string tracker;
83 boost::optional<std::string> display_name = url_has_argument(uri, "dn");
84 if (display_name) name = unescape_string(display_name->c_str());
85 boost::optional<std::string> tracker_string = url_has_argument(uri, "tr");
86 if (tracker_string) tracker = unescape_string(tracker_string->c_str());
88 boost::optional<std::string> btih = url_has_argument(uri, "xt");
89 if (!btih) return torrent_handle();
91 if (btih->compare(0, 9, "urn:btih:") != 0) return torrent_handle();
93 sha1_hash info_hash(base32decode(btih->substr(9)));
95 return ses.add_torrent(tracker.empty() ? 0 : tracker.c_str(), info_hash
96 , name.empty() ? 0 : name.c_str(), save_path, entry()
97 , storage_mode, paused, sc, userdata);
99 #endif
101 torrent_handle add_magnet_uri(session& ses, std::string const& uri
102 , add_torrent_params p)
104 std::string name;
105 std::string tracker;
107 boost::optional<std::string> display_name = url_has_argument(uri, "dn");
108 if (display_name) name = unescape_string(display_name->c_str());
109 boost::optional<std::string> tracker_string = url_has_argument(uri, "tr");
110 if (tracker_string) tracker = unescape_string(tracker_string->c_str());
112 boost::optional<std::string> btih = url_has_argument(uri, "xt");
113 if (!btih) return torrent_handle();
115 if (btih->compare(0, 9, "urn:btih:") != 0) return torrent_handle();
117 sha1_hash info_hash(base32decode(btih->substr(9)));
119 if (!tracker.empty()) p.tracker_url = tracker.c_str();
120 p.info_hash = info_hash;
121 if (!name.empty()) p.name = name.c_str();
122 return ses.add_torrent(p);