Merge pull request #901 from cvium/dispose_webresponse_on_errors

Properly dispose HttpWebResponse when the request failed to avoid 'too many open files'
This commit is contained in:
Bond-009 2019-02-16 00:15:59 +01:00 committed by GitHub
commit 6d74184cfb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -66,11 +66,6 @@ namespace Emby.Server.Implementations.HttpClientManager
// http://stackoverflow.com/questions/566437/http-post-returns-the-error-417-expectation-failed-c // http://stackoverflow.com/questions/566437/http-post-returns-the-error-417-expectation-failed-c
ServicePointManager.Expect100Continue = false; ServicePointManager.Expect100Continue = false;
#if NET46
// Trakt requests sometimes fail without this
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
#endif
} }
/// <summary> /// <summary>
@ -106,23 +101,6 @@ namespace Emby.Server.Implementations.HttpClientManager
return client; return client;
} }
private static WebRequest CreateWebRequest(string url)
{
try
{
return WebRequest.Create(url);
}
catch (NotSupportedException)
{
//Webrequest creation does fail on MONO randomly when using WebRequest.Create
//the issue occurs in the GetCreator method here: http://www.oschina.net/code/explore/mono-2.8.1/mcs/class/System/System.Net/WebRequest.cs
var type = Type.GetType("System.Net.HttpRequestCreator, System, Version=4.0.0.0,Culture=neutral, PublicKeyToken=b77a5c561934e089");
var creator = Activator.CreateInstance(type, nonPublic: true) as IWebRequestCreate;
return creator.Create(new Uri(url)) as HttpWebRequest;
}
}
private WebRequest GetRequest(HttpRequestOptions options, string method) private WebRequest GetRequest(HttpRequestOptions options, string method)
{ {
string url = options.Url; string url = options.Url;
@ -135,7 +113,7 @@ namespace Emby.Server.Implementations.HttpClientManager
url = url.Replace(userInfo + "@", string.Empty); url = url.Replace(userInfo + "@", string.Empty);
} }
var request = CreateWebRequest(url); var request = WebRequest.Create(url);
if (request is HttpWebRequest httpWebRequest) if (request is HttpWebRequest httpWebRequest)
{ {
@ -627,14 +605,16 @@ namespace Emby.Server.Implementations.HttpClientManager
var exception = new HttpException(webException.Message, webException); var exception = new HttpException(webException.Message, webException);
var response = webException.Response as HttpWebResponse; using (var response = webException.Response as HttpWebResponse)
if (response != null)
{ {
exception.StatusCode = response.StatusCode; if (response != null)
if ((int)response.StatusCode == 429)
{ {
client.LastTimeout = DateTime.UtcNow; exception.StatusCode = response.StatusCode;
if ((int)response.StatusCode == 429)
{
client.LastTimeout = DateTime.UtcNow;
}
} }
} }