json.cc 4.7 KB

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