XmlRpcMethod.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <cassert>
  37. #include <sstream>
  38. #include "DownloadEngine.h"
  39. #include "BDE.h"
  40. #include "LogFactory.h"
  41. #include "RecoverableException.h"
  42. #include "message.h"
  43. #include "OptionParser.h"
  44. #include "OptionHandler.h"
  45. #include "Option.h"
  46. #include "array_fun.h"
  47. #include "download_helper.h"
  48. #include "XmlRpcRequest.h"
  49. namespace aria2 {
  50. namespace xmlrpc {
  51. XmlRpcMethod::XmlRpcMethod():
  52. _optionParser(OptionParser::getInstance()),
  53. _logger(LogFactory::getInstance()) {}
  54. static BDE createErrorResponse(const Exception& e)
  55. {
  56. BDE params = BDE::list();
  57. params << BDE("ERROR");
  58. params << BDE(e.what());
  59. return params;
  60. }
  61. static void encodeValue(const BDE& value, std::ostream& o);
  62. template<typename InputIterator>
  63. static void encodeArray
  64. (InputIterator first, InputIterator last, std::ostream& o)
  65. {
  66. o << "<array>" << "<data>";
  67. for(; first != last; ++first) {
  68. encodeValue(*first, o);
  69. }
  70. o << "</data>" << "</array>";
  71. }
  72. template<typename InputIterator>
  73. static void encodeStruct
  74. (InputIterator first, InputIterator last, std::ostream& o)
  75. {
  76. o << "<struct>";
  77. for(; first != last; ++first) {
  78. o << "<member>"
  79. << "<name>" << (*first).first << "</name>";
  80. encodeValue((*first).second, o);
  81. o << "</member>";
  82. }
  83. o << "</struct>";
  84. }
  85. static void encodeValue(const BDE& value, std::ostream& o)
  86. {
  87. o << "<value>";
  88. if(value.isString()) {
  89. o << "<string>" << value.s() << "</string>";
  90. } else if(value.isList()) {
  91. encodeArray(value.listBegin(), value.listEnd(), o);
  92. } else if(value.isDict()) {
  93. encodeStruct(value.dictBegin(), value.dictEnd(), o);
  94. }
  95. o << "</value>";
  96. }
  97. template<typename InputIterator>
  98. static void encodeParams
  99. (InputIterator first, InputIterator last, std::ostream& o)
  100. {
  101. o << "<params>";
  102. for(; first != last; ++first) {
  103. o << "<param>";
  104. encodeValue(*first, o);
  105. o << "</param>";
  106. }
  107. o << "</params>";
  108. }
  109. static std::string encodeXml(const BDE& params)
  110. {
  111. assert(params.isList());
  112. std::stringstream o;
  113. o << "<?xml version=\"1.0\"?>" << "<methodResponse>";
  114. encodeParams(params.listBegin(), params.listEnd(), o);
  115. o << "</methodResponse>";
  116. return o.str();
  117. }
  118. std::string XmlRpcMethod::execute(const XmlRpcRequest& req, DownloadEngine* e)
  119. {
  120. try {
  121. BDE retparams = process(req, e);
  122. return encodeXml(retparams);
  123. } catch(RecoverableException& e) {
  124. _logger->debug(EX_EXCEPTION_CAUGHT, e);
  125. return encodeXml(createErrorResponse(e));
  126. }
  127. }
  128. void XmlRpcMethod::gatherRequestOption
  129. (const SharedHandle<Option>& option, const BDE& optionsDict)
  130. {
  131. for(std::vector<std::string>::const_iterator i = listRequestOptions().begin();
  132. i != listRequestOptions().end(); ++i) {
  133. if(optionsDict.containsKey(*i)) {
  134. const BDE& value = optionsDict[*i];
  135. if(value.isString()) {
  136. _optionParser->findByName(*i)->parse
  137. (*option.get(), value.s());
  138. }
  139. }
  140. }
  141. }
  142. } // namespace xmlrpc
  143. } // namespace aria2