XmlRpcMethod.cc 6.6 KB

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