RpcMethod.cc 5.5 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()
  55. : optionParser_(OptionParser::getInstance())
  56. {}
  57. RpcMethod::~RpcMethod() {}
  58. std::unique_ptr<ValueBase> RpcMethod::createErrorResponse
  59. (const Exception& e, const RpcRequest& req)
  60. {
  61. auto params = Dict::g();
  62. params->put((req.jsonRpc ? "code" : "faultCode"), Integer::g(1));
  63. params->put((req.jsonRpc ? "message" : "faultString"), std::string(e.what()));
  64. return std::move(params);
  65. }
  66. void RpcMethod::authorize(RpcRequest& req, DownloadEngine* e)
  67. {
  68. std::string token;
  69. // We always treat first parameter as token if it is string and
  70. // starts with "token:" and remove it from parameter list, so that
  71. // we don't have to add conditionals to all RPCMethod
  72. // implementations.
  73. if(req.params && !req.params->empty()) {
  74. auto t = downcast<String>(req.params->get(0));
  75. if(t) {
  76. if(util::startsWith(t->s(), "token:")) {
  77. token = t->s().substr(6);
  78. req.params->pop_front();
  79. }
  80. }
  81. }
  82. if (!e || !e->validateToken(token)) {
  83. throw DL_ABORT_EX("Unauthorized");
  84. }
  85. }
  86. RpcResponse RpcMethod::execute(RpcRequest req, DownloadEngine* e)
  87. {
  88. try {
  89. authorize(req, e);
  90. auto r = process(req, e);
  91. return RpcResponse(0, std::move(r), std::move(req.id));
  92. } catch(RecoverableException& ex) {
  93. A2_LOG_DEBUG_EX(EX_EXCEPTION_CAUGHT, ex);
  94. return RpcResponse(1, createErrorResponse(ex, req), std::move(req.id));
  95. }
  96. }
  97. namespace {
  98. template<typename InputIterator, typename Pred>
  99. void gatherOption
  100. (InputIterator first, InputIterator last,
  101. 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. } else if(handler->getCumulative()) {
  117. // header and index-out option can take array as value
  118. const List* oplist = downcast<List>((*first).second);
  119. if(oplist) {
  120. for(auto & elem : *oplist) {
  121. const String* opval = downcast<String>(elem);
  122. if(opval) {
  123. handler->parse(*option, opval->s());
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130. } // namespace
  131. void RpcMethod::gatherRequestOption(Option* option, const Dict* optionsDict)
  132. {
  133. if(optionsDict) {
  134. gatherOption(optionsDict->begin(), optionsDict->end(),
  135. std::mem_fn(&OptionHandler::getInitialOption),
  136. option, optionParser_);
  137. }
  138. }
  139. void RpcMethod::gatherChangeableOption(Option* option, const Dict* optionsDict)
  140. {
  141. if(optionsDict) {
  142. gatherOption(optionsDict->begin(), optionsDict->end(),
  143. std::mem_fn(&OptionHandler::getChangeOption),
  144. option, optionParser_);
  145. }
  146. }
  147. void RpcMethod::gatherChangeableOptionForReserved
  148. (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
  158. (Option* option, const Dict* optionsDict)
  159. {
  160. if(optionsDict) {
  161. gatherOption(optionsDict->begin(), optionsDict->end(),
  162. std::mem_fn(&OptionHandler::getChangeGlobalOption),
  163. option, optionParser_);
  164. }
  165. }
  166. } // namespace rpc
  167. } // namespace aria2