Value ParseXmlRpcParam(XmlParser& p) { p.PassTag("param"); Value v = ParseXmlRpcValue(p); p.PassEnd(); return v; }
RpcGet RpcRequest::Execute() { if(!shouldExecute) return RpcGet(); shouldExecute = false; String request; if(json) { ContentType("application/json"); static Atomic id; Json json; json("jsonrpc", "2.0") ("method", method); if(data.out.GetCount()) { JsonArray a; for(int i = 0; i < data.out.GetCount(); i++) { const Value& v = data.out[i]; if(v.Is<RawJsonText>()) a.CatRaw(v.To<RawJsonText>().json); else a << JsonRpcData(v); } json("params", a); } else if(data.out_map.GetCount()) { Json m; for(int i = 0; i < data.out_map.GetCount(); i++) { const Value& v = data.out_map.GetValue(i); String key = (String)data.out_map.GetKey(i); if(v.Is<RawJsonText>()) m.CatRaw(key, v.To<RawJsonText>().json); else m(key, JsonRpcData(v)); } json("params", m); } json("id", id); AtomicInc(id); request = ~json; } else { ContentType("text/xml"); request = XmlHeader(); request << XmlTag("methodCall")(XmlTag("methodName")(method) + FormatXmlRpcParams(data.out)); } if(sLogRpcCalls) { if(sLogRpcCallsCompress) RLOG("=== XmlRpc call request:\n" << CompressLog(request)); else RLOG("=== XmlRpc call request:\n" << request); } String response; New(); if(shorted) response = RpcExecuteShorted(request); else response = Post(request).Execute(); if(sLogRpcCalls) { if(sLogRpcCallsCompress) RLOG("=== XmlRpc call response:\n" << CompressLog(response)); else RLOG("=== XmlRpc call response:\n" << response); } RpcGet h; if(IsNull(response)) { faultCode = RPC_CLIENT_HTTP_ERROR; faultString = GetErrorDesc(); error = "Http request failed: " + faultString; LLOG(error); h.v = ErrorValue(error); return h; } if(json) { try { Value r = ParseJSON(response); if(IsValueMap(r)) { ValueMap m = r; Value result = m["result"]; if(!result.IsVoid()) { data.in.Clear(); data.in.Add(result); data.ii = 0; h.v = result; return h; } Value e = m["error"]; if(IsValueMap(e)) { Value c = e["code"]; Value m = e["message"]; if(IsNumber(c) && IsString(m)) { faultCode = e["code"]; faultString = e["message"]; error.Clear(); error << "Failed '" << faultString << "' (" << faultCode << ')'; LLOG(s); h.v = ErrorValue(error); return h; } } } String s; faultString = "Invalid response"; faultCode = RPC_CLIENT_RESPONSE_ERROR; error = faultString; LLOG(error); h.v = ErrorValue(error); return h; } catch(CParser::Error e) { String s; faultString = e; faultCode = RPC_CLIENT_JSON_ERROR; error.Clear(); error << "JSON Error: " << faultString; LLOG(error); h.v = ErrorValue(error); return h; } } else { XmlParser p(response); try { p.ReadPI(); p.PassTag("methodResponse"); if(p.Tag("fault")) { Value m = ParseXmlRpcValue(p); if(IsValueMap(m)) { ValueMap mm = m; faultString = mm["faultString"]; faultCode = mm["faultCode"]; error.Clear(); error << "Failed '" << faultString << "' (" << faultCode << ')'; LLOG(s); h.v = ErrorValue(error); return h; } } else { data.in = ParseXmlRpcParams(p); data.ii = 0; p.PassEnd(); } } catch(XmlError e) { String s; faultString = e; faultCode = RPC_CLIENT_XML_ERROR; error.Clear(); error << "XML Error: " << faultString; LLOG(error << ": " << p.GetPtr()); h.v = ErrorValue(error); return h; } h.v = data.in.GetCount() ? data.in[0] : Null; return h; } }
NAMESPACE_UPP Value ParseXmlRpcValue(XmlParser& p) { Value r; p.PassTag("value"); if(p.Tag("int") || p.Tag("i4")) { String s = p.ReadText(); CParser p(s); if(!p.IsInt()) throw XmlError("integer expected"); r = p.ReadInt(); } else if(p.Tag("boolean")) { int n = StrInt(p.ReadText()); if(n != 0 && n != 1) throw XmlError("boolean expected"); r = (bool)n; } else if(p.Tag("double")) { String s = p.ReadText(); CParser p(s); if(!p.IsDouble()) throw XmlError("double expected"); r = p.ReadDouble(); } else if(p.Tag("string") || p.Tag("base64")) r = p.ReadText(); else if(p.TagE("nil")) { p.PassEnd(); return r; } else if(p.Tag("dateTime.iso8601")) { String s = TrimBoth(p.ReadText()); // 19980717T14:08:55 // 01234567890123456 if(s.GetCount() != 17 || s[8] != 'T' || s[11] != ':' || s[14] != ':') throw XmlError("invalid dateTime format"); Time tm; tm.year = atoi(s.Mid(0, 4)); tm.month = atoi(s.Mid(4, 2)); tm.day = atoi(s.Mid(6, 2)); tm.hour = atoi(s.Mid(9, 2)); tm.minute = atoi(s.Mid(12, 2)); tm.second = atoi(s.Mid(15, 2)); r = tm; } else if(p.Tag("array")) { ValueArray va; p.PassTag("data"); while(!p.End()) va.Add(ParseXmlRpcValue(p)); r = va; } else if(p.Tag("struct")) { ValueMap vm; while(p.Tag("member")) { p.PassTag("name"); String name = p.ReadText(); p.PassEnd(); vm.Add((Value)name, ParseXmlRpcValue(p)); p.PassEnd(); } r = vm; } else throw XmlError("unknown value type"); p.PassEnd(); p.PassEnd(); return r; }