XmlRpcMethod.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "XmlRpcMethod.h"
  36. #include "DownloadEngine.h"
  37. #include "BDE.h"
  38. #include "LogFactory.h"
  39. #include "RecoverableException.h"
  40. #include "message.h"
  41. #include "OptionParser.h"
  42. #include "OptionHandler.h"
  43. #include "Option.h"
  44. #include "array_fun.h"
  45. #include "download_helper.h"
  46. #include "XmlRpcRequest.h"
  47. #include "XmlRpcResponse.h"
  48. #include "prefs.h"
  49. #include "StringFormat.h"
  50. namespace aria2 {
  51. namespace xmlrpc {
  52. XmlRpcMethod::XmlRpcMethod():
  53. _optionParser(OptionParser::getInstance()),
  54. _logger(LogFactory::getInstance()) {}
  55. BDE XmlRpcMethod::createErrorResponse(const Exception& e)
  56. {
  57. BDE params = BDE::dict();
  58. params["faultCode"] = BDE(1);
  59. params["faultString"] = BDE(e.what());
  60. return params;
  61. }
  62. XmlRpcResponse XmlRpcMethod::execute
  63. (const XmlRpcRequest& req, DownloadEngine* e)
  64. {
  65. try {
  66. return XmlRpcResponse(0, process(req, e));
  67. } catch(RecoverableException& e) {
  68. _logger->debug(EX_EXCEPTION_CAUGHT, e);
  69. return XmlRpcResponse(1, createErrorResponse(e));
  70. }
  71. }
  72. template<typename InputIterator>
  73. static void gatherOption
  74. (InputIterator first, InputIterator last,
  75. const std::set<std::string>& changeableOptions,
  76. const SharedHandle<Option>& option,
  77. const SharedHandle<OptionParser>& optionParser)
  78. {
  79. for(; first != last; ++first) {
  80. const std::string& optionName = (*first).first;
  81. if(changeableOptions.count(optionName) == 0) {
  82. throw DL_ABORT_EX
  83. (StringFormat
  84. ("%s cannot be changed or unknown option.", optionName.c_str()).str());
  85. } else {
  86. SharedHandle<OptionHandler> optionHandler =
  87. optionParser->findByName(optionName);
  88. if(optionHandler.isNull()) {
  89. throw DL_ABORT_EX
  90. (StringFormat
  91. ("We don't know how to deal with %s option",
  92. optionName.c_str()).str());
  93. }
  94. // header and index-out option can take array as value
  95. const BDE& value = (*first).second;
  96. if((optionName == PREF_HEADER || optionName == PREF_INDEX_OUT) &&
  97. value.isList()){
  98. for(BDE::List::const_iterator argiter = value.listBegin();
  99. argiter != value.listEnd(); ++argiter) {
  100. if((*argiter).isString()) {
  101. optionHandler->parse(*option.get(), (*argiter).s());
  102. }
  103. }
  104. } else if(value.isString()) {
  105. optionHandler->parse(*option.get(), value.s());
  106. }
  107. }
  108. }
  109. }
  110. void XmlRpcMethod::gatherRequestOption
  111. (const SharedHandle<Option>& option, const BDE& optionsDict)
  112. {
  113. gatherOption(optionsDict.dictBegin(), optionsDict.dictEnd(),
  114. listRequestOptions(),
  115. option, _optionParser);
  116. }
  117. // Copy option in the range [optNameFirst, optNameLast) from src to
  118. // dest.
  119. template<typename InputIterator>
  120. static void applyOption(InputIterator optNameFirst,
  121. InputIterator optNameLast,
  122. Option* dest,
  123. Option* src)
  124. {
  125. for(; optNameFirst != optNameLast; ++optNameFirst) {
  126. if(src->defined(*optNameFirst)) {
  127. dest->put(*optNameFirst, src->get(*optNameFirst));
  128. }
  129. }
  130. }
  131. const std::set<std::string>& listChangeableOptions()
  132. {
  133. static const std::string OPTIONS[] = {
  134. PREF_BT_MAX_PEERS,
  135. PREF_BT_REQUEST_PEER_SPEED_LIMIT,
  136. PREF_MAX_DOWNLOAD_LIMIT,
  137. PREF_MAX_UPLOAD_LIMIT
  138. };
  139. static std::set<std::string> options
  140. (&OPTIONS[0], &OPTIONS[arrayLength(OPTIONS)]);
  141. return options;
  142. }
  143. void XmlRpcMethod::gatherChangeableOption
  144. (const SharedHandle<Option>& option, const BDE& optionsDict)
  145. {
  146. gatherOption(optionsDict.dictBegin(), optionsDict.dictEnd(),
  147. listChangeableOptions(),
  148. option, _optionParser);
  149. }
  150. void XmlRpcMethod::applyChangeableOption(Option* dest, Option* src) const
  151. {
  152. applyOption(listChangeableOptions().begin(), listChangeableOptions().end(),
  153. dest, src);
  154. }
  155. const std::set<std::string>& listChangeableGlobalOptions()
  156. {
  157. static const std::string OPTIONS[] = {
  158. PREF_MAX_OVERALL_UPLOAD_LIMIT,
  159. PREF_MAX_OVERALL_DOWNLOAD_LIMIT,
  160. PREF_MAX_CONCURRENT_DOWNLOADS,
  161. };
  162. static std::set<std::string> options
  163. (&OPTIONS[0], &OPTIONS[arrayLength(OPTIONS)]);
  164. return options;
  165. }
  166. void XmlRpcMethod::gatherChangeableGlobalOption
  167. (const SharedHandle<Option>& option, const BDE& optionsDict)
  168. {
  169. gatherOption(optionsDict.dictBegin(), optionsDict.dictEnd(),
  170. listChangeableGlobalOptions(),
  171. option, _optionParser);
  172. }
  173. void XmlRpcMethod::applyChangeableGlobalOption(Option* dest, Option* src) const
  174. {
  175. applyOption(listChangeableGlobalOptions().begin(),
  176. listChangeableGlobalOptions().end(),
  177. dest, src);
  178. }
  179. } // namespace xmlrpc
  180. } // namespace aria2