XmlRpcMethodImpl.cc 13 KB

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