~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to src/magnet_uri.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Sauthier
  • Date: 2010-08-10 12:59:37 UTC
  • mfrom: (1.3.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100810125937-jbcmmf17y8yo9hgz
Tags: 0.15.0-0ubuntu1
* New upstream version.
* debian/patches/100_fix_html_docs.patch: refreshed.
* debian/control: bump up standards-version to 3.9.1 (no changes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#include "libtorrent/escape_string.hpp"
37
37
 
38
38
#include <string>
39
 
#include <sstream>
40
 
#include <boost/lexical_cast.hpp>
41
39
 
42
40
namespace libtorrent
43
41
{
44
42
        std::string make_magnet_uri(torrent_handle const& handle)
45
43
        {
46
 
                std::stringstream ret;
47
 
                if (!handle.is_valid()) return ret.str();
 
44
                if (!handle.is_valid()) return "";
 
45
 
 
46
                char ret[1024];
 
47
                sha1_hash const& ih = handle.info_hash();
 
48
                int num_chars = snprintf(ret, sizeof(ret), "magnet:?xt=urn:btih:%s"
 
49
                        , base32encode(std::string((char const*)&ih[0], 20)).c_str());
48
50
 
49
51
                std::string name = handle.name();
50
52
 
51
 
                ret << "magnet:?xt=urn:btih:" << base32encode(
52
 
                        std::string((char*)handle.info_hash().begin(), 20));
53
53
                if (!name.empty())
54
 
                        ret << "&dn=" << escape_string(name.c_str(), name.length());
 
54
                        num_chars += snprintf(ret + num_chars, sizeof(ret) - num_chars, "&dn=%s"
 
55
                                , escape_string(name.c_str(), name.length()).c_str());
 
56
 
 
57
                char const* tracker = 0;
55
58
                torrent_status st = handle.status();
56
59
                if (!st.current_tracker.empty())
57
60
                {
58
 
                        ret << "&tr=" << escape_string(st.current_tracker.c_str()
59
 
                                , st.current_tracker.length());
 
61
                        tracker = st.current_tracker.c_str();
60
62
                }
61
63
                else
62
64
                {
63
65
                        std::vector<announce_entry> const& tr = handle.trackers();
64
 
                        if (!tr.empty())
65
 
                        {
66
 
                                ret << "&tr=" << escape_string(tr[0].url.c_str()
67
 
                                        , tr[0].url.length());
68
 
                        }
 
66
                        if (!tr.empty()) tracker = tr[0].url.c_str();
69
67
                }
70
 
                return ret.str();
 
68
                if (tracker)
 
69
                        num_chars += snprintf(ret + num_chars, sizeof(ret) - num_chars, "&tr=%s"
 
70
                                , escape_string(tracker, strlen(tracker)).c_str());
 
71
 
 
72
                return ret;
71
73
        }
72
74
 
73
75
        std::string make_magnet_uri(torrent_info const& info)
74
76
        {
75
 
                std::stringstream ret;
76
 
                if (!info.is_valid()) return ret.str();
77
 
 
78
 
                std::string name = info.name();
79
 
 
80
 
                ret << "magnet:?xt=urn:btih:" << base32encode(
81
 
                        std::string((char*)info.info_hash().begin(), 20));
 
77
                char ret[1024];
 
78
                sha1_hash const& ih = info.info_hash();
 
79
                int num_chars = snprintf(ret, sizeof(ret), "magnet:?xt=urn:btih:%s"
 
80
                        , base32encode(std::string((char*)&ih[0], 20)).c_str());
 
81
 
 
82
                std::string const& name = info.name();
 
83
 
82
84
                if (!name.empty())
83
 
                        ret << "&dn=" << escape_string(name.c_str(), name.length());
 
85
                        num_chars += snprintf(ret + num_chars, sizeof(ret) - num_chars, "&dn=%s"
 
86
                                , escape_string(name.c_str(), name.length()).c_str());
 
87
 
84
88
                std::vector<announce_entry> const& tr = info.trackers();
85
89
                if (!tr.empty())
86
90
                {
87
 
                        ret << "&tr=" << escape_string(tr[0].url.c_str()
88
 
                                , tr[0].url.length());
 
91
                        num_chars += snprintf(ret + num_chars, sizeof(ret) - num_chars, "&tr=%s"
 
92
                                , escape_string(tr[0].url.c_str(), tr[0].url.length()).c_str());
89
93
                }
90
 
                return ret.str();
 
94
 
 
95
                return ret;
91
96
        }
92
97
 
 
98
#ifndef BOOST_NO_EXCEPTIONS
93
99
#ifndef TORRENT_NO_DEPRECATE
94
100
        torrent_handle add_magnet_uri(session& ses, std::string const& uri
95
101
                , fs::path const& save_path
101
107
                std::string name;
102
108
                std::string tracker;
103
109
 
 
110
                error_code ec;
104
111
                boost::optional<std::string> display_name = url_has_argument(uri, "dn");
105
 
                if (display_name) name = unescape_string(display_name->c_str());
 
112
                if (display_name) name = unescape_string(display_name->c_str(), ec);
106
113
                boost::optional<std::string> tracker_string = url_has_argument(uri, "tr");
107
 
                if (tracker_string) tracker = unescape_string(tracker_string->c_str());
 
114
                if (tracker_string) tracker = unescape_string(tracker_string->c_str(), ec);
108
115
        
109
116
                boost::optional<std::string> btih = url_has_argument(uri, "xt");
110
117
                if (!btih) return torrent_handle();
112
119
                if (btih->compare(0, 9, "urn:btih:") != 0) return torrent_handle();
113
120
 
114
121
                sha1_hash info_hash;
115
 
                if (btih->size() == 40 + 9) info_hash = boost::lexical_cast<sha1_hash>(btih->substr(9));
 
122
                if (btih->size() == 40 + 9) from_hex(&(*btih)[9], 40, (char*)&info_hash[0]);
116
123
                else info_hash.assign(base32decode(btih->substr(9)));
117
124
 
118
125
                return ses.add_torrent(tracker.empty() ? 0 : tracker.c_str(), info_hash
124
131
        torrent_handle add_magnet_uri(session& ses, std::string const& uri
125
132
                , add_torrent_params p)
126
133
        {
 
134
                error_code ec;
 
135
                torrent_handle ret = add_magnet_uri(ses, uri, p, ec);
 
136
                if (ec) throw libtorrent_exception(ec);
 
137
                return ret;
 
138
        }
 
139
#endif
 
140
        torrent_handle add_magnet_uri(session& ses, std::string const& uri
 
141
                , add_torrent_params p, error_code& ec)
 
142
        {
127
143
                std::string name;
128
144
                std::string tracker;
129
145
 
 
146
                error_code e;
130
147
                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());
 
148
                if (display_name) name = unescape_string(display_name->c_str(), e);
 
149
                int pos = std::string::npos;
 
150
                boost::optional<std::string> tracker_string = url_has_argument(uri, "tr", &pos);
 
151
                if (tracker_string) tracker = unescape_string(tracker_string->c_str(), e);
134
152
        
135
153
                boost::optional<std::string> btih = url_has_argument(uri, "xt");
136
 
                if (!btih) return torrent_handle();
 
154
                if (!btih)
 
155
                {
 
156
                        ec = errors::missing_info_hash_in_uri;
 
157
                        return torrent_handle();
 
158
                }
137
159
 
138
 
                if (btih->compare(0, 9, "urn:btih:") != 0) return torrent_handle();
 
160
                if (btih->compare(0, 9, "urn:btih:") != 0)
 
161
                {
 
162
                        ec = errors::missing_info_hash_in_uri;
 
163
                        return torrent_handle();
 
164
                }
139
165
 
140
166
                sha1_hash info_hash;
141
 
                if (btih->size() == 40 + 9) info_hash = boost::lexical_cast<sha1_hash>(btih->substr(9));
 
167
                if (btih->size() == 40 + 9) from_hex(&(*btih)[9], 40, (char*)&info_hash[0]);
142
168
                else info_hash.assign(base32decode(btih->substr(9)));
143
169
 
144
170
                if (!tracker.empty()) p.tracker_url = tracker.c_str();
145
171
                p.info_hash = info_hash;
146
172
                if (!name.empty()) p.name = name.c_str();
147
 
                return ses.add_torrent(p);
 
173
                torrent_handle ret = ses.add_torrent(p, ec);
 
174
 
 
175
                int tier = 1;
 
176
                // there might be more trackers in the url
 
177
                while (pos != std::string::npos)
 
178
                {
 
179
                        pos = uri.find("&tr=", pos);
 
180
                        if (pos == std::string::npos) break;
 
181
                        pos += 4;
 
182
                        announce_entry ae(uri.substr(pos, uri.find('&', pos) - pos));
 
183
                        ae.tier = tier++;
 
184
                        ret.add_tracker(ae);
 
185
                }
 
186
                return ret;
148
187
        }
149
188
}
150
189