XmlRpcMethodImpl.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 "BDE.h"
  42. #include "XmlRpcRequest.h"
  43. namespace aria2 {
  44. class DownloadResult;
  45. class RequestGroup;
  46. namespace xmlrpc {
  47. class AddUriXmlRpcMethod:public XmlRpcMethod {
  48. protected:
  49. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  50. public:
  51. static const std::string& getMethodName()
  52. {
  53. static std::string methodName = "aria2.addUri";
  54. return methodName;
  55. }
  56. };
  57. class RemoveXmlRpcMethod:public XmlRpcMethod {
  58. protected:
  59. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  60. public:
  61. static const std::string& getMethodName()
  62. {
  63. static std::string methodName = "aria2.remove";
  64. return methodName;
  65. }
  66. };
  67. class ForceRemoveXmlRpcMethod:public XmlRpcMethod {
  68. protected:
  69. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  70. public:
  71. static const std::string& getMethodName()
  72. {
  73. static std::string methodName = "aria2.forceRemove";
  74. return methodName;
  75. }
  76. };
  77. class PauseXmlRpcMethod:public XmlRpcMethod {
  78. protected:
  79. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  80. public:
  81. static const std::string& getMethodName()
  82. {
  83. static std::string methodName = "aria2.pause";
  84. return methodName;
  85. }
  86. };
  87. class UnpauseXmlRpcMethod:public XmlRpcMethod {
  88. protected:
  89. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  90. public:
  91. static const std::string& getMethodName()
  92. {
  93. static std::string methodName = "aria2.unpause";
  94. return methodName;
  95. }
  96. };
  97. #ifdef ENABLE_BITTORRENT
  98. class AddTorrentXmlRpcMethod:public XmlRpcMethod {
  99. protected:
  100. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  101. public:
  102. static const std::string& getMethodName()
  103. {
  104. static std::string methodName = "aria2.addTorrent";
  105. return methodName;
  106. }
  107. };
  108. #endif // ENABLE_BITTORRENT
  109. #ifdef ENABLE_METALINK
  110. class AddMetalinkXmlRpcMethod:public XmlRpcMethod {
  111. protected:
  112. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  113. public:
  114. static const std::string& getMethodName()
  115. {
  116. static std::string methodName = "aria2.addMetalink";
  117. return methodName;
  118. }
  119. };
  120. #endif // ENABLE_METALINK
  121. class PurgeDownloadResultXmlRpcMethod:public XmlRpcMethod {
  122. protected:
  123. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  124. public:
  125. static const std::string& getMethodName()
  126. {
  127. static std::string methodName = "aria2.purgeDownloadResult";
  128. return methodName;
  129. }
  130. };
  131. class GetUrisXmlRpcMethod:public XmlRpcMethod {
  132. protected:
  133. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  134. public:
  135. static const std::string& getMethodName()
  136. {
  137. static std::string methodName = "aria2.getUris";
  138. return methodName;
  139. }
  140. };
  141. class GetFilesXmlRpcMethod:public XmlRpcMethod {
  142. protected:
  143. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  144. public:
  145. static const std::string& getMethodName()
  146. {
  147. static std::string methodName = "aria2.getFiles";
  148. return methodName;
  149. }
  150. };
  151. #ifdef ENABLE_BITTORRENT
  152. class GetPeersXmlRpcMethod:public XmlRpcMethod {
  153. protected:
  154. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  155. public:
  156. static const std::string& getMethodName()
  157. {
  158. static std::string methodName = "aria2.getPeers";
  159. return methodName;
  160. }
  161. };
  162. #endif // ENABLE_BITTORRENT
  163. class GetServersXmlRpcMethod:public XmlRpcMethod {
  164. protected:
  165. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  166. public:
  167. static const std::string& getMethodName()
  168. {
  169. static std::string methodName = "aria2.getServers";
  170. return methodName;
  171. }
  172. };
  173. class TellStatusXmlRpcMethod:public XmlRpcMethod {
  174. protected:
  175. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  176. public:
  177. static const std::string& getMethodName()
  178. {
  179. static std::string methodName = "aria2.tellStatus";
  180. return methodName;
  181. }
  182. };
  183. class TellActiveXmlRpcMethod:public XmlRpcMethod {
  184. protected:
  185. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  186. public:
  187. static const std::string& getMethodName()
  188. {
  189. static std::string methodName = "aria2.tellActive";
  190. return methodName;
  191. }
  192. };
  193. template<typename T>
  194. class AbstractPaginationXmlRpcMethod:public XmlRpcMethod {
  195. private:
  196. template<typename InputIterator>
  197. std::pair<InputIterator, InputIterator>
  198. getPaginationRange
  199. (ssize_t offset, size_t num, InputIterator first, InputIterator last)
  200. {
  201. size_t size = std::distance(first, last);
  202. if(offset < 0) {
  203. ssize_t tempoffset = offset+size;
  204. if(tempoffset < 0) {
  205. return std::make_pair(last, last);
  206. }
  207. offset = tempoffset-(num-1);
  208. if(offset < 0) {
  209. offset = 0;
  210. num = tempoffset+1;
  211. }
  212. } else if(size <= (size_t)offset) {
  213. return std::make_pair(last, last);
  214. }
  215. BDE list = BDE::list();
  216. size_t lastDistance;
  217. if(size < offset+num) {
  218. lastDistance = size;
  219. } else {
  220. lastDistance = offset+num;
  221. }
  222. last = first;
  223. std::advance(first, offset);
  224. std::advance(last, lastDistance);
  225. return std::make_pair(first, last);
  226. }
  227. void checkPaginationParams(const BDE& params) const
  228. {
  229. assert(params.isList());
  230. if(params.size() != 2 ||
  231. !params[0].isInteger() || !params[1].isInteger() ||
  232. params[1].i() < 0) {
  233. throw DL_ABORT_EX("Invalid argument. Specify offset and num in integer.");
  234. }
  235. }
  236. protected:
  237. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e)
  238. {
  239. const BDE& params = req._params;
  240. checkPaginationParams(params);
  241. ssize_t offset = params[0].i();
  242. size_t num = params[1].i();
  243. const std::deque<SharedHandle<T> >& items = getItems(e);
  244. std::pair<typename std::deque<SharedHandle<T> >::const_iterator,
  245. typename std::deque<SharedHandle<T> >::const_iterator> range =
  246. getPaginationRange(offset, num, items.begin(), items.end());
  247. BDE list = BDE::list();
  248. for(; range.first != range.second; ++range.first) {
  249. BDE entryDict = BDE::dict();
  250. createEntry(entryDict, *range.first, e);
  251. list << entryDict;
  252. }
  253. if(offset < 0) {
  254. std::reverse(list.listBegin(), list.listEnd());
  255. }
  256. return list;
  257. }
  258. virtual const std::deque<SharedHandle<T> >&
  259. getItems(DownloadEngine* e) const = 0;
  260. virtual void createEntry
  261. (BDE& entryDict, const SharedHandle<T>& item, DownloadEngine* e) const = 0;
  262. };
  263. class TellWaitingXmlRpcMethod:
  264. public AbstractPaginationXmlRpcMethod<RequestGroup> {
  265. protected:
  266. virtual const std::deque<SharedHandle<RequestGroup> >&
  267. getItems(DownloadEngine* e) const;
  268. virtual void createEntry
  269. (BDE& entryDict, const SharedHandle<RequestGroup>& item,
  270. DownloadEngine* e) const;
  271. public:
  272. static const std::string& getMethodName()
  273. {
  274. static std::string methodName = "aria2.tellWaiting";
  275. return methodName;
  276. }
  277. };
  278. class TellStoppedXmlRpcMethod:
  279. public AbstractPaginationXmlRpcMethod<DownloadResult> {
  280. protected:
  281. virtual const std::deque<SharedHandle<DownloadResult> >&
  282. getItems(DownloadEngine* e) const;
  283. virtual void createEntry
  284. (BDE& entryDict, const SharedHandle<DownloadResult>& item,
  285. DownloadEngine* e) const;
  286. public:
  287. static const std::string& getMethodName()
  288. {
  289. static std::string methodName = "aria2.tellStopped";
  290. return methodName;
  291. }
  292. };
  293. class ChangeOptionXmlRpcMethod:public XmlRpcMethod {
  294. protected:
  295. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  296. public:
  297. static const std::string& getMethodName()
  298. {
  299. static std::string methodName = "aria2.changeOption";
  300. return methodName;
  301. }
  302. };
  303. class ChangeGlobalOptionXmlRpcMethod:public XmlRpcMethod {
  304. protected:
  305. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  306. public:
  307. static const std::string& getMethodName()
  308. {
  309. static std::string methodName = "aria2.changeGlobalOption";
  310. return methodName;
  311. }
  312. };
  313. class GetVersionXmlRpcMethod:public XmlRpcMethod {
  314. protected:
  315. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  316. public:
  317. static const std::string& getMethodName()
  318. {
  319. static std::string methodName = "aria2.getVersion";
  320. return methodName;
  321. }
  322. };
  323. class GetOptionXmlRpcMethod:public XmlRpcMethod {
  324. protected:
  325. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  326. public:
  327. static const std::string& getMethodName()
  328. {
  329. static std::string methodName = "aria2.getOption";
  330. return methodName;
  331. }
  332. };
  333. class GetGlobalOptionXmlRpcMethod:public XmlRpcMethod {
  334. protected:
  335. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  336. public:
  337. static const std::string& getMethodName()
  338. {
  339. static std::string methodName = "aria2.getGlobalOption";
  340. return methodName;
  341. }
  342. };
  343. class ChangePositionXmlRpcMethod:public XmlRpcMethod {
  344. protected:
  345. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  346. public:
  347. static const std::string& getMethodName()
  348. {
  349. static std::string methodName = "aria2.changePosition";
  350. return methodName;
  351. }
  352. };
  353. class ChangeUriXmlRpcMethod:public XmlRpcMethod {
  354. protected:
  355. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  356. public:
  357. static const std::string& getMethodName()
  358. {
  359. static std::string methodName = "aria2.changeUri";
  360. return methodName;
  361. }
  362. };
  363. class GetSessionInfoXmlRpcMethod:public XmlRpcMethod {
  364. protected:
  365. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  366. public:
  367. static const std::string& getMethodName()
  368. {
  369. static std::string methodName = "aria2.getSessionInfo";
  370. return methodName;
  371. }
  372. };
  373. class ShutdownXmlRpcMethod:public XmlRpcMethod {
  374. protected:
  375. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  376. public:
  377. static const std::string& getMethodName()
  378. {
  379. static std::string methodName = "aria2.shutdown";
  380. return methodName;
  381. }
  382. };
  383. class ForceShutdownXmlRpcMethod:public XmlRpcMethod {
  384. protected:
  385. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  386. public:
  387. static const std::string& getMethodName()
  388. {
  389. static std::string methodName = "aria2.forceShutdown";
  390. return methodName;
  391. }
  392. };
  393. class SystemMulticallXmlRpcMethod:public XmlRpcMethod {
  394. protected:
  395. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  396. public:
  397. static const std::string& getMethodName()
  398. {
  399. static std::string methodName = "system.multicall";
  400. return methodName;
  401. }
  402. };
  403. class NoSuchMethodXmlRpcMethod:public XmlRpcMethod {
  404. protected:
  405. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  406. };
  407. // Helper function to store data to entryDict from ds. This function
  408. // is used by tellStatus method.
  409. void gatherStoppedDownload
  410. (BDE& entryDict, const SharedHandle<DownloadResult>& ds);
  411. // Helper function to store data to entryDict from group. This
  412. // function is used by tellStatus/tellActive/tellWaiting method
  413. void gatherProgressCommon
  414. (BDE& entryDict, const SharedHandle<RequestGroup>& group);
  415. #ifdef ENABLE_BITTORRENT
  416. // Helper function to store BitTorrent metadata from torrentAttrs in
  417. // btDict. btDict must be an BDE::Dict.
  418. void gatherBitTorrentMetadata(BDE& btDict, const BDE& torrentAttrs);
  419. #endif // ENABLE_BITTORRENT
  420. } // namespace xmlrpc
  421. } // namespace aria2
  422. #endif // _D_XML_RPC_METHOD_IMPL_H_