xmlrpc http hander: remove another temporary string

This commit is contained in:
UbitUmarov
2020-12-01 13:17:54 +00:00
parent ff1302f8c8
commit 3e113b4aec
3 changed files with 51 additions and 92 deletions

View File

@@ -1153,8 +1153,6 @@ namespace OpenSim.Framework.Servers.HttpServer
/// <param name="response"></param>
public void HandleXmlRpcRequests(OSHttpRequest request, OSHttpResponse response)
{
String requestBody = null;
Stream requestStream = request.InputStream;
Stream innerStream = null;
try
@@ -1162,59 +1160,45 @@ namespace OpenSim.Framework.Servers.HttpServer
if ((request.Headers["Content-Encoding"] == "gzip") || (request.Headers["X-Content-Encoding"] == "gzip"))
{
innerStream = requestStream;
requestStream = new GZipStream(innerStream, System.IO.Compression.CompressionMode.Decompress);
requestStream = new GZipStream(innerStream, CompressionMode.Decompress);
}
using (StreamReader reader = new StreamReader(requestStream, Encoding.UTF8))
requestBody = reader.ReadToEnd();
}
catch
{
requestBody = null;
}
finally
{
if (innerStream != null && innerStream.CanRead)
innerStream.Dispose();
if (requestStream.CanRead)
requestStream.Dispose();
}
if (innerStream != null && innerStream.CanRead)
innerStream.Dispose();
if (string.IsNullOrWhiteSpace(requestBody))
{
response.StatusCode = (int)HttpStatusCode.NotFound;
response.StatusCode = (int)HttpStatusCode.BadRequest;
response.KeepAlive = false;
return;
}
//m_log.Debug(requestBody);
requestBody = requestBody.Replace("<base64></base64>", "");
XmlRpcRequest xmlRprcRequest = null;
try
{
xmlRprcRequest = (XmlRpcRequest) (new XmlRpcRequestDeserializer()).Deserialize(requestBody);
}
catch (XmlException e)
{
if (DebugLevel >= 1)
using (StreamReader reader = new StreamReader(requestStream, Encoding.UTF8))
{
if (DebugLevel >= 2)
m_log.Warn(
string.Format(
"[BASE HTTP SERVER]: Got XMLRPC request with invalid XML from {0}. XML was '{1}'. Sending blank response. Exception ",
request.RemoteIPEndPoint, requestBody),
e);
else
{
m_log.WarnFormat(
"[BASE HTTP SERVER]: Got XMLRPC request with invalid XML from {0}, length {1}. Sending blank response.",
request.RemoteIPEndPoint, requestBody.Length);
}
var xmlDes = new XmlRpcRequestDeserializer();
xmlRprcRequest = (XmlRpcRequest)xmlDes.Deserialize(reader);
}
}
catch (Exception e)
{
m_log.WarnFormat(
"[BASE HTTP SERVER]: Fail to decode XMLRPC request {0}: {1}",
request.RemoteIPEndPoint, e.Message);
}
finally
{
if (requestStream.CanRead)
requestStream.Dispose();
if (innerStream != null && innerStream.CanRead)
innerStream.Dispose();
}
if(xmlRprcRequest == null)
if (xmlRprcRequest == null)
{
response.StatusCode = (int)HttpStatusCode.NotFound;
response.KeepAlive = false;
@@ -1298,32 +1282,26 @@ namespace OpenSim.Framework.Servers.HttpServer
String.Format("Requested method [{0}] not found", methodName));
}
response.KeepAlive = keepAlive;
response.ContentType = "text/xml";
string responseString = String.Empty;
using (MemoryStream outs = new MemoryStream())
using (MemoryStream outs = new MemoryStream(64 * 1024))
{
using (XmlTextWriter writer = new XmlTextWriter(outs, UTF8NoBOM))
{
writer.Formatting = Formatting.None;
XmlRpcResponseSerializer.Singleton.Serialize(writer, xmlRpcResponse);
var xmlrpcSer = new XmlRpcResponseSerializer();
xmlrpcSer.Serialize(writer, xmlRpcResponse);
writer.Flush();
//outs.Seek(0, SeekOrigin.Begin);
//using (StreamReader sr = new StreamReader(outs))
// responseString = sr.ReadToEnd();
response.RawBuffer = outs.GetBuffer();
response.RawBufferLen = (int)outs.Length;
}
}
//response.RawBuffer = Util.UTF8NBGetbytes(responseString);
response.StatusCode = (int)HttpStatusCode.OK;
response.KeepAlive = keepAlive;
response.ContentType = "text/xml";
}
public void HandleXmlRpcRequests(OSHttpRequest request, OSHttpResponse response, Dictionary<string, XmlRpcMethod> rpcHandlers)
{
String requestBody = null;
Stream requestStream = request.InputStream;
Stream innerStream = null;
try
@@ -1331,57 +1309,43 @@ namespace OpenSim.Framework.Servers.HttpServer
if ((request.Headers["Content-Encoding"] == "gzip") || (request.Headers["X-Content-Encoding"] == "gzip"))
{
innerStream = requestStream;
requestStream = new GZipStream(innerStream, System.IO.Compression.CompressionMode.Decompress);
requestStream = new GZipStream(innerStream, CompressionMode.Decompress);
}
using (StreamReader reader = new StreamReader(requestStream, Encoding.UTF8))
requestBody = reader.ReadToEnd();
}
catch
{
requestBody = null;
}
finally
{
if (innerStream != null && innerStream.CanRead)
innerStream.Dispose();
if (requestStream.CanRead)
requestStream.Dispose();
}
if (innerStream != null && innerStream.CanRead)
innerStream.Dispose();
if (string.IsNullOrWhiteSpace(requestBody))
{
response.StatusCode = (int)HttpStatusCode.NotFound;
response.StatusCode = (int)HttpStatusCode.BadRequest;
response.KeepAlive = false;
return;
}
//m_log.Debug(requestBody);
requestBody = requestBody.Replace("<base64></base64>", "");
XmlRpcRequest xmlRprcRequest = null;
try
{
xmlRprcRequest = (XmlRpcRequest)(new XmlRpcRequestDeserializer()).Deserialize(requestBody);
}
catch (XmlException e)
{
if (DebugLevel >= 1)
using (StreamReader reader = new StreamReader(requestStream, Encoding.UTF8))
{
if (DebugLevel >= 2)
m_log.Warn(
string.Format(
"[BASE HTTP SERVER]: Got XMLRPC request with invalid XML from {0}. XML was '{1}'. Sending blank response. Exception ",
request.RemoteIPEndPoint, requestBody),
e);
else
{
m_log.WarnFormat(
"[BASE HTTP SERVER]: Got XMLRPC request with invalid XML from {0}, length {1}. Sending blank response.",
request.RemoteIPEndPoint, requestBody.Length);
}
var xmlDes = new XmlRpcRequestDeserializer();
xmlRprcRequest = (XmlRpcRequest)xmlDes.Deserialize(reader);
}
}
catch (Exception e)
{
m_log.WarnFormat(
"[BASE HTTP SERVER]: Fail to decode XMLRPC request {0}: {1}",
request.RemoteIPEndPoint, e.Message);
}
finally
{
if (requestStream.CanRead)
requestStream.Dispose();
if (innerStream != null && innerStream.CanRead)
innerStream.Dispose();
}
if (xmlRprcRequest == null)
{
@@ -1437,8 +1401,7 @@ namespace OpenSim.Framework.Servers.HttpServer
}
catch (Exception e)
{
string errorMessage
= String.Format(
string errorMessage = string.Format(
"Requested method [{0}] from {1} threw exception: {2} {3}",
methodName, request.RemoteIPEndPoint.Address, e.Message, e.StackTrace);
@@ -1461,26 +1424,22 @@ namespace OpenSim.Framework.Servers.HttpServer
String.Format("Requested method [{0}] not found", methodName));
}
string responseString = String.Empty;
using (MemoryStream outs = new MemoryStream(64 * 1024))
{
using (XmlTextWriter writer = new XmlTextWriter(outs, UTF8NoBOM))
{
writer.Formatting = Formatting.None;
XmlRpcResponseSerializer.Singleton.Serialize(writer, xmlRpcResponse);
var xmlrpcSer = new XmlRpcResponseSerializer();
xmlrpcSer.Serialize(writer, xmlRpcResponse);
writer.Flush();
//outs.Seek(0, SeekOrigin.Begin);
//using (StreamReader sr = new StreamReader(outs))
// responseString = sr.ReadToEnd();
response.RawBuffer = outs.GetBuffer();
response.RawBufferLen = (int)outs.Length;
}
}
response.StatusCode = (int)HttpStatusCode.OK;
response.ContentType = "text/xml";
response.KeepAlive = false;
//response.RawBuffer = Util.UTF8NBGetbytes(responseString);
response.ContentType = "text/xml";
}
// JsonRpc (v2.0 only)