RpcMethod.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "RpcMethod.h"
  36. #include "DownloadEngine.h"
  37. #include "LogFactory.h"
  38. #include "RecoverableException.h"
  39. #include "message.h"
  40. #include "OptionParser.h"
  41. #include "OptionHandler.h"
  42. #include "Option.h"
  43. #include "array_fun.h"
  44. #include "download_helper.h"
  45. #include "RpcRequest.h"
  46. #include "RpcResponse.h"
  47. #include "prefs.h"
  48. #include "fmt.h"
  49. #include "DlAbortEx.h"
  50. #include "a2functional.h"
  51. #include "util.h"
  52. namespace aria2 {
  53. namespace rpc {
  54. RpcMethod::RpcMethod() : optionParser_(OptionParser::getInstance()) {}
  55. RpcMethod::~RpcMethod() {}
  56. std::unique_ptr<ValueBase> RpcMethod::createErrorResponse(const Exception& e,
  57. const RpcRequest& req)
  58. {
  59. auto params = Dict::g();
  60. params->put((req.jsonRpc ? "code" : "faultCode"), Integer::g(1));
  61. params->put((req.jsonRpc ? "message" : "faultString"), std::string(e.what()));
  62. return std::move(params);
  63. }
  64. void RpcMethod::authorize(RpcRequest& req, DownloadEngine* e)
  65. {
  66. std::string token;
  67. // We always treat first parameter as token if it is string and
  68. // starts with "token:" and remove it from parameter list, so that
  69. // we don't have to add conditionals to all RPCMethod
  70. // implementations.
  71. if (req.params && !req.params->empty()) {
  72. auto t = downcast<String>(req.params->get(0));
  73. if (t) {
  74. if (util::startsWith(t->s(), "token:")) {
  75. token = t->s().substr(6);
  76. req.params->pop_front();
  77. }
  78. }
  79. }
  80. if (!e || !e->validateToken(token)) {
  81. throw DL_ABORT_EX("Unauthorized");
  82. }
  83. }
  84. RpcResponse RpcMethod::execute(RpcRequest req, DownloadEngine* e)
  85. {
  86. auto authorized = RpcResponse::NOTAUTHORIZED;
  87. try {
  88. authorize(req, e);
  89. authorized = RpcResponse::AUTHORIZED;
  90. auto r = process(req, e);
  91. return RpcResponse(0, authorized, std::move(r), std::move(req.id));
  92. }
  93. catch (RecoverableException& ex) {
  94. A2_LOG_DEBUG_EX(EX_EXCEPTION_CAUGHT, ex);
  95. return RpcResponse(1, authorized, createErrorResponse(ex, req),
  96. std::move(req.id));
  97. }
  98. }
  99. namespace {
  100. template <typename InputIterator, typename Pred>
  101. void gatherOption(InputIterator first, InputIterator last, Pred pred,
  102. Option* option,
  103. const std::shared_ptr<OptionParser>& optionParser)
  104. {
  105. for (; first != last; ++first) {
  106. const std::string& optionName = (*first).first;
  107. PrefPtr pref = option::k2p(optionName);
  108. const OptionHandler* handler = optionParser->find(pref);
  109. if (!handler || !pred(handler)) {
  110. // Just ignore the unacceptable options in this context.
  111. continue;
  112. }
  113. const String* opval = downcast<String>((*first).second);
  114. if (opval) {
  115. handler->parse(*option, opval->s());
  116. }
  117. else if (handler->getCumulative()) {
  118. // header and index-out option can take array as value
  119. const List* oplist = downcast<List>((*first).second);
  120. if (oplist) {
  121. for (auto& elem : *oplist) {
  122. const String* opval = downcast<String>(elem);
  123. if (opval) {
  124. handler->parse(*option, opval->s());
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }
  131. } // namespace
  132. void RpcMethod::gatherRequestOption(Option* option, const Dict* optionsDict)
  133. {
  134. if (optionsDict) {
  135. gatherOption(optionsDict->begin(), optionsDict->end(),
  136. std::mem_fn(&OptionHandler::getInitialOption), option,
  137. optionParser_);
  138. }
  139. }
  140. void RpcMethod::gatherChangeableOption(Option* option, const Dict* optionsDict)
  141. {
  142. if (optionsDict) {
  143. gatherOption(optionsDict->begin(), optionsDict->end(),
  144. std::mem_fn(&OptionHandler::getChangeOption), option,
  145. optionParser_);
  146. }
  147. }
  148. void RpcMethod::gatherChangeableOptionForReserved(Option* option,
  149. const Dict* optionsDict)
  150. {
  151. if (optionsDict) {
  152. gatherOption(optionsDict->begin(), optionsDict->end(),
  153. std::mem_fn(&OptionHandler::getChangeOptionForReserved),
  154. option, optionParser_);
  155. }
  156. }
  157. void RpcMethod::gatherChangeableGlobalOption(Option* option,
  158. const Dict* optionsDict)
  159. {
  160. if (optionsDict) {
  161. gatherOption(optionsDict->begin(), optionsDict->end(),
  162. std::mem_fn(&OptionHandler::getChangeGlobalOption), option,
  163. optionParser_);
  164. }
  165. }
  166. } // namespace rpc
  167. } // namespace aria2