RpcResponse.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 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) CXX11_OVERRIDE
  56. {
  57. o_ << "<value><string>" << util::htmlEscape(v.s()) << "</string></value>";
  58. }
  59. virtual void visit(const Integer& v) CXX11_OVERRIDE
  60. {
  61. o_ << "<value><int>" << v.i() << "</int></value>";
  62. }
  63. virtual void visit(const Bool& boolValue) CXX11_OVERRIDE {}
  64. virtual void visit(const Null& nullValue) CXX11_OVERRIDE {}
  65. virtual void visit(const List& v) CXX11_OVERRIDE
  66. {
  67. o_ << "<value><array><data>";
  68. for (const auto& e : v) {
  69. e->accept(*this);
  70. }
  71. o_ << "</data></array></value>";
  72. }
  73. virtual void visit(const Dict& v) CXX11_OVERRIDE
  74. {
  75. o_ << "<value><struct>";
  76. for (const auto& e : v) {
  77. o_ << "<member><name>" << util::htmlEscape(e.first) << "</name>";
  78. e.second->accept(*this);
  79. o_ << "</member>";
  80. }
  81. o_ << "</struct></value>";
  82. }
  83. };
  84. XmlValueBaseVisitor visitor(o);
  85. value->accept(visitor);
  86. }
  87. } // namespace
  88. namespace {
  89. template <typename OutputStream>
  90. std::string encodeAll(OutputStream& o, int code, const ValueBase* param)
  91. {
  92. o << "<?xml version=\"1.0\"?>"
  93. << "<methodResponse>";
  94. if (code == 0) {
  95. o << "<params>"
  96. << "<param>";
  97. encodeValue(param, o);
  98. o << "</param>"
  99. << "</params>";
  100. }
  101. else {
  102. o << "<fault>";
  103. encodeValue(param, o);
  104. o << "</fault>";
  105. }
  106. o << "</methodResponse>";
  107. return o.str();
  108. }
  109. } // namespace
  110. RpcResponse::RpcResponse(int code, RpcResponse::authorization_t authorized,
  111. std::unique_ptr<ValueBase> param,
  112. std::unique_ptr<ValueBase> id)
  113. : param{std::move(param)},
  114. id{std::move(id)},
  115. code{code},
  116. authorized{authorized}
  117. {
  118. }
  119. std::string toXml(const RpcResponse& res, bool gzip)
  120. {
  121. if (gzip) {
  122. #ifdef HAVE_ZLIB
  123. GZipEncoder o;
  124. o.init();
  125. return encodeAll(o, res.code, res.param.get());
  126. #else // !HAVE_ZLIB
  127. abort();
  128. #endif // !HAVE_ZLIB
  129. }
  130. else {
  131. std::stringstream o;
  132. return encodeAll(o, res.code, res.param.get());
  133. }
  134. }
  135. namespace {
  136. template <typename OutputStream>
  137. OutputStream& encodeJsonAll(OutputStream& o, int code, const ValueBase* param,
  138. const ValueBase* id,
  139. const std::string& callback = A2STR::NIL)
  140. {
  141. if (!callback.empty()) {
  142. o << callback << "(";
  143. }
  144. o << "{\"id\":";
  145. json::encode(o, id);
  146. o << ",\"jsonrpc\":\"2.0\",";
  147. if (code == 0) {
  148. o << "\"result\":";
  149. }
  150. else {
  151. o << "\"error\":";
  152. }
  153. json::encode(o, param);
  154. o << "}";
  155. if (!callback.empty()) {
  156. o << ")";
  157. }
  158. return o;
  159. }
  160. } // namespace
  161. std::string toJson(const RpcResponse& res, const std::string& callback,
  162. bool gzip)
  163. {
  164. if (gzip) {
  165. #ifdef HAVE_ZLIB
  166. GZipEncoder o;
  167. o.init();
  168. return encodeJsonAll(o, res.code, res.param.get(), res.id.get(), callback)
  169. .str();
  170. #else // !HAVE_ZLIB
  171. abort();
  172. #endif // !HAVE_ZLIB
  173. }
  174. else {
  175. std::stringstream o;
  176. return encodeJsonAll(o, res.code, res.param.get(), res.id.get(), callback)
  177. .str();
  178. }
  179. }
  180. namespace {
  181. template <typename OutputStream>
  182. OutputStream& encodeJsonBatchAll(OutputStream& o,
  183. const std::vector<RpcResponse>& results,
  184. const std::string& callback)
  185. {
  186. if (!callback.empty()) {
  187. o << callback << "(";
  188. }
  189. o << "[";
  190. if (!results.empty()) {
  191. encodeJsonAll(o, results[0].code, results[0].param.get(),
  192. results[0].id.get());
  193. for (auto i = std::begin(results) + 1, eoi = std::end(results); i != eoi;
  194. ++i) {
  195. o << ",";
  196. encodeJsonAll(o, (*i).code, (*i).param.get(), (*i).id.get());
  197. }
  198. }
  199. o << "]";
  200. if (!callback.empty()) {
  201. o << ")";
  202. }
  203. return o;
  204. }
  205. } // namespace
  206. std::string toJsonBatch(const std::vector<RpcResponse>& results,
  207. const std::string& callback, bool gzip)
  208. {
  209. if (gzip) {
  210. #ifdef HAVE_ZLIB
  211. GZipEncoder o;
  212. o.init();
  213. return encodeJsonBatchAll(o, results, callback).str();
  214. #else // !HAVE_ZLIB
  215. abort();
  216. #endif // !HAVE_ZLIB
  217. }
  218. else {
  219. std::stringstream o;
  220. return encodeJsonBatchAll(o, results, callback).str();
  221. }
  222. }
  223. } // namespace rpc
  224. } // namespace aria2