From 5fafea6631ec1da18803c6a599d9bc912cc15053 Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Thu, 27 Feb 2014 22:52:43 +0000 Subject: refactor: More consistently use using construct within WebUtil to match other existing code there. --- OpenSim/Framework/WebUtil.cs | 443 +++++++++++++++++++++---------------------- 1 file changed, 218 insertions(+), 225 deletions(-) diff --git a/OpenSim/Framework/WebUtil.cs b/OpenSim/Framework/WebUtil.cs index 07c9e3f..6b39345 100644 --- a/OpenSim/Framework/WebUtil.cs +++ b/OpenSim/Framework/WebUtil.cs @@ -794,172 +794,166 @@ namespace OpenSim.Framework if (maxConnections > 0 && ht.ServicePoint.ConnectionLimit < maxConnections) ht.ServicePoint.ConnectionLimit = maxConnections; - WebResponse response = null; TResponse deserial = default(TResponse); XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); request.Method = verb; + MemoryStream buffer = null; - if (verb == "POST") + try { - request.ContentType = "text/xml"; - - buffer = new MemoryStream(); - - XmlWriterSettings settings = new XmlWriterSettings(); - settings.Encoding = Encoding.UTF8; - - using (XmlWriter writer = XmlWriter.Create(buffer, settings)) + if (verb == "POST") { - XmlSerializer serializer = new XmlSerializer(type); - serializer.Serialize(writer, obj); - writer.Flush(); - } + request.ContentType = "text/xml"; - int length = (int)buffer.Length; - request.ContentLength = length; + buffer = new MemoryStream(); - if (WebUtil.DebugLevel >= 5) - WebUtil.LogOutgoingDetail(buffer); + XmlWriterSettings settings = new XmlWriterSettings(); + settings.Encoding = Encoding.UTF8; - request.BeginGetRequestStream(delegate(IAsyncResult res) - { - Stream requestStream = request.EndGetRequestStream(res); + using (XmlWriter writer = XmlWriter.Create(buffer, settings)) + { + XmlSerializer serializer = new XmlSerializer(type); + serializer.Serialize(writer, obj); + writer.Flush(); + } - requestStream.Write(buffer.ToArray(), 0, length); - requestStream.Close(); + int length = (int)buffer.Length; + request.ContentLength = length; - // capture how much time was spent writing - tickdata = Util.EnvironmentTickCountSubtract(tickstart); + if (WebUtil.DebugLevel >= 5) + WebUtil.LogOutgoingDetail(buffer); - request.BeginGetResponse(delegate(IAsyncResult ar) + request.BeginGetRequestStream(delegate(IAsyncResult res) { - response = request.EndGetResponse(ar); - Stream respStream = null; - try - { - respStream = response.GetResponseStream(); - deserial = (TResponse)deserializer.Deserialize( - respStream); - } - catch (System.InvalidOperationException) - { - } - finally + using (Stream requestStream = request.EndGetRequestStream(res)) + requestStream.Write(buffer.ToArray(), 0, length); + + // capture how much time was spent writing + tickdata = Util.EnvironmentTickCountSubtract(tickstart); + + request.BeginGetResponse(delegate(IAsyncResult ar) { - // Let's not close this - //buffer.Close(); - respStream.Close(); - response.Close(); - } + using (WebResponse response = request.EndGetResponse(ar)) + { + try + { + using (Stream respStream = response.GetResponseStream()) + deserial = (TResponse)deserializer.Deserialize(respStream); + } + catch (System.InvalidOperationException) + { + } + } - action(deserial); + action(deserial); + }, null); }, null); - }, null); - } - else - { - request.BeginGetResponse(delegate(IAsyncResult res2) + } + else { - try + request.BeginGetResponse(delegate(IAsyncResult res2) { - // If the server returns a 404, this appears to trigger a System.Net.WebException even though that isn't - // documented in MSDN - response = request.EndGetResponse(res2); - - Stream respStream = null; try { - respStream = response.GetResponseStream(); - deserial = (TResponse)deserializer.Deserialize(respStream); - } - catch (System.InvalidOperationException) - { - } - finally - { - respStream.Close(); - response.Close(); + // If the server returns a 404, this appears to trigger a System.Net.WebException even though that isn't + // documented in MSDN + using (WebResponse response = request.EndGetResponse(res2)) + { + try + { + using (Stream respStream = response.GetResponseStream()) + deserial = (TResponse)deserializer.Deserialize(respStream); + } + catch (System.InvalidOperationException) + { + } + } } - } - catch (WebException e) - { - if (e.Status == WebExceptionStatus.ProtocolError) + catch (WebException e) { - if (e.Response is HttpWebResponse) + if (e.Status == WebExceptionStatus.ProtocolError) { - 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); + if (e.Response is HttpWebResponse) + { + 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); + } } } } + else + { + m_log.ErrorFormat( + "[ASYNC REQUEST]: Request {0} {1} failed with status {2} and message {3}", + verb, requestUrl, e.Status, e.Message); + } } - else + catch (Exception e) { m_log.ErrorFormat( - "[ASYNC REQUEST]: Request {0} {1} failed with status {2} and message {3}", - verb, requestUrl, e.Status, e.Message); + "[ASYNC REQUEST]: Request {0} {1} failed with exception {2}{3}", + verb, requestUrl, e.Message, e.StackTrace); } - } - catch (Exception e) - { - m_log.ErrorFormat( - "[ASYNC REQUEST]: Request {0} {1} failed with exception {2}{3}", - verb, requestUrl, e.Message, e.StackTrace); - } - - // m_log.DebugFormat("[ASYNC REQUEST]: Received {0}", deserial.ToString()); + + // m_log.DebugFormat("[ASYNC REQUEST]: Received {0}", deserial.ToString()); - try - { - action(deserial); - } - catch (Exception e) + try + { + action(deserial); + } + catch (Exception e) + { + m_log.ErrorFormat( + "[ASYNC REQUEST]: Request {0} {1} callback failed with exception {2}{3}", + verb, requestUrl, e.Message, e.StackTrace); + } + + }, null); + } + + int tickdiff = Util.EnvironmentTickCountSubtract(tickstart); + if (tickdiff > WebUtil.LongCallTime) + { + string originalRequest = null; + + if (buffer != null) { - m_log.ErrorFormat( - "[ASYNC REQUEST]: Request {0} {1} callback failed with exception {2}{3}", - verb, requestUrl, e.Message, e.StackTrace); - } - - }, null); - } + originalRequest = Encoding.UTF8.GetString(buffer.ToArray()); - int tickdiff = Util.EnvironmentTickCountSubtract(tickstart); - if (tickdiff > WebUtil.LongCallTime) - { - string originalRequest = null; + if (originalRequest.Length > WebUtil.MaxRequestDiagLength) + originalRequest = originalRequest.Remove(WebUtil.MaxRequestDiagLength); + } - if (buffer != null) + m_log.InfoFormat( + "[ASYNC REQUEST]: Slow request {0} {1} {2} took {3}ms, {4}ms writing, {5}", + reqnum, + verb, + requestUrl, + tickdiff, + tickdata, + originalRequest); + } + else if (WebUtil.DebugLevel >= 4) { - originalRequest = Encoding.UTF8.GetString(buffer.ToArray()); - - if (originalRequest.Length > WebUtil.MaxRequestDiagLength) - originalRequest = originalRequest.Remove(WebUtil.MaxRequestDiagLength); + m_log.DebugFormat( + "[WEB UTIL]: HTTP OUT {0} took {1}ms, {2}ms writing", + reqnum, tickdiff, tickdata); } - - m_log.InfoFormat( - "[ASYNC REQUEST]: Slow request {0} {1} {2} took {3}ms, {4}ms writing, {5}", - reqnum, - verb, - requestUrl, - tickdiff, - tickdata, - originalRequest); } - else if (WebUtil.DebugLevel >= 4) - { - m_log.DebugFormat( - "[WEB UTIL]: HTTP OUT {0} took {1}ms, {2}ms writing", - reqnum, tickdiff, tickdata); + finally + { + if (buffer != null) + buffer.Dispose(); } } } @@ -1056,11 +1050,6 @@ namespace OpenSim.Framework "[FORMS]: Exception occured on receiving {0} {1}: {2}{3}", verb, requestUrl, e.Message, e.StackTrace); } - finally - { - if (respStream != null) - respStream.Close(); - } } } } @@ -1145,128 +1134,132 @@ namespace OpenSim.Framework request.Method = verb; MemoryStream buffer = null; - if ((verb == "POST") || (verb == "PUT")) + try { - request.ContentType = "text/xml"; + if ((verb == "POST") || (verb == "PUT")) + { + request.ContentType = "text/xml"; - buffer = new 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(); - } - - 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(); + } - if (WebUtil.DebugLevel >= 5) - WebUtil.LogOutgoingDetail(buffer); + int length = (int)buffer.Length; + request.ContentLength = length; - Stream requestStream = null; - try - { - requestStream = request.GetRequestStream(); - requestStream.Write(buffer.ToArray(), 0, length); - } - catch (Exception e) - { - m_log.DebugFormat( - "[SynchronousRestObjectRequester]: Exception in making request {0} {1}: {2}{3}", - verb, requestUrl, e.Message, e.StackTrace); + if (WebUtil.DebugLevel >= 5) + WebUtil.LogOutgoingDetail(buffer); - return deserial; - } - finally - { - if (requestStream != null) - requestStream.Dispose(); + try + { + using (Stream requestStream = request.GetRequestStream()) + requestStream.Write(buffer.ToArray(), 0, length); + } + catch (Exception e) + { + m_log.DebugFormat( + "[SynchronousRestObjectRequester]: Exception in making request {0} {1}: {2}{3}", + verb, requestUrl, e.Message, e.StackTrace); - // capture how much time was spent writing - tickdata = Util.EnvironmentTickCountSubtract(tickstart); + return deserial; + } + finally + { + // capture how much time was spent writing + tickdata = Util.EnvironmentTickCountSubtract(tickstart); + } } - } - try - { - using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse()) + try { - if (resp.ContentLength != 0) + using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse()) { - using (Stream respStream = resp.GetResponseStream()) + if (resp.ContentLength != 0) + { + using (Stream respStream = resp.GetResponseStream()) + { + XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); + deserial = (TResponse)deserializer.Deserialize(respStream); + } + } + else { - XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); - deserial = (TResponse)deserializer.Deserialize(respStream); + m_log.DebugFormat( + "[SynchronousRestObjectRequester]: Oops! no content found in response stream from {0} {1}", + verb, requestUrl); } } - else + } + catch (WebException e) + { + using (HttpWebResponse hwr = (HttpWebResponse)e.Response) { - m_log.DebugFormat( - "[SynchronousRestObjectRequester]: Oops! no content found in response stream from {0} {1}", - verb, requestUrl); + 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 (WebException e) - { - using (HttpWebResponse hwr = (HttpWebResponse)e.Response) + catch (System.InvalidOperationException) { - 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); + // This is what happens when there is invalid XML + m_log.DebugFormat( + "[SynchronousRestObjectRequester]: Invalid XML from {0} {1} {2}", + verb, requestUrl, typeof(TResponse).ToString()); + } + catch (Exception e) + { + m_log.DebugFormat( + "[SynchronousRestObjectRequester]: Exception on response from {0} {1}: {2}{3}", + verb, requestUrl, e.Message, e.StackTrace); } - } - catch (System.InvalidOperationException) - { - // This is what happens when there is invalid XML - m_log.DebugFormat( - "[SynchronousRestObjectRequester]: Invalid XML from {0} {1} {2}", - verb, requestUrl, typeof(TResponse).ToString()); - } - catch (Exception e) - { - m_log.DebugFormat( - "[SynchronousRestObjectRequester]: Exception on response from {0} {1}: {2}{3}", - verb, requestUrl, e.Message, e.StackTrace); - } - - int tickdiff = Util.EnvironmentTickCountSubtract(tickstart); - if (tickdiff > WebUtil.LongCallTime) - { - string originalRequest = null; - if (buffer != null) + int tickdiff = Util.EnvironmentTickCountSubtract(tickstart); + if (tickdiff > WebUtil.LongCallTime) { - originalRequest = Encoding.UTF8.GetString(buffer.ToArray()); + string originalRequest = null; - if (originalRequest.Length > WebUtil.MaxRequestDiagLength) - originalRequest = originalRequest.Remove(WebUtil.MaxRequestDiagLength); - } + if (buffer != null) + { + originalRequest = Encoding.UTF8.GetString(buffer.ToArray()); - m_log.InfoFormat( - "[SynchronousRestObjectRequester]: Slow request {0} {1} {2} took {3}ms, {4}ms writing, {5}", - reqnum, - verb, - requestUrl, - tickdiff, - tickdata, - originalRequest); + if (originalRequest.Length > WebUtil.MaxRequestDiagLength) + originalRequest = originalRequest.Remove(WebUtil.MaxRequestDiagLength); + } + + m_log.InfoFormat( + "[SynchronousRestObjectRequester]: Slow request {0} {1} {2} took {3}ms, {4}ms writing, {5}", + reqnum, + verb, + requestUrl, + tickdiff, + tickdata, + originalRequest); + } + else if (WebUtil.DebugLevel >= 4) + { + m_log.DebugFormat( + "[WEB UTIL]: HTTP OUT {0} took {1}ms, {2}ms writing", + reqnum, tickdiff, tickdata); + } } - else if (WebUtil.DebugLevel >= 4) + finally { - m_log.DebugFormat( - "[WEB UTIL]: HTTP OUT {0} took {1}ms, {2}ms writing", - reqnum, tickdiff, tickdata); + if (buffer != null) + buffer.Dispose(); } return deserial; } } -} +} \ No newline at end of file -- cgit v1.1