aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBond_009 <bond.009@outlook.com>2019-06-14 17:31:56 +0200
committerBond_009 <bond.009@outlook.com>2019-07-06 20:04:45 +0200
commitb117b364f2f60db33a100a46d12ff5f2b2c4193d (patch)
tree8fb3cbf64f7943dea5b37b98b86dc0e639aded99
parent3603c64fa6033eca4a92c4f537fddc182817998a (diff)
Remove duplicate code
-rw-r--r--Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs112
1 files changed, 45 insertions, 67 deletions
diff --git a/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs b/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs
index 7af0efc17..b8c52a53f 100644
--- a/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs
+++ b/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs
@@ -310,38 +310,28 @@ namespace Emby.Server.Implementations.HttpClientManager
|| !string.IsNullOrEmpty(options.RequestContent)
|| httpMethod == HttpMethod.Post)
{
- try
- {
- if (options.RequestContentBytes != null)
- {
- httpWebRequest.Content = new ByteArrayContent(options.RequestContentBytes);
- }
- else if (options.RequestContent != null)
- {
- httpWebRequest.Content = new StringContent(options.RequestContent);
- }
- else
- {
- httpWebRequest.Content = new ByteArrayContent(Array.Empty<byte>());
- }
- /*
- var contentType = options.RequestContentType ?? "application/x-www-form-urlencoded";
-
- if (options.AppendCharsetToMimeType)
- {
- contentType = contentType.TrimEnd(';') + "; charset=\"utf-8\"";
- }
- httpWebRequest.Headers.Add(HeaderNames.ContentType, contentType);*/
- using (var response = await client.SendAsync(httpWebRequest).ConfigureAwait(false))
- {
- return await HandleResponseAsync(response, options).ConfigureAwait(false);
- }
+ if (options.RequestContentBytes != null)
+ {
+ httpWebRequest.Content = new ByteArrayContent(options.RequestContentBytes);
+ }
+ else if (options.RequestContent != null)
+ {
+ httpWebRequest.Content = new StringContent(options.RequestContent);
}
- catch (Exception ex)
+ else
+ {
+ httpWebRequest.Content = new ByteArrayContent(Array.Empty<byte>());
+ }
+ /*
+ var contentType = options.RequestContentType ?? "application/x-www-form-urlencoded";
+
+ if (options.AppendCharsetToMimeType)
{
- throw new HttpException(ex.Message) { IsTimedOut = true };
+ contentType = contentType.TrimEnd(';') + "; charset=\"utf-8\"";
}
+
+ httpWebRequest.Headers.Add(HeaderNames.ContentType, contentType);*/
}
if (options.LogRequest)
@@ -349,54 +339,42 @@ namespace Emby.Server.Implementations.HttpClientManager
_logger.LogDebug("HttpClientManager {0}: {1}", httpMethod.ToString(), options.Url);
}
- try
- {
- options.CancellationToken.ThrowIfCancellationRequested();
+ options.CancellationToken.ThrowIfCancellationRequested();
- /*if (!options.BufferContent)
- {
- var response = await client.HttpClient.SendAsync(httpWebRequest).ConfigureAwait(false);
+ /*if (!options.BufferContent)
+ {
+ var response = await client.HttpClient.SendAsync(httpWebRequest).ConfigureAwait(false);
- await EnsureSuccessStatusCode(client, response, options).ConfigureAwait(false);
+ await EnsureSuccessStatusCode(client, response, options).ConfigureAwait(false);
- options.CancellationToken.ThrowIfCancellationRequested();
+ options.CancellationToken.ThrowIfCancellationRequested();
- return GetResponseInfo(response, await response.Content.ReadAsStreamAsync().ConfigureAwait(false), response.Content.Headers.ContentLength, response);
- }*/
+ return GetResponseInfo(response, await response.Content.ReadAsStreamAsync().ConfigureAwait(false), response.Content.Headers.ContentLength, response);
+ }*/
- using (var response = await client.SendAsync(httpWebRequest).ConfigureAwait(false))
- {
- return await HandleResponseAsync(response, options).ConfigureAwait(false);
- }
- }
- catch (OperationCanceledException ex)
+ using (var response = await client.SendAsync(httpWebRequest, options.CancellationToken).ConfigureAwait(false))
{
- throw GetCancellationException(options, options.CancellationToken, ex);
- }
- }
-
- private async Task<HttpResponseInfo> HandleResponseAsync(HttpResponseMessage response, HttpRequestOptions options)
- {
- await EnsureSuccessStatusCode(response, options).ConfigureAwait(false);
-
- options.CancellationToken.ThrowIfCancellationRequested();
+ await EnsureSuccessStatusCode(response, options).ConfigureAwait(false);
- using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
- {
- var memoryStream = new MemoryStream();
- await stream.CopyToAsync(memoryStream, 81920, options.CancellationToken).ConfigureAwait(false);
- memoryStream.Position = 0;
+ options.CancellationToken.ThrowIfCancellationRequested();
- var responseInfo = new HttpResponseInfo(response.Headers)
+ using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
{
- Content = memoryStream,
- StatusCode = response.StatusCode,
- ContentType = response.Content.Headers.ContentType?.MediaType,
- ContentLength = memoryStream.Length,
- ResponseUrl = response.Content.Headers.ContentLocation?.ToString()
- };
+ var memoryStream = new MemoryStream();
+ await stream.CopyToAsync(memoryStream, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false);
+ memoryStream.Position = 0;
- return responseInfo;
+ var responseInfo = new HttpResponseInfo(response.Headers)
+ {
+ Content = memoryStream,
+ StatusCode = response.StatusCode,
+ ContentType = response.Content.Headers.ContentType?.MediaType,
+ ContentLength = memoryStream.Length,
+ ResponseUrl = response.Content.Headers.ContentLocation?.ToString()
+ };
+
+ return responseInfo;
+ }
}
}
@@ -603,7 +581,7 @@ namespace Emby.Server.Implementations.HttpClientManager
}
var msg = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
- _logger.LogError(msg);
+ _logger.LogError("HTTP request failed with message: {Message}", msg);
throw new HttpException(response.ReasonPhrase)
{