RpcResponse.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 "RpcResponse.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 rpc {
  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. RpcResponse::RpcResponse
  110. (int code,
  111. const SharedHandle<ValueBase>& param,
  112. const SharedHandle<ValueBase>& id)
  113. : code(code), param(param), id(id)
  114. {}
  115. RpcResponse::RpcResponse(const RpcResponse& c)
  116. : code(c.code),
  117. param(c.param),
  118. id(c.id)
  119. {}
  120. RpcResponse::~RpcResponse() {}
  121. RpcResponse& RpcResponse::operator=(const RpcResponse& c)
  122. {
  123. if(this != &c) {
  124. code = c.code;
  125. param = c.param;
  126. }
  127. return *this;
  128. }
  129. std::string RpcResponse::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 = A2STR::NIL)
  152. {
  153. if(!callback.empty()) {
  154. o << callback << "(";
  155. }
  156. SharedHandle<Dict> dict = Dict::g();
  157. dict->put("jsonrpc", "2.0");
  158. dict->put("id", id);
  159. if(code == 0) {
  160. dict->put("result", param);
  161. } else {
  162. dict->put("error", param);
  163. }
  164. json::encode(o, dict);
  165. if(!callback.empty()) {
  166. o << ")";
  167. }
  168. return o;
  169. }
  170. } // namespace
  171. std::string RpcResponse::toJson(const std::string& callback, bool gzip) const
  172. {
  173. if(gzip) {
  174. #ifdef HAVE_ZLIB
  175. GZipEncoder o;
  176. o.init();
  177. return encodeJsonAll(o, code, param, id, callback).str();
  178. #else // !HAVE_ZLIB
  179. abort();
  180. #endif // !HAVE_ZLIB
  181. } else {
  182. std::stringstream o;
  183. return encodeJsonAll(o, code, param, id, callback).str();
  184. }
  185. }
  186. namespace {
  187. template<typename OutputStream>
  188. OutputStream& encodeJsonBatchAll
  189. (OutputStream& o,
  190. const std::vector<RpcResponse>& results,
  191. const std::string& callback)
  192. {
  193. if(!callback.empty()) {
  194. o << callback << "(";
  195. }
  196. o << "[";
  197. if(!results.empty()) {
  198. encodeJsonAll(o, results[0].code, results[0].param, results[0].id);
  199. }
  200. for(std::vector<RpcResponse>::const_iterator i = results.begin()+1,
  201. eoi = results.end(); i != eoi; ++i) {
  202. o << ",";
  203. encodeJsonAll(o, (*i).code, (*i).param, (*i).id);
  204. }
  205. o << "]";
  206. if(!callback.empty()) {
  207. o << ")";
  208. }
  209. return o;
  210. }
  211. } // namespace
  212. std::string toJsonBatch
  213. (const std::vector<RpcResponse>& results,
  214. const std::string& callback,
  215. bool gzip)
  216. {
  217. if(gzip) {
  218. #ifdef HAVE_ZLIB
  219. GZipEncoder o;
  220. o.init();
  221. return encodeJsonBatchAll(o, results, callback).str();
  222. #else // !HAVE_ZLIB
  223. abort();
  224. #endif // !HAVE_ZLIB
  225. } else {
  226. std::stringstream o;
  227. return encodeJsonBatchAll(o, results, callback).str();
  228. }
  229. }
  230. } // namespace rpc
  231. } // namespace aria2