XmlRpcResponse.cc 5.2 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 "XmlRpcResponse.h"
  36. #include <cassert>
  37. #include <sstream>
  38. #include "util.h"
  39. #include "json.h"
  40. #ifdef HAVE_ZLIB
  41. # include "GZipEncoder.h"
  42. #endif // HAVE_ZLIB
  43. namespace aria2 {
  44. namespace xmlrpc {
  45. namespace {
  46. template<typename OutputStream>
  47. void encodeValue(const SharedHandle<ValueBase>& value, OutputStream& o)
  48. {
  49. class XmlValueBaseVisitor:public ValueBaseVisitor {
  50. private:
  51. OutputStream& o_;
  52. public:
  53. XmlValueBaseVisitor(OutputStream& o):o_(o) {}
  54. virtual ~XmlValueBaseVisitor() {}
  55. virtual void visit(const String& v)
  56. {
  57. o_ << "<value><string>" << util::htmlEscape(v.s()) << "</string></value>";
  58. }
  59. virtual void visit(const Integer& v)
  60. {
  61. o_ << "<value><int>" << v.i() << "</int></value>";
  62. }
  63. virtual void visit(const Bool& boolValue) {}
  64. virtual void visit(const Null& nullValue) {}
  65. virtual void visit(const List& v)
  66. {
  67. o_ << "<value><array><data>";
  68. for(List::ValueType::const_iterator i = v.begin(), eoi = v.end();
  69. i != eoi; ++i) {
  70. (*i)->accept(*this);
  71. }
  72. o_ << "</data></array></value>";
  73. }
  74. virtual void visit(const Dict& v)
  75. {
  76. o_ << "<value><struct>";
  77. for(Dict::ValueType::const_iterator i = v.begin(), eoi = v.end();
  78. i != eoi; ++i) {
  79. o_ << "<member><name>" << util::htmlEscape((*i).first) << "</name>";
  80. (*i).second->accept(*this);
  81. o_ << "</member>";
  82. }
  83. o_ << "</struct></value>";
  84. }
  85. };
  86. XmlValueBaseVisitor visitor(o);
  87. value->accept(visitor);
  88. }
  89. } // namespace
  90. namespace {
  91. template<typename OutputStream>
  92. std::string encodeAll
  93. (OutputStream& o, int code, const SharedHandle<ValueBase>& param)
  94. {
  95. o << "<?xml version=\"1.0\"?>" << "<methodResponse>";
  96. if(code == 0) {
  97. o << "<params>" << "<param>";
  98. encodeValue(param, o);
  99. o << "</param>" << "</params>";
  100. } else {
  101. o << "<fault>";
  102. encodeValue(param, o);
  103. o << "</fault>";
  104. }
  105. o << "</methodResponse>";
  106. return o.str();
  107. }
  108. } // namespace
  109. XmlRpcResponse::XmlRpcResponse
  110. (int code,
  111. const SharedHandle<ValueBase>& param,
  112. const SharedHandle<ValueBase>& id)
  113. : code(code), param(param), id(id)
  114. {}
  115. XmlRpcResponse::XmlRpcResponse(const XmlRpcResponse& c)
  116. : code(c.code),
  117. param(c.param),
  118. id(c.id)
  119. {}
  120. XmlRpcResponse::~XmlRpcResponse() {}
  121. XmlRpcResponse& XmlRpcResponse::operator=(const XmlRpcResponse& c)
  122. {
  123. if(this != &c) {
  124. code = c.code;
  125. param = c.param;
  126. }
  127. return *this;
  128. }
  129. std::string XmlRpcResponse::toXml(bool gzip) const
  130. {
  131. if(gzip) {
  132. #ifdef HAVE_ZLIB
  133. GZipEncoder o;
  134. o.init();
  135. return encodeAll(o, code, param);
  136. #else // !HAVE_ZLIB
  137. abort();
  138. #endif // !HAVE_ZLIB
  139. } else {
  140. std::stringstream o;
  141. return encodeAll(o, code, param);
  142. }
  143. }
  144. namespace {
  145. template<typename OutputStream>
  146. OutputStream& encodeJsonAll
  147. (OutputStream& o,
  148. int code,
  149. const SharedHandle<ValueBase>& param,
  150. const SharedHandle<ValueBase>& id,
  151. const std::string& callback)
  152. {
  153. if(!callback.empty()) {
  154. o << callback << '(';
  155. }
  156. SharedHandle<Dict> dict = Dict::g();
  157. dict->put("jsonrpc", "2.0");
  158. // TODO id may be null?
  159. if(id) {
  160. dict->put("id", id);
  161. }
  162. if(code == 0) {
  163. dict->put("result", param);
  164. } else {
  165. dict->put("error", param);
  166. }
  167. json::encode(o, dict).str();
  168. if(!callback.empty()) {
  169. o << ')';
  170. }
  171. return o;
  172. }
  173. } // namespace
  174. std::string XmlRpcResponse::toJson(const std::string& callback, bool gzip) const
  175. {
  176. if(gzip) {
  177. #ifdef HAVE_ZLIB
  178. GZipEncoder o;
  179. o.init();
  180. return encodeJsonAll(o, code, param, id, callback).str();
  181. #else // !HAVE_ZLIB
  182. abort();
  183. #endif // !HAVE_ZLIB
  184. } else {
  185. std::stringstream o;
  186. return encodeJsonAll(o, code, param, id, callback).str();
  187. }
  188. }
  189. } // namespace xmlrpc
  190. } // namespace aria2