1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2009 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#include "XmlRpcMethodImpl.h"
#include <cassert>
#include <algorithm>
#include "Logger.h"
#include "BDE.h"
#include "DlAbortEx.h"
#include "Option.h"
#include "OptionParser.h"
#include "OptionHandler.h"
#include "DownloadEngine.h"
#include "RequestGroup.h"
#include "download_helper.h"
#include "Util.h"
#include "RequestGroupMan.h"
#include "StringFormat.h"
#include "XmlRpcRequest.h"
#include "PieceStorage.h"
#include "PeerStorage.h"
#include "BtContext.h"
#include "BtRegistry.h"
#include "Peer.h"
#include "DiskAdaptor.h"
#include "FileEntry.h"
#include "BtProgressInfoFile.h"
#include "BtRuntime.h"
#include "BtAnnounce.h"
#include "prefs.h"
#include "message.h"
namespace aria2 {
namespace xmlrpc {
static const BDE BDE_TRUE = BDE("true");
static const BDE BDE_FALSE = BDE("false");
static const BDE BDE_OK = BDE("OK");
static const BDE BDE_ACTIVE = BDE("active");
static const BDE BDE_WAITING = BDE("waiting");
static const BDE BDE_REMOVED = BDE("removed");
static const BDE BDE_ERROR = BDE("error");
static const BDE BDE_COMPLETE = BDE("complete");
static BDE createGIDResponse(int32_t gid)
{
return BDE(Util::itos(gid));
}
BDE AddUriXmlRpcMethod::process(const XmlRpcRequest& req, DownloadEngine* e)
{
const BDE& params = req._params;
assert(params.isList());
if(params.empty() || !params[0].isList() || params[0].empty()) {
throw DlAbortEx("URI is not provided.");
}
std::deque<std::string> uris;
for(BDE::List::const_iterator i = params[0].listBegin();
i != params[0].listEnd(); ++i) {
if((*i).isString()) {
uris.push_back((*i).s());
}
}
SharedHandle<Option> requestOption(new Option(*e->option));
if(params.size() > 1 && params[1].isDict()) {
gatherRequestOption(requestOption, params[1]);
}
std::deque<SharedHandle<RequestGroup> > result;
createRequestGroupForUri(result, requestOption, uris,
/* ignoreForceSeq = */ true,
/* ignoreNonURI = */ true);
if(!result.empty()) {
e->_requestGroupMan->addReservedGroup(result.front());
return createGIDResponse(result.front()->getGID());
} else {
throw DlAbortEx("No URI to download.");
}
}
BDE AddTorrentXmlRpcMethod::process
(const XmlRpcRequest& req, DownloadEngine* e)
{
const BDE& params = req._params;
assert(params.isList());
if(params.empty() || !params[0].isString()) {
throw DlAbortEx("Torrent data is not provided.");
}
std::deque<std::string> uris;
if(params.size() > 1 && params[1].isList()) {
for(BDE::List::const_iterator i = params[1].listBegin();
i != params[1].listEnd(); ++i) {
if((*i).isString()) {
uris.push_back((*i).s());
}
}
}
SharedHandle<Option> requestOption(new Option(*e->option));
if(params.size() > 2 && params[2].isDict()) {
gatherRequestOption(requestOption, params[2]);
}
std::deque<SharedHandle<RequestGroup> > result;
createRequestGroupForBitTorrent(result, requestOption,
uris,
params[0].s());
if(!result.empty()) {
e->_requestGroupMan->addReservedGroup(result.front());
return createGIDResponse(result.front()->getGID());
} else {
throw DlAbortEx("No Torrent to download.");
}
}
BDE AddMetalinkXmlRpcMethod::process
(const XmlRpcRequest& req, DownloadEngine* e)
{
const BDE& params = req._params;
assert(params.isList());
if(params.empty() || !params[0].isString()) {
throw DlAbortEx("Metalink data is not provided.");
}
SharedHandle<Option> requestOption(new Option(*e->option));
if(params.size() > 1 && params[1].isDict()) {
gatherRequestOption(requestOption, params[1]);
}
std::deque<SharedHandle<RequestGroup> > result;
createRequestGroupForMetalink(result, requestOption, params[0].s());
if(!result.empty()) {
e->_requestGroupMan->addReservedGroup(result);
BDE gids = BDE::list();
for(std::deque<SharedHandle<RequestGroup> >::const_iterator i =
result.begin(); i != result.end(); ++i) {
gids << BDE(Util::itos((*i)->getGID()));
}
return gids;
} else {
throw DlAbortEx("No files to download.");
}
}
BDE RemoveXmlRpcMethod::process(const XmlRpcRequest& req, DownloadEngine* e)
{
const BDE& params = req._params;
assert(params.isList());
if(params.empty() || !params[0].isString()) {
throw DlAbortEx(MSG_GID_NOT_PROVIDED);
}
int32_t gid = Util::parseInt(params[0].s());
SharedHandle<RequestGroup> group = e->_requestGroupMan->findRequestGroup(gid);
if(group.isNull()) {
group = e->_requestGroupMan->findReservedGroup(gid);
if(group.isNull()) {
throw DlAbortEx
(StringFormat("Active Download not found for GID#%d", gid).str());
}
if(group->isDependencyResolved()) {
e->_requestGroupMan->removeReservedGroup(gid);
} else {
throw DlAbortEx
(StringFormat("GID#%d cannot be removed now", gid).str());
}
} else {
group->setHaltRequested(true, RequestGroup::USER_REQUEST);
}
return createGIDResponse(gid);
}
static void gatherProgressCommon
(BDE& entryDict, const SharedHandle<RequestGroup>& group)
{
entryDict["gid"] = Util::itos(group->getGID());
// This is "filtered" total length if --select-file is used.
entryDict["totalLength"] = Util::uitos(group->getTotalLength());
// This is "filtered" total length if --select-file is used.
entryDict["completedLength"] = Util::uitos(group->getCompletedLength());
TransferStat stat = group->calculateStat();
entryDict["downloadSpeed"] = Util::uitos(stat.getDownloadSpeed());
entryDict["uploadSpeed"] = Util::uitos(stat.getUploadSpeed());
entryDict["connections"] = Util::uitos(group->getNumConnection());
SharedHandle<PieceStorage> ps = group->getPieceStorage();
if(!ps.isNull()) {
if(ps->getBitfieldLength() > 0) {
entryDict["bitfield"] = Util::toHex(ps->getBitfield(),
ps->getBitfieldLength());
}
}
entryDict["pieceLength"] =
Util::uitos(group->getDownloadContext()->getPieceLength());
entryDict["numPieces"] =
Util::uitos(group->getDownloadContext()->getNumPieces());
}
static void gatherProgressBitTorrent
(BDE& entryDict, const SharedHandle<BtContext>& btctx)
{
entryDict["infoHash"] = btctx->getInfoHashAsString();
}
static void gatherPeer(BDE& peers, const SharedHandle<PeerStorage>& ps)
{
std::deque<SharedHandle<Peer> > activePeers;
ps->getActivePeers(activePeers);
for(std::deque<SharedHandle<Peer> >::const_iterator i =
activePeers.begin(); i != activePeers.end(); ++i) {
BDE peerEntry = BDE::dict();
peerEntry["peerId"] = Util::torrentUrlencode((*i)->getPeerId(),
PEER_ID_LENGTH);
peerEntry["ip"] = (*i)->ipaddr;
peerEntry["port"] = Util::uitos((*i)->port);
peerEntry["bitfield"] = Util::toHex((*i)->getBitfield(),
(*i)->getBitfieldLength());
peerEntry["amChoking"] = (*i)->amChoking()?BDE_TRUE:BDE_FALSE;
peerEntry["peerChoking"] = (*i)->peerChoking()?BDE_TRUE:BDE_FALSE;
TransferStat stat = ps->getTransferStatFor(*i);
peerEntry["downloadSpeed"] = Util::uitos(stat.getDownloadSpeed());
peerEntry["uploadSpeed"] = Util::uitos(stat.getUploadSpeed());
peerEntry["seeder"] = (*i)->isSeeder()?BDE_TRUE:BDE_FALSE;
peers << peerEntry;
}
}
static void gatherProgress
(BDE& entryDict, const SharedHandle<RequestGroup>& group, DownloadEngine* e)
{
gatherProgressCommon(entryDict, group);
SharedHandle<BtContext> btctx =
dynamic_pointer_cast<BtContext>(group->getDownloadContext());
if(!btctx.isNull()) {
gatherProgressBitTorrent(entryDict, btctx);
}
}
static void gatherStoppedDownload
(BDE& entryDict, const SharedHandle<DownloadResult>& ds)
{
entryDict["gid"] = Util::itos(ds->gid);
if(ds->result == DownloadResult::IN_PROGRESS) {
entryDict["status"] = BDE_REMOVED;
} else if(ds->result == DownloadResult::FINISHED) {
entryDict["status"] = BDE_COMPLETE;
} else {
entryDict["status"] = BDE_ERROR;
}
}
static
SharedHandle<RequestGroup>
findRequestGroup(const SharedHandle<RequestGroupMan>& rgman, int32_t gid)
{
SharedHandle<RequestGroup> group = rgman->findRequestGroup(gid);
if(group.isNull()) {
group = rgman->findReservedGroup(gid);
}
return group;
}
template<typename InputIterator>
static void createFileEntry(BDE& files, InputIterator first, InputIterator last)
{
size_t index = 1;
for(; first != last; ++first, ++index) {
BDE entry = BDE::dict();
entry["index"] = Util::uitos(index);
entry["path"] = (*first)->getPath();
entry["selected"] = (*first)->isRequested()?BDE_TRUE:BDE_FALSE;
entry["length"] = Util::uitos((*first)->getLength());
files << entry;
}
}
BDE GetFilesXmlRpcMethod::process
(const XmlRpcRequest& req, DownloadEngine* e)
{
const BDE& params = req._params;
assert(params.isList());
if(params.empty() || !params[0].isString()) {
throw DlAbortEx(MSG_GID_NOT_PROVIDED);
}
int32_t gid = Util::parseInt(params[0].s());
BDE files = BDE::list();
SharedHandle<RequestGroup> group = findRequestGroup(e->_requestGroupMan, gid);
if(group.isNull()) {
SharedHandle<DownloadResult> dr =
e->_requestGroupMan->findDownloadResult(gid);
if(dr.isNull()) {
throw DlAbortEx
(StringFormat("No file data is available for GID#%d", gid).str());
} else {
createFileEntry(files, dr->fileEntries.begin(), dr->fileEntries.end());
}
} else {
std::deque<SharedHandle<FileEntry> > fileEntries =
group->getDownloadContext()->getFileEntries();
createFileEntry(files, fileEntries.begin(), fileEntries.end());
}
return files;
}
BDE GetUrisXmlRpcMethod::process
(const XmlRpcRequest& req, DownloadEngine* e)
{
const BDE& params = req._params;
assert(params.isList());
if(params.empty() || !params[0].isString()) {
throw DlAbortEx(MSG_GID_NOT_PROVIDED);
}
int32_t gid = Util::parseInt(params[0].s());
SharedHandle<RequestGroup> group = findRequestGroup(e->_requestGroupMan, gid);
if(group.isNull()) {
throw DlAbortEx
(StringFormat("No URI data is available for GID#%d", gid).str());
}
BDE uriList = BDE::list();
std::deque<std::string> uris;
group->getURIs(uris);
for(std::deque<std::string>::const_iterator i = uris.begin(); i != uris.end();
++i) {
BDE entry = BDE::dict();
entry["uri"] = *i;
uriList << entry;
}
return uriList;
}
BDE GetPeersXmlRpcMethod::process
(const XmlRpcRequest& req, DownloadEngine* e)
{
const BDE& params = req._params;
assert(params.isList());
if(params.empty() || !params[0].isString()) {
throw DlAbortEx(MSG_GID_NOT_PROVIDED);
}
int32_t gid = Util::parseInt(params[0].s());
SharedHandle<RequestGroup> group = findRequestGroup(e->_requestGroupMan, gid);
if(group.isNull()) {
throw DlAbortEx
(StringFormat("No peer data is available for GID#%d", gid).str());
}
BDE peers = BDE::list();
SharedHandle<BtContext> btctx =
dynamic_pointer_cast<BtContext>(group->getDownloadContext());
if(!btctx.isNull()) {
SharedHandle<BtRegistry> btreg = e->getBtRegistry();
SharedHandle<PeerStorage> ps =
btreg->getPeerStorage(btctx->getInfoHashAsString());
if(!ps.isNull()) {
BDE entry = BDE::dict();
gatherPeer(peers, ps);
}
}
return peers;
}
BDE TellStatusXmlRpcMethod::process
(const XmlRpcRequest& req, DownloadEngine* e)
{
const BDE& params = req._params;
assert(params.isList());
if(params.empty() || !params[0].isString()) {
throw DlAbortEx(MSG_GID_NOT_PROVIDED);
}
int32_t gid = Util::parseInt(params[0].s());
SharedHandle<RequestGroup> group = e->_requestGroupMan->findRequestGroup(gid);
BDE entryDict = BDE::dict();
if(group.isNull()) {
group = e->_requestGroupMan->findReservedGroup(gid);
if(group.isNull()) {
SharedHandle<DownloadResult> ds =
e->_requestGroupMan->findDownloadResult(gid);
if(ds.isNull()) {
throw DlAbortEx
(StringFormat("No such download for GID#%d", gid).str());
}
gatherStoppedDownload(entryDict, ds);
} else {
entryDict["status"] = BDE_WAITING;
gatherProgress(entryDict, group, e);
}
} else {
entryDict["status"] = BDE_ACTIVE;
gatherProgress(entryDict, group, e);
}
return entryDict;
}
BDE TellActiveXmlRpcMethod::process
(const XmlRpcRequest& req, DownloadEngine* e)
{
BDE list = BDE::list();
const std::deque<SharedHandle<RequestGroup> >& groups =
e->_requestGroupMan->getRequestGroups();
for(std::deque<SharedHandle<RequestGroup> >::const_iterator i =
groups.begin(); i != groups.end(); ++i) {
BDE entryDict = BDE::dict();
entryDict["status"] = BDE_ACTIVE;
gatherProgress(entryDict, *i, e);
list << entryDict;
}
return list;
}
BDE TellAllXmlRpcMethod::process
(const XmlRpcRequest& req, DownloadEngine* e)
{
BDE list = BDE::list();
const std::deque<SharedHandle<RequestGroup> >& groups =
e->_requestGroupMan->getRequestGroups();
for(std::deque<SharedHandle<RequestGroup> >::const_iterator i =
groups.begin(); i != groups.end(); ++i) {
BDE entryDict = BDE::dict();
entryDict["status"] = BDE_ACTIVE;
gatherProgress(entryDict, *i, e);
list << entryDict;
}
const std::deque<SharedHandle<RequestGroup> >& waiting =
e->_requestGroupMan->getReservedGroups();
for(std::deque<SharedHandle<RequestGroup> >::const_iterator i =
waiting.begin(); i != waiting.end(); ++i) {
BDE entryDict = BDE::dict();
entryDict["status"] = BDE_WAITING;
gatherProgress(entryDict, *i, e);
list << entryDict;
}
const std::deque<SharedHandle<DownloadResult> >& stopped =
e->_requestGroupMan->getDownloadResults();
for(std::deque<SharedHandle<DownloadResult> >::const_iterator i =
stopped.begin(); i != stopped.end(); ++i) {
BDE entryDict = BDE::dict();
gatherStoppedDownload(entryDict, *i);
list << entryDict;
}
return list;
}
BDE PurgeDownloadResultXmlRpcMethod::process
(const XmlRpcRequest& req, DownloadEngine* e)
{
e->_requestGroupMan->purgeDownloadResult();
return BDE_OK;
}
BDE ChangeOptionXmlRpcMethod::process
(const XmlRpcRequest& req, DownloadEngine* e)
{
const BDE& params = req._params;
assert(params.isList());
if(params.empty() || !params[0].isString()) {
throw DlAbortEx(MSG_GID_NOT_PROVIDED);
}
int32_t gid = Util::parseInt(params[0].s());
SharedHandle<RequestGroup> group = findRequestGroup(e->_requestGroupMan, gid);
if(group.isNull()) {
throw DlAbortEx
(StringFormat("Cannot change option for GID#%d", gid).str());
}
SharedHandle<Option> option(new Option(*group->getOption().get()));
if(params.size() > 1 && params[1].isDict()) {
gatherChangeableOption(option, params[1]);
}
if(option->defined(PREF_MAX_DOWNLOAD_LIMIT)) {
group->setMaxDownloadSpeedLimit(option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
}
if(option->defined(PREF_MAX_UPLOAD_LIMIT)) {
group->setMaxUploadSpeedLimit(option->getAsInt(PREF_MAX_UPLOAD_LIMIT));
}
return BDE_OK;
}
BDE ChangeGlobalOptionXmlRpcMethod::process
(const XmlRpcRequest& req, DownloadEngine* e)
{
const BDE& params = req._params;
assert(params.isList());
if(params.empty() || !params[0].isDict()) {
return BDE_OK;
}
SharedHandle<Option> option(new Option(*e->option));
gatherChangeableGlobalOption(option, params[0]);
if(option->defined(PREF_MAX_OVERALL_DOWNLOAD_LIMIT)) {
e->_requestGroupMan->setMaxOverallDownloadSpeedLimit
(option->getAsInt(PREF_MAX_OVERALL_DOWNLOAD_LIMIT));
}
if(option->defined(PREF_MAX_OVERALL_UPLOAD_LIMIT)) {
e->_requestGroupMan->setMaxOverallUploadSpeedLimit
(option->getAsInt(PREF_MAX_OVERALL_UPLOAD_LIMIT));
}
return BDE_OK;
}
BDE NoSuchMethodXmlRpcMethod::process
(const XmlRpcRequest& req, DownloadEngine* e)
{
throw DlAbortEx
(StringFormat("No such method: %s", req._methodName.c_str()).str());
}
} // namespace xmlrpc
} // namespace aria2
|