RpcMethod.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. namespace aria2 {
  52. namespace rpc {
  53. RpcMethod::RpcMethod()
  54. : optionParser_(OptionParser::getInstance())
  55. {}
  56. RpcMethod::~RpcMethod() {}
  57. SharedHandle<ValueBase> RpcMethod::createErrorResponse
  58. (const Exception& e, const RpcRequest& req)
  59. {
  60. SharedHandle<Dict> params = Dict::g();
  61. params->put((req.jsonRpc ? "code" : "faultCode"), Integer::g(1));
  62. params->put((req.jsonRpc ? "message" : "faultString"), std::string(e.what()));
  63. return params;
  64. }
  65. RpcResponse RpcMethod::execute
  66. (const RpcRequest& req, DownloadEngine* e)
  67. {
  68. try {
  69. return RpcResponse(0, process(req, e), req.id);
  70. } catch(RecoverableException& ex) {
  71. A2_LOG_DEBUG_EX(EX_EXCEPTION_CAUGHT, ex);
  72. return RpcResponse(1, createErrorResponse(ex, req), req.id);
  73. }
  74. }
  75. namespace {
  76. template<typename InputIterator, typename Pred>
  77. void gatherOption
  78. (InputIterator first, InputIterator last,
  79. Pred pred,
  80. Option* option,
  81. const SharedHandle<OptionParser>& optionParser)
  82. {
  83. for(; first != last; ++first) {
  84. const std::string& optionName = (*first).first;
  85. const Pref* pref = option::k2p(optionName);
  86. const OptionHandler* handler = optionParser->find(pref);
  87. if(!handler || !pred(handler)) {
  88. // Just ignore the unacceptable options in this context.
  89. continue;
  90. }
  91. const String* opval = downcast<String>((*first).second);
  92. if(opval) {
  93. handler->parse(*option, opval->s());
  94. } else if(handler->getCumulative()) {
  95. // header and index-out option can take array as value
  96. const List* oplist = downcast<List>((*first).second);
  97. if(oplist) {
  98. for(List::ValueType::const_iterator argiter = oplist->begin(),
  99. eoi = oplist->end(); argiter != eoi; ++argiter) {
  100. const String* opval = downcast<String>(*argiter);
  101. if(opval) {
  102. handler->parse(*option, opval->s());
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. } // namespace
  110. void RpcMethod::gatherRequestOption(Option* option, const Dict* optionsDict)
  111. {
  112. if(optionsDict) {
  113. gatherOption(optionsDict->begin(), optionsDict->end(),
  114. std::mem_fun(&OptionHandler::getInitialOption),
  115. option, optionParser_);
  116. }
  117. }
  118. void RpcMethod::gatherChangeableOption(Option* option, const Dict* optionsDict)
  119. {
  120. if(optionsDict) {
  121. gatherOption(optionsDict->begin(), optionsDict->end(),
  122. std::mem_fun(&OptionHandler::getChangeOption),
  123. option, optionParser_);
  124. }
  125. }
  126. void RpcMethod::gatherChangeableOptionForReserved
  127. (Option* option,
  128. const Dict* optionsDict)
  129. {
  130. if(optionsDict) {
  131. gatherOption(optionsDict->begin(), optionsDict->end(),
  132. std::mem_fun(&OptionHandler::getChangeOptionForReserved),
  133. option, optionParser_);
  134. }
  135. }
  136. void RpcMethod::gatherChangeableGlobalOption
  137. (Option* option, const Dict* optionsDict)
  138. {
  139. if(optionsDict) {
  140. gatherOption(optionsDict->begin(), optionsDict->end(),
  141. std::mem_fun(&OptionHandler::getChangeGlobalOption),
  142. option, optionParser_);
  143. }
  144. }
  145. } // namespace rpc
  146. } // namespace aria2