XmlRpcMethodImpl.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 << 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"] = Util::itos(group->getGID());
  152. // This is "filtered" total length if --select-file is used.
  153. entryDict["totalLength"] = Util::uitos(group->getTotalLength());
  154. // This is "filtered" total length if --select-file is used.
  155. entryDict["completedLength"] = Util::uitos(group->getCompletedLength());
  156. TransferStat stat = group->calculateStat();
  157. entryDict["downloadSpeed"] = Util::uitos(stat.getDownloadSpeed());
  158. entryDict["uploadSpeed"] = Util::uitos(stat.getUploadSpeed());
  159. entryDict["connections"] = Util::uitos(group->getNumConnection());
  160. SharedHandle<PieceStorage> ps = group->getPieceStorage();
  161. if(!ps.isNull()) {
  162. if(ps->getBitfieldLength() > 0) {
  163. entryDict["bitfield"] = Util::toHex(ps->getBitfield(),
  164. ps->getBitfieldLength());
  165. }
  166. }
  167. entryDict["pieceLength"] =
  168. Util::uitos(group->getDownloadContext()->getPieceLength());
  169. entryDict["numPieces"] =
  170. Util::uitos(group->getDownloadContext()->getNumPieces());
  171. }
  172. static void gatherProgressBitTorrent
  173. (BDE& entryDict, const SharedHandle<BtContext>& btctx)
  174. {
  175. entryDict["infoHash"] = btctx->getInfoHashAsString();
  176. }
  177. static void gatherPeer(BDE& peers, const SharedHandle<PeerStorage>& ps)
  178. {
  179. std::deque<SharedHandle<Peer> > activePeers;
  180. ps->getActivePeers(activePeers);
  181. for(std::deque<SharedHandle<Peer> >::const_iterator i =
  182. activePeers.begin(); i != activePeers.end(); ++i) {
  183. BDE peerEntry = BDE::dict();
  184. peerEntry["peerId"] = Util::torrentUrlencode((*i)->getPeerId(),
  185. PEER_ID_LENGTH);
  186. peerEntry["ip"] = (*i)->ipaddr;
  187. peerEntry["port"] = Util::uitos((*i)->port);
  188. peerEntry["bitfield"] = Util::toHex((*i)->getBitfield(),
  189. (*i)->getBitfieldLength());
  190. peers << peerEntry;
  191. }
  192. }
  193. static void gatherProgress
  194. (BDE& entryDict, const SharedHandle<RequestGroup>& group, DownloadEngine* e)
  195. {
  196. gatherProgressCommon(entryDict, group);
  197. SharedHandle<BtContext> btctx =
  198. dynamic_pointer_cast<BtContext>(group->getDownloadContext());
  199. if(!btctx.isNull()) {
  200. gatherProgressBitTorrent(entryDict, btctx);
  201. }
  202. }
  203. static void gatherStoppedDownload
  204. (BDE& entryDict, const SharedHandle<DownloadResult>& ds)
  205. {
  206. entryDict["gid"] = Util::itos(ds->gid);
  207. if(ds->result == DownloadResult::IN_PROGRESS) {
  208. entryDict["status"] = BDE("removed");
  209. } else if(ds->result == DownloadResult::FINISHED) {
  210. entryDict["status"] = BDE("complete");
  211. } else {
  212. entryDict["status"] = BDE("error");
  213. }
  214. }
  215. static
  216. SharedHandle<RequestGroup>
  217. findRequestGroup(const SharedHandle<RequestGroupMan>& rgman, int32_t gid)
  218. {
  219. SharedHandle<RequestGroup> group = rgman->findRequestGroup(gid);
  220. if(group.isNull()) {
  221. group = rgman->findReservedGroup(gid);
  222. }
  223. return group;
  224. }
  225. BDE GetFilesXmlRpcMethod::process
  226. (const XmlRpcRequest& req, DownloadEngine* e)
  227. {
  228. const BDE& params = req._params;
  229. assert(params.isList());
  230. if(params.empty() || !params[0].isString()) {
  231. throw DlAbortEx("GID is not provided.");
  232. }
  233. int32_t gid = Util::parseInt(params[0].s());
  234. SharedHandle<RequestGroup> group = findRequestGroup(e->_requestGroupMan, gid);
  235. if(group.isNull()) {
  236. throw DlAbortEx
  237. (StringFormat("No file data is available for GID#%d", gid).str());
  238. }
  239. BDE files = BDE::list();
  240. SharedHandle<BtContext> btctx =
  241. dynamic_pointer_cast<BtContext>(group->getDownloadContext());
  242. if(btctx.isNull()) {
  243. BDE entry = BDE::dict();
  244. entry["index"] = BDE("1");
  245. entry["path"] = group->getDownloadContext()->getActualBasePath();
  246. entry["selected"] = BDE("true");
  247. files << entry;
  248. } else {
  249. std::deque<int32_t> fileIndexes = btctx->getFileFilter().flush();
  250. std::sort(fileIndexes.begin(), fileIndexes.end());
  251. fileIndexes.erase(std::unique(fileIndexes.begin(), fileIndexes.end()),
  252. fileIndexes.end());
  253. std::deque<SharedHandle<FileEntry> > fileEntries =
  254. btctx->getFileEntries();
  255. bool selectAll = fileIndexes.empty() || fileEntries.size() == 1;
  256. int32_t index = 1;
  257. for(std::deque<SharedHandle<FileEntry> >::const_iterator i =
  258. fileEntries.begin(); i != fileEntries.end(); ++i, ++index) {
  259. BDE entry = BDE::dict();
  260. entry["index"] = Util::itos(index);
  261. entry["path"] = (*i)->getPath();
  262. if(selectAll ||
  263. std::binary_search(fileIndexes.begin(), fileIndexes.end(), index)) {
  264. entry["selected"] = BDE("true");
  265. } else {
  266. entry["selected"] = BDE("false");
  267. }
  268. files << entry;
  269. }
  270. }
  271. BDE resParams = BDE::list();
  272. resParams << files;
  273. return resParams;
  274. }
  275. BDE GetUrisXmlRpcMethod::process
  276. (const XmlRpcRequest& req, DownloadEngine* e)
  277. {
  278. const BDE& params = req._params;
  279. assert(params.isList());
  280. if(params.empty() || !params[0].isString()) {
  281. throw DlAbortEx("GID is not provided.");
  282. }
  283. int32_t gid = Util::parseInt(params[0].s());
  284. SharedHandle<RequestGroup> group = findRequestGroup(e->_requestGroupMan, gid);
  285. if(group.isNull()) {
  286. throw DlAbortEx
  287. (StringFormat("No URI data is available for GID#%d", gid).str());
  288. }
  289. BDE uriList = BDE::list();
  290. std::deque<std::string> uris;
  291. group->getURIs(uris);
  292. for(std::deque<std::string>::const_iterator i = uris.begin(); i != uris.end();
  293. ++i) {
  294. BDE entry = BDE::dict();
  295. entry["uri"] = *i;
  296. uriList << entry;
  297. }
  298. BDE resParams = BDE::list();
  299. resParams << uriList;
  300. return resParams;
  301. }
  302. BDE GetPeersXmlRpcMethod::process
  303. (const XmlRpcRequest& req, DownloadEngine* e)
  304. {
  305. const BDE& params = req._params;
  306. assert(params.isList());
  307. if(params.empty() || !params[0].isString()) {
  308. throw DlAbortEx("GID is not provided.");
  309. }
  310. int32_t gid = Util::parseInt(params[0].s());
  311. SharedHandle<RequestGroup> group = findRequestGroup(e->_requestGroupMan, gid);
  312. if(group.isNull()) {
  313. throw DlAbortEx
  314. (StringFormat("No peer data is available for GID#%d", gid).str());
  315. }
  316. BDE peers = BDE::list();
  317. SharedHandle<BtContext> btctx =
  318. dynamic_pointer_cast<BtContext>(group->getDownloadContext());
  319. if(!btctx.isNull()) {
  320. SharedHandle<BtRegistry> btreg = e->getBtRegistry();
  321. SharedHandle<PeerStorage> ps =
  322. btreg->getPeerStorage(btctx->getInfoHashAsString());
  323. if(!ps.isNull()) {
  324. BDE entry = BDE::dict();
  325. gatherPeer(peers, ps);
  326. }
  327. }
  328. BDE resParams = BDE::list();
  329. resParams << peers;
  330. return resParams;
  331. }
  332. BDE TellStatusXmlRpcMethod::process
  333. (const XmlRpcRequest& req, DownloadEngine* e)
  334. {
  335. const BDE& params = req._params;
  336. assert(params.isList());
  337. if(params.empty() || !params[0].isString()) {
  338. throw DlAbortEx("GID is not provided.");
  339. }
  340. int32_t gid = Util::parseInt(params[0].s());
  341. SharedHandle<RequestGroup> group = e->_requestGroupMan->findRequestGroup(gid);
  342. BDE entryDict = BDE::dict();
  343. if(group.isNull()) {
  344. group = e->_requestGroupMan->findReservedGroup(gid);
  345. if(group.isNull()) {
  346. SharedHandle<DownloadResult> ds =
  347. e->_requestGroupMan->findDownloadResult(gid);
  348. if(ds.isNull()) {
  349. throw DlAbortEx
  350. (StringFormat("No such download for GID#%d", gid).str());
  351. }
  352. gatherStoppedDownload(entryDict, ds);
  353. } else {
  354. entryDict["status"] = BDE("waiting");
  355. gatherProgress(entryDict, group, e);
  356. }
  357. } else {
  358. entryDict["status"] = BDE("active");
  359. gatherProgress(entryDict, group, e);
  360. }
  361. BDE resParams = BDE::list();
  362. resParams << entryDict;
  363. return resParams;
  364. }
  365. BDE TellActiveXmlRpcMethod::process
  366. (const XmlRpcRequest& req, DownloadEngine* e)
  367. {
  368. BDE list = BDE::list();
  369. const std::deque<SharedHandle<RequestGroup> >& groups =
  370. e->_requestGroupMan->getRequestGroups();
  371. for(std::deque<SharedHandle<RequestGroup> >::const_iterator i =
  372. groups.begin(); i != groups.end(); ++i) {
  373. BDE entryDict = BDE::dict();
  374. entryDict["status"] = BDE("active");
  375. gatherProgressCommon(entryDict, *i);
  376. SharedHandle<BtContext> btctx =
  377. dynamic_pointer_cast<BtContext>((*i)->getDownloadContext());
  378. if(!btctx.isNull()) {
  379. gatherProgressBitTorrent(entryDict, btctx);
  380. }
  381. list << entryDict;
  382. }
  383. BDE resParams = BDE::list();
  384. resParams << list;
  385. return resParams;
  386. }
  387. BDE NoSuchMethodXmlRpcMethod::process
  388. (const XmlRpcRequest& req, DownloadEngine* e)
  389. {
  390. throw DlAbortEx
  391. (StringFormat("No such method: %s", req._methodName.c_str()).str());
  392. }
  393. } // namespace xmlrpc
  394. } // namespace aria2