From d15a3b10a3031b9552d67d2e2d435a689b448e2f Mon Sep 17 00:00:00 2001 From: Oren Hurvitz Date: Thu, 24 Apr 2014 14:19:03 +0300 Subject: When sending JSON-RPC calls (for UserProfile), use WebUtil instead of constructing the HTTP requests manually. This allows the calls to be logged when using "debug http all 6". --- .../Servers/HttpServer/JsonRpcRequestManager.cs | 175 +++++++++------------ 1 file changed, 77 insertions(+), 98 deletions(-) (limited to 'OpenSim/Framework/Servers') diff --git a/OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs b/OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs index a44f471..ed6a14c 100644 --- a/OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs +++ b/OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs @@ -48,7 +48,6 @@ namespace OpenSim.Framework.Servers.HttpServer { } - #region Web Util /// /// Sends json-rpc request with a serializable type. /// @@ -70,64 +69,62 @@ namespace OpenSim.Framework.Servers.HttpServer public bool JsonRpcRequest(ref object parameters, string method, string uri, string jsonId) { if (jsonId == null) - throw new ArgumentNullException ("jsonId"); + throw new ArgumentNullException("jsonId"); if (uri == null) - throw new ArgumentNullException ("uri"); + throw new ArgumentNullException("uri"); if (method == null) - throw new ArgumentNullException ("method"); + throw new ArgumentNullException("method"); if (parameters == null) - throw new ArgumentNullException ("parameters"); - - // Prep our payload - OSDMap json = new OSDMap(); - - json.Add("jsonrpc", OSD.FromString("2.0")); - json.Add("id", OSD.FromString(jsonId)); - json.Add("method", OSD.FromString(method)); - - json.Add("params", OSD.SerializeMembers(parameters)); - - string jsonRequestData = OSDParser.SerializeJsonString(json); - byte[] content = Encoding.UTF8.GetBytes(jsonRequestData); - - HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri); - - webRequest.ContentType = "application/json-rpc"; - webRequest.Method = "POST"; - - //Stream dataStream = webRequest.GetRequestStream(); - //dataStream.Write(content, 0, content.Length); - //dataStream.Close(); - - using (Stream dataStream = webRequest.GetRequestStream()) - dataStream.Write(content, 0, content.Length); - - WebResponse webResponse = null; + throw new ArgumentNullException("parameters"); + + OSDMap request = new OSDMap(); + request.Add("jsonrpc", OSD.FromString("2.0")); + request.Add("id", OSD.FromString(jsonId)); + request.Add("method", OSD.FromString(method)); + request.Add("params", OSD.SerializeMembers(parameters)); + + OSDMap response; try { - webResponse = webRequest.GetResponse(); + response = WebUtil.PostToService(uri, request, 10000, true); + } + catch (Exception e) + { + m_log.Debug(string.Format("JsonRpc request '{0}' failed", method), e); + return false; + } + + if (!response.ContainsKey("_Result")) + { + m_log.DebugFormat("JsonRpc request '{0}' returned an invalid response: {1}", + method, OSDParser.SerializeJsonString(response)); + return false; } - catch (WebException e) + response = (OSDMap)response["_Result"]; + + OSD data; + + if (response.ContainsKey("error")) { - Console.WriteLine("Web Error" + e.Message); - Console.WriteLine ("Please check input"); + data = response["error"]; + m_log.DebugFormat("JsonRpc request '{0}' returned an error: {1}", + method, OSDParser.SerializeJsonString(data)); return false; } - - using (webResponse) - using (Stream rstream = webResponse.GetResponseStream()) + + if (!response.ContainsKey("result")) { - OSDMap mret = (OSDMap)OSDParser.DeserializeJson(rstream); - - if (mret.ContainsKey("error")) - return false; - - // get params... - OSD.DeserializeMembers(ref parameters, (OSDMap)mret["result"]); - return true; + m_log.DebugFormat("JsonRpc request '{0}' returned an invalid response: {1}", + method, OSDParser.SerializeJsonString(response)); + return false; } + + data = response["result"]; + OSD.DeserializeMembers(ref parameters, (OSDMap)data); + + return true; } - + /// /// Sends json-rpc request with OSD parameter. /// @@ -135,7 +132,7 @@ namespace OpenSim.Framework.Servers.HttpServer /// The rpc request. /// /// - /// data - incoming as parameters, outgong as result/error + /// data - incoming as parameters, outgoing as result/error /// /// /// Json-rpc method to call. @@ -148,64 +145,46 @@ namespace OpenSim.Framework.Servers.HttpServer /// public bool JsonRpcRequest(ref OSD data, string method, string uri, string jsonId) { - OSDMap map = new OSDMap(); - - map["jsonrpc"] = "2.0"; - if(string.IsNullOrEmpty(jsonId)) - map["id"] = UUID.Random().ToString(); - else - map["id"] = jsonId; - - map["method"] = method; - map["params"] = data; - - string jsonRequestData = OSDParser.SerializeJsonString(map); - byte[] content = Encoding.UTF8.GetBytes(jsonRequestData); - - HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri); - webRequest.ContentType = "application/json-rpc"; - webRequest.Method = "POST"; - - using (Stream dataStream = webRequest.GetRequestStream()) - dataStream.Write(content, 0, content.Length); - - WebResponse webResponse = null; + if (string.IsNullOrEmpty(jsonId)) + jsonId = UUID.Random().ToString(); + + OSDMap request = new OSDMap(); + request.Add("jsonrpc", OSD.FromString("2.0")); + request.Add("id", OSD.FromString(jsonId)); + request.Add("method", OSD.FromString(method)); + request.Add("params", data); + + OSDMap response; try { - webResponse = webRequest.GetResponse(); + response = WebUtil.PostToService(uri, request, 10000, true); } - catch (WebException e) + catch (Exception e) { - Console.WriteLine("Web Error" + e.Message); - Console.WriteLine ("Please check input"); + m_log.Debug(string.Format("JsonRpc request '{0}' failed", method), e); return false; } - - using (webResponse) - using (Stream rstream = webResponse.GetResponseStream()) + + if (!response.ContainsKey("_Result")) { - OSDMap response = new OSDMap(); - try - { - response = (OSDMap)OSDParser.DeserializeJson(rstream); - } - catch (Exception e) - { - m_log.DebugFormat("[JSONRPC]: JsonRpcRequest Error {0}", e.Message); - return false; - } - - if (response.ContainsKey("error")) - { - data = response["error"]; - return false; - } - - data = response; - - return true; + m_log.DebugFormat("JsonRpc request '{0}' returned an invalid response: {1}", + method, OSDParser.SerializeJsonString(response)); + return false; } + response = (OSDMap)response["_Result"]; + + if (response.ContainsKey("error")) + { + data = response["error"]; + m_log.DebugFormat("JsonRpc request '{0}' returned an error: {1}", + method, OSDParser.SerializeJsonString(data)); + return false; + } + + data = response; + + return true; } - #endregion Web Util + } } -- cgit v1.1