RpcMethodImpl.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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_RPC_METHOD_IMPL_H
  36. #define D_RPC_METHOD_IMPL_H
  37. #include "RpcMethod.h"
  38. #include <cassert>
  39. #include <deque>
  40. #include <algorithm>
  41. #include "RpcRequest.h"
  42. #include "ValueBase.h"
  43. #include "TorrentAttribute.h"
  44. #include "DlAbortEx.h"
  45. #include "fmt.h"
  46. #include "IndexedList.h"
  47. #include "GroupId.h"
  48. #include "RequestGroupMan.h"
  49. namespace aria2 {
  50. struct DownloadResult;
  51. class RequestGroup;
  52. namespace rpc {
  53. template <typename T>
  54. const T* checkParam(const RpcRequest& req, size_t index, bool required = false)
  55. {
  56. const T* p = 0;
  57. if (req.params->size() > index) {
  58. if ((p = downcast<T>(req.params->get(index))) == 0) {
  59. throw DL_ABORT_EX(fmt("The parameter at %lu has wrong type.",
  60. static_cast<unsigned long>(index)));
  61. }
  62. }
  63. else if (required) {
  64. throw DL_ABORT_EX(fmt("The parameter at %lu is required but missing.",
  65. static_cast<unsigned long>(index)));
  66. }
  67. return p;
  68. }
  69. template <typename T>
  70. const T* checkRequiredParam(const RpcRequest& req, size_t index)
  71. {
  72. return checkParam<T>(req, index, true);
  73. }
  74. struct IntegerGE {
  75. IntegerGE(int32_t min) : min(min) {}
  76. bool operator()(const Integer* param, std::string* error) const
  77. {
  78. if (min <= param->i()) {
  79. return true;
  80. }
  81. else {
  82. if (error) {
  83. *error = fmt("the value must be greater than or equal to %d.", min);
  84. }
  85. return false;
  86. }
  87. }
  88. int32_t min;
  89. };
  90. template <typename Validator>
  91. const Integer* checkRequiredInteger(const RpcRequest& req, size_t index,
  92. Validator validator)
  93. {
  94. const Integer* param = checkRequiredParam<Integer>(req, index);
  95. std::string error;
  96. if (!validator(param, &error)) {
  97. throw DL_ABORT_EX(fmt("The integer parameter at %lu has invalid value: %s",
  98. static_cast<unsigned long>(index), error.c_str()));
  99. }
  100. return param;
  101. }
  102. template <typename OutputIterator>
  103. void toStringList(OutputIterator out, const List* src)
  104. {
  105. if (!src) {
  106. return;
  107. }
  108. for (auto& elem : *src) {
  109. const String* s = downcast<String>(elem);
  110. if (s) {
  111. *out++ = s->s();
  112. }
  113. }
  114. }
  115. class AddUriRpcMethod : public RpcMethod {
  116. protected:
  117. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  118. DownloadEngine* e) CXX11_OVERRIDE;
  119. public:
  120. static const char* getMethodName() { return "aria2.addUri"; }
  121. };
  122. class RemoveRpcMethod : public RpcMethod {
  123. protected:
  124. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  125. DownloadEngine* e) CXX11_OVERRIDE;
  126. public:
  127. static const char* getMethodName() { return "aria2.remove"; }
  128. };
  129. class ForceRemoveRpcMethod : public RpcMethod {
  130. protected:
  131. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  132. DownloadEngine* e) CXX11_OVERRIDE;
  133. public:
  134. static const char* getMethodName() { return "aria2.forceRemove"; }
  135. };
  136. class PauseRpcMethod : public RpcMethod {
  137. protected:
  138. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  139. DownloadEngine* e) CXX11_OVERRIDE;
  140. public:
  141. static const char* getMethodName() { return "aria2.pause"; }
  142. };
  143. class ForcePauseRpcMethod : public RpcMethod {
  144. protected:
  145. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  146. DownloadEngine* e) CXX11_OVERRIDE;
  147. public:
  148. static const char* getMethodName() { return "aria2.forcePause"; }
  149. };
  150. class PauseAllRpcMethod : public RpcMethod {
  151. protected:
  152. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  153. DownloadEngine* e) CXX11_OVERRIDE;
  154. public:
  155. static const char* getMethodName() { return "aria2.pauseAll"; }
  156. };
  157. class ForcePauseAllRpcMethod : public RpcMethod {
  158. protected:
  159. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  160. DownloadEngine* e) CXX11_OVERRIDE;
  161. public:
  162. static const char* getMethodName() { return "aria2.forcePauseAll"; }
  163. };
  164. class UnpauseRpcMethod : public RpcMethod {
  165. protected:
  166. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  167. DownloadEngine* e) CXX11_OVERRIDE;
  168. public:
  169. static const char* getMethodName() { return "aria2.unpause"; }
  170. };
  171. class UnpauseAllRpcMethod : public RpcMethod {
  172. protected:
  173. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  174. DownloadEngine* e) CXX11_OVERRIDE;
  175. public:
  176. static const char* getMethodName() { return "aria2.unpauseAll"; }
  177. };
  178. #ifdef ENABLE_BITTORRENT
  179. class AddTorrentRpcMethod : public RpcMethod {
  180. protected:
  181. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  182. DownloadEngine* e) CXX11_OVERRIDE;
  183. public:
  184. static const char* getMethodName() { return "aria2.addTorrent"; }
  185. };
  186. #endif // ENABLE_BITTORRENT
  187. #ifdef ENABLE_METALINK
  188. class AddMetalinkRpcMethod : public RpcMethod {
  189. protected:
  190. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  191. DownloadEngine* e) CXX11_OVERRIDE;
  192. public:
  193. static const char* getMethodName() { return "aria2.addMetalink"; }
  194. };
  195. #endif // ENABLE_METALINK
  196. class PurgeDownloadResultRpcMethod : public RpcMethod {
  197. protected:
  198. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  199. DownloadEngine* e) CXX11_OVERRIDE;
  200. public:
  201. static const char* getMethodName() { return "aria2.purgeDownloadResult"; }
  202. };
  203. class RemoveDownloadResultRpcMethod : public RpcMethod {
  204. protected:
  205. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  206. DownloadEngine* e) CXX11_OVERRIDE;
  207. public:
  208. static const char* getMethodName() { return "aria2.removeDownloadResult"; }
  209. };
  210. class GetUrisRpcMethod : public RpcMethod {
  211. protected:
  212. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  213. DownloadEngine* e) CXX11_OVERRIDE;
  214. public:
  215. static const char* getMethodName() { return "aria2.getUris"; }
  216. };
  217. class GetFilesRpcMethod : public RpcMethod {
  218. protected:
  219. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  220. DownloadEngine* e) CXX11_OVERRIDE;
  221. public:
  222. static const char* getMethodName() { return "aria2.getFiles"; }
  223. };
  224. #ifdef ENABLE_BITTORRENT
  225. class GetPeersRpcMethod : public RpcMethod {
  226. protected:
  227. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  228. DownloadEngine* e) CXX11_OVERRIDE;
  229. public:
  230. static const char* getMethodName() { return "aria2.getPeers"; }
  231. };
  232. #endif // ENABLE_BITTORRENT
  233. class GetServersRpcMethod : public RpcMethod {
  234. protected:
  235. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  236. DownloadEngine* e) CXX11_OVERRIDE;
  237. public:
  238. static const char* getMethodName() { return "aria2.getServers"; }
  239. };
  240. class TellStatusRpcMethod : public RpcMethod {
  241. protected:
  242. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  243. DownloadEngine* e) CXX11_OVERRIDE;
  244. public:
  245. static const char* getMethodName() { return "aria2.tellStatus"; }
  246. };
  247. class TellActiveRpcMethod : public RpcMethod {
  248. protected:
  249. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  250. DownloadEngine* e) CXX11_OVERRIDE;
  251. public:
  252. static const char* getMethodName() { return "aria2.tellActive"; }
  253. };
  254. template <typename T> class AbstractPaginationRpcMethod : public RpcMethod {
  255. private:
  256. template <typename InputIterator>
  257. std::pair<InputIterator, InputIterator>
  258. getPaginationRange(int64_t offset, int64_t num, InputIterator first,
  259. InputIterator last)
  260. {
  261. if (num <= 0) {
  262. return std::make_pair(last, last);
  263. }
  264. int64_t size = std::distance(first, last);
  265. if (offset < 0) {
  266. int64_t tempoffset = offset + size;
  267. if (tempoffset < 0) {
  268. return std::make_pair(last, last);
  269. }
  270. offset = tempoffset - (num - 1);
  271. if (offset < 0) {
  272. offset = 0;
  273. num = tempoffset + 1;
  274. }
  275. }
  276. else if (size <= offset) {
  277. return std::make_pair(last, last);
  278. }
  279. int64_t lastDistance;
  280. if (size < offset + num) {
  281. lastDistance = size;
  282. }
  283. else {
  284. lastDistance = offset + num;
  285. }
  286. last = first;
  287. std::advance(first, offset);
  288. std::advance(last, lastDistance);
  289. return std::make_pair(first, last);
  290. }
  291. protected:
  292. typedef IndexedList<a2_gid_t, std::shared_ptr<T>> ItemListType;
  293. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  294. DownloadEngine* e) CXX11_OVERRIDE
  295. {
  296. const Integer* offsetParam = checkRequiredParam<Integer>(req, 0);
  297. const Integer* numParam = checkRequiredInteger(req, 1, IntegerGE(0));
  298. const List* keysParam = checkParam<List>(req, 2);
  299. int64_t offset = offsetParam->i();
  300. int64_t num = numParam->i();
  301. std::vector<std::string> keys;
  302. toStringList(std::back_inserter(keys), keysParam);
  303. const ItemListType& items = getItems(e);
  304. auto range =
  305. getPaginationRange(offset, num, std::begin(items), std::end(items));
  306. auto list = List::g();
  307. for (; range.first != range.second; ++range.first) {
  308. auto entryDict = Dict::g();
  309. createEntry(entryDict.get(), *range.first, e, keys);
  310. list->append(std::move(entryDict));
  311. }
  312. if (offset < 0) {
  313. std::reverse(list->begin(), list->end());
  314. }
  315. return std::move(list);
  316. }
  317. virtual const ItemListType& getItems(DownloadEngine* e) const = 0;
  318. virtual void createEntry(Dict* entryDict, const std::shared_ptr<T>& item,
  319. DownloadEngine* e,
  320. const std::vector<std::string>& keys) const = 0;
  321. };
  322. class TellWaitingRpcMethod : public AbstractPaginationRpcMethod<RequestGroup> {
  323. protected:
  324. virtual const RequestGroupList&
  325. getItems(DownloadEngine* e) const CXX11_OVERRIDE;
  326. virtual void
  327. createEntry(Dict* entryDict, const std::shared_ptr<RequestGroup>& item,
  328. DownloadEngine* e,
  329. const std::vector<std::string>& keys) const CXX11_OVERRIDE;
  330. public:
  331. static const char* getMethodName() { return "aria2.tellWaiting"; }
  332. };
  333. class TellStoppedRpcMethod
  334. : public AbstractPaginationRpcMethod<DownloadResult> {
  335. protected:
  336. virtual const DownloadResultList&
  337. getItems(DownloadEngine* e) const CXX11_OVERRIDE;
  338. virtual void
  339. createEntry(Dict* entryDict, const std::shared_ptr<DownloadResult>& item,
  340. DownloadEngine* e,
  341. const std::vector<std::string>& keys) const CXX11_OVERRIDE;
  342. public:
  343. static const char* getMethodName() { return "aria2.tellStopped"; }
  344. };
  345. class ChangeOptionRpcMethod : public RpcMethod {
  346. protected:
  347. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  348. DownloadEngine* e) CXX11_OVERRIDE;
  349. public:
  350. static const char* getMethodName() { return "aria2.changeOption"; }
  351. };
  352. class ChangeGlobalOptionRpcMethod : public RpcMethod {
  353. protected:
  354. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  355. DownloadEngine* e) CXX11_OVERRIDE;
  356. public:
  357. static const char* getMethodName() { return "aria2.changeGlobalOption"; }
  358. };
  359. class GetVersionRpcMethod : public RpcMethod {
  360. protected:
  361. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  362. DownloadEngine* e) CXX11_OVERRIDE;
  363. public:
  364. static const char* getMethodName() { return "aria2.getVersion"; }
  365. };
  366. class GetOptionRpcMethod : public RpcMethod {
  367. protected:
  368. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  369. DownloadEngine* e) CXX11_OVERRIDE;
  370. public:
  371. static const char* getMethodName() { return "aria2.getOption"; }
  372. };
  373. class GetGlobalOptionRpcMethod : public RpcMethod {
  374. protected:
  375. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  376. DownloadEngine* e) CXX11_OVERRIDE;
  377. public:
  378. static const char* getMethodName() { return "aria2.getGlobalOption"; }
  379. };
  380. class ChangePositionRpcMethod : public RpcMethod {
  381. protected:
  382. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  383. DownloadEngine* e) CXX11_OVERRIDE;
  384. public:
  385. static const char* getMethodName() { return "aria2.changePosition"; }
  386. };
  387. class ChangeUriRpcMethod : public RpcMethod {
  388. protected:
  389. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  390. DownloadEngine* e) CXX11_OVERRIDE;
  391. public:
  392. static const char* getMethodName() { return "aria2.changeUri"; }
  393. };
  394. class GetSessionInfoRpcMethod : public RpcMethod {
  395. protected:
  396. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  397. DownloadEngine* e) CXX11_OVERRIDE;
  398. public:
  399. static const char* getMethodName() { return "aria2.getSessionInfo"; }
  400. };
  401. class ShutdownRpcMethod : public RpcMethod {
  402. protected:
  403. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  404. DownloadEngine* e) CXX11_OVERRIDE;
  405. public:
  406. static const char* getMethodName() { return "aria2.shutdown"; }
  407. };
  408. class GetGlobalStatRpcMethod : public RpcMethod {
  409. protected:
  410. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  411. DownloadEngine* e) CXX11_OVERRIDE;
  412. public:
  413. static const char* getMethodName() { return "aria2.getGlobalStat"; }
  414. };
  415. class ForceShutdownRpcMethod : public RpcMethod {
  416. protected:
  417. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  418. DownloadEngine* e) CXX11_OVERRIDE;
  419. public:
  420. static const char* getMethodName() { return "aria2.forceShutdown"; }
  421. };
  422. class SaveSessionRpcMethod : public RpcMethod {
  423. protected:
  424. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  425. DownloadEngine* e) CXX11_OVERRIDE;
  426. public:
  427. static const char* getMethodName() { return "aria2.saveSession"; }
  428. };
  429. class SystemMulticallRpcMethod : public RpcMethod {
  430. protected:
  431. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  432. DownloadEngine* e) CXX11_OVERRIDE;
  433. public:
  434. virtual RpcResponse execute(RpcRequest req, DownloadEngine* e) CXX11_OVERRIDE;
  435. static const char* getMethodName() { return "system.multicall"; }
  436. };
  437. class SystemListMethodsRpcMethod : public RpcMethod {
  438. protected:
  439. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  440. DownloadEngine* e) CXX11_OVERRIDE;
  441. public:
  442. virtual RpcResponse execute(RpcRequest req, DownloadEngine* e) CXX11_OVERRIDE;
  443. static const char* getMethodName() { return "system.listMethods"; }
  444. };
  445. class NoSuchMethodRpcMethod : public RpcMethod {
  446. protected:
  447. virtual std::unique_ptr<ValueBase> process(const RpcRequest& req,
  448. DownloadEngine* e) CXX11_OVERRIDE;
  449. };
  450. // Helper function to store data to entryDict from ds. This function
  451. // is used by tellStatus method.
  452. void gatherStoppedDownload(Dict* entryDict,
  453. const std::shared_ptr<DownloadResult>& ds,
  454. const std::vector<std::string>& keys);
  455. // Helper function to store data to entryDict from group. This
  456. // function is used by tellStatus/tellActive/tellWaiting method
  457. void gatherProgressCommon(Dict* entryDict,
  458. const std::shared_ptr<RequestGroup>& group,
  459. const std::vector<std::string>& keys);
  460. #ifdef ENABLE_BITTORRENT
  461. // Helper function to store BitTorrent metadata from torrentAttrs.
  462. void gatherBitTorrentMetadata(Dict* btDict, TorrentAttribute* torrentAttrs);
  463. #endif // ENABLE_BITTORRENT
  464. } // namespace rpc
  465. bool pauseRequestGroup(const std::shared_ptr<RequestGroup>& group,
  466. bool reserved, bool forcePause);
  467. void changeOption(const std::shared_ptr<RequestGroup>& group,
  468. const Option& option, DownloadEngine* e);
  469. void changeGlobalOption(const Option& option, DownloadEngine* e);
  470. } // namespace aria2
  471. #endif // D_RPC_METHOD_IMPL_H