From 99ac770abbe3a95887c4b10c82f3985aa878eeef Mon Sep 17 00:00:00 2001 From: Oren Hurvitz Date: Sun, 1 Jun 2014 17:39:11 +0300 Subject: Close streams immediately when we finish using them --- .../Framework/Servers/HttpServer/BaseHttpServer.cs | 94 ++++++++++++++-------- .../Servers/HttpServer/RestObjectPoster.cs | 28 ++++--- .../Servers/HttpServer/RestObjectPosterResponse.cs | 29 +++---- .../Servers/HttpServer/RestSessionService.cs | 61 +++++++------- 4 files changed, 119 insertions(+), 93 deletions(-) (limited to 'OpenSim/Framework/Servers/HttpServer') diff --git a/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs b/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs index b92c25b..093855c 100644 --- a/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs +++ b/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs @@ -832,28 +832,40 @@ namespace OpenSim.Framework.Servers.HttpServer return; // never log these; they're just binary data Stream inputStream = Util.Copy(request.InputStream); - - if ((request.Headers["Content-Encoding"] == "gzip") || (request.Headers["X-Content-Encoding"] == "gzip")) - inputStream = new GZipStream(inputStream, System.IO.Compression.CompressionMode.Decompress); - - using (StreamReader reader = new StreamReader(inputStream, Encoding.UTF8)) + Stream innerStream = null; + try { - string output; - - if (DebugLevel == 5) + if ((request.Headers["Content-Encoding"] == "gzip") || (request.Headers["X-Content-Encoding"] == "gzip")) { - char[] chars = new char[WebUtil.MaxRequestDiagLength + 1]; // +1 so we know to add "..." only if needed - int len = reader.Read(chars, 0, WebUtil.MaxRequestDiagLength + 1); - output = new string(chars, 0, Math.Min(len, WebUtil.MaxRequestDiagLength)); - if (len > WebUtil.MaxRequestDiagLength) - output += "..."; + innerStream = inputStream; + inputStream = new GZipStream(innerStream, System.IO.Compression.CompressionMode.Decompress); } - else + + using (StreamReader reader = new StreamReader(inputStream, Encoding.UTF8)) { - output = reader.ReadToEnd(); - } + string output; - m_log.DebugFormat("[LOGHTTP] {0}", Util.BinaryToASCII(output)); + if (DebugLevel == 5) + { + char[] chars = new char[WebUtil.MaxRequestDiagLength + 1]; // +1 so we know to add "..." only if needed + int len = reader.Read(chars, 0, WebUtil.MaxRequestDiagLength + 1); + output = new string(chars, 0, Math.Min(len, WebUtil.MaxRequestDiagLength)); + if (len > WebUtil.MaxRequestDiagLength) + output += "..."; + } + else + { + output = reader.ReadToEnd(); + } + + m_log.DebugFormat("[LOGHTTP] {0}", Util.BinaryToASCII(output)); + } + } + finally + { + if (innerStream != null) + innerStream.Dispose(); + inputStream.Dispose(); } } @@ -981,19 +993,33 @@ namespace OpenSim.Framework.Servers.HttpServer /// private byte[] HandleXmlRpcRequests(OSHttpRequest request, OSHttpResponse response) { - Stream requestStream = request.InputStream; + String requestBody; - if ((request.Headers["Content-Encoding"] == "gzip") || (request.Headers["X-Content-Encoding"] == "gzip")) - requestStream = new GZipStream(requestStream, System.IO.Compression.CompressionMode.Decompress); + Stream requestStream = request.InputStream; + Stream innerStream = null; + try + { + if ((request.Headers["Content-Encoding"] == "gzip") || (request.Headers["X-Content-Encoding"] == "gzip")) + { + innerStream = requestStream; + requestStream = new GZipStream(innerStream, System.IO.Compression.CompressionMode.Decompress); + } - Encoding encoding = Encoding.UTF8; - StreamReader reader = new StreamReader(requestStream, encoding); + using (StreamReader reader = new StreamReader(requestStream, Encoding.UTF8)) + { + requestBody = reader.ReadToEnd(); + } + } + finally + { + if (innerStream != null) + innerStream.Dispose(); + requestStream.Dispose(); + } - string requestBody = reader.ReadToEnd(); - reader.Close(); - requestStream.Close(); //m_log.Debug(requestBody); requestBody = requestBody.Replace("", ""); + string responseString = String.Empty; XmlRpcRequest xmlRprcRequest = null; @@ -1089,18 +1115,16 @@ namespace OpenSim.Framework.Servers.HttpServer response.ContentType = "text/xml"; using (MemoryStream outs = new MemoryStream()) + using (XmlTextWriter writer = new XmlTextWriter(outs, Encoding.UTF8)) { - using (XmlTextWriter writer = new XmlTextWriter(outs, Encoding.UTF8)) + writer.Formatting = Formatting.None; + XmlRpcResponseSerializer.Singleton.Serialize(writer, xmlRpcResponse); + writer.Flush(); + outs.Flush(); + outs.Position = 0; + using (StreamReader sr = new StreamReader(outs)) { - writer.Formatting = Formatting.None; - XmlRpcResponseSerializer.Singleton.Serialize(writer, xmlRpcResponse); - writer.Flush(); - outs.Flush(); - outs.Position = 0; - using (StreamReader sr = new StreamReader(outs)) - { - responseString = sr.ReadToEnd(); - } + responseString = sr.ReadToEnd(); } } } diff --git a/OpenSim/Framework/Servers/HttpServer/RestObjectPoster.cs b/OpenSim/Framework/Servers/HttpServer/RestObjectPoster.cs index 48ced19..afe052f 100644 --- a/OpenSim/Framework/Servers/HttpServer/RestObjectPoster.cs +++ b/OpenSim/Framework/Servers/HttpServer/RestObjectPoster.cs @@ -52,23 +52,25 @@ namespace OpenSim.Framework.Servers.HttpServer request.Method = verb; request.ContentType = "text/xml"; - MemoryStream buffer = new MemoryStream(); + using (MemoryStream buffer = new MemoryStream()) + { + XmlWriterSettings settings = new XmlWriterSettings(); + settings.Encoding = Encoding.UTF8; - XmlWriterSettings settings = new XmlWriterSettings(); - settings.Encoding = Encoding.UTF8; + using (XmlWriter writer = XmlWriter.Create(buffer, settings)) + { + XmlSerializer serializer = new XmlSerializer(type); + serializer.Serialize(writer, obj); + writer.Flush(); + } - using (XmlWriter writer = XmlWriter.Create(buffer, settings)) - { - XmlSerializer serializer = new XmlSerializer(type); - serializer.Serialize(writer, obj); - writer.Flush(); - } + int length = (int)buffer.Length; + request.ContentLength = length; - int length = (int) buffer.Length; - request.ContentLength = length; + using (Stream requestStream = request.GetRequestStream()) + requestStream.Write(buffer.ToArray(), 0, length); + } - Stream requestStream = request.GetRequestStream(); - requestStream.Write(buffer.ToArray(), 0, length); // IAsyncResult result = request.BeginGetResponse(AsyncCallback, request); request.BeginGetResponse(AsyncCallback, request); } diff --git a/OpenSim/Framework/Servers/HttpServer/RestObjectPosterResponse.cs b/OpenSim/Framework/Servers/HttpServer/RestObjectPosterResponse.cs index 451745c..a911b9f 100644 --- a/OpenSim/Framework/Servers/HttpServer/RestObjectPosterResponse.cs +++ b/OpenSim/Framework/Servers/HttpServer/RestObjectPosterResponse.cs @@ -60,24 +60,25 @@ namespace OpenSim.Framework.Servers.HttpServer request.ContentType = "text/xml"; request.Timeout = 10000; - MemoryStream buffer = new MemoryStream(); + using (MemoryStream buffer = new MemoryStream()) + { + XmlWriterSettings settings = new XmlWriterSettings(); + settings.Encoding = Encoding.UTF8; + + using (XmlWriter writer = XmlWriter.Create(buffer, settings)) + { + XmlSerializer serializer = new XmlSerializer(type); + serializer.Serialize(writer, obj); + writer.Flush(); + } - XmlWriterSettings settings = new XmlWriterSettings(); - settings.Encoding = Encoding.UTF8; + int length = (int)buffer.Length; + request.ContentLength = length; - using (XmlWriter writer = XmlWriter.Create(buffer, settings)) - { - XmlSerializer serializer = new XmlSerializer(type); - serializer.Serialize(writer, obj); - writer.Flush(); + using (Stream requestStream = request.GetRequestStream()) + requestStream.Write(buffer.ToArray(), 0, length); } - int length = (int) buffer.Length; - request.ContentLength = length; - - Stream requestStream = request.GetRequestStream(); - requestStream.Write(buffer.ToArray(), 0, length); - requestStream.Close(); // IAsyncResult result = request.BeginGetResponse(AsyncCallback, request); request.BeginGetResponse(AsyncCallback, request); } diff --git a/OpenSim/Framework/Servers/HttpServer/RestSessionService.cs b/OpenSim/Framework/Servers/HttpServer/RestSessionService.cs index 83c9848..ad69cd2 100644 --- a/OpenSim/Framework/Servers/HttpServer/RestSessionService.cs +++ b/OpenSim/Framework/Servers/HttpServer/RestSessionService.cs @@ -77,25 +77,24 @@ namespace OpenSim.Framework.Servers.HttpServer request.ContentType = "text/xml"; request.Timeout = 20000; - MemoryStream buffer = new MemoryStream(); - - XmlWriterSettings settings = new XmlWriterSettings(); - settings.Encoding = Encoding.UTF8; - - using (XmlWriter writer = XmlWriter.Create(buffer, settings)) + using (MemoryStream buffer = new MemoryStream()) { - XmlSerializer serializer = new XmlSerializer(type); - serializer.Serialize(writer, sobj); - writer.Flush(); - } + XmlWriterSettings settings = new XmlWriterSettings(); + settings.Encoding = Encoding.UTF8; - int length = (int)buffer.Length; - request.ContentLength = length; + using (XmlWriter writer = XmlWriter.Create(buffer, settings)) + { + XmlSerializer serializer = new XmlSerializer(type); + serializer.Serialize(writer, sobj); + writer.Flush(); + } + + int length = (int)buffer.Length; + request.ContentLength = length; - Stream requestStream = request.GetRequestStream(); - requestStream.Write(buffer.ToArray(), 0, length); - buffer.Close(); - requestStream.Close(); + using (Stream requestStream = request.GetRequestStream()) + requestStream.Write(buffer.ToArray(), 0, length); + } TResponse deserial = default(TResponse); using (WebResponse resp = request.GetResponse()) @@ -133,25 +132,25 @@ namespace OpenSim.Framework.Servers.HttpServer request.ContentType = "text/xml"; request.Timeout = 10000; - MemoryStream buffer = new MemoryStream(); + using (MemoryStream buffer = new MemoryStream()) + { + XmlWriterSettings settings = new XmlWriterSettings(); + settings.Encoding = Encoding.UTF8; + + using (XmlWriter writer = XmlWriter.Create(buffer, settings)) + { + XmlSerializer serializer = new XmlSerializer(type); + serializer.Serialize(writer, sobj); + writer.Flush(); + } - XmlWriterSettings settings = new XmlWriterSettings(); - settings.Encoding = Encoding.UTF8; + int length = (int)buffer.Length; + request.ContentLength = length; - using (XmlWriter writer = XmlWriter.Create(buffer, settings)) - { - XmlSerializer serializer = new XmlSerializer(type); - serializer.Serialize(writer, sobj); - writer.Flush(); + using (Stream requestStream = request.GetRequestStream()) + requestStream.Write(buffer.ToArray(), 0, length); } - buffer.Close(); - - int length = (int)buffer.Length; - request.ContentLength = length; - Stream requestStream = request.GetRequestStream(); - requestStream.Write(buffer.ToArray(), 0, length); - requestStream.Close(); // IAsyncResult result = request.BeginGetResponse(AsyncCallback, request); request.BeginGetResponse(AsyncCallback, request); } -- cgit v1.1