Dispose httprequestmessage

pull/702/head
Luke Pulverenti 12 years ago
parent 4c971ed161
commit 5ecc276cb7

@ -136,8 +136,8 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
{ {
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
var isCacheValid = (!cachedInfo.MustRevalidate && !string.IsNullOrEmpty(cachedInfo.Etag) && (now - cachedInfo.RequestDate).TotalDays < 7) var isCacheValid = cachedInfo.Expires.HasValue ? cachedInfo.Expires.Value > now :
|| (cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > now); !cachedInfo.MustRevalidate && !string.IsNullOrEmpty(cachedInfo.Etag) && (now - cachedInfo.RequestDate).TotalDays < 5;
if (isCacheValid) if (isCacheValid)
{ {
@ -157,89 +157,90 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
options.CancellationToken.ThrowIfCancellationRequested(); options.CancellationToken.ThrowIfCancellationRequested();
var message = GetHttpRequestMessage(options); using (var message = GetHttpRequestMessage(options))
if (options.EnableResponseCache && cachedInfo != null)
{ {
if (!string.IsNullOrEmpty(cachedInfo.Etag)) if (options.EnableResponseCache && cachedInfo != null)
{ {
message.Headers.Add("If-None-Match", cachedInfo.Etag); if (!string.IsNullOrEmpty(cachedInfo.Etag))
{
message.Headers.Add("If-None-Match", cachedInfo.Etag);
}
else if (cachedInfo.LastModified.HasValue)
{
message.Headers.IfModifiedSince = new DateTimeOffset(cachedInfo.LastModified.Value);
}
} }
else if (cachedInfo.LastModified.HasValue)
if (options.ResourcePool != null)
{ {
message.Headers.IfModifiedSince = new DateTimeOffset(cachedInfo.LastModified.Value); await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false);
} }
}
if (options.ResourcePool != null) _logger.Info("HttpClientManager.Get url: {0}", options.Url);
{
await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false);
}
_logger.Info("HttpClientManager.Get url: {0}", options.Url);
try try
{ {
options.CancellationToken.ThrowIfCancellationRequested(); options.CancellationToken.ThrowIfCancellationRequested();
var response = await GetHttpClient(GetHostFromUrl(options.Url)).SendAsync(message, HttpCompletionOption.ResponseHeadersRead, options.CancellationToken).ConfigureAwait(false); var response = await GetHttpClient(GetHostFromUrl(options.Url)).SendAsync(message, HttpCompletionOption.ResponseHeadersRead, options.CancellationToken).ConfigureAwait(false);
if (options.EnableResponseCache) if (options.EnableResponseCache)
{
if (response.StatusCode != HttpStatusCode.NotModified)
{ {
EnsureSuccessStatusCode(response); if (response.StatusCode != HttpStatusCode.NotModified)
} {
EnsureSuccessStatusCode(response);
}
options.CancellationToken.ThrowIfCancellationRequested(); options.CancellationToken.ThrowIfCancellationRequested();
cachedInfo = UpdateInfoCache(cachedInfo, options.Url, cachedInfoPath, response); cachedInfo = UpdateInfoCache(cachedInfo, options.Url, cachedInfoPath, response);
if (response.StatusCode == HttpStatusCode.NotModified) if (response.StatusCode == HttpStatusCode.NotModified)
{ {
_logger.Debug("Server indicates not modified for {0}. Returning cached result.", options.Url); _logger.Debug("Server indicates not modified for {0}. Returning cached result.", options.Url);
return GetCachedResponse(cachedReponsePath);
}
if (!string.IsNullOrEmpty(cachedInfo.Etag) || cachedInfo.LastModified.HasValue || return GetCachedResponse(cachedReponsePath);
(cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > DateTime.UtcNow)) }
if (!string.IsNullOrEmpty(cachedInfo.Etag) || cachedInfo.LastModified.HasValue ||
(cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > DateTime.UtcNow))
{
await UpdateResponseCache(response, cachedReponsePath).ConfigureAwait(false);
return GetCachedResponse(cachedReponsePath);
}
}
else
{ {
await UpdateResponseCache(response, cachedReponsePath).ConfigureAwait(false); EnsureSuccessStatusCode(response);
return GetCachedResponse(cachedReponsePath); options.CancellationToken.ThrowIfCancellationRequested();
} }
return await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
} }
else catch (OperationCanceledException ex)
{ {
EnsureSuccessStatusCode(response); throw GetCancellationException(options.Url, options.CancellationToken, ex);
options.CancellationToken.ThrowIfCancellationRequested();
} }
catch (HttpRequestException ex)
{
_logger.ErrorException("Error getting response from " + options.Url, ex);
return await response.Content.ReadAsStreamAsync().ConfigureAwait(false); throw new HttpException(ex.Message, ex);
} }
catch (OperationCanceledException ex) catch (Exception ex)
{ {
throw GetCancellationException(options.Url, options.CancellationToken, ex); _logger.ErrorException("Error getting response from " + options.Url, ex);
}
catch (HttpRequestException ex)
{
_logger.ErrorException("Error getting response from " + options.Url, ex);
throw new HttpException(ex.Message, ex);
}
catch (Exception ex)
{
_logger.ErrorException("Error getting response from " + options.Url, ex);
throw; throw;
} }
finally finally
{
if (options.ResourcePool != null)
{ {
options.ResourcePool.Release(); if (options.ResourcePool != null)
{
options.ResourcePool.Release();
}
} }
} }
} }
@ -469,39 +470,42 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
{ {
options.CancellationToken.ThrowIfCancellationRequested(); options.CancellationToken.ThrowIfCancellationRequested();
using (var response = await GetHttpClient(GetHostFromUrl(options.Url)).SendAsync(GetHttpRequestMessage(options), HttpCompletionOption.ResponseHeadersRead, options.CancellationToken).ConfigureAwait(false)) using (var message = GetHttpRequestMessage(options))
{ {
EnsureSuccessStatusCode(response); using (var response = await GetHttpClient(GetHostFromUrl(options.Url)).SendAsync(message, HttpCompletionOption.ResponseHeadersRead, options.CancellationToken).ConfigureAwait(false))
{
EnsureSuccessStatusCode(response);
options.CancellationToken.ThrowIfCancellationRequested(); options.CancellationToken.ThrowIfCancellationRequested();
var contentLength = GetContentLength(response); var contentLength = GetContentLength(response);
if (!contentLength.HasValue) if (!contentLength.HasValue)
{
// We're not able to track progress
using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
{ {
using (var fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous)) // We're not able to track progress
using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
{ {
await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); using (var fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
{
await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false);
}
} }
} }
} else
else
{
using (var stream = ProgressStream.CreateReadProgressStream(await response.Content.ReadAsStreamAsync().ConfigureAwait(false), options.Progress.Report, contentLength.Value))
{ {
using (var fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous)) using (var stream = ProgressStream.CreateReadProgressStream(await response.Content.ReadAsStreamAsync().ConfigureAwait(false), options.Progress.Report, contentLength.Value))
{ {
await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); using (var fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
{
await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false);
}
} }
} }
}
options.Progress.Report(100); options.Progress.Report(100);
options.CancellationToken.ThrowIfCancellationRequested(); options.CancellationToken.ThrowIfCancellationRequested();
}
} }
} }
catch (Exception ex) catch (Exception ex)

Loading…
Cancel
Save