From e41e52e09727842d990a31e2a5f7f3e9c88fe8b3 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 --- .../AuthenticationServerPostHandler.cs | 17 ++----- .../Handlers/Avatar/AvatarServerPostHandler.cs | 15 +----- .../Handlers/Friends/FriendsServerPostHandler.cs | 15 +----- .../Server/Handlers/Grid/GridServerPostHandler.cs | 15 +----- .../Handlers/GridUser/GridUserServerPostHandler.cs | 16 +------ .../Hypergrid/HGFriendsServerPostHandler.cs | 20 ++------ .../Handlers/Inventory/XInventoryInConnector.cs | 13 +----- .../Server/Handlers/Map/MapAddServerConnector.cs | 15 +----- .../Handlers/Presence/PresenceServerPostHandler.cs | 15 +----- .../Server/Handlers/Simulation/AgentHandlers.cs | 54 +++++++++++++++++----- .../UserAccounts/UserAccountServerPostHandler.cs | 15 +----- 11 files changed, 65 insertions(+), 145 deletions(-) (limited to 'OpenSim/Server/Handlers') diff --git a/OpenSim/Server/Handlers/Authentication/AuthenticationServerPostHandler.cs b/OpenSim/Server/Handlers/Authentication/AuthenticationServerPostHandler.cs index 5d65f67..6ee98b3 100644 --- a/OpenSim/Server/Handlers/Authentication/AuthenticationServerPostHandler.cs +++ b/OpenSim/Server/Handlers/Authentication/AuthenticationServerPostHandler.cs @@ -209,7 +209,7 @@ namespace OpenSim.Server.Handlers.Authentication rootElement.AppendChild(result); - return DocToBytes(doc); + return Util.DocToBytes(doc); } byte[] GetAuthInfo(UUID principalID) @@ -279,7 +279,7 @@ namespace OpenSim.Server.Handlers.Authentication rootElement.AppendChild(result); - return DocToBytes(doc); + return Util.DocToBytes(doc); } private byte[] SuccessResult(string token) @@ -306,18 +306,7 @@ namespace OpenSim.Server.Handlers.Authentication rootElement.AppendChild(t); - return DocToBytes(doc); - } - - private byte[] DocToBytes(XmlDocument doc) - { - MemoryStream ms = new MemoryStream(); - XmlTextWriter xw = new XmlTextWriter(ms, null); - xw.Formatting = Formatting.Indented; - doc.WriteTo(xw); - xw.Flush(); - - return ms.GetBuffer(); + return Util.DocToBytes(doc); } private byte[] ResultToBytes(Dictionary result) diff --git a/OpenSim/Server/Handlers/Avatar/AvatarServerPostHandler.cs b/OpenSim/Server/Handlers/Avatar/AvatarServerPostHandler.cs index 59dbed4..ff8699f 100644 --- a/OpenSim/Server/Handlers/Avatar/AvatarServerPostHandler.cs +++ b/OpenSim/Server/Handlers/Avatar/AvatarServerPostHandler.cs @@ -247,7 +247,7 @@ namespace OpenSim.Server.Handlers.Avatar rootElement.AppendChild(result); - return DocToBytes(doc); + return Util.DocToBytes(doc); } private byte[] FailureResult() @@ -269,18 +269,7 @@ namespace OpenSim.Server.Handlers.Avatar rootElement.AppendChild(result); - return DocToBytes(doc); - } - - private byte[] DocToBytes(XmlDocument doc) - { - MemoryStream ms = new MemoryStream(); - XmlTextWriter xw = new XmlTextWriter(ms, null); - xw.Formatting = Formatting.Indented; - doc.WriteTo(xw); - xw.Flush(); - - return ms.ToArray(); + return Util.DocToBytes(doc); } } diff --git a/OpenSim/Server/Handlers/Friends/FriendsServerPostHandler.cs b/OpenSim/Server/Handlers/Friends/FriendsServerPostHandler.cs index d442443..3aab30b 100644 --- a/OpenSim/Server/Handlers/Friends/FriendsServerPostHandler.cs +++ b/OpenSim/Server/Handlers/Friends/FriendsServerPostHandler.cs @@ -229,7 +229,7 @@ namespace OpenSim.Server.Handlers.Friends rootElement.AppendChild(result); - return DocToBytes(doc); + return Util.DocToBytes(doc); } private byte[] FailureResult() @@ -261,18 +261,7 @@ namespace OpenSim.Server.Handlers.Friends rootElement.AppendChild(message); - return DocToBytes(doc); - } - - private byte[] DocToBytes(XmlDocument doc) - { - MemoryStream ms = new MemoryStream(); - XmlTextWriter xw = new XmlTextWriter(ms, null); - xw.Formatting = Formatting.Indented; - doc.WriteTo(xw); - xw.Flush(); - - return ms.ToArray(); + return Util.DocToBytes(doc); } void FromKeyValuePairs(Dictionary kvp, out string principalID, out string friend, out int flags) diff --git a/OpenSim/Server/Handlers/Grid/GridServerPostHandler.cs b/OpenSim/Server/Handlers/Grid/GridServerPostHandler.cs index dda4756..d5a9d67 100644 --- a/OpenSim/Server/Handlers/Grid/GridServerPostHandler.cs +++ b/OpenSim/Server/Handlers/Grid/GridServerPostHandler.cs @@ -602,7 +602,7 @@ namespace OpenSim.Server.Handlers.Grid rootElement.AppendChild(result); - return DocToBytes(doc); + return Util.DocToBytes(doc); } private byte[] FailureResult() @@ -634,18 +634,7 @@ namespace OpenSim.Server.Handlers.Grid rootElement.AppendChild(message); - return DocToBytes(doc); - } - - private byte[] DocToBytes(XmlDocument doc) - { - MemoryStream ms = new MemoryStream(); - XmlTextWriter xw = new XmlTextWriter(ms, null); - xw.Formatting = Formatting.Indented; - doc.WriteTo(xw); - xw.Flush(); - - return ms.ToArray(); + return Util.DocToBytes(doc); } #endregion diff --git a/OpenSim/Server/Handlers/GridUser/GridUserServerPostHandler.cs b/OpenSim/Server/Handlers/GridUser/GridUserServerPostHandler.cs index 006f6ab..9237c63 100644 --- a/OpenSim/Server/Handlers/GridUser/GridUserServerPostHandler.cs +++ b/OpenSim/Server/Handlers/GridUser/GridUserServerPostHandler.cs @@ -279,7 +279,7 @@ namespace OpenSim.Server.Handlers.GridUser rootElement.AppendChild(result); - return DocToBytes(doc); + return Util.DocToBytes(doc); } private byte[] FailureResult() @@ -301,20 +301,8 @@ namespace OpenSim.Server.Handlers.GridUser rootElement.AppendChild(result); - return DocToBytes(doc); + return Util.DocToBytes(doc); } - private byte[] DocToBytes(XmlDocument doc) - { - MemoryStream ms = new MemoryStream(); - XmlTextWriter xw = new XmlTextWriter(ms, null); - xw.Formatting = Formatting.Indented; - doc.WriteTo(xw); - xw.Flush(); - - return ms.ToArray(); - } - - } } diff --git a/OpenSim/Server/Handlers/Hypergrid/HGFriendsServerPostHandler.cs b/OpenSim/Server/Handlers/Hypergrid/HGFriendsServerPostHandler.cs index a2bdadb..37b47ed 100644 --- a/OpenSim/Server/Handlers/Hypergrid/HGFriendsServerPostHandler.cs +++ b/OpenSim/Server/Handlers/Hypergrid/HGFriendsServerPostHandler.cs @@ -335,7 +335,7 @@ namespace OpenSim.Server.Handlers.Hypergrid rootElement.AppendChild(result); - return DocToBytes(doc); + return Util.DocToBytes(doc); } private byte[] SuccessResult(string value) @@ -362,7 +362,7 @@ namespace OpenSim.Server.Handlers.Hypergrid rootElement.AppendChild(message); - return DocToBytes(doc); + return Util.DocToBytes(doc); } @@ -395,7 +395,7 @@ namespace OpenSim.Server.Handlers.Hypergrid rootElement.AppendChild(message); - return DocToBytes(doc); + return Util.DocToBytes(doc); } private byte[] BoolResult(bool value) @@ -417,21 +417,9 @@ namespace OpenSim.Server.Handlers.Hypergrid rootElement.AppendChild(result); - return DocToBytes(doc); + return Util.DocToBytes(doc); } - private byte[] DocToBytes(XmlDocument doc) - { - MemoryStream ms = new MemoryStream(); - XmlTextWriter xw = new XmlTextWriter(ms, null); - xw.Formatting = Formatting.Indented; - doc.WriteTo(xw); - xw.Flush(); - - return ms.ToArray(); - } - - #endregion } } diff --git a/OpenSim/Server/Handlers/Inventory/XInventoryInConnector.cs b/OpenSim/Server/Handlers/Inventory/XInventoryInConnector.cs index 7283237..0288fa6 100644 --- a/OpenSim/Server/Handlers/Inventory/XInventoryInConnector.cs +++ b/OpenSim/Server/Handlers/Inventory/XInventoryInConnector.cs @@ -191,18 +191,7 @@ namespace OpenSim.Server.Handlers.Asset rootElement.AppendChild(result); - return DocToBytes(doc); - } - - private byte[] DocToBytes(XmlDocument doc) - { - MemoryStream ms = new MemoryStream(); - XmlTextWriter xw = new XmlTextWriter(ms, null); - xw.Formatting = Formatting.Indented; - doc.WriteTo(xw); - xw.Flush(); - - return ms.ToArray(); + return Util.DocToBytes(doc); } byte[] HandleCreateUserInventory(Dictionary request) diff --git a/OpenSim/Server/Handlers/Map/MapAddServerConnector.cs b/OpenSim/Server/Handlers/Map/MapAddServerConnector.cs index a896fdb..649a27e 100644 --- a/OpenSim/Server/Handlers/Map/MapAddServerConnector.cs +++ b/OpenSim/Server/Handlers/Map/MapAddServerConnector.cs @@ -188,7 +188,7 @@ namespace OpenSim.Server.Handlers.MapImage rootElement.AppendChild(result); - return DocToBytes(doc); + return Util.DocToBytes(doc); } private byte[] FailureResult(string msg) @@ -215,18 +215,7 @@ namespace OpenSim.Server.Handlers.MapImage rootElement.AppendChild(message); - return DocToBytes(doc); - } - - private byte[] DocToBytes(XmlDocument doc) - { - MemoryStream ms = new MemoryStream(); - XmlTextWriter xw = new XmlTextWriter(ms, null); - xw.Formatting = Formatting.Indented; - doc.WriteTo(xw); - xw.Flush(); - - return ms.ToArray(); + return Util.DocToBytes(doc); } private System.Net.IPAddress GetCallerIP(IOSHttpRequest request) diff --git a/OpenSim/Server/Handlers/Presence/PresenceServerPostHandler.cs b/OpenSim/Server/Handlers/Presence/PresenceServerPostHandler.cs index 0b3b961..49dbcb5 100644 --- a/OpenSim/Server/Handlers/Presence/PresenceServerPostHandler.cs +++ b/OpenSim/Server/Handlers/Presence/PresenceServerPostHandler.cs @@ -265,7 +265,7 @@ namespace OpenSim.Server.Handlers.Presence rootElement.AppendChild(result); - return DocToBytes(doc); + return Util.DocToBytes(doc); } private byte[] FailureResult() @@ -287,18 +287,7 @@ namespace OpenSim.Server.Handlers.Presence rootElement.AppendChild(result); - return DocToBytes(doc); - } - - private byte[] DocToBytes(XmlDocument doc) - { - MemoryStream ms = new MemoryStream(); - XmlTextWriter xw = new XmlTextWriter(ms, null); - xw.Formatting = Formatting.Indented; - doc.WriteTo(xw); - xw.Flush(); - - return ms.ToArray(); + return Util.DocToBytes(doc); } } diff --git a/OpenSim/Server/Handlers/Simulation/AgentHandlers.cs b/OpenSim/Server/Handlers/Simulation/AgentHandlers.cs index 6d3a3a7..da2bfeb 100644 --- a/OpenSim/Server/Handlers/Simulation/AgentHandlers.cs +++ b/OpenSim/Server/Handlers/Simulation/AgentHandlers.cs @@ -250,14 +250,30 @@ namespace OpenSim.Server.Handlers.Simulation return encoding.GetBytes("false"); } + string requestBody; + Stream inputStream = request; - if ((httpRequest.ContentType == "application/x-gzip" || httpRequest.Headers["Content-Encoding"] == "gzip") || (httpRequest.Headers["X-Content-Encoding"] == "gzip")) - inputStream = new GZipStream(inputStream, CompressionMode.Decompress); + Stream innerStream = null; + try + { + if ((httpRequest.ContentType == "application/x-gzip" || httpRequest.Headers["Content-Encoding"] == "gzip") || (httpRequest.Headers["X-Content-Encoding"] == "gzip")) + { + innerStream = inputStream; + inputStream = new GZipStream(innerStream, CompressionMode.Decompress); + } - StreamReader reader = new StreamReader(inputStream, encoding); + using (StreamReader reader = new StreamReader(inputStream, encoding)) + { + requestBody = reader.ReadToEnd(); + } + } + finally + { + if (innerStream != null) + innerStream.Dispose(); + inputStream.Dispose(); + } - string requestBody = reader.ReadToEnd(); - reader.Close(); keysvals.Add("body", requestBody); Hashtable responsedata = new Hashtable(); @@ -461,15 +477,31 @@ namespace OpenSim.Server.Handlers.Simulation keysvals.Add("headers", headervals); keysvals.Add("querystringkeys", querystringkeys); + String requestBody; + Encoding encoding = Encoding.UTF8; + Stream inputStream = request; - if ((httpRequest.ContentType == "application/x-gzip" || httpRequest.Headers["Content-Encoding"] == "gzip") || (httpRequest.Headers["X-Content-Encoding"] == "gzip")) - inputStream = new GZipStream(inputStream, CompressionMode.Decompress); + Stream innerStream = null; + try + { + if ((httpRequest.ContentType == "application/x-gzip" || httpRequest.Headers["Content-Encoding"] == "gzip") || (httpRequest.Headers["X-Content-Encoding"] == "gzip")) + { + innerStream = inputStream; + inputStream = new GZipStream(innerStream, CompressionMode.Decompress); + } - Encoding encoding = Encoding.UTF8; - StreamReader reader = new StreamReader(inputStream, encoding); + using (StreamReader reader = new StreamReader(inputStream, encoding)) + { + requestBody = reader.ReadToEnd(); + } + } + finally + { + if (innerStream != null) + innerStream.Dispose(); + inputStream.Dispose(); + } - string requestBody = reader.ReadToEnd(); - reader.Close(); keysvals.Add("body", requestBody); httpResponse.StatusCode = 200; diff --git a/OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs b/OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs index c87e022..a77d78e 100644 --- a/OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs +++ b/OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs @@ -315,7 +315,7 @@ namespace OpenSim.Server.Handlers.UserAccounts rootElement.AppendChild(result); - return DocToBytes(doc); + return Util.DocToBytes(doc); } private byte[] FailureResult() @@ -337,18 +337,7 @@ namespace OpenSim.Server.Handlers.UserAccounts rootElement.AppendChild(result); - return DocToBytes(doc); - } - - private byte[] DocToBytes(XmlDocument doc) - { - MemoryStream ms = new MemoryStream(); - XmlTextWriter xw = new XmlTextWriter(ms, null); - xw.Formatting = Formatting.Indented; - doc.WriteTo(xw); - xw.Flush(); - - return ms.ToArray(); + return Util.DocToBytes(doc); } private byte[] ResultToBytes(Dictionary result) -- cgit v1.1