XmlRpcMethod.cc 6.2 KB

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