|
|
@ -23,30 +23,24 @@ namespace Emby.Server.Implementations.HttpClientManager
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
public class HttpClientManager : IHttpClient
|
|
|
|
public class HttpClientManager : IHttpClient
|
|
|
|
{
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// When one request to a host times out, we'll ban all other requests for this period of time, to prevent scans from stalling
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
private const int TimeoutSeconds = 30;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// The _logger
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// The _app paths
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
|
|
|
|
|
|
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
private readonly Func<string> _defaultUserAgentFn;
|
|
|
|
private readonly Func<string> _defaultUserAgentFn;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Holds a dictionary of http clients by host. Use GetHttpClient(host) to retrieve or create a client for web requests.
|
|
|
|
|
|
|
|
/// DON'T dispose it after use.
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <value>The HTTP clients.</value>
|
|
|
|
|
|
|
|
private readonly ConcurrentDictionary<string, HttpClient> _httpClients = new ConcurrentDictionary<string, HttpClient>();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="HttpClientManager" /> class.
|
|
|
|
/// Initializes a new instance of the <see cref="HttpClientManager" /> class.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
public HttpClientManager(
|
|
|
|
public HttpClientManager(
|
|
|
|
IApplicationPaths appPaths,
|
|
|
|
IApplicationPaths appPaths,
|
|
|
|
ILoggerFactory loggerFactory,
|
|
|
|
ILogger<HttpClientManager> logger,
|
|
|
|
IFileSystem fileSystem,
|
|
|
|
IFileSystem fileSystem,
|
|
|
|
Func<string> defaultUserAgentFn)
|
|
|
|
Func<string> defaultUserAgentFn)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -55,41 +49,28 @@ namespace Emby.Server.Implementations.HttpClientManager
|
|
|
|
throw new ArgumentNullException(nameof(appPaths));
|
|
|
|
throw new ArgumentNullException(nameof(appPaths));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (loggerFactory == null)
|
|
|
|
if (logger == null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(loggerFactory));
|
|
|
|
throw new ArgumentNullException(nameof(logger));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_logger = loggerFactory.CreateLogger(nameof(HttpClientManager));
|
|
|
|
_logger = logger;
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
_appPaths = appPaths;
|
|
|
|
_appPaths = appPaths;
|
|
|
|
_defaultUserAgentFn = defaultUserAgentFn;
|
|
|
|
_defaultUserAgentFn = defaultUserAgentFn;
|
|
|
|
|
|
|
|
|
|
|
|
// http://stackoverflow.com/questions/566437/http-post-returns-the-error-417-expectation-failed-c
|
|
|
|
|
|
|
|
ServicePointManager.Expect100Continue = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Holds a dictionary of http clients by host. Use GetHttpClient(host) to retrieve or create a client for web requests.
|
|
|
|
/// Gets the correct http client for the given url.
|
|
|
|
/// DON'T dispose it after use.
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <value>The HTTP clients.</value>
|
|
|
|
|
|
|
|
private readonly ConcurrentDictionary<string, HttpClient> _httpClients = new ConcurrentDictionary<string, HttpClient>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Gets
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="url">The host.</param>
|
|
|
|
/// <param name="url">The url.</param>
|
|
|
|
/// <param name="enableHttpCompression">if set to <c>true</c> [enable HTTP compression].</param>
|
|
|
|
|
|
|
|
/// <returns>HttpClient.</returns>
|
|
|
|
/// <returns>HttpClient.</returns>
|
|
|
|
/// <exception cref="ArgumentNullException">host</exception>
|
|
|
|
private HttpClient GetHttpClient(string url)
|
|
|
|
private HttpClient GetHttpClient(string url, bool enableHttpCompression)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var key = GetHostFromUrl(url) + enableHttpCompression;
|
|
|
|
var key = GetHostFromUrl(url);
|
|
|
|
|
|
|
|
|
|
|
|
if (!_httpClients.TryGetValue(key, out var client))
|
|
|
|
if (!_httpClients.TryGetValue(key, out var client))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
client = new HttpClient()
|
|
|
|
client = new HttpClient()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
BaseAddress = new Uri(url)
|
|
|
|
BaseAddress = new Uri(url)
|
|
|
@ -109,24 +90,26 @@ namespace Emby.Server.Implementations.HttpClientManager
|
|
|
|
if (!string.IsNullOrWhiteSpace(userInfo))
|
|
|
|
if (!string.IsNullOrWhiteSpace(userInfo))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.LogWarning("Found userInfo in url: {0} ... url: {1}", userInfo, url);
|
|
|
|
_logger.LogWarning("Found userInfo in url: {0} ... url: {1}", userInfo, url);
|
|
|
|
url = url.Replace(userInfo + "@", string.Empty);
|
|
|
|
url = url.Replace(userInfo + '@', string.Empty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var request = new HttpRequestMessage(method, url);
|
|
|
|
var request = new HttpRequestMessage(method, url);
|
|
|
|
|
|
|
|
|
|
|
|
AddRequestHeaders(request, options);
|
|
|
|
AddRequestHeaders(request, options);
|
|
|
|
|
|
|
|
|
|
|
|
if (options.EnableHttpCompression)
|
|
|
|
switch (options.DecompressionMethod)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (options.DecompressionMethod.HasValue
|
|
|
|
case CompressionMethod.Deflate | CompressionMethod.Gzip:
|
|
|
|
&& options.DecompressionMethod.Value == CompressionMethod.Gzip)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
request.Headers.Add(HeaderNames.AcceptEncoding, new[] { "gzip", "deflate" });
|
|
|
|
request.Headers.Add(HeaderNames.AcceptEncoding, new[] { "gzip", "deflate" });
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
case CompressionMethod.Deflate:
|
|
|
|
{
|
|
|
|
|
|
|
|
request.Headers.Add(HeaderNames.AcceptEncoding, "deflate");
|
|
|
|
request.Headers.Add(HeaderNames.AcceptEncoding, "deflate");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CompressionMethod.Gzip:
|
|
|
|
|
|
|
|
request.Headers.Add(HeaderNames.AcceptEncoding, "gzip");
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (options.EnableKeepAlive)
|
|
|
|
if (options.EnableKeepAlive)
|
|
|
@ -134,20 +117,8 @@ namespace Emby.Server.Implementations.HttpClientManager
|
|
|
|
request.Headers.Add(HeaderNames.Connection, "Keep-Alive");
|
|
|
|
request.Headers.Add(HeaderNames.Connection, "Keep-Alive");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(options.Host))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
request.Headers.Add(HeaderNames.Host, options.Host);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(options.Referer))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
request.Headers.Add(HeaderNames.Referer, options.Referer);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//request.Headers.Add(HeaderNames.CacheControl, "no-cache");
|
|
|
|
//request.Headers.Add(HeaderNames.CacheControl, "no-cache");
|
|
|
|
|
|
|
|
|
|
|
|
//request.Headers.Add(HeaderNames., options.TimeoutMs;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
if (!string.IsNullOrWhiteSpace(userInfo))
|
|
|
|
if (!string.IsNullOrWhiteSpace(userInfo))
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -188,9 +159,7 @@ namespace Emby.Server.Implementations.HttpClientManager
|
|
|
|
/// <param name="options">The options.</param>
|
|
|
|
/// <param name="options">The options.</param>
|
|
|
|
/// <returns>Task{HttpResponseInfo}.</returns>
|
|
|
|
/// <returns>Task{HttpResponseInfo}.</returns>
|
|
|
|
public Task<HttpResponseInfo> GetResponse(HttpRequestOptions options)
|
|
|
|
public Task<HttpResponseInfo> GetResponse(HttpRequestOptions options)
|
|
|
|
{
|
|
|
|
=> SendAsync(options, HttpMethod.Get);
|
|
|
|
return SendAsync(options, HttpMethod.Get);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Performs a GET request and returns the resulting stream
|
|
|
|
/// Performs a GET request and returns the resulting stream
|
|
|
@ -209,8 +178,6 @@ namespace Emby.Server.Implementations.HttpClientManager
|
|
|
|
/// <param name="options">The options.</param>
|
|
|
|
/// <param name="options">The options.</param>
|
|
|
|
/// <param name="httpMethod">The HTTP method.</param>
|
|
|
|
/// <param name="httpMethod">The HTTP method.</param>
|
|
|
|
/// <returns>Task{HttpResponseInfo}.</returns>
|
|
|
|
/// <returns>Task{HttpResponseInfo}.</returns>
|
|
|
|
/// <exception cref="HttpException">
|
|
|
|
|
|
|
|
/// </exception>
|
|
|
|
|
|
|
|
public Task<HttpResponseInfo> SendAsync(HttpRequestOptions options, string httpMethod)
|
|
|
|
public Task<HttpResponseInfo> SendAsync(HttpRequestOptions options, string httpMethod)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var httpMethod2 = GetHttpMethod(httpMethod);
|
|
|
|
var httpMethod2 = GetHttpMethod(httpMethod);
|
|
|
@ -223,8 +190,6 @@ namespace Emby.Server.Implementations.HttpClientManager
|
|
|
|
/// <param name="options">The options.</param>
|
|
|
|
/// <param name="options">The options.</param>
|
|
|
|
/// <param name="httpMethod">The HTTP method.</param>
|
|
|
|
/// <param name="httpMethod">The HTTP method.</param>
|
|
|
|
/// <returns>Task{HttpResponseInfo}.</returns>
|
|
|
|
/// <returns>Task{HttpResponseInfo}.</returns>
|
|
|
|
/// <exception cref="HttpException">
|
|
|
|
|
|
|
|
/// </exception>
|
|
|
|
|
|
|
|
public async Task<HttpResponseInfo> SendAsync(HttpRequestOptions options, HttpMethod httpMethod)
|
|
|
|
public async Task<HttpResponseInfo> SendAsync(HttpRequestOptions options, HttpMethod httpMethod)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (options.CacheMode == CacheMode.None)
|
|
|
|
if (options.CacheMode == CacheMode.None)
|
|
|
@ -324,32 +289,37 @@ namespace Emby.Server.Implementations.HttpClientManager
|
|
|
|
|
|
|
|
|
|
|
|
options.CancellationToken.ThrowIfCancellationRequested();
|
|
|
|
options.CancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
|
|
var client = GetHttpClient(options.Url, options.EnableHttpCompression);
|
|
|
|
var client = GetHttpClient(options.Url);
|
|
|
|
|
|
|
|
|
|
|
|
var httpWebRequest = GetRequestMessage(options, httpMethod);
|
|
|
|
var httpWebRequest = GetRequestMessage(options, httpMethod);
|
|
|
|
|
|
|
|
|
|
|
|
if (options.RequestContentBytes != null ||
|
|
|
|
if (options.RequestContentBytes != null
|
|
|
|
!string.IsNullOrEmpty(options.RequestContent) ||
|
|
|
|
|| !string.IsNullOrEmpty(options.RequestContent)
|
|
|
|
httpMethod == HttpMethod.Post)
|
|
|
|
|| httpMethod == HttpMethod.Post)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
try
|
|
|
|
if (options.RequestContentBytes != null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
httpWebRequest.Content = new StringContent(Encoding.UTF8.GetString(options.RequestContentBytes) ?? options.RequestContent ?? string.Empty);
|
|
|
|
httpWebRequest.Content = new ByteArrayContent(options.RequestContentBytes);
|
|
|
|
|
|
|
|
}
|
|
|
|
var contentType = options.RequestContentType ?? "application/x-www-form-urlencoded";
|
|
|
|
else if (options.RequestContent != null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
httpWebRequest.Content = new StringContent(options.RequestContent);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
httpWebRequest.Content = new ByteArrayContent(Array.Empty<byte>());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (options.AppendCharsetToMimeType)
|
|
|
|
// TODO: add correct content type
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
contentType = contentType.TrimEnd(';') + "; charset=\"utf-8\"";
|
|
|
|
var contentType = options.RequestContentType ?? "application/x-www-form-urlencoded";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
httpWebRequest.Headers.Add(HeaderNames.ContentType, contentType);
|
|
|
|
if (options.AppendCharsetToMimeType)
|
|
|
|
await client.SendAsync(httpWebRequest).ConfigureAwait(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
throw new HttpException(ex.Message) { IsTimedOut = true };
|
|
|
|
contentType = contentType.TrimEnd(';') + "; charset=\"utf-8\"";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
httpWebRequest.Headers.Add(HeaderNames.ContentType, contentType);*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (options.LogRequest)
|
|
|
|
if (options.LogRequest)
|
|
|
@ -357,92 +327,53 @@ namespace Emby.Server.Implementations.HttpClientManager
|
|
|
|
_logger.LogDebug("HttpClientManager {0}: {1}", httpMethod.ToString(), options.Url);
|
|
|
|
_logger.LogDebug("HttpClientManager {0}: {1}", httpMethod.ToString(), options.Url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
options.CancellationToken.ThrowIfCancellationRequested();
|
|
|
|
{
|
|
|
|
|
|
|
|
options.CancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*if (!options.BufferContent)
|
|
|
|
if (!options.BufferContent)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var response = await client.HttpClient.SendAsync(httpWebRequest).ConfigureAwait(false);
|
|
|
|
var response = await client.SendAsync(httpWebRequest, options.CancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
|
|
await EnsureSuccessStatusCode(client, response, options).ConfigureAwait(false);
|
|
|
|
await EnsureSuccessStatusCode(response, options).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
|
|
options.CancellationToken.ThrowIfCancellationRequested();
|
|
|
|
options.CancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
|
|
return GetResponseInfo(response, await response.Content.ReadAsStreamAsync().ConfigureAwait(false), response.Content.Headers.ContentLength, response);
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using (var response = await client.SendAsync(httpWebRequest).ConfigureAwait(false))
|
|
|
|
var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
return new HttpResponseInfo(response.Headers)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
await EnsureSuccessStatusCode(response, options).ConfigureAwait(false);
|
|
|
|
Content = stream,
|
|
|
|
|
|
|
|
StatusCode = response.StatusCode,
|
|
|
|
options.CancellationToken.ThrowIfCancellationRequested();
|
|
|
|
ContentType = response.Content.Headers.ContentType?.MediaType,
|
|
|
|
|
|
|
|
ContentLength = stream.Length,
|
|
|
|
using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
|
|
|
|
ResponseUrl = response.Content.Headers.ContentLocation?.ToString()
|
|
|
|
{
|
|
|
|
};
|
|
|
|
var memoryStream = new MemoryStream();
|
|
|
|
|
|
|
|
await stream.CopyToAsync(memoryStream).ConfigureAwait(false);
|
|
|
|
|
|
|
|
memoryStream.Position = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return GetResponseInfo(response, memoryStream, memoryStream.Length, null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (OperationCanceledException ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
throw GetCancellationException(options, options.CancellationToken, ex);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private HttpResponseInfo GetResponseInfo(HttpResponseMessage httpResponse, Stream content, long? contentLength, IDisposable disposable)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var responseInfo = new HttpResponseInfo(disposable)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Content = content,
|
|
|
|
|
|
|
|
StatusCode = httpResponse.StatusCode,
|
|
|
|
|
|
|
|
ContentType = httpResponse.Content.Headers.ContentType?.MediaType,
|
|
|
|
|
|
|
|
ContentLength = contentLength,
|
|
|
|
|
|
|
|
ResponseUrl = httpResponse.Content.Headers.ContentLocation?.ToString()
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (httpResponse.Headers != null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
SetHeaders(httpResponse.Content.Headers, responseInfo);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return responseInfo;
|
|
|
|
using (var response = await client.SendAsync(httpWebRequest, options.CancellationToken).ConfigureAwait(false))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private HttpResponseInfo GetResponseInfo(HttpResponseMessage httpResponse, string tempFile, long? contentLength)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var responseInfo = new HttpResponseInfo
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
TempFilePath = tempFile,
|
|
|
|
await EnsureSuccessStatusCode(response, options).ConfigureAwait(false);
|
|
|
|
StatusCode = httpResponse.StatusCode,
|
|
|
|
|
|
|
|
ContentType = httpResponse.Content.Headers.ContentType?.MediaType,
|
|
|
|
|
|
|
|
ContentLength = contentLength
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (httpResponse.Headers != null)
|
|
|
|
options.CancellationToken.ThrowIfCancellationRequested();
|
|
|
|
{
|
|
|
|
|
|
|
|
SetHeaders(httpResponse.Content.Headers, responseInfo);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return responseInfo;
|
|
|
|
using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
|
|
|
|
}
|
|
|
|
{
|
|
|
|
|
|
|
|
var memoryStream = new MemoryStream();
|
|
|
|
|
|
|
|
await stream.CopyToAsync(memoryStream, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
memoryStream.Position = 0;
|
|
|
|
|
|
|
|
|
|
|
|
private static void SetHeaders(HttpContentHeaders headers, HttpResponseInfo responseInfo)
|
|
|
|
return new HttpResponseInfo(response.Headers)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
foreach (var key in headers)
|
|
|
|
Content = memoryStream,
|
|
|
|
{
|
|
|
|
StatusCode = response.StatusCode,
|
|
|
|
responseInfo.Headers[key.Key] = string.Join(", ", key.Value);
|
|
|
|
ContentType = response.Content.Headers.ContentType?.MediaType,
|
|
|
|
|
|
|
|
ContentLength = memoryStream.Length,
|
|
|
|
|
|
|
|
ResponseUrl = response.Content.Headers.ContentLocation?.ToString()
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task<HttpResponseInfo> Post(HttpRequestOptions options)
|
|
|
|
public Task<HttpResponseInfo> Post(HttpRequestOptions options)
|
|
|
|
{
|
|
|
|
=> SendAsync(options, HttpMethod.Post);
|
|
|
|
return SendAsync(options, HttpMethod.Post);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Downloads the contents of a given url into a temporary location
|
|
|
|
/// Downloads the contents of a given url into a temporary location
|
|
|
@ -451,10 +382,8 @@ namespace Emby.Server.Implementations.HttpClientManager
|
|
|
|
/// <returns>Task{System.String}.</returns>
|
|
|
|
/// <returns>Task{System.String}.</returns>
|
|
|
|
public async Task<string> GetTempFile(HttpRequestOptions options)
|
|
|
|
public async Task<string> GetTempFile(HttpRequestOptions options)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
using (var response = await GetTempFileResponse(options).ConfigureAwait(false))
|
|
|
|
var response = await GetTempFileResponse(options).ConfigureAwait(false);
|
|
|
|
{
|
|
|
|
return response.TempFilePath;
|
|
|
|
return response.TempFilePath;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<HttpResponseInfo> GetTempFileResponse(HttpRequestOptions options)
|
|
|
|
public async Task<HttpResponseInfo> GetTempFileResponse(HttpRequestOptions options)
|
|
|
@ -481,13 +410,13 @@ namespace Emby.Server.Implementations.HttpClientManager
|
|
|
|
_logger.LogDebug("HttpClientManager.GetTempFileResponse url: {0}", options.Url);
|
|
|
|
_logger.LogDebug("HttpClientManager.GetTempFileResponse url: {0}", options.Url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var client = GetHttpClient(options.Url, options.EnableHttpCompression);
|
|
|
|
var client = GetHttpClient(options.Url);
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
|
|
|
options.CancellationToken.ThrowIfCancellationRequested();
|
|
|
|
options.CancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
|
|
using (var response = (await client.SendAsync(httpWebRequest).ConfigureAwait(false)))
|
|
|
|
using (var response = (await client.SendAsync(httpWebRequest, options.CancellationToken).ConfigureAwait(false)))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
await EnsureSuccessStatusCode(response, options).ConfigureAwait(false);
|
|
|
|
await EnsureSuccessStatusCode(response, options).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
|
@ -501,8 +430,15 @@ namespace Emby.Server.Implementations.HttpClientManager
|
|
|
|
|
|
|
|
|
|
|
|
options.Progress.Report(100);
|
|
|
|
options.Progress.Report(100);
|
|
|
|
|
|
|
|
|
|
|
|
var contentLength = response.Content.Headers.ContentLength;
|
|
|
|
var responseInfo = new HttpResponseInfo(response.Headers)
|
|
|
|
return GetResponseInfo(response, tempFile, contentLength);
|
|
|
|
{
|
|
|
|
|
|
|
|
TempFilePath = tempFile,
|
|
|
|
|
|
|
|
StatusCode = response.StatusCode,
|
|
|
|
|
|
|
|
ContentType = response.Content.Headers.ContentType?.MediaType,
|
|
|
|
|
|
|
|
ContentLength = response.Content.Headers.ContentLength
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return responseInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
catch (Exception ex)
|
|
|
@ -530,7 +466,7 @@ namespace Emby.Server.Implementations.HttpClientManager
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (options.LogErrors)
|
|
|
|
if (options.LogErrors)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.LogError(webException, "Error {status} getting response from {url}", webException.Status, options.Url);
|
|
|
|
_logger.LogError(webException, "Error {Status} getting response from {Url}", webException.Status, options.Url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var exception = new HttpException(webException.Message, webException);
|
|
|
|
var exception = new HttpException(webException.Message, webException);
|
|
|
@ -565,7 +501,7 @@ namespace Emby.Server.Implementations.HttpClientManager
|
|
|
|
|
|
|
|
|
|
|
|
if (options.LogErrors)
|
|
|
|
if (options.LogErrors)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.LogError(ex, "Error getting response from {url}", options.Url);
|
|
|
|
_logger.LogError(ex, "Error getting response from {Url}", options.Url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ex;
|
|
|
|
return ex;
|
|
|
@ -639,7 +575,7 @@ namespace Emby.Server.Implementations.HttpClientManager
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var msg = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
|
|
|
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)
|
|
|
|
throw new HttpException(response.ReasonPhrase)
|
|
|
|
{
|
|
|
|
{
|
|
|
|