XmlRpcMethod.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 "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 "XmlRpcRequest.h"
  46. #include "XmlRpcResponse.h"
  47. #include "prefs.h"
  48. #include "fmt.h"
  49. #include "DlAbortEx.h"
  50. namespace aria2 {
  51. namespace xmlrpc {
  52. XmlRpcMethod::XmlRpcMethod()
  53. : optionParser_(OptionParser::getInstance())
  54. {}
  55. XmlRpcMethod::~XmlRpcMethod() {}
  56. SharedHandle<ValueBase> XmlRpcMethod::createErrorResponse
  57. (const Exception& e)
  58. {
  59. SharedHandle<Dict> params = Dict::g();
  60. params->put("faultCode", Integer::g(1));
  61. params->put("faultString", std::string(e.what()));
  62. return params;
  63. }
  64. XmlRpcResponse XmlRpcMethod::execute
  65. (const XmlRpcRequest& req, DownloadEngine* e)
  66. {
  67. try {
  68. return XmlRpcResponse(0, process(req, e));
  69. } catch(RecoverableException& e) {
  70. A2_LOG_DEBUG_EX(EX_EXCEPTION_CAUGHT, e);
  71. return XmlRpcResponse(1, createErrorResponse(e));
  72. }
  73. }
  74. namespace {
  75. template<typename InputIterator>
  76. void gatherOption
  77. (InputIterator first, InputIterator last,
  78. const std::set<std::string>& allowedOptions,
  79. const SharedHandle<Option>& option,
  80. const SharedHandle<OptionParser>& optionParser)
  81. {
  82. for(; first != last; ++first) {
  83. const std::string& optionName = (*first).first;
  84. if(allowedOptions.count(optionName) == 0) {
  85. throw DL_ABORT_EX
  86. (fmt("%s option cannot be used in this context.",
  87. optionName.c_str()));
  88. } else {
  89. SharedHandle<OptionHandler> optionHandler =
  90. optionParser->findByName(optionName);
  91. if(!optionHandler) {
  92. throw DL_ABORT_EX
  93. (fmt("We don't know how to deal with %s option",
  94. optionName.c_str()));
  95. }
  96. const String* opval = asString((*first).second);
  97. if(opval) {
  98. optionHandler->parse(*option.get(), opval->s());
  99. } else {
  100. // header and index-out option can take array as value
  101. const List* oplist = asList((*first).second);
  102. if(oplist &&
  103. (optionName == PREF_HEADER || optionName == PREF_INDEX_OUT)) {
  104. for(List::ValueType::const_iterator argiter = oplist->begin(),
  105. eoi = oplist->end(); argiter != eoi; ++argiter) {
  106. const String* opval = asString(*argiter);
  107. if(opval) {
  108. optionHandler->parse(*option.get(), opval->s());
  109. }
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }
  116. } // namespace
  117. void XmlRpcMethod::gatherRequestOption
  118. (const SharedHandle<Option>& option, const Dict* optionsDict)
  119. {
  120. if(optionsDict) {
  121. gatherOption(optionsDict->begin(), optionsDict->end(),
  122. listRequestOptions(),
  123. option, optionParser_);
  124. }
  125. }
  126. namespace {
  127. // Copy option in the range [optNameFirst, optNameLast) from src to
  128. // dest.
  129. template<typename InputIterator>
  130. void applyOption(InputIterator optNameFirst,
  131. InputIterator optNameLast,
  132. Option* dest,
  133. Option* src)
  134. {
  135. for(; optNameFirst != optNameLast; ++optNameFirst) {
  136. if(src->defined(*optNameFirst)) {
  137. dest->put(*optNameFirst, src->get(*optNameFirst));
  138. }
  139. }
  140. }
  141. } // namespace
  142. const std::set<std::string>& listChangeableOptions()
  143. {
  144. static const std::string OPTIONS[] = {
  145. PREF_BT_MAX_PEERS,
  146. PREF_BT_REQUEST_PEER_SPEED_LIMIT,
  147. PREF_MAX_DOWNLOAD_LIMIT,
  148. PREF_MAX_UPLOAD_LIMIT
  149. };
  150. static std::set<std::string> options(vbegin(OPTIONS), vend(OPTIONS));
  151. return options;
  152. }
  153. void XmlRpcMethod::gatherChangeableOption
  154. (const SharedHandle<Option>& option, const Dict* optionsDict)
  155. {
  156. if(optionsDict) {
  157. gatherOption(optionsDict->begin(), optionsDict->end(),
  158. listChangeableOptions(),
  159. option, optionParser_);
  160. }
  161. }
  162. void XmlRpcMethod::applyChangeableOption(Option* dest, Option* src) const
  163. {
  164. applyOption(listChangeableOptions().begin(), listChangeableOptions().end(),
  165. dest, src);
  166. }
  167. const std::set<std::string>& listChangeableGlobalOptions()
  168. {
  169. static const std::string OPTIONS[] = {
  170. PREF_MAX_OVERALL_UPLOAD_LIMIT,
  171. PREF_MAX_OVERALL_DOWNLOAD_LIMIT,
  172. PREF_MAX_CONCURRENT_DOWNLOADS,
  173. PREF_LOG,
  174. PREF_LOG_LEVEL
  175. };
  176. static std::set<std::string> options(vbegin(OPTIONS), vend(OPTIONS));
  177. return options;
  178. }
  179. void XmlRpcMethod::gatherChangeableGlobalOption
  180. (const SharedHandle<Option>& option, const Dict* optionsDict)
  181. {
  182. if(optionsDict) {
  183. gatherOption(optionsDict->begin(), optionsDict->end(),
  184. listChangeableGlobalOptions(),
  185. option, optionParser_);
  186. }
  187. }
  188. void XmlRpcMethod::applyChangeableGlobalOption(Option* dest, Option* src) const
  189. {
  190. applyOption(listChangeableGlobalOptions().begin(),
  191. listChangeableGlobalOptions().end(),
  192. dest, src);
  193. }
  194. } // namespace xmlrpc
  195. } // namespace aria2