diff --git a/OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs b/OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs index c615961d9f..4bd3a82b92 100644 --- a/OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs +++ b/OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs @@ -68,28 +68,30 @@ namespace OpenSim.Framework.Servers.HttpServer /// public bool JsonRpcRequest(ref object parameters, string method, string uri, string jsonId) { - if (jsonId == null) + if (jsonId is null) throw new ArgumentNullException("jsonId"); - if (uri == null) + if (uri is null) throw new ArgumentNullException("uri"); - if (method == null) + if (method is null) throw new ArgumentNullException("method"); - if (parameters == null) + if (parameters is null) throw new ArgumentNullException("parameters"); if(string.IsNullOrWhiteSpace(uri)) return false; - 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 request = new() + { + { "jsonrpc", OSD.FromString("2.0") }, + { "id", OSD.FromString(jsonId) }, + { "method", OSD.FromString(method) }, + { "params", OSD.SerializeMembers(parameters) } + }; - OSDMap response; + OSDMap outerResponse; try { - response = WebUtil.PostToService(uri, request, 10000, true); + outerResponse = WebUtil.PostToService(uri, request, 10000, true); } catch (Exception e) { @@ -98,14 +100,13 @@ namespace OpenSim.Framework.Servers.HttpServer } OSD osdtmp; - if (!response.TryGetValue("_Result", out osdtmp) || !(osdtmp is OSDMap)) + if (!outerResponse.TryGetValue("_Result", out osdtmp) || (osdtmp is not OSDMap response)) { m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an invalid response: {2}", - method, uri, OSDParser.SerializeJsonString(response)); + method, uri, OSDParser.SerializeJsonString(outerResponse)); return false; } - response = osdtmp as OSDMap; if (response.TryGetValue("error", out osdtmp)) { m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an error: {2}", @@ -113,14 +114,14 @@ namespace OpenSim.Framework.Servers.HttpServer return false; } - if (!response.TryGetValue("result", out osdtmp) || !(osdtmp is OSDMap)) + if (!response.TryGetValue("result", out osdtmp) || (osdtmp is not OSDMap resultmap)) { m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an invalid response: {2}", method, uri, OSDParser.SerializeJsonString(response)); return false; } - OSD.DeserializeMembers(ref parameters, (OSDMap)osdtmp); + OSD.DeserializeMembers(ref parameters, resultmap); return true; } @@ -147,16 +148,18 @@ namespace OpenSim.Framework.Servers.HttpServer 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 request = new() + { + { "jsonrpc", OSD.FromString("2.0") }, + { "id", OSD.FromString(jsonId) }, + { "method", OSD.FromString(method) }, + { "params", data } + }; - OSDMap response; + OSDMap outerresponse; try { - response = WebUtil.PostToService(uri, request, 10000, true); + outerresponse = WebUtil.PostToService(uri, request, 10000, true); } catch (Exception e) { @@ -165,13 +168,12 @@ namespace OpenSim.Framework.Servers.HttpServer } OSD osdtmp; - if (!response.TryGetValue("_Result", out osdtmp) || !(osdtmp is OSDMap)) + if (!outerresponse.TryGetValue("_Result", out osdtmp) || (osdtmp is not OSDMap response)) { m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an invalid response: {2}", - method, uri, OSDParser.SerializeJsonString(response)); + method, uri, OSDParser.SerializeJsonString(outerresponse)); return false; } - response = osdtmp as OSDMap; if (response.TryGetValue("error", out osdtmp)) { diff --git a/OpenSim/Region/CoreModules/Avatar/UserProfiles/UserProfileModule.cs b/OpenSim/Region/CoreModules/Avatar/UserProfiles/UserProfileModule.cs index 09156d71f2..d50e4ef77b 100644 --- a/OpenSim/Region/CoreModules/Avatar/UserProfiles/UserProfileModule.cs +++ b/OpenSim/Region/CoreModules/Avatar/UserProfiles/UserProfileModule.cs @@ -1870,71 +1870,7 @@ namespace OpenSim.Region.CoreModules.Avatar.UserProfiles /// bool JsonRpcRequest(ref object parameters, string method, string uri, string jsonId) { - if (jsonId is null) - throw new ArgumentNullException(nameof(jsonId)); - if (uri is null) - throw new ArgumentNullException(nameof(uri)); - if (method is null) - throw new ArgumentNullException(nameof(method)); - if (parameters is null) - throw new ArgumentNullException(nameof(parameters)); - - // Prep our payload - OSDMap json = new() - { - { "jsonrpc", OSD.FromString("2.0") }, - { "id", OSD.FromString(jsonId) }, - { "method", OSD.FromString(method) }, - { "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"; - - WebResponse webResponse; - try - { - using(Stream dataStream = webRequest.GetRequestStream()) - dataStream.Write(content,0,content.Length); - - webResponse = webRequest.GetResponse(); - } - catch (WebException e) - { - Console.WriteLine("Web Error" + e.Message); - Console.WriteLine ("Please check input"); - return false; - } - - OSDMap mret = new(); - - using (Stream rstream = webResponse.GetResponseStream()) - { - try - { - mret = (OSDMap)OSDParser.DeserializeJson(rstream); - } - catch (Exception e) - { - m_log.DebugFormat("[PROFILES]: JsonRpcRequest Error {0} - remote user with legacy profiles?", e.Message); - webResponse?.Close(); - return false; - } - } - - webResponse?.Close(); - - if (mret.ContainsKey("error")) - return false; - - // get params... - OSD.DeserializeMembers(ref parameters, (OSDMap) mret["result"]); - return true; + return rpc?.JsonRpcRequest(ref parameters, method, uri, jsonId) ?? false; } /// @@ -1957,66 +1893,7 @@ namespace OpenSim.Region.CoreModules.Avatar.UserProfiles /// bool JsonRpcRequest(ref OSD data, string method, string uri, string jsonId) { - OSDMap map = new() - { - ["jsonrpc"] = "2.0", - ["method"] = method, - ["params"] = data - }; - if (string.IsNullOrEmpty(jsonId)) - map["id"] = UUID.Random().ToString(); - else - map["id"] = jsonId; - - 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"; - - WebResponse webResponse; - try - { - using(Stream dataStream = webRequest.GetRequestStream()) - dataStream.Write(content,0,content.Length); - - webResponse = webRequest.GetResponse(); - } - catch (WebException e) - { - Console.WriteLine("Web Error" + e.Message); - Console.WriteLine ("Please check input"); - return false; - } - - OSDMap response = new(); - - using (Stream rstream = webResponse.GetResponseStream()) - { - try - { - response = (OSDMap)OSDParser.DeserializeJson(rstream); - } - catch (Exception e) - { - m_log.DebugFormat("[PROFILES]: JsonRpcRequest Error {0} - remote user with legacy profiles?", e.Message); - webResponse?.Close(); - return false; - } - } - - webResponse?.Close(); - - if(response.ContainsKey("error")) - { - data = response["error"]; - return false; - } - - data = response; - - return true; + return rpc?.JsonRpcRequest(ref data, method, uri, jsonId) ?? false; } #endregion Web Util }