Make sure we always dispose of disposables inside RestClient.Request()

mb-throttle-test
Justin Clark-Casey (justincc) 2014-12-20 01:56:33 +00:00
parent 9cff0bbd7c
commit 7100475b90
1 changed files with 30 additions and 26 deletions

View File

@ -352,42 +352,46 @@ namespace OpenSim.Framework.Communications
m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} REST {1} to {2}", reqnum, _request.Method, _request.RequestUri); m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} REST {1} to {2}", reqnum, _request.Method, _request.RequestUri);
// IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request); // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request);
try try
{ {
_response = (HttpWebResponse) _request.GetResponse(); using (_response = (HttpWebResponse) _request.GetResponse())
{
using (Stream src = _response.GetResponseStream())
{
int length = src.Read(_readbuf, 0, BufferSize);
while (length > 0)
{
_resource.Write(_readbuf, 0, length);
length = src.Read(_readbuf, 0, BufferSize);
}
// TODO! Implement timeout, without killing the server
// this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
//ThreadPool.RegisterWaitForSingleObject(responseAsyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true);
// _allDone.WaitOne();
}
}
} }
catch (WebException e) catch (WebException e)
{ {
HttpWebResponse errorResponse = e.Response as HttpWebResponse; using (HttpWebResponse errorResponse = e.Response as HttpWebResponse)
if (null != errorResponse && HttpStatusCode.NotFound == errorResponse.StatusCode)
{ {
// This is often benign. E.g., requesting a missing asset will return 404. if (null != errorResponse && HttpStatusCode.NotFound == errorResponse.StatusCode)
m_log.DebugFormat("[REST CLIENT] Resource not found (404): {0}", _request.Address.ToString()); {
} // This is often benign. E.g., requesting a missing asset will return 404.
else m_log.DebugFormat("[REST CLIENT] Resource not found (404): {0}", _request.Address.ToString());
{ }
m_log.Error(string.Format("[REST CLIENT] Error fetching resource from server: {0} ", _request.Address.ToString()), e); else
{
m_log.Error(string.Format("[REST CLIENT] Error fetching resource from server: {0} ", _request.Address.ToString()), e);
}
} }
return null; return null;
} }
Stream src = _response.GetResponseStream();
int length = src.Read(_readbuf, 0, BufferSize);
while (length > 0)
{
_resource.Write(_readbuf, 0, length);
length = src.Read(_readbuf, 0, BufferSize);
}
// TODO! Implement timeout, without killing the server
// this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
//ThreadPool.RegisterWaitForSingleObject(responseAsyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true);
// _allDone.WaitOne();
if (_response != null)
_response.Close();
if (_asyncException != null) if (_asyncException != null)
throw _asyncException; throw _asyncException;
@ -515,4 +519,4 @@ namespace OpenSim.Framework.Communications
#endregion Async Invocation #endregion Async Invocation
} }
} }