Просмотр исходного кода

Properly dispose HttpWebResponse when the request failed to avoid 'too many open files'

Claus Vium 6 лет назад
Родитель
Сommit
2845e7e101
1 измененных файлов с 9 добавлено и 29 удалено
  1. 9 29
      Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs

+ 9 - 29
Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs

@@ -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;
-                if (response != null)
+                using (var response = webException.Response as HttpWebResponse)
                 {
                 {
-                    exception.StatusCode = response.StatusCode;
-
-                    if ((int)response.StatusCode == 429)
+                    if (response != null)
                     {
                     {
-                        client.LastTimeout = DateTime.UtcNow;
+                        exception.StatusCode = response.StatusCode;
+
+                        if ((int)response.StatusCode == 429)
+                        {
+                            client.LastTimeout = DateTime.UtcNow;
+                        }
                     }
                     }
                 }
                 }