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".
---
OpenSim/Data/MySQL/MySQLUserProfilesData.cs | 5 -
OpenSim/Data/SQLite/SQLiteUserProfilesData.cs | 6 -
.../Servers/HttpServer/JsonRpcRequestManager.cs | 175 +++++++++------------
OpenSim/Framework/WebUtil.cs | 24 +--
.../Simulation/SimulationServiceConnector.cs | 10 +-
5 files changed, 94 insertions(+), 126 deletions(-)
diff --git a/OpenSim/Data/MySQL/MySQLUserProfilesData.cs b/OpenSim/Data/MySQL/MySQLUserProfilesData.cs
index cc4c5b0..e301bbe 100644
--- a/OpenSim/Data/MySQL/MySQLUserProfilesData.cs
+++ b/OpenSim/Data/MySQL/MySQLUserProfilesData.cs
@@ -634,8 +634,6 @@ namespace OpenSim.Data.MySQL
{
if(reader.HasRows)
{
- m_log.DebugFormat("[PROFILES_DATA]" +
- ": Getting data for {0}.", props.UserId);
reader.Read();
props.WebUrl = (string)reader["profileURL"];
UUID.TryParse((string)reader["profileImage"], out props.ImageId);
@@ -651,9 +649,6 @@ namespace OpenSim.Data.MySQL
}
else
{
- m_log.DebugFormat("[PROFILES_DATA]" +
- ": No data for {0}", props.UserId);
-
props.WebUrl = string.Empty;
props.ImageId = UUID.Zero;
props.AboutText = string.Empty;
diff --git a/OpenSim/Data/SQLite/SQLiteUserProfilesData.cs b/OpenSim/Data/SQLite/SQLiteUserProfilesData.cs
index 0f5b4c8..5494091 100644
--- a/OpenSim/Data/SQLite/SQLiteUserProfilesData.cs
+++ b/OpenSim/Data/SQLite/SQLiteUserProfilesData.cs
@@ -584,9 +584,6 @@ namespace OpenSim.Data.SQLite
}
if(reader != null && reader.Read())
{
- m_log.DebugFormat("[PROFILES_DATA]" +
- ": Getting data for {0}.", props.UserId);
-
props.WebUrl = (string)reader["profileURL"];
UUID.TryParse((string)reader["profileImage"], out props.ImageId);
props.AboutText = (string)reader["profileAboutText"];
@@ -601,9 +598,6 @@ namespace OpenSim.Data.SQLite
}
else
{
- m_log.DebugFormat("[PROFILES_DATA]" +
- ": No data for {0}", props.UserId);
-
props.WebUrl = string.Empty;
props.ImageId = UUID.Zero;
props.AboutText = string.Empty;
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
+
}
}
diff --git a/OpenSim/Framework/WebUtil.cs b/OpenSim/Framework/WebUtil.cs
index 768ff16..8f5bc0c 100644
--- a/OpenSim/Framework/WebUtil.cs
+++ b/OpenSim/Framework/WebUtil.cs
@@ -127,41 +127,41 @@ namespace OpenSim.Framework
///
public static OSDMap PutToServiceCompressed(string url, OSDMap data, int timeout)
{
- return ServiceOSDRequest(url,data, "PUT", timeout, true);
+ return ServiceOSDRequest(url,data, "PUT", timeout, true, false);
}
public static OSDMap PutToService(string url, OSDMap data, int timeout)
{
- return ServiceOSDRequest(url,data, "PUT", timeout, false);
+ return ServiceOSDRequest(url,data, "PUT", timeout, false, false);
}
- public static OSDMap PostToService(string url, OSDMap data, int timeout)
+ public static OSDMap PostToService(string url, OSDMap data, int timeout, bool rpc)
{
- return ServiceOSDRequest(url, data, "POST", timeout, false);
+ return ServiceOSDRequest(url, data, "POST", timeout, false, rpc);
}
public static OSDMap PostToServiceCompressed(string url, OSDMap data, int timeout)
{
- return ServiceOSDRequest(url, data, "POST", timeout, true);
+ return ServiceOSDRequest(url, data, "POST", timeout, true, false);
}
public static OSDMap GetFromService(string url, int timeout)
{
- return ServiceOSDRequest(url, null, "GET", timeout, false);
+ return ServiceOSDRequest(url, null, "GET", timeout, false, false);
}
-
- public static OSDMap ServiceOSDRequest(string url, OSDMap data, string method, int timeout, bool compressed)
+
+ public static OSDMap ServiceOSDRequest(string url, OSDMap data, string method, int timeout, bool compressed, bool rpc)
{
if (SerializeOSDRequestsPerEndpoint)
{
lock (EndPointLock(url))
{
- return ServiceOSDRequestWorker(url, data, method, timeout, compressed);
+ return ServiceOSDRequestWorker(url, data, method, timeout, compressed, rpc);
}
}
else
{
- return ServiceOSDRequestWorker(url, data, method, timeout, compressed);
+ return ServiceOSDRequestWorker(url, data, method, timeout, compressed, rpc);
}
}
@@ -217,7 +217,7 @@ namespace OpenSim.Framework
LogOutgoingDetail("RESPONSE: ", input);
}
- private static OSDMap ServiceOSDRequestWorker(string url, OSDMap data, string method, int timeout, bool compressed)
+ private static OSDMap ServiceOSDRequestWorker(string url, OSDMap data, string method, int timeout, bool compressed, bool rpc)
{
int reqnum = RequestNumber++;
@@ -251,7 +251,7 @@ namespace OpenSim.Framework
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(strBuffer);
- request.ContentType = "application/json";
+ request.ContentType = rpc ? "application/json-rpc" : "application/json";
if (compressed)
{
diff --git a/OpenSim/Services/Connectors/Simulation/SimulationServiceConnector.cs b/OpenSim/Services/Connectors/Simulation/SimulationServiceConnector.cs
index a5520c4..d9fe5a0 100644
--- a/OpenSim/Services/Connectors/Simulation/SimulationServiceConnector.cs
+++ b/OpenSim/Services/Connectors/Simulation/SimulationServiceConnector.cs
@@ -136,7 +136,7 @@ namespace OpenSim.Services.Connectors.Simulation
}
// Try the old version, uncompressed
- result = WebUtil.PostToService(uri, args, 30000);
+ result = WebUtil.PostToService(uri, args, 30000, false);
if (result["Success"].AsBoolean())
{
@@ -302,7 +302,7 @@ namespace OpenSim.Services.Connectors.Simulation
try
{
- OSDMap result = WebUtil.ServiceOSDRequest(uri, request, "QUERYACCESS", 30000, false);
+ OSDMap result = WebUtil.ServiceOSDRequest(uri, request, "QUERYACCESS", 30000, false, false);
bool success = result["success"].AsBoolean();
if (result.ContainsKey("_Result"))
{
@@ -365,7 +365,7 @@ namespace OpenSim.Services.Connectors.Simulation
try
{
- WebUtil.ServiceOSDRequest(uri, null, "DELETE", 10000, false);
+ WebUtil.ServiceOSDRequest(uri, null, "DELETE", 10000, false, false);
}
catch (Exception e)
{
@@ -384,7 +384,7 @@ namespace OpenSim.Services.Connectors.Simulation
try
{
- WebUtil.ServiceOSDRequest(uri, null, "DELETE", 10000, false);
+ WebUtil.ServiceOSDRequest(uri, null, "DELETE", 10000, false, false);
}
catch (Exception e)
{
@@ -431,7 +431,7 @@ namespace OpenSim.Services.Connectors.Simulation
args["destination_name"] = OSD.FromString(destination.RegionName);
args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
- OSDMap result = WebUtil.PostToService(uri, args, 40000);
+ OSDMap result = WebUtil.PostToService(uri, args, 40000, false);
if (result == null)
return false;
--
cgit v1.1