From 80c19b7cac52a57fd04966169c657400aeee3de8 Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Wed, 27 Feb 2013 00:21:02 +0000 Subject: Make sure we dispose of WebResponse, StreamReader and Stream in various places where we were not already. --- .../Configuration/HTTP/HTTPConfiguration.cs | 32 +++++++----- .../RegionLoader/Web/RegionLoaderWebServer.cs | 41 ++++++++++----- .../Servers/HttpServer/RestSessionService.cs | 15 ++---- OpenSim/Framework/WebUtil.cs | 61 +++++++++++----------- 4 files changed, 81 insertions(+), 68 deletions(-) (limited to 'OpenSim/Framework') diff --git a/OpenSim/Framework/Configuration/HTTP/HTTPConfiguration.cs b/OpenSim/Framework/Configuration/HTTP/HTTPConfiguration.cs index 3dce578..6681c37 100644 --- a/OpenSim/Framework/Configuration/HTTP/HTTPConfiguration.cs +++ b/OpenSim/Framework/Configuration/HTTP/HTTPConfiguration.cs @@ -65,23 +65,27 @@ namespace OpenSim.Framework.Configuration.HTTP byte[] buf = new byte[8192]; HttpWebRequest request = (HttpWebRequest) WebRequest.Create(remoteConfigSettings.baseConfigURL + configFileName); - HttpWebResponse response = (HttpWebResponse) request.GetResponse(); - - Stream resStream = response.GetResponseStream(); - - string tempString = null; - int count = 0; - - do + using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { - count = resStream.Read(buf, 0, buf.Length); - if (count != 0) + using (Stream resStream = response.GetResponseStream()) { - tempString = Util.UTF8.GetString(buf, 0, count); - sb.Append(tempString); + string tempString = null; + int count = 0; + + do + { + count = resStream.Read(buf, 0, buf.Length); + if (count != 0) + { + tempString = Util.UTF8.GetString(buf, 0, count); + sb.Append(tempString); + } + } + while (count > 0); + + LoadDataFromString(sb.ToString()); } - } while (count > 0); - LoadDataFromString(sb.ToString()); + } } catch (WebException) { diff --git a/OpenSim/Framework/RegionLoader/Web/RegionLoaderWebServer.cs b/OpenSim/Framework/RegionLoader/Web/RegionLoaderWebServer.cs index a2f5d9c..05c64fa 100644 --- a/OpenSim/Framework/RegionLoader/Web/RegionLoaderWebServer.cs +++ b/OpenSim/Framework/RegionLoader/Web/RegionLoaderWebServer.cs @@ -74,16 +74,26 @@ namespace OpenSim.Framework.RegionLoader.Web try { - HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse(); - m_log.Debug("[WEBLOADER]: Downloading region information..."); - StreamReader reader = new StreamReader(webResponse.GetResponseStream()); string xmlSource = String.Empty; - string tempStr = reader.ReadLine(); - while (tempStr != null) + + using (HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse()) { - xmlSource = xmlSource + tempStr; - tempStr = reader.ReadLine(); + m_log.Debug("[WEBLOADER]: Downloading region information..."); + + using (Stream s = webResponse.GetResponseStream()) + { + using (StreamReader reader = new StreamReader(s)) + { + string tempStr = reader.ReadLine(); + while (tempStr != null) + { + xmlSource = xmlSource + tempStr; + tempStr = reader.ReadLine(); + } + } + } } + m_log.Debug("[WEBLOADER]: Done downloading region information from server. Total Bytes: " + xmlSource.Length); XmlDocument xmlDoc = new XmlDocument(); @@ -107,17 +117,24 @@ namespace OpenSim.Framework.RegionLoader.Web } catch (WebException ex) { - if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.NotFound) + using (HttpWebResponse response = (HttpWebResponse)ex.Response) { - if (!allowRegionless) + if (response.StatusCode == HttpStatusCode.NotFound) + { + if (!allowRegionless) + throw ex; + } + else + { throw ex; + } } - else - throw ex; } if (regionCount > 0 | allowRegionless) + { return regionInfos; + } else { m_log.Error("[WEBLOADER]: No region configs were available."); @@ -127,4 +144,4 @@ namespace OpenSim.Framework.RegionLoader.Web } } } -} +} \ No newline at end of file diff --git a/OpenSim/Framework/Servers/HttpServer/RestSessionService.cs b/OpenSim/Framework/Servers/HttpServer/RestSessionService.cs index 19c03a8..edcd134 100644 --- a/OpenSim/Framework/Servers/HttpServer/RestSessionService.cs +++ b/OpenSim/Framework/Servers/HttpServer/RestSessionService.cs @@ -101,20 +101,11 @@ namespace OpenSim.Framework.Servers.HttpServer using (WebResponse resp = request.GetResponse()) { XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); - Stream respStream = null; - try - { - respStream = resp.GetResponseStream(); + + using (Stream respStream = resp.GetResponseStream()) deserial = (TResponse)deserializer.Deserialize(respStream); - } - catch { } - finally - { - if (respStream != null) - respStream.Close(); - resp.Close(); - } } + return deserial; } } diff --git a/OpenSim/Framework/WebUtil.cs b/OpenSim/Framework/WebUtil.cs index 5c34cf4..701fbb0 100644 --- a/OpenSim/Framework/WebUtil.cs +++ b/OpenSim/Framework/WebUtil.cs @@ -228,8 +228,8 @@ namespace OpenSim.Framework errorMessage = we.Message; if (we.Status == WebExceptionStatus.ProtocolError) { - HttpWebResponse webResponse = (HttpWebResponse)we.Response; - errorMessage = String.Format("[{0}] {1}",webResponse.StatusCode,webResponse.StatusDescription); + using (HttpWebResponse webResponse = (HttpWebResponse)we.Response) + errorMessage = String.Format("[{0}] {1}", webResponse.StatusCode, webResponse.StatusDescription); } } catch (Exception ex) @@ -387,8 +387,8 @@ namespace OpenSim.Framework errorMessage = we.Message; if (we.Status == WebExceptionStatus.ProtocolError) { - HttpWebResponse webResponse = (HttpWebResponse)we.Response; - errorMessage = String.Format("[{0}] {1}",webResponse.StatusCode,webResponse.StatusDescription); + using (HttpWebResponse webResponse = (HttpWebResponse)we.Response) + errorMessage = String.Format("[{0}] {1}",webResponse.StatusCode,webResponse.StatusDescription); } } catch (Exception ex) @@ -834,15 +834,16 @@ namespace OpenSim.Framework { if (e.Response is HttpWebResponse) { - HttpWebResponse httpResponse = (HttpWebResponse)e.Response; - - if (httpResponse.StatusCode != HttpStatusCode.NotFound) - { - // We don't appear to be handling any other status codes, so log these feailures to that - // people don't spend unnecessary hours hunting phantom bugs. - m_log.DebugFormat( - "[ASYNC REQUEST]: Request {0} {1} failed with unexpected status code {2}", - verb, requestUrl, httpResponse.StatusCode); + using (HttpWebResponse httpResponse = (HttpWebResponse)e.Response) + { + if (httpResponse.StatusCode != HttpStatusCode.NotFound) + { + // We don't appear to be handling any other status codes, so log these feailures to that + // people don't spend unnecessary hours hunting phantom bugs. + m_log.DebugFormat( + "[ASYNC REQUEST]: Request {0} {1} failed with unexpected status code {2}", + verb, requestUrl, httpResponse.StatusCode); + } } } } @@ -983,11 +984,9 @@ namespace OpenSim.Framework Stream respStream = null; try { - respStream = resp.GetResponseStream(); - using (StreamReader reader = new StreamReader(respStream)) - { - respstring = reader.ReadToEnd(); - } + using (respStream = resp.GetResponseStream()) + using (StreamReader reader = new StreamReader(respStream)) + respstring = reader.ReadToEnd(); } catch (Exception e) { @@ -1127,10 +1126,11 @@ namespace OpenSim.Framework { if (resp.ContentLength != 0) { - Stream respStream = resp.GetResponseStream(); - XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); - deserial = (TResponse)deserializer.Deserialize(respStream); - respStream.Close(); + using (Stream respStream = resp.GetResponseStream()) + { + XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); + deserial = (TResponse)deserializer.Deserialize(respStream); + } } else { @@ -1142,14 +1142,15 @@ namespace OpenSim.Framework } catch (WebException e) { - HttpWebResponse hwr = (HttpWebResponse)e.Response; - - if (hwr != null && hwr.StatusCode == HttpStatusCode.NotFound) - return deserial; - else - m_log.ErrorFormat( - "[SynchronousRestObjectRequester]: WebException for {0} {1} {2}: {3} {4}", - verb, requestUrl, typeof(TResponse).ToString(), e.Message, e.StackTrace); + using (HttpWebResponse hwr = (HttpWebResponse)e.Response) + { + if (hwr != null && hwr.StatusCode == HttpStatusCode.NotFound) + return deserial; + else + m_log.ErrorFormat( + "[SynchronousRestObjectRequester]: WebException for {0} {1} {2}: {3} {4}", + verb, requestUrl, typeof(TResponse).ToString(), e.Message, e.StackTrace); + } } catch (System.InvalidOperationException) { -- cgit v1.1