complete .net core support

This commit is contained in:
Luke Pulverenti
2016-10-29 14:46:14 -04:00
parent 370e496314
commit 67c6d0fd8f
6 changed files with 1734 additions and 36 deletions

View File

@@ -13,7 +13,6 @@ using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Cache;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@@ -70,11 +69,13 @@ namespace Emby.Common.Implementations.HttpClientManager
_memoryStreamProvider = memoryStreamProvider;
_appPaths = appPaths;
#if NET46
// http://stackoverflow.com/questions/566437/http-post-returns-the-error-417-expectation-failed-c
ServicePointManager.Expect100Continue = false;
// Trakt requests sometimes fail without this
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
#endif
}
/// <summary>
@@ -131,6 +132,7 @@ namespace Emby.Common.Implementations.HttpClientManager
private void AddIpv4Option(HttpWebRequest request, HttpRequestOptions options)
{
#if NET46
request.ServicePoint.BindIPEndPointDelegate = (servicePount, remoteEndPoint, retryCount) =>
{
if (remoteEndPoint.AddressFamily == AddressFamily.InterNetwork)
@@ -139,6 +141,7 @@ namespace Emby.Common.Implementations.HttpClientManager
}
throw new InvalidOperationException("no IPv4 address");
};
#endif
}
private WebRequest GetRequest(HttpRequestOptions options, string method)
@@ -165,34 +168,52 @@ namespace Emby.Common.Implementations.HttpClientManager
AddRequestHeaders(httpWebRequest, options);
httpWebRequest.AutomaticDecompression = options.EnableHttpCompression ?
(options.DecompressionMethod ?? DecompressionMethods.Deflate) :
#if NET46
httpWebRequest.AutomaticDecompression = options.EnableHttpCompression ?
(options.DecompressionMethod ?? DecompressionMethods.Deflate) :
DecompressionMethods.None;
#endif
}
request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
#if NET46
request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
#endif
if (httpWebRequest != null)
{
if (options.EnableKeepAlive)
{
#if NET46
httpWebRequest.KeepAlive = true;
#endif
}
}
request.Method = method;
#if NET46
request.Timeout = options.TimeoutMs;
#endif
if (httpWebRequest != null)
{
if (!string.IsNullOrEmpty(options.Host))
{
#if NET46
httpWebRequest.Host = options.Host;
#elif NETSTANDARD1_6
httpWebRequest.Headers["Host"] = options.Host;
#endif
}
if (!string.IsNullOrEmpty(options.Referer))
{
#if NET46
httpWebRequest.Referer = options.Referer;
#elif NETSTANDARD1_6
httpWebRequest.Headers["Referer"] = options.Referer;
#endif
}
}
@@ -202,7 +223,10 @@ namespace Emby.Common.Implementations.HttpClientManager
if (parts.Length == 2)
{
request.Credentials = GetCredential(url, parts[0], parts[1]);
// TODO: .net core ??
#if NET46
request.PreAuthenticate = true;
#endif
}
}
@@ -227,11 +251,19 @@ namespace Emby.Common.Implementations.HttpClientManager
}
else if (string.Equals(header.Key, "User-Agent", StringComparison.OrdinalIgnoreCase))
{
#if NET46
request.UserAgent = header.Value;
#elif NETSTANDARD1_6
request.Headers["User-Agent"] = header.Value;
#endif
}
else
{
#if NET46
request.Headers.Set(header.Key, header.Value);
#elif NETSTANDARD1_6
request.Headers[header.Key] = header.Value;
#endif
}
}
}
@@ -407,8 +439,10 @@ namespace Emby.Common.Implementations.HttpClientManager
httpWebRequest.ContentType = options.RequestContentType ?? "application/x-www-form-urlencoded";
#if NET46
httpWebRequest.ContentLength = bytes.Length;
httpWebRequest.GetRequestStream().Write(bytes, 0, bytes.Length);
#endif
(await httpWebRequest.GetRequestStreamAsync().ConfigureAwait(false)).Write(bytes, 0, bytes.Length);
}
if (options.ResourcePool != null)
@@ -885,6 +919,7 @@ namespace Emby.Common.Implementations.HttpClientManager
private Task<WebResponse> GetResponseAsync(WebRequest request, TimeSpan timeout)
{
#if NET46
var taskCompletion = new TaskCompletionSource<WebResponse>();
Task<WebResponse> asyncTask = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
@@ -897,6 +932,9 @@ namespace Emby.Common.Implementations.HttpClientManager
asyncTask.ContinueWith(callback.OnError, TaskContinuationOptions.OnlyOnFaulted);
return taskCompletion.Task;
#endif
return request.GetResponseAsync();
}
private static void TimeoutCallback(object state, bool timedOut)