more on HttpWebRequest in userprofiles

This commit is contained in:
UbitUmarov
2023-05-18 14:25:09 +01:00
parent f3663371dc
commit 3882ef3eb0
2 changed files with 30 additions and 151 deletions

View File

@@ -68,28 +68,30 @@ namespace OpenSim.Framework.Servers.HttpServer
/// </param>
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))
{

View File

@@ -1870,71 +1870,7 @@ namespace OpenSim.Region.CoreModules.Avatar.UserProfiles
/// </param>
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;
}
/// <summary>
@@ -1957,66 +1893,7 @@ namespace OpenSim.Region.CoreModules.Avatar.UserProfiles
/// </param>
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
}