HttpClientManager.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using MediaBrowser.Common.Configuration;
  12. using MediaBrowser.Common.Extensions;
  13. using MediaBrowser.Common.Net;
  14. using MediaBrowser.Model.IO;
  15. using Microsoft.Extensions.Logging;
  16. using MediaBrowser.Model.Net;
  17. namespace Emby.Server.Implementations.HttpClientManager
  18. {
  19. /// <summary>
  20. /// Class HttpClientManager
  21. /// </summary>
  22. public class HttpClientManager : IHttpClient
  23. {
  24. /// <summary>
  25. /// When one request to a host times out, we'll ban all other requests for this period of time, to prevent scans from stalling
  26. /// </summary>
  27. private const int TimeoutSeconds = 30;
  28. /// <summary>
  29. /// The _logger
  30. /// </summary>
  31. private readonly ILogger _logger;
  32. /// <summary>
  33. /// The _app paths
  34. /// </summary>
  35. private readonly IApplicationPaths _appPaths;
  36. private readonly IFileSystem _fileSystem;
  37. private readonly Func<string> _defaultUserAgentFn;
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="HttpClientManager" /> class.
  40. /// </summary>
  41. public HttpClientManager(IApplicationPaths appPaths, ILogger logger, IFileSystem fileSystem, Func<string> defaultUserAgentFn)
  42. {
  43. if (appPaths == null)
  44. {
  45. throw new ArgumentNullException("appPaths");
  46. }
  47. if (logger == null)
  48. {
  49. throw new ArgumentNullException("logger");
  50. }
  51. _logger = logger;
  52. _fileSystem = fileSystem;
  53. _appPaths = appPaths;
  54. _defaultUserAgentFn = defaultUserAgentFn;
  55. // http://stackoverflow.com/questions/566437/http-post-returns-the-error-417-expectation-failed-c
  56. ServicePointManager.Expect100Continue = false;
  57. #if NET46
  58. // Trakt requests sometimes fail without this
  59. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
  60. #endif
  61. }
  62. /// <summary>
  63. /// Holds a dictionary of http clients by host. Use GetHttpClient(host) to retrieve or create a client for web requests.
  64. /// DON'T dispose it after use.
  65. /// </summary>
  66. /// <value>The HTTP clients.</value>
  67. private readonly ConcurrentDictionary<string, HttpClientInfo> _httpClients = new ConcurrentDictionary<string, HttpClientInfo>();
  68. /// <summary>
  69. /// Gets
  70. /// </summary>
  71. /// <param name="host">The host.</param>
  72. /// <param name="enableHttpCompression">if set to <c>true</c> [enable HTTP compression].</param>
  73. /// <returns>HttpClient.</returns>
  74. /// <exception cref="System.ArgumentNullException">host</exception>
  75. private HttpClientInfo GetHttpClient(string host, bool enableHttpCompression)
  76. {
  77. if (string.IsNullOrEmpty(host))
  78. {
  79. throw new ArgumentNullException("host");
  80. }
  81. HttpClientInfo client;
  82. var key = host + enableHttpCompression;
  83. if (!_httpClients.TryGetValue(key, out client))
  84. {
  85. client = new HttpClientInfo();
  86. _httpClients.TryAdd(key, client);
  87. }
  88. return client;
  89. }
  90. private WebRequest CreateWebRequest(string url)
  91. {
  92. try
  93. {
  94. return WebRequest.Create(url);
  95. }
  96. catch (NotSupportedException)
  97. {
  98. //Webrequest creation does fail on MONO randomly when using WebRequest.Create
  99. //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
  100. var type = Type.GetType("System.Net.HttpRequestCreator, System, Version=4.0.0.0,Culture=neutral, PublicKeyToken=b77a5c561934e089");
  101. var creator = Activator.CreateInstance(type, nonPublic: true) as IWebRequestCreate;
  102. return creator.Create(new Uri(url)) as HttpWebRequest;
  103. }
  104. }
  105. private WebRequest GetRequest(HttpRequestOptions options, string method)
  106. {
  107. var url = options.Url;
  108. var uriAddress = new Uri(url);
  109. var userInfo = uriAddress.UserInfo;
  110. if (!string.IsNullOrWhiteSpace(userInfo))
  111. {
  112. _logger.LogInformation("Found userInfo in url: {0} ... url: {1}", userInfo, url);
  113. url = url.Replace(userInfo + "@", string.Empty);
  114. }
  115. var request = CreateWebRequest(url);
  116. if (request is HttpWebRequest httpWebRequest)
  117. {
  118. AddRequestHeaders(httpWebRequest, options);
  119. if (options.EnableHttpCompression)
  120. {
  121. if (options.DecompressionMethod.HasValue)
  122. {
  123. httpWebRequest.AutomaticDecompression = options.DecompressionMethod.Value == CompressionMethod.Gzip
  124. ? DecompressionMethods.GZip
  125. : DecompressionMethods.Deflate;
  126. }
  127. else
  128. {
  129. httpWebRequest.AutomaticDecompression = DecompressionMethods.Deflate;
  130. }
  131. }
  132. else
  133. {
  134. httpWebRequest.AutomaticDecompression = DecompressionMethods.None;
  135. }
  136. if (options.EnableKeepAlive)
  137. {
  138. httpWebRequest.KeepAlive = true;
  139. }
  140. if (!string.IsNullOrEmpty(options.Host))
  141. {
  142. httpWebRequest.Host = options.Host;
  143. }
  144. if (!string.IsNullOrEmpty(options.Referer))
  145. {
  146. httpWebRequest.Referer = options.Referer;
  147. }
  148. }
  149. request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
  150. request.Method = method;
  151. request.Timeout = options.TimeoutMs;
  152. if (!string.IsNullOrWhiteSpace(userInfo))
  153. {
  154. var parts = userInfo.Split(':');
  155. if (parts.Length == 2)
  156. {
  157. request.Credentials = GetCredential(url, parts[0], parts[1]);
  158. // TODO: .net core ??
  159. request.PreAuthenticate = true;
  160. }
  161. }
  162. return request;
  163. }
  164. private CredentialCache GetCredential(string url, string username, string password)
  165. {
  166. //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
  167. CredentialCache credentialCache = new CredentialCache();
  168. credentialCache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
  169. return credentialCache;
  170. }
  171. private void AddRequestHeaders(HttpWebRequest request, HttpRequestOptions options)
  172. {
  173. var hasUserAgent = false;
  174. foreach (var header in options.RequestHeaders)
  175. {
  176. if (string.Equals(header.Key, "Accept", StringComparison.OrdinalIgnoreCase))
  177. {
  178. request.Accept = header.Value;
  179. }
  180. else if (string.Equals(header.Key, "User-Agent", StringComparison.OrdinalIgnoreCase))
  181. {
  182. SetUserAgent(request, header.Value);
  183. hasUserAgent = true;
  184. }
  185. else
  186. {
  187. request.Headers.Set(header.Key, header.Value);
  188. }
  189. }
  190. if (!hasUserAgent && options.EnableDefaultUserAgent)
  191. {
  192. SetUserAgent(request, _defaultUserAgentFn());
  193. }
  194. }
  195. private void SetUserAgent(HttpWebRequest request, string userAgent)
  196. {
  197. request.UserAgent = userAgent;
  198. }
  199. /// <summary>
  200. /// Gets the response internal.
  201. /// </summary>
  202. /// <param name="options">The options.</param>
  203. /// <returns>Task{HttpResponseInfo}.</returns>
  204. public Task<HttpResponseInfo> GetResponse(HttpRequestOptions options)
  205. {
  206. return SendAsync(options, "GET");
  207. }
  208. /// <summary>
  209. /// Performs a GET request and returns the resulting stream
  210. /// </summary>
  211. /// <param name="options">The options.</param>
  212. /// <returns>Task{Stream}.</returns>
  213. public async Task<Stream> Get(HttpRequestOptions options)
  214. {
  215. var response = await GetResponse(options).ConfigureAwait(false);
  216. return response.Content;
  217. }
  218. /// <summary>
  219. /// send as an asynchronous operation.
  220. /// </summary>
  221. /// <param name="options">The options.</param>
  222. /// <param name="httpMethod">The HTTP method.</param>
  223. /// <returns>Task{HttpResponseInfo}.</returns>
  224. /// <exception cref="HttpException">
  225. /// </exception>
  226. public async Task<HttpResponseInfo> SendAsync(HttpRequestOptions options, string httpMethod)
  227. {
  228. if (options.CacheMode == CacheMode.None)
  229. {
  230. return await SendAsyncInternal(options, httpMethod).ConfigureAwait(false);
  231. }
  232. var url = options.Url;
  233. var urlHash = url.ToLower().GetMD5().ToString("N");
  234. var responseCachePath = Path.Combine(_appPaths.CachePath, "httpclient", urlHash);
  235. var response = await GetCachedResponse(responseCachePath, options.CacheLength, url).ConfigureAwait(false);
  236. if (response != null)
  237. {
  238. return response;
  239. }
  240. response = await SendAsyncInternal(options, httpMethod).ConfigureAwait(false);
  241. if (response.StatusCode == HttpStatusCode.OK)
  242. {
  243. await CacheResponse(response, responseCachePath).ConfigureAwait(false);
  244. }
  245. return response;
  246. }
  247. private async Task<HttpResponseInfo> GetCachedResponse(string responseCachePath, TimeSpan cacheLength, string url)
  248. {
  249. try
  250. {
  251. if (_fileSystem.GetLastWriteTimeUtc(responseCachePath).Add(cacheLength) > DateTime.UtcNow)
  252. {
  253. using (var stream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read, true))
  254. {
  255. var memoryStream = new MemoryStream();
  256. await stream.CopyToAsync(memoryStream).ConfigureAwait(false);
  257. memoryStream.Position = 0;
  258. return new HttpResponseInfo
  259. {
  260. ResponseUrl = url,
  261. Content = memoryStream,
  262. StatusCode = HttpStatusCode.OK,
  263. ContentLength = memoryStream.Length
  264. };
  265. }
  266. }
  267. }
  268. catch (FileNotFoundException)
  269. {
  270. }
  271. catch (DirectoryNotFoundException)
  272. {
  273. }
  274. return null;
  275. }
  276. private async Task CacheResponse(HttpResponseInfo response, string responseCachePath)
  277. {
  278. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(responseCachePath));
  279. using (var responseStream = response.Content)
  280. using (var memoryStream = new MemoryStream())
  281. {
  282. await responseStream.CopyToAsync(memoryStream).ConfigureAwait(false);
  283. memoryStream.Position = 0;
  284. using (var fileStream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.None, true))
  285. {
  286. await memoryStream.CopyToAsync(fileStream).ConfigureAwait(false);
  287. memoryStream.Position = 0;
  288. response.Content = memoryStream;
  289. }
  290. }
  291. }
  292. private async Task<HttpResponseInfo> SendAsyncInternal(HttpRequestOptions options, string httpMethod)
  293. {
  294. ValidateParams(options);
  295. options.CancellationToken.ThrowIfCancellationRequested();
  296. var client = GetHttpClient(GetHostFromUrl(options.Url), options.EnableHttpCompression);
  297. if ((DateTime.UtcNow - client.LastTimeout).TotalSeconds < TimeoutSeconds)
  298. {
  299. throw new HttpException(string.Format("Cancelling connection to {0} due to a previous timeout.", options.Url))
  300. {
  301. IsTimedOut = true
  302. };
  303. }
  304. var httpWebRequest = GetRequest(options, httpMethod);
  305. if (options.RequestContentBytes != null ||
  306. !string.IsNullOrEmpty(options.RequestContent) ||
  307. string.Equals(httpMethod, "post", StringComparison.OrdinalIgnoreCase))
  308. {
  309. try
  310. {
  311. var bytes = options.RequestContentBytes ?? Encoding.UTF8.GetBytes(options.RequestContent ?? string.Empty);
  312. var contentType = options.RequestContentType ?? "application/x-www-form-urlencoded";
  313. if (options.AppendCharsetToMimeType)
  314. {
  315. contentType = contentType.TrimEnd(';') + "; charset=\"utf-8\"";
  316. }
  317. httpWebRequest.ContentType = contentType;
  318. httpWebRequest.ContentLength = bytes.Length;
  319. (await httpWebRequest.GetRequestStreamAsync().ConfigureAwait(false)).Write(bytes, 0, bytes.Length);
  320. }
  321. catch (Exception ex)
  322. {
  323. throw new HttpException(ex.Message) { IsTimedOut = true };
  324. }
  325. }
  326. if (options.ResourcePool != null)
  327. {
  328. await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false);
  329. }
  330. if ((DateTime.UtcNow - client.LastTimeout).TotalSeconds < TimeoutSeconds)
  331. {
  332. options.ResourcePool?.Release();
  333. throw new HttpException(string.Format("Connection to {0} timed out", options.Url)) { IsTimedOut = true };
  334. }
  335. if (options.LogRequest)
  336. {
  337. if (options.LogRequestAsDebug)
  338. {
  339. _logger.LogDebug("HttpClientManager {0}: {1}", httpMethod.ToUpper(), options.Url);
  340. }
  341. else
  342. {
  343. _logger.LogInformation("HttpClientManager {0}: {1}", httpMethod.ToUpper(), options.Url);
  344. }
  345. }
  346. try
  347. {
  348. options.CancellationToken.ThrowIfCancellationRequested();
  349. if (!options.BufferContent)
  350. {
  351. var response = await GetResponseAsync(httpWebRequest, TimeSpan.FromMilliseconds(options.TimeoutMs)).ConfigureAwait(false);
  352. var httpResponse = (HttpWebResponse)response;
  353. EnsureSuccessStatusCode(client, httpResponse, options);
  354. options.CancellationToken.ThrowIfCancellationRequested();
  355. return GetResponseInfo(httpResponse, httpResponse.GetResponseStream(), GetContentLength(httpResponse), httpResponse);
  356. }
  357. using (var response = await GetResponseAsync(httpWebRequest, TimeSpan.FromMilliseconds(options.TimeoutMs)).ConfigureAwait(false))
  358. {
  359. var httpResponse = (HttpWebResponse)response;
  360. EnsureSuccessStatusCode(client, httpResponse, options);
  361. options.CancellationToken.ThrowIfCancellationRequested();
  362. using (var stream = httpResponse.GetResponseStream())
  363. using (var memoryStream = new MemoryStream())
  364. {
  365. await stream.CopyToAsync(memoryStream).ConfigureAwait(false);
  366. memoryStream.Position = 0;
  367. return GetResponseInfo(httpResponse, memoryStream, memoryStream.Length, null);
  368. }
  369. }
  370. }
  371. catch (OperationCanceledException ex)
  372. {
  373. throw GetCancellationException(options, client, options.CancellationToken, ex);
  374. }
  375. catch (Exception ex)
  376. {
  377. throw GetException(ex, options, client);
  378. }
  379. finally
  380. {
  381. options.ResourcePool?.Release();
  382. }
  383. }
  384. private HttpResponseInfo GetResponseInfo(HttpWebResponse httpResponse, Stream content, long? contentLength, IDisposable disposable)
  385. {
  386. var responseInfo = new HttpResponseInfo(disposable)
  387. {
  388. Content = content,
  389. StatusCode = httpResponse.StatusCode,
  390. ContentType = httpResponse.ContentType,
  391. ContentLength = contentLength,
  392. ResponseUrl = httpResponse.ResponseUri.ToString()
  393. };
  394. if (httpResponse.Headers != null)
  395. {
  396. SetHeaders(httpResponse.Headers, responseInfo);
  397. }
  398. return responseInfo;
  399. }
  400. private HttpResponseInfo GetResponseInfo(HttpWebResponse httpResponse, string tempFile, long? contentLength)
  401. {
  402. var responseInfo = new HttpResponseInfo
  403. {
  404. TempFilePath = tempFile,
  405. StatusCode = httpResponse.StatusCode,
  406. ContentType = httpResponse.ContentType,
  407. ContentLength = contentLength
  408. };
  409. if (httpResponse.Headers != null)
  410. {
  411. SetHeaders(httpResponse.Headers, responseInfo);
  412. }
  413. return responseInfo;
  414. }
  415. private void SetHeaders(WebHeaderCollection headers, HttpResponseInfo responseInfo)
  416. {
  417. foreach (var key in headers.AllKeys)
  418. {
  419. responseInfo.Headers[key] = headers[key];
  420. }
  421. }
  422. public Task<HttpResponseInfo> Post(HttpRequestOptions options)
  423. {
  424. return SendAsync(options, "POST");
  425. }
  426. /// <summary>
  427. /// Performs a POST request
  428. /// </summary>
  429. /// <param name="options">The options.</param>
  430. /// <param name="postData">Params to add to the POST data.</param>
  431. /// <returns>stream on success, null on failure</returns>
  432. public async Task<Stream> Post(HttpRequestOptions options, Dictionary<string, string> postData)
  433. {
  434. options.SetPostData(postData);
  435. var response = await Post(options).ConfigureAwait(false);
  436. return response.Content;
  437. }
  438. /// <summary>
  439. /// Downloads the contents of a given url into a temporary location
  440. /// </summary>
  441. /// <param name="options">The options.</param>
  442. /// <returns>Task{System.String}.</returns>
  443. public async Task<string> GetTempFile(HttpRequestOptions options)
  444. {
  445. var response = await GetTempFileResponse(options).ConfigureAwait(false);
  446. return response.TempFilePath;
  447. }
  448. public async Task<HttpResponseInfo> GetTempFileResponse(HttpRequestOptions options)
  449. {
  450. ValidateParams(options);
  451. _fileSystem.CreateDirectory(_appPaths.TempDirectory);
  452. var tempFile = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + ".tmp");
  453. if (options.Progress == null)
  454. {
  455. throw new ArgumentNullException("progress");
  456. }
  457. options.CancellationToken.ThrowIfCancellationRequested();
  458. var httpWebRequest = GetRequest(options, "GET");
  459. if (options.ResourcePool != null)
  460. {
  461. await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false);
  462. }
  463. options.Progress.Report(0);
  464. if (options.LogRequest)
  465. {
  466. if (options.LogRequestAsDebug)
  467. {
  468. _logger.LogDebug("HttpClientManager.GetTempFileResponse url: {0}", options.Url);
  469. }
  470. else
  471. {
  472. _logger.LogInformation("HttpClientManager.GetTempFileResponse url: {0}", options.Url);
  473. }
  474. }
  475. var client = GetHttpClient(GetHostFromUrl(options.Url), options.EnableHttpCompression);
  476. try
  477. {
  478. options.CancellationToken.ThrowIfCancellationRequested();
  479. using (var response = await httpWebRequest.GetResponseAsync().ConfigureAwait(false))
  480. {
  481. var httpResponse = (HttpWebResponse)response;
  482. EnsureSuccessStatusCode(client, httpResponse, options);
  483. options.CancellationToken.ThrowIfCancellationRequested();
  484. var contentLength = GetContentLength(httpResponse);
  485. if (contentLength.HasValue)
  486. {
  487. using (var fs = _fileSystem.GetFileStream(tempFile, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true))
  488. {
  489. await httpResponse.GetResponseStream().CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false);
  490. }
  491. }
  492. else
  493. {
  494. // We're not able to track progress
  495. using (var stream = httpResponse.GetResponseStream())
  496. using (var fs = _fileSystem.GetFileStream(tempFile, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true))
  497. {
  498. await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false);
  499. }
  500. }
  501. options.Progress.Report(100);
  502. return GetResponseInfo(httpResponse, tempFile, contentLength);
  503. }
  504. }
  505. catch (Exception ex)
  506. {
  507. DeleteTempFile(tempFile);
  508. throw GetException(ex, options, client);
  509. }
  510. finally
  511. {
  512. options.ResourcePool?.Release();
  513. }
  514. }
  515. private long? GetContentLength(HttpWebResponse response)
  516. {
  517. var length = response.ContentLength;
  518. if (length == 0)
  519. {
  520. return null;
  521. }
  522. return length;
  523. }
  524. protected static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  525. private Exception GetException(Exception ex, HttpRequestOptions options, HttpClientInfo client)
  526. {
  527. if (ex is HttpException)
  528. {
  529. return ex;
  530. }
  531. var webException = ex as WebException
  532. ?? ex.InnerException as WebException;
  533. if (webException != null)
  534. {
  535. if (options.LogErrors)
  536. {
  537. _logger.LogError(webException, "Error {status} getting response from {url}", webException.Status, options.Url);
  538. }
  539. var exception = new HttpException(webException.Message, webException);
  540. var response = webException.Response as HttpWebResponse;
  541. if (response != null)
  542. {
  543. exception.StatusCode = response.StatusCode;
  544. if ((int)response.StatusCode == 429)
  545. {
  546. client.LastTimeout = DateTime.UtcNow;
  547. }
  548. }
  549. if (!exception.StatusCode.HasValue)
  550. {
  551. if (webException.Status == WebExceptionStatus.NameResolutionFailure ||
  552. webException.Status == WebExceptionStatus.ConnectFailure)
  553. {
  554. exception.IsTimedOut = true;
  555. }
  556. }
  557. return exception;
  558. }
  559. var operationCanceledException = ex as OperationCanceledException
  560. ?? ex.InnerException as OperationCanceledException;
  561. if (operationCanceledException != null)
  562. {
  563. return GetCancellationException(options, client, options.CancellationToken, operationCanceledException);
  564. }
  565. if (options.LogErrors)
  566. {
  567. _logger.LogError(ex, "Error getting response from {url}", options.Url);
  568. }
  569. return ex;
  570. }
  571. private void DeleteTempFile(string file)
  572. {
  573. try
  574. {
  575. _fileSystem.DeleteFile(file);
  576. }
  577. catch (IOException)
  578. {
  579. // Might not have been created at all. No need to worry.
  580. }
  581. }
  582. private void ValidateParams(HttpRequestOptions options)
  583. {
  584. if (string.IsNullOrEmpty(options.Url))
  585. {
  586. throw new ArgumentNullException("options");
  587. }
  588. }
  589. /// <summary>
  590. /// Gets the host from URL.
  591. /// </summary>
  592. /// <param name="url">The URL.</param>
  593. /// <returns>System.String.</returns>
  594. private string GetHostFromUrl(string url)
  595. {
  596. var index = url.IndexOf("://", StringComparison.OrdinalIgnoreCase);
  597. if (index != -1)
  598. {
  599. url = url.Substring(index + 3);
  600. var host = url.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
  601. if (!string.IsNullOrWhiteSpace(host))
  602. {
  603. return host;
  604. }
  605. }
  606. return url;
  607. }
  608. /// <summary>
  609. /// Throws the cancellation exception.
  610. /// </summary>
  611. /// <param name="options">The options.</param>
  612. /// <param name="client">The client.</param>
  613. /// <param name="cancellationToken">The cancellation token.</param>
  614. /// <param name="exception">The exception.</param>
  615. /// <returns>Exception.</returns>
  616. private Exception GetCancellationException(HttpRequestOptions options, HttpClientInfo client, CancellationToken cancellationToken, OperationCanceledException exception)
  617. {
  618. // If the HttpClient's timeout is reached, it will cancel the Task internally
  619. if (!cancellationToken.IsCancellationRequested)
  620. {
  621. var msg = string.Format("Connection to {0} timed out", options.Url);
  622. if (options.LogErrors)
  623. {
  624. _logger.LogError(msg);
  625. }
  626. client.LastTimeout = DateTime.UtcNow;
  627. // Throw an HttpException so that the caller doesn't think it was cancelled by user code
  628. return new HttpException(msg, exception)
  629. {
  630. IsTimedOut = true
  631. };
  632. }
  633. return exception;
  634. }
  635. private void EnsureSuccessStatusCode(HttpClientInfo client, HttpWebResponse response, HttpRequestOptions options)
  636. {
  637. var statusCode = response.StatusCode;
  638. var isSuccessful = statusCode >= HttpStatusCode.OK && statusCode <= (HttpStatusCode)299;
  639. if (isSuccessful)
  640. {
  641. return;
  642. }
  643. if (options.LogErrorResponseBody)
  644. {
  645. try
  646. {
  647. using (var stream = response.GetResponseStream())
  648. {
  649. if (stream != null)
  650. {
  651. using (var reader = new StreamReader(stream))
  652. {
  653. var msg = reader.ReadToEnd();
  654. _logger.LogError(msg);
  655. }
  656. }
  657. }
  658. }
  659. catch
  660. {
  661. }
  662. }
  663. throw new HttpException(response.StatusDescription)
  664. {
  665. StatusCode = response.StatusCode
  666. };
  667. }
  668. private Task<WebResponse> GetResponseAsync(WebRequest request, TimeSpan timeout)
  669. {
  670. var taskCompletion = new TaskCompletionSource<WebResponse>();
  671. Task<WebResponse> asyncTask = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
  672. ThreadPool.RegisterWaitForSingleObject((asyncTask as IAsyncResult).AsyncWaitHandle, TimeoutCallback, request, timeout, true);
  673. var callback = new TaskCallback { taskCompletion = taskCompletion };
  674. asyncTask.ContinueWith(callback.OnSuccess, TaskContinuationOptions.NotOnFaulted);
  675. // Handle errors
  676. asyncTask.ContinueWith(callback.OnError, TaskContinuationOptions.OnlyOnFaulted);
  677. return taskCompletion.Task;
  678. }
  679. private static void TimeoutCallback(object state, bool timedOut)
  680. {
  681. if (timedOut && state != null)
  682. {
  683. WebRequest request = (WebRequest)state;
  684. request.Abort();
  685. }
  686. }
  687. private class TaskCallback
  688. {
  689. public TaskCompletionSource<WebResponse> taskCompletion;
  690. public void OnSuccess(Task<WebResponse> task)
  691. {
  692. taskCompletion.TrySetResult(task.Result);
  693. }
  694. public void OnError(Task<WebResponse> task)
  695. {
  696. if (task.Exception == null)
  697. {
  698. taskCompletion.TrySetException(Enumerable.Empty<Exception>());
  699. }
  700. else
  701. {
  702. taskCompletion.TrySetException(task.Exception);
  703. }
  704. }
  705. }
  706. }
  707. }