XmlRpcMethodImpl.cc 16 KB

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