XmlRpcMethodImpl.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. #ifndef D_XML_RPC_METHOD_IMPL_H
  36. #define D_XML_RPC_METHOD_IMPL_H
  37. #include "XmlRpcMethod.h"
  38. #include <cassert>
  39. #include <deque>
  40. #include <algorithm>
  41. #include "XmlRpcRequest.h"
  42. #include "ValueBase.h"
  43. #include "TorrentAttribute.h"
  44. #include "DlAbortEx.h"
  45. namespace aria2 {
  46. struct DownloadResult;
  47. class RequestGroup;
  48. namespace xmlrpc {
  49. template<typename OutputIterator>
  50. void toStringList(OutputIterator out, const List* src)
  51. {
  52. if(!src) {
  53. return;
  54. }
  55. for(List::ValueType::const_iterator i = src->begin(), eoi = src->end();
  56. i != eoi; ++i) {
  57. const String* s = asString(*i);
  58. if(s) {
  59. *out++ = s->s();
  60. }
  61. }
  62. }
  63. class AddUriXmlRpcMethod:public XmlRpcMethod {
  64. protected:
  65. virtual SharedHandle<ValueBase> process
  66. (const XmlRpcRequest& req, DownloadEngine* e);
  67. public:
  68. static const std::string& getMethodName()
  69. {
  70. static std::string methodName = "aria2.addUri";
  71. return methodName;
  72. }
  73. };
  74. class RemoveXmlRpcMethod:public XmlRpcMethod {
  75. protected:
  76. virtual SharedHandle<ValueBase> process
  77. (const XmlRpcRequest& req, DownloadEngine* e);
  78. public:
  79. static const std::string& getMethodName()
  80. {
  81. static std::string methodName = "aria2.remove";
  82. return methodName;
  83. }
  84. };
  85. class ForceRemoveXmlRpcMethod:public XmlRpcMethod {
  86. protected:
  87. virtual SharedHandle<ValueBase> process
  88. (const XmlRpcRequest& req, DownloadEngine* e);
  89. public:
  90. static const std::string& getMethodName()
  91. {
  92. static std::string methodName = "aria2.forceRemove";
  93. return methodName;
  94. }
  95. };
  96. class PauseXmlRpcMethod:public XmlRpcMethod {
  97. protected:
  98. virtual SharedHandle<ValueBase> process
  99. (const XmlRpcRequest& req, DownloadEngine* e);
  100. public:
  101. static const std::string& getMethodName()
  102. {
  103. static std::string methodName = "aria2.pause";
  104. return methodName;
  105. }
  106. };
  107. class ForcePauseXmlRpcMethod:public XmlRpcMethod {
  108. protected:
  109. virtual SharedHandle<ValueBase> process
  110. (const XmlRpcRequest& req, DownloadEngine* e);
  111. public:
  112. static const std::string& getMethodName()
  113. {
  114. static std::string methodName = "aria2.forcePause";
  115. return methodName;
  116. }
  117. };
  118. class PauseAllXmlRpcMethod:public XmlRpcMethod {
  119. protected:
  120. virtual SharedHandle<ValueBase> process
  121. (const XmlRpcRequest& req, DownloadEngine* e);
  122. public:
  123. static const std::string& getMethodName()
  124. {
  125. static std::string methodName = "aria2.pauseAll";
  126. return methodName;
  127. }
  128. };
  129. class ForcePauseAllXmlRpcMethod:public XmlRpcMethod {
  130. protected:
  131. virtual SharedHandle<ValueBase> process
  132. (const XmlRpcRequest& req, DownloadEngine* e);
  133. public:
  134. static const std::string& getMethodName()
  135. {
  136. static std::string methodName = "aria2.forcePauseAll";
  137. return methodName;
  138. }
  139. };
  140. class UnpauseXmlRpcMethod:public XmlRpcMethod {
  141. protected:
  142. virtual SharedHandle<ValueBase> process
  143. (const XmlRpcRequest& req, DownloadEngine* e);
  144. public:
  145. static const std::string& getMethodName()
  146. {
  147. static std::string methodName = "aria2.unpause";
  148. return methodName;
  149. }
  150. };
  151. class UnpauseAllXmlRpcMethod:public XmlRpcMethod {
  152. protected:
  153. virtual SharedHandle<ValueBase> process
  154. (const XmlRpcRequest& req, DownloadEngine* e);
  155. public:
  156. static const std::string& getMethodName()
  157. {
  158. static std::string methodName = "aria2.unpauseAll";
  159. return methodName;
  160. }
  161. };
  162. #ifdef ENABLE_BITTORRENT
  163. class AddTorrentXmlRpcMethod:public XmlRpcMethod {
  164. protected:
  165. virtual SharedHandle<ValueBase> process
  166. (const XmlRpcRequest& req, DownloadEngine* e);
  167. public:
  168. static const std::string& getMethodName()
  169. {
  170. static std::string methodName = "aria2.addTorrent";
  171. return methodName;
  172. }
  173. };
  174. #endif // ENABLE_BITTORRENT
  175. #ifdef ENABLE_METALINK
  176. class AddMetalinkXmlRpcMethod:public XmlRpcMethod {
  177. protected:
  178. virtual SharedHandle<ValueBase> process
  179. (const XmlRpcRequest& req, DownloadEngine* e);
  180. public:
  181. static const std::string& getMethodName()
  182. {
  183. static std::string methodName = "aria2.addMetalink";
  184. return methodName;
  185. }
  186. };
  187. #endif // ENABLE_METALINK
  188. class PurgeDownloadResultXmlRpcMethod:public XmlRpcMethod {
  189. protected:
  190. virtual SharedHandle<ValueBase> process
  191. (const XmlRpcRequest& req, DownloadEngine* e);
  192. public:
  193. static const std::string& getMethodName()
  194. {
  195. static std::string methodName = "aria2.purgeDownloadResult";
  196. return methodName;
  197. }
  198. };
  199. class GetUrisXmlRpcMethod:public XmlRpcMethod {
  200. protected:
  201. virtual SharedHandle<ValueBase> process
  202. (const XmlRpcRequest& req, DownloadEngine* e);
  203. public:
  204. static const std::string& getMethodName()
  205. {
  206. static std::string methodName = "aria2.getUris";
  207. return methodName;
  208. }
  209. };
  210. class GetFilesXmlRpcMethod:public XmlRpcMethod {
  211. protected:
  212. virtual SharedHandle<ValueBase> process
  213. (const XmlRpcRequest& req, DownloadEngine* e);
  214. public:
  215. static const std::string& getMethodName()
  216. {
  217. static std::string methodName = "aria2.getFiles";
  218. return methodName;
  219. }
  220. };
  221. #ifdef ENABLE_BITTORRENT
  222. class GetPeersXmlRpcMethod:public XmlRpcMethod {
  223. protected:
  224. virtual SharedHandle<ValueBase> process
  225. (const XmlRpcRequest& req, DownloadEngine* e);
  226. public:
  227. static const std::string& getMethodName()
  228. {
  229. static std::string methodName = "aria2.getPeers";
  230. return methodName;
  231. }
  232. };
  233. #endif // ENABLE_BITTORRENT
  234. class GetServersXmlRpcMethod:public XmlRpcMethod {
  235. protected:
  236. virtual SharedHandle<ValueBase> process
  237. (const XmlRpcRequest& req, DownloadEngine* e);
  238. public:
  239. static const std::string& getMethodName()
  240. {
  241. static std::string methodName = "aria2.getServers";
  242. return methodName;
  243. }
  244. };
  245. class TellStatusXmlRpcMethod:public XmlRpcMethod {
  246. protected:
  247. virtual SharedHandle<ValueBase> process
  248. (const XmlRpcRequest& req, DownloadEngine* e);
  249. public:
  250. static const std::string& getMethodName()
  251. {
  252. static std::string methodName = "aria2.tellStatus";
  253. return methodName;
  254. }
  255. };
  256. class TellActiveXmlRpcMethod:public XmlRpcMethod {
  257. protected:
  258. virtual SharedHandle<ValueBase> process
  259. (const XmlRpcRequest& req, DownloadEngine* e);
  260. public:
  261. static const std::string& getMethodName()
  262. {
  263. static std::string methodName = "aria2.tellActive";
  264. return methodName;
  265. }
  266. };
  267. template<typename T>
  268. class AbstractPaginationXmlRpcMethod:public XmlRpcMethod {
  269. private:
  270. template<typename InputIterator>
  271. std::pair<InputIterator, InputIterator>
  272. getPaginationRange
  273. (ssize_t offset, size_t num, InputIterator first, InputIterator last)
  274. {
  275. size_t size = std::distance(first, last);
  276. if(offset < 0) {
  277. ssize_t tempoffset = offset+size;
  278. if(tempoffset < 0) {
  279. return std::make_pair(last, last);
  280. }
  281. offset = tempoffset-(num-1);
  282. if(offset < 0) {
  283. offset = 0;
  284. num = tempoffset+1;
  285. }
  286. } else if(size <= (size_t)offset) {
  287. return std::make_pair(last, last);
  288. }
  289. size_t lastDistance;
  290. if(size < offset+num) {
  291. lastDistance = size;
  292. } else {
  293. lastDistance = offset+num;
  294. }
  295. last = first;
  296. std::advance(first, offset);
  297. std::advance(last, lastDistance);
  298. return std::make_pair(first, last);
  299. }
  300. void checkPaginationParams(const SharedHandle<List>& params) const
  301. {
  302. if(params->size() < 2) {
  303. throw DL_ABORT_EX("Invalid argument. Specify offset and num in integer.");
  304. }
  305. const Integer* p1 = asInteger(params->get(0));
  306. const Integer* p2 = asInteger(params->get(1));
  307. if(!p1 || !p2 || p2->i() < 0) {
  308. throw DL_ABORT_EX("Invalid argument. Specify offset and num in integer.");
  309. }
  310. }
  311. protected:
  312. virtual SharedHandle<ValueBase> process
  313. (const XmlRpcRequest& req, DownloadEngine* e)
  314. {
  315. const SharedHandle<List>& params = req.params;
  316. checkPaginationParams(params);
  317. ssize_t offset = req.getIntegerParam(0)->i();
  318. size_t num = req.getIntegerParam(1)->i();
  319. const List* keysParam = req.getListParam(2);
  320. std::vector<std::string> keys;
  321. toStringList(std::back_inserter(keys), keysParam);
  322. const std::deque<SharedHandle<T> >& items = getItems(e);
  323. std::pair<typename std::deque<SharedHandle<T> >::const_iterator,
  324. typename std::deque<SharedHandle<T> >::const_iterator> range =
  325. getPaginationRange(offset, num, items.begin(), items.end());
  326. SharedHandle<List> list = List::g();
  327. for(; range.first != range.second; ++range.first) {
  328. SharedHandle<Dict> entryDict = Dict::g();
  329. createEntry(entryDict, *range.first, e, keys);
  330. list->append(entryDict);
  331. }
  332. if(offset < 0) {
  333. std::reverse(list->begin(), list->end());
  334. }
  335. return list;
  336. }
  337. virtual const std::deque<SharedHandle<T> >&
  338. getItems(DownloadEngine* e) const = 0;
  339. virtual void createEntry
  340. (const SharedHandle<Dict>& entryDict,
  341. const SharedHandle<T>& item,
  342. DownloadEngine* e,
  343. const std::vector<std::string>& keys) const = 0;
  344. };
  345. class TellWaitingXmlRpcMethod:
  346. public AbstractPaginationXmlRpcMethod<RequestGroup> {
  347. protected:
  348. virtual const std::deque<SharedHandle<RequestGroup> >&
  349. getItems(DownloadEngine* e) const;
  350. virtual void createEntry
  351. (const SharedHandle<Dict>& entryDict,
  352. const SharedHandle<RequestGroup>& item,
  353. DownloadEngine* e,
  354. const std::vector<std::string>& keys) const;
  355. public:
  356. static const std::string& getMethodName()
  357. {
  358. static std::string methodName = "aria2.tellWaiting";
  359. return methodName;
  360. }
  361. };
  362. class TellStoppedXmlRpcMethod:
  363. public AbstractPaginationXmlRpcMethod<DownloadResult> {
  364. protected:
  365. virtual const std::deque<SharedHandle<DownloadResult> >&
  366. getItems(DownloadEngine* e) const;
  367. virtual void createEntry
  368. (const SharedHandle<Dict>& entryDict,
  369. const SharedHandle<DownloadResult>& item,
  370. DownloadEngine* e,
  371. const std::vector<std::string>& keys) const;
  372. public:
  373. static const std::string& getMethodName()
  374. {
  375. static std::string methodName = "aria2.tellStopped";
  376. return methodName;
  377. }
  378. };
  379. class ChangeOptionXmlRpcMethod:public XmlRpcMethod {
  380. protected:
  381. virtual SharedHandle<ValueBase> process
  382. (const XmlRpcRequest& req, DownloadEngine* e);
  383. public:
  384. static const std::string& getMethodName()
  385. {
  386. static std::string methodName = "aria2.changeOption";
  387. return methodName;
  388. }
  389. };
  390. class ChangeGlobalOptionXmlRpcMethod:public XmlRpcMethod {
  391. protected:
  392. virtual SharedHandle<ValueBase> process
  393. (const XmlRpcRequest& req, DownloadEngine* e);
  394. public:
  395. static const std::string& getMethodName()
  396. {
  397. static std::string methodName = "aria2.changeGlobalOption";
  398. return methodName;
  399. }
  400. };
  401. class GetVersionXmlRpcMethod:public XmlRpcMethod {
  402. protected:
  403. virtual SharedHandle<ValueBase> process
  404. (const XmlRpcRequest& req, DownloadEngine* e);
  405. public:
  406. static const std::string& getMethodName()
  407. {
  408. static std::string methodName = "aria2.getVersion";
  409. return methodName;
  410. }
  411. };
  412. class GetOptionXmlRpcMethod:public XmlRpcMethod {
  413. protected:
  414. virtual SharedHandle<ValueBase> process
  415. (const XmlRpcRequest& req, DownloadEngine* e);
  416. public:
  417. static const std::string& getMethodName()
  418. {
  419. static std::string methodName = "aria2.getOption";
  420. return methodName;
  421. }
  422. };
  423. class GetGlobalOptionXmlRpcMethod:public XmlRpcMethod {
  424. protected:
  425. virtual SharedHandle<ValueBase> process
  426. (const XmlRpcRequest& req, DownloadEngine* e);
  427. public:
  428. static const std::string& getMethodName()
  429. {
  430. static std::string methodName = "aria2.getGlobalOption";
  431. return methodName;
  432. }
  433. };
  434. class ChangePositionXmlRpcMethod:public XmlRpcMethod {
  435. protected:
  436. virtual SharedHandle<ValueBase> process
  437. (const XmlRpcRequest& req, DownloadEngine* e);
  438. public:
  439. static const std::string& getMethodName()
  440. {
  441. static std::string methodName = "aria2.changePosition";
  442. return methodName;
  443. }
  444. };
  445. class ChangeUriXmlRpcMethod:public XmlRpcMethod {
  446. protected:
  447. virtual SharedHandle<ValueBase> process
  448. (const XmlRpcRequest& req, DownloadEngine* e);
  449. public:
  450. static const std::string& getMethodName()
  451. {
  452. static std::string methodName = "aria2.changeUri";
  453. return methodName;
  454. }
  455. };
  456. class GetSessionInfoXmlRpcMethod:public XmlRpcMethod {
  457. protected:
  458. virtual SharedHandle<ValueBase> process
  459. (const XmlRpcRequest& req, DownloadEngine* e);
  460. public:
  461. static const std::string& getMethodName()
  462. {
  463. static std::string methodName = "aria2.getSessionInfo";
  464. return methodName;
  465. }
  466. };
  467. class ShutdownXmlRpcMethod:public XmlRpcMethod {
  468. protected:
  469. virtual SharedHandle<ValueBase> process
  470. (const XmlRpcRequest& req, DownloadEngine* e);
  471. public:
  472. static const std::string& getMethodName()
  473. {
  474. static std::string methodName = "aria2.shutdown";
  475. return methodName;
  476. }
  477. };
  478. class ForceShutdownXmlRpcMethod:public XmlRpcMethod {
  479. protected:
  480. virtual SharedHandle<ValueBase> process
  481. (const XmlRpcRequest& req, DownloadEngine* e);
  482. public:
  483. static const std::string& getMethodName()
  484. {
  485. static std::string methodName = "aria2.forceShutdown";
  486. return methodName;
  487. }
  488. };
  489. class SystemMulticallXmlRpcMethod:public XmlRpcMethod {
  490. protected:
  491. virtual SharedHandle<ValueBase> process
  492. (const XmlRpcRequest& req, DownloadEngine* e);
  493. public:
  494. static const std::string& getMethodName()
  495. {
  496. static std::string methodName = "system.multicall";
  497. return methodName;
  498. }
  499. };
  500. class NoSuchMethodXmlRpcMethod:public XmlRpcMethod {
  501. protected:
  502. virtual SharedHandle<ValueBase> process
  503. (const XmlRpcRequest& req, DownloadEngine* e);
  504. };
  505. // Helper function to store data to entryDict from ds. This function
  506. // is used by tellStatus method.
  507. void gatherStoppedDownload
  508. (const SharedHandle<Dict>& entryDict, const SharedHandle<DownloadResult>& ds,
  509. const std::vector<std::string>& keys);
  510. // Helper function to store data to entryDict from group. This
  511. // function is used by tellStatus/tellActive/tellWaiting method
  512. void gatherProgressCommon
  513. (const SharedHandle<Dict>& entryDict, const SharedHandle<RequestGroup>& group,
  514. const std::vector<std::string>& keys);
  515. #ifdef ENABLE_BITTORRENT
  516. // Helper function to store BitTorrent metadata from torrentAttrs.
  517. void gatherBitTorrentMetadata
  518. (const SharedHandle<Dict>& btDict,
  519. const SharedHandle<TorrentAttribute>& torrentAttrs);
  520. #endif // ENABLE_BITTORRENT
  521. } // namespace xmlrpc
  522. } // namespace aria2
  523. #endif // D_XML_RPC_METHOD_IMPL_H