json.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2011 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 "json.h"
  36. #include <sstream>
  37. #include "array_fun.h"
  38. #include "a2functional.h"
  39. #include "util.h"
  40. #include "base64.h"
  41. namespace aria2 {
  42. namespace json {
  43. std::string jsonEscape(const std::string& s)
  44. {
  45. std::string t;
  46. for (std::string::const_iterator i = s.begin(), eoi = s.end(); i != eoi;
  47. ++i) {
  48. if (*i == '"' || *i == '\\' || *i == '/') {
  49. t += "\\";
  50. t += *i;
  51. }
  52. else if (*i == '\b') {
  53. t += "\\b";
  54. }
  55. else if (*i == '\f') {
  56. t += "\\f";
  57. }
  58. else if (*i == '\n') {
  59. t += "\\n";
  60. }
  61. else if (*i == '\r') {
  62. t += "\\r";
  63. }
  64. else if (*i == '\t') {
  65. t += "\\t";
  66. }
  67. else if (in(static_cast<unsigned char>(*i), 0x00u, 0x1Fu)) {
  68. t += "\\u00";
  69. char temp[3];
  70. temp[2] = '\0';
  71. temp[0] = (*i >> 4);
  72. temp[1] = (*i) & 0x0Fu;
  73. for (int j = 0; j < 2; ++j) {
  74. if (temp[j] < 10) {
  75. temp[j] += '0';
  76. }
  77. else {
  78. temp[j] += 'A' - 10;
  79. }
  80. }
  81. t += temp;
  82. }
  83. else {
  84. t.append(i, i + 1);
  85. }
  86. }
  87. return t;
  88. }
  89. // Serializes JSON object or array.
  90. std::string encode(const ValueBase* json)
  91. {
  92. std::ostringstream out;
  93. return encode(out, json).str();
  94. }
  95. JsonGetParam::JsonGetParam(const std::string& request,
  96. const std::string& callback)
  97. : request(request), callback(callback)
  98. {
  99. }
  100. JsonGetParam decodeGetParams(const std::string& query)
  101. {
  102. std::string jsonRequest;
  103. std::string callback;
  104. if (query.empty() || query[0] != '?') {
  105. return JsonGetParam(jsonRequest, callback);
  106. }
  107. Scip method = std::make_pair(query.end(), query.end());
  108. Scip id = std::make_pair(query.end(), query.end());
  109. Scip params = std::make_pair(query.end(), query.end());
  110. std::vector<Scip> getParams;
  111. util::splitIter(query.begin() + 1, query.end(), std::back_inserter(getParams),
  112. '&');
  113. for (const auto& i : getParams) {
  114. if (util::startsWith(i.first, i.second, "method=")) {
  115. method.first = i.first + 7;
  116. method.second = i.second;
  117. }
  118. else if (util::startsWith(i.first, i.second, "id=")) {
  119. id.first = i.first + 3;
  120. id.second = i.second;
  121. }
  122. else if (util::startsWith(i.first, i.second, "params=")) {
  123. params.first = i.first + 7;
  124. params.second = i.second;
  125. }
  126. else if (util::startsWith(i.first, i.second, "jsoncallback=")) {
  127. callback.assign(i.first + 13, i.second);
  128. }
  129. }
  130. std::string decparam = util::percentDecode(params.first, params.second);
  131. std::string jsonParam = base64::decode(decparam.begin(), decparam.end());
  132. if (method.first == method.second && id.first == id.second) {
  133. // Assume batch call.
  134. jsonRequest = jsonParam;
  135. }
  136. else {
  137. jsonRequest = "{";
  138. if (method.first != method.second) {
  139. jsonRequest += "\"method\":\"";
  140. jsonRequest.append(method.first, method.second);
  141. jsonRequest += "\"";
  142. }
  143. if (id.first != id.second) {
  144. jsonRequest += ",\"id\":\"";
  145. jsonRequest.append(id.first, id.second);
  146. jsonRequest += "\"";
  147. }
  148. if (params.first != params.second) {
  149. jsonRequest += ",\"params\":";
  150. jsonRequest += jsonParam;
  151. }
  152. jsonRequest += "}";
  153. }
  154. return JsonGetParam(jsonRequest, callback);
  155. }
  156. } // namespace json
  157. } // namespace aria2