diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2011-03-14 00:57:05 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2011-03-14 00:57:05 +0900 |
| commit | bf01bb84b6b96fe2cba529f588e29a9038c0daf6 (patch) | |
| tree | 929c8ad8e4ec3eebdae8ffd046f7990b9f6dccae /src/json.cc | |
| parent | feb4e2e53a368baa64dffd5e2182204548fe86d5 (diff) | |
Added JSONP support. Callback query parameter is "jsoncallback".
Diffstat (limited to 'src/json.cc')
| -rw-r--r-- | src/json.cc | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/json.cc b/src/json.cc index 05d4e618..f1b84d40 100644 --- a/src/json.cc +++ b/src/json.cc @@ -42,6 +42,7 @@ #include "a2functional.h" #include "util.h" #include "fmt.h" +#include "Base64.h" namespace aria2 { @@ -607,6 +608,54 @@ std::string encode(const SharedHandle<ValueBase>& json) return encode(out, json.get()).str(); } +JsonGetParam::JsonGetParam +(const std::string& request, const std::string& callback) + : request(request), callback(callback) +{} + +JsonGetParam +decodeGetParams(const std::string& query) +{ + std::string jsonRequest; + std::string callback; + if(!query.empty() && query[0] == '?') { + std::string method; + std::string id; + std::string params; + std::vector<std::string> getParams; + util::split(query.substr(1), std::back_inserter(getParams), "&"); + for(std::vector<std::string>::const_iterator i = + getParams.begin(), eoi = getParams.end(); i != eoi; ++i) { + if(util::startsWith(*i, "method=")) { + method = (*i).substr(7); + } else if(util::startsWith(*i, "id=")) { + id = (*i).substr(3); + } else if(util::startsWith(*i, "params=")) { + params = (*i).substr(7); + } else if(util::startsWith(*i, "jsoncallback=")) { + callback = (*i).substr(13); + } + } + std::string jsonParam = + Base64::decode(util::percentDecode(params)); + if(method.empty() && id.empty()) { + // Assume batch call. + jsonRequest = jsonParam; + } else { + jsonRequest = '{'; + if(!method.empty()) { + strappend(jsonRequest, "\"method\":\"", method, "\","); + } + if(!id.empty()) { + strappend(jsonRequest, "\"id\":\"", id, "\","); + } + strappend(jsonRequest, "\"params\":", jsonParam); + jsonRequest += '}'; + } + } + return JsonGetParam(jsonRequest, callback); +} + } // namespace json } // namespace aria2 |
