XmlRpcMethodImpl.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. namespace aria2 {
  62. namespace xmlrpc {
  63. static BDE createGIDResponse(int32_t gid)
  64. {
  65. return BDE(Util::itos(gid));
  66. }
  67. BDE AddUriXmlRpcMethod::process(const XmlRpcRequest& req, DownloadEngine* e)
  68. {
  69. const BDE& params = req._params;
  70. assert(params.isList());
  71. if(params.empty() || !params[0].isList() || params[0].empty()) {
  72. throw DlAbortEx("URI is not provided.");
  73. }
  74. std::deque<std::string> uris;
  75. for(BDE::List::const_iterator i = params[0].listBegin();
  76. i != params[0].listEnd(); ++i) {
  77. if((*i).isString()) {
  78. uris.push_back((*i).s());
  79. }
  80. }
  81. SharedHandle<Option> requestOption(new Option(*e->option));
  82. if(params.size() > 1 && params[1].isDict()) {
  83. gatherRequestOption(requestOption, params[1]);
  84. }
  85. std::deque<SharedHandle<RequestGroup> > result;
  86. createRequestGroupForUri(result, requestOption, uris,
  87. /* ignoreForceSeq = */ true,
  88. /* ignoreNonURI = */ true);
  89. if(!result.empty()) {
  90. e->_requestGroupMan->addReservedGroup(result.front());
  91. return createGIDResponse(result.front()->getGID());
  92. } else {
  93. throw DlAbortEx("No URI to download.");
  94. }
  95. }
  96. BDE AddTorrentXmlRpcMethod::process
  97. (const XmlRpcRequest& req, DownloadEngine* e)
  98. {
  99. const BDE& params = req._params;
  100. assert(params.isList());
  101. if(params.empty() || !params[0].isString()) {
  102. throw DlAbortEx("Torrent data is not provided.");
  103. }
  104. std::deque<std::string> uris;
  105. if(params.size() > 1 && params[1].isList()) {
  106. for(BDE::List::const_iterator i = params[1].listBegin();
  107. i != params[1].listEnd(); ++i) {
  108. if((*i).isString()) {
  109. uris.push_back((*i).s());
  110. }
  111. }
  112. }
  113. SharedHandle<Option> requestOption(new Option(*e->option));
  114. if(params.size() > 2 && params[2].isDict()) {
  115. gatherRequestOption(requestOption, params[2]);
  116. }
  117. std::deque<SharedHandle<RequestGroup> > result;
  118. createRequestGroupForBitTorrent(result, requestOption,
  119. uris,
  120. params[0].s());
  121. if(!result.empty()) {
  122. e->_requestGroupMan->addReservedGroup(result.front());
  123. return createGIDResponse(result.front()->getGID());
  124. } else {
  125. throw DlAbortEx("No Torrent to download.");
  126. }
  127. }
  128. BDE AddMetalinkXmlRpcMethod::process
  129. (const XmlRpcRequest& req, DownloadEngine* e)
  130. {
  131. const BDE& params = req._params;
  132. assert(params.isList());
  133. if(params.empty() || !params[0].isString()) {
  134. throw DlAbortEx("Metalink data is not provided.");
  135. }
  136. SharedHandle<Option> requestOption(new Option(*e->option));
  137. if(params.size() > 1 && params[1].isDict()) {
  138. gatherRequestOption(requestOption, params[1]);
  139. }
  140. std::deque<SharedHandle<RequestGroup> > result;
  141. createRequestGroupForMetalink(result, requestOption, params[0].s());
  142. if(!result.empty()) {
  143. e->_requestGroupMan->addReservedGroup(result);
  144. BDE gids = BDE::list();
  145. for(std::deque<SharedHandle<RequestGroup> >::const_iterator i =
  146. result.begin(); i != result.end(); ++i) {
  147. gids << BDE(Util::itos((*i)->getGID()));
  148. }
  149. return gids;
  150. } else {
  151. throw DlAbortEx("No files to download.");
  152. }
  153. }
  154. BDE RemoveXmlRpcMethod::process(const XmlRpcRequest& req, DownloadEngine* e)
  155. {
  156. const BDE& params = req._params;
  157. assert(params.isList());
  158. if(params.empty() || !params[0].isString()) {
  159. throw DlAbortEx("GID is not provided.");
  160. }
  161. int32_t gid = Util::parseInt(params[0].s());
  162. SharedHandle<RequestGroup> group = e->_requestGroupMan->findRequestGroup(gid);
  163. if(group.isNull()) {
  164. group = e->_requestGroupMan->findReservedGroup(gid);
  165. if(group.isNull()) {
  166. throw DlAbortEx
  167. (StringFormat("Active Download not found for GID#%d", gid).str());
  168. }
  169. if(group->isDependencyResolved()) {
  170. e->_requestGroupMan->removeReservedGroup(gid);
  171. } else {
  172. throw DlAbortEx
  173. (StringFormat("GID#%d cannot be removed now", gid).str());
  174. }
  175. } else {
  176. group->setHaltRequested(true, RequestGroup::USER_REQUEST);
  177. }
  178. return createGIDResponse(gid);
  179. }
  180. static void gatherProgressCommon
  181. (BDE& entryDict, const SharedHandle<RequestGroup>& group)
  182. {
  183. entryDict["gid"] = Util::itos(group->getGID());
  184. // This is "filtered" total length if --select-file is used.
  185. entryDict["totalLength"] = Util::uitos(group->getTotalLength());
  186. // This is "filtered" total length if --select-file is used.
  187. entryDict["completedLength"] = Util::uitos(group->getCompletedLength());
  188. TransferStat stat = group->calculateStat();
  189. entryDict["downloadSpeed"] = Util::uitos(stat.getDownloadSpeed());
  190. entryDict["uploadSpeed"] = Util::uitos(stat.getUploadSpeed());
  191. entryDict["connections"] = Util::uitos(group->getNumConnection());
  192. SharedHandle<PieceStorage> ps = group->getPieceStorage();
  193. if(!ps.isNull()) {
  194. if(ps->getBitfieldLength() > 0) {
  195. entryDict["bitfield"] = Util::toHex(ps->getBitfield(),
  196. ps->getBitfieldLength());
  197. }
  198. }
  199. entryDict["pieceLength"] =
  200. Util::uitos(group->getDownloadContext()->getPieceLength());
  201. entryDict["numPieces"] =
  202. Util::uitos(group->getDownloadContext()->getNumPieces());
  203. }
  204. static void gatherProgressBitTorrent
  205. (BDE& entryDict, const SharedHandle<BtContext>& btctx)
  206. {
  207. entryDict["infoHash"] = btctx->getInfoHashAsString();
  208. }
  209. static void gatherPeer(BDE& peers, const SharedHandle<PeerStorage>& ps)
  210. {
  211. std::deque<SharedHandle<Peer> > activePeers;
  212. ps->getActivePeers(activePeers);
  213. for(std::deque<SharedHandle<Peer> >::const_iterator i =
  214. activePeers.begin(); i != activePeers.end(); ++i) {
  215. BDE peerEntry = BDE::dict();
  216. peerEntry["peerId"] = Util::torrentUrlencode((*i)->getPeerId(),
  217. PEER_ID_LENGTH);
  218. peerEntry["ip"] = (*i)->ipaddr;
  219. peerEntry["port"] = Util::uitos((*i)->port);
  220. peerEntry["bitfield"] = Util::toHex((*i)->getBitfield(),
  221. (*i)->getBitfieldLength());
  222. peerEntry["amChoking"] = (*i)->amChoking()?BDE("true"):BDE("false");
  223. peerEntry["peerChoking"] = (*i)->peerChoking()?BDE("true"):BDE("false");
  224. TransferStat stat = ps->getTransferStatFor(*i);
  225. peerEntry["downloadSpeed"] = Util::uitos(stat.getDownloadSpeed());
  226. peerEntry["uploadSpeed"] = Util::uitos(stat.getUploadSpeed());
  227. peerEntry["seeder"] = (*i)->isSeeder()?BDE("true"):BDE("false");
  228. peers << peerEntry;
  229. }
  230. }
  231. static void gatherProgress
  232. (BDE& entryDict, const SharedHandle<RequestGroup>& group, DownloadEngine* e)
  233. {
  234. gatherProgressCommon(entryDict, group);
  235. SharedHandle<BtContext> btctx =
  236. dynamic_pointer_cast<BtContext>(group->getDownloadContext());
  237. if(!btctx.isNull()) {
  238. gatherProgressBitTorrent(entryDict, btctx);
  239. }
  240. }
  241. static void gatherStoppedDownload
  242. (BDE& entryDict, const SharedHandle<DownloadResult>& ds)
  243. {
  244. entryDict["gid"] = Util::itos(ds->gid);
  245. if(ds->result == DownloadResult::IN_PROGRESS) {
  246. entryDict["status"] = BDE("removed");
  247. } else if(ds->result == DownloadResult::FINISHED) {
  248. entryDict["status"] = BDE("complete");
  249. } else {
  250. entryDict["status"] = BDE("error");
  251. }
  252. }
  253. static
  254. SharedHandle<RequestGroup>
  255. findRequestGroup(const SharedHandle<RequestGroupMan>& rgman, int32_t gid)
  256. {
  257. SharedHandle<RequestGroup> group = rgman->findRequestGroup(gid);
  258. if(group.isNull()) {
  259. group = rgman->findReservedGroup(gid);
  260. }
  261. return group;
  262. }
  263. template<typename InputIterator>
  264. static void createFileEntry(BDE& files, InputIterator first, InputIterator last)
  265. {
  266. size_t index = 1;
  267. for(; first != last; ++first, ++index) {
  268. BDE entry = BDE::dict();
  269. entry["index"] = Util::uitos(index);
  270. entry["path"] = (*first)->getPath();
  271. entry["selected"] = (*first)->isRequested()?BDE("true"):BDE("false");
  272. entry["length"] = Util::uitos((*first)->getLength());
  273. files << entry;
  274. }
  275. }
  276. BDE GetFilesXmlRpcMethod::process
  277. (const XmlRpcRequest& req, DownloadEngine* e)
  278. {
  279. const BDE& params = req._params;
  280. assert(params.isList());
  281. if(params.empty() || !params[0].isString()) {
  282. throw DlAbortEx("GID is not provided.");
  283. }
  284. int32_t gid = Util::parseInt(params[0].s());
  285. BDE files = BDE::list();
  286. SharedHandle<RequestGroup> group = findRequestGroup(e->_requestGroupMan, gid);
  287. if(group.isNull()) {
  288. SharedHandle<DownloadResult> dr =
  289. e->_requestGroupMan->findDownloadResult(gid);
  290. if(dr.isNull()) {
  291. throw DlAbortEx
  292. (StringFormat("No file data is available for GID#%d", gid).str());
  293. } else {
  294. createFileEntry(files, dr->fileEntries.begin(), dr->fileEntries.end());
  295. }
  296. } else {
  297. std::deque<SharedHandle<FileEntry> > fileEntries =
  298. group->getDownloadContext()->getFileEntries();
  299. createFileEntry(files, fileEntries.begin(), fileEntries.end());
  300. }
  301. return files;
  302. }
  303. BDE GetUrisXmlRpcMethod::process
  304. (const XmlRpcRequest& req, DownloadEngine* e)
  305. {
  306. const BDE& params = req._params;
  307. assert(params.isList());
  308. if(params.empty() || !params[0].isString()) {
  309. throw DlAbortEx("GID is not provided.");
  310. }
  311. int32_t gid = Util::parseInt(params[0].s());
  312. SharedHandle<RequestGroup> group = findRequestGroup(e->_requestGroupMan, gid);
  313. if(group.isNull()) {
  314. throw DlAbortEx
  315. (StringFormat("No URI data is available for GID#%d", gid).str());
  316. }
  317. BDE uriList = BDE::list();
  318. std::deque<std::string> uris;
  319. group->getURIs(uris);
  320. for(std::deque<std::string>::const_iterator i = uris.begin(); i != uris.end();
  321. ++i) {
  322. BDE entry = BDE::dict();
  323. entry["uri"] = *i;
  324. uriList << entry;
  325. }
  326. return uriList;
  327. }
  328. BDE GetPeersXmlRpcMethod::process
  329. (const XmlRpcRequest& req, DownloadEngine* e)
  330. {
  331. const BDE& params = req._params;
  332. assert(params.isList());
  333. if(params.empty() || !params[0].isString()) {
  334. throw DlAbortEx("GID is not provided.");
  335. }
  336. int32_t gid = Util::parseInt(params[0].s());
  337. SharedHandle<RequestGroup> group = findRequestGroup(e->_requestGroupMan, gid);
  338. if(group.isNull()) {
  339. throw DlAbortEx
  340. (StringFormat("No peer data is available for GID#%d", gid).str());
  341. }
  342. BDE peers = BDE::list();
  343. SharedHandle<BtContext> btctx =
  344. dynamic_pointer_cast<BtContext>(group->getDownloadContext());
  345. if(!btctx.isNull()) {
  346. SharedHandle<BtRegistry> btreg = e->getBtRegistry();
  347. SharedHandle<PeerStorage> ps =
  348. btreg->getPeerStorage(btctx->getInfoHashAsString());
  349. if(!ps.isNull()) {
  350. BDE entry = BDE::dict();
  351. gatherPeer(peers, ps);
  352. }
  353. }
  354. return peers;
  355. }
  356. BDE TellStatusXmlRpcMethod::process
  357. (const XmlRpcRequest& req, DownloadEngine* e)
  358. {
  359. const BDE& params = req._params;
  360. assert(params.isList());
  361. if(params.empty() || !params[0].isString()) {
  362. throw DlAbortEx("GID is not provided.");
  363. }
  364. int32_t gid = Util::parseInt(params[0].s());
  365. SharedHandle<RequestGroup> group = e->_requestGroupMan->findRequestGroup(gid);
  366. BDE entryDict = BDE::dict();
  367. if(group.isNull()) {
  368. group = e->_requestGroupMan->findReservedGroup(gid);
  369. if(group.isNull()) {
  370. SharedHandle<DownloadResult> ds =
  371. e->_requestGroupMan->findDownloadResult(gid);
  372. if(ds.isNull()) {
  373. throw DlAbortEx
  374. (StringFormat("No such download for GID#%d", gid).str());
  375. }
  376. gatherStoppedDownload(entryDict, ds);
  377. } else {
  378. entryDict["status"] = BDE("waiting");
  379. gatherProgress(entryDict, group, e);
  380. }
  381. } else {
  382. entryDict["status"] = BDE("active");
  383. gatherProgress(entryDict, group, e);
  384. }
  385. return entryDict;
  386. }
  387. BDE TellActiveXmlRpcMethod::process
  388. (const XmlRpcRequest& req, DownloadEngine* e)
  389. {
  390. BDE list = BDE::list();
  391. const std::deque<SharedHandle<RequestGroup> >& groups =
  392. e->_requestGroupMan->getRequestGroups();
  393. for(std::deque<SharedHandle<RequestGroup> >::const_iterator i =
  394. groups.begin(); i != groups.end(); ++i) {
  395. BDE entryDict = BDE::dict();
  396. entryDict["status"] = BDE("active");
  397. gatherProgressCommon(entryDict, *i);
  398. SharedHandle<BtContext> btctx =
  399. dynamic_pointer_cast<BtContext>((*i)->getDownloadContext());
  400. if(!btctx.isNull()) {
  401. gatherProgressBitTorrent(entryDict, btctx);
  402. }
  403. list << entryDict;
  404. }
  405. return list;
  406. }
  407. BDE NoSuchMethodXmlRpcMethod::process
  408. (const XmlRpcRequest& req, DownloadEngine* e)
  409. {
  410. throw DlAbortEx
  411. (StringFormat("No such method: %s", req._methodName.c_str()).str());
  412. }
  413. } // namespace xmlrpc
  414. } // namespace aria2