XmlRpcMethod.cc 6.4 KB

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