XmlRpcMethod.cc 6.3 KB

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