HttpClientManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Common.Configuration;
  10. using MediaBrowser.Common.Extensions;
  11. using MediaBrowser.Common.Net;
  12. using MediaBrowser.Model.IO;
  13. using MediaBrowser.Model.Net;
  14. using Microsoft.Extensions.Logging;
  15. using Microsoft.Net.Http.Headers;
  16. namespace Emby.Server.Implementations.HttpClientManager
  17. {
  18. /// <summary>
  19. /// Class HttpClientManager.
  20. /// </summary>
  21. public class HttpClientManager : IHttpClient
  22. {
  23. private readonly ILogger _logger;
  24. private readonly IApplicationPaths _appPaths;
  25. private readonly IFileSystem _fileSystem;
  26. private readonly Func<string> _defaultUserAgentFn;
  27. /// <summary>
  28. /// Holds a dictionary of http clients by host. Use GetHttpClient(host) to retrieve or create a client for web requests.
  29. /// DON'T dispose it after use.
  30. /// </summary>
  31. /// <value>The HTTP clients.</value>
  32. private readonly ConcurrentDictionary<string, HttpClient> _httpClients = new ConcurrentDictionary<string, HttpClient>();
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="HttpClientManager" /> class.
  35. /// </summary>
  36. public HttpClientManager(
  37. IApplicationPaths appPaths,
  38. ILogger<HttpClientManager> logger,
  39. IFileSystem fileSystem,
  40. Func<string> defaultUserAgentFn)
  41. {
  42. _logger = logger ?? throw new ArgumentNullException(nameof(logger));
  43. _fileSystem = fileSystem;
  44. _appPaths = appPaths ?? throw new ArgumentNullException(nameof(appPaths));
  45. _defaultUserAgentFn = defaultUserAgentFn;
  46. }
  47. /// <summary>
  48. /// Gets the correct http client for the given url.
  49. /// </summary>
  50. /// <param name="url">The url.</param>
  51. /// <returns>HttpClient.</returns>
  52. private HttpClient GetHttpClient(string url)
  53. {
  54. var key = GetHostFromUrl(url);
  55. if (!_httpClients.TryGetValue(key, out var client))
  56. {
  57. client = new HttpClient()
  58. {
  59. BaseAddress = new Uri(url)
  60. };
  61. _httpClients.TryAdd(key, client);
  62. }
  63. return client;
  64. }
  65. private HttpRequestMessage GetRequestMessage(HttpRequestOptions options, HttpMethod method)
  66. {
  67. string url = options.Url;
  68. var uriAddress = new Uri(url);
  69. string userInfo = uriAddress.UserInfo;
  70. if (!string.IsNullOrWhiteSpace(userInfo))
  71. {
  72. _logger.LogWarning("Found userInfo in url: {0} ... url: {1}", userInfo, url);
  73. url = url.Replace(userInfo + '@', string.Empty);
  74. }
  75. var request = new HttpRequestMessage(method, url);
  76. AddRequestHeaders(request, options);
  77. switch (options.DecompressionMethod)
  78. {
  79. case CompressionMethod.Deflate | CompressionMethod.Gzip:
  80. request.Headers.Add(HeaderNames.AcceptEncoding, new[] { "gzip", "deflate" });
  81. break;
  82. case CompressionMethod.Deflate:
  83. request.Headers.Add(HeaderNames.AcceptEncoding, "deflate");
  84. break;
  85. case CompressionMethod.Gzip:
  86. request.Headers.Add(HeaderNames.AcceptEncoding, "gzip");
  87. break;
  88. default:
  89. break;
  90. }
  91. if (options.EnableKeepAlive)
  92. {
  93. request.Headers.Add(HeaderNames.Connection, "Keep-Alive");
  94. }
  95. // request.Headers.Add(HeaderNames.CacheControl, "no-cache");
  96. /*
  97. if (!string.IsNullOrWhiteSpace(userInfo))
  98. {
  99. var parts = userInfo.Split(':');
  100. if (parts.Length == 2)
  101. {
  102. request.Headers.Add(HeaderNames., GetCredential(url, parts[0], parts[1]);
  103. }
  104. }
  105. */
  106. return request;
  107. }
  108. private void AddRequestHeaders(HttpRequestMessage request, HttpRequestOptions options)
  109. {
  110. var hasUserAgent = false;
  111. foreach (var header in options.RequestHeaders)
  112. {
  113. if (string.Equals(header.Key, HeaderNames.UserAgent, StringComparison.OrdinalIgnoreCase))
  114. {
  115. hasUserAgent = true;
  116. }
  117. request.Headers.Add(header.Key, header.Value);
  118. }
  119. if (!hasUserAgent && options.EnableDefaultUserAgent)
  120. {
  121. request.Headers.Add(HeaderNames.UserAgent, _defaultUserAgentFn());
  122. }
  123. }
  124. /// <summary>
  125. /// Gets the response internal.
  126. /// </summary>
  127. /// <param name="options">The options.</param>
  128. /// <returns>Task{HttpResponseInfo}.</returns>
  129. public Task<HttpResponseInfo> GetResponse(HttpRequestOptions options)
  130. => SendAsync(options, HttpMethod.Get);
  131. /// <summary>
  132. /// Performs a GET request and returns the resulting stream
  133. /// </summary>
  134. /// <param name="options">The options.</param>
  135. /// <returns>Task{Stream}.</returns>
  136. public async Task<Stream> Get(HttpRequestOptions options)
  137. {
  138. var response = await GetResponse(options).ConfigureAwait(false);
  139. return response.Content;
  140. }
  141. /// <summary>
  142. /// send as an asynchronous operation.
  143. /// </summary>
  144. /// <param name="options">The options.</param>
  145. /// <param name="httpMethod">The HTTP method.</param>
  146. /// <returns>Task{HttpResponseInfo}.</returns>
  147. public Task<HttpResponseInfo> SendAsync(HttpRequestOptions options, string httpMethod)
  148. => SendAsync(options, new HttpMethod(httpMethod));
  149. /// <summary>
  150. /// send as an asynchronous operation.
  151. /// </summary>
  152. /// <param name="options">The options.</param>
  153. /// <param name="httpMethod">The HTTP method.</param>
  154. /// <returns>Task{HttpResponseInfo}.</returns>
  155. public async Task<HttpResponseInfo> SendAsync(HttpRequestOptions options, HttpMethod httpMethod)
  156. {
  157. if (options.CacheMode == CacheMode.None)
  158. {
  159. return await SendAsyncInternal(options, httpMethod).ConfigureAwait(false);
  160. }
  161. var url = options.Url;
  162. var urlHash = url.ToUpperInvariant().GetMD5().ToString("N", CultureInfo.InvariantCulture);
  163. var responseCachePath = Path.Combine(_appPaths.CachePath, "httpclient", urlHash);
  164. var response = GetCachedResponse(responseCachePath, options.CacheLength, url);
  165. if (response != null)
  166. {
  167. return response;
  168. }
  169. response = await SendAsyncInternal(options, httpMethod).ConfigureAwait(false);
  170. if (response.StatusCode == HttpStatusCode.OK)
  171. {
  172. await CacheResponse(response, responseCachePath).ConfigureAwait(false);
  173. }
  174. return response;
  175. }
  176. private HttpResponseInfo GetCachedResponse(string responseCachePath, TimeSpan cacheLength, string url)
  177. {
  178. if (File.Exists(responseCachePath)
  179. && _fileSystem.GetLastWriteTimeUtc(responseCachePath).Add(cacheLength) > DateTime.UtcNow)
  180. {
  181. var stream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read, true);
  182. return new HttpResponseInfo
  183. {
  184. ResponseUrl = url,
  185. Content = stream,
  186. StatusCode = HttpStatusCode.OK,
  187. ContentLength = stream.Length
  188. };
  189. }
  190. return null;
  191. }
  192. private async Task CacheResponse(HttpResponseInfo response, string responseCachePath)
  193. {
  194. Directory.CreateDirectory(Path.GetDirectoryName(responseCachePath));
  195. using (var fileStream = new FileStream(
  196. responseCachePath,
  197. FileMode.Create,
  198. FileAccess.Write,
  199. FileShare.None,
  200. StreamDefaults.DefaultFileStreamBufferSize,
  201. true))
  202. {
  203. await response.Content.CopyToAsync(fileStream).ConfigureAwait(false);
  204. response.Content.Position = 0;
  205. }
  206. }
  207. private async Task<HttpResponseInfo> SendAsyncInternal(HttpRequestOptions options, HttpMethod httpMethod)
  208. {
  209. ValidateParams(options);
  210. options.CancellationToken.ThrowIfCancellationRequested();
  211. var client = GetHttpClient(options.Url);
  212. var httpWebRequest = GetRequestMessage(options, httpMethod);
  213. if (options.RequestContentBytes != null
  214. || !string.IsNullOrEmpty(options.RequestContent)
  215. || httpMethod == HttpMethod.Post)
  216. {
  217. if (options.RequestContentBytes != null)
  218. {
  219. httpWebRequest.Content = new ByteArrayContent(options.RequestContentBytes);
  220. }
  221. else if (options.RequestContent != null)
  222. {
  223. httpWebRequest.Content = new StringContent(
  224. options.RequestContent,
  225. null,
  226. options.RequestContentType);
  227. }
  228. else
  229. {
  230. httpWebRequest.Content = new ByteArrayContent(Array.Empty<byte>());
  231. }
  232. }
  233. options.CancellationToken.ThrowIfCancellationRequested();
  234. var response = await client.SendAsync(
  235. httpWebRequest,
  236. options.BufferContent || options.CacheMode == CacheMode.Unconditional ? HttpCompletionOption.ResponseContentRead : HttpCompletionOption.ResponseHeadersRead,
  237. options.CancellationToken).ConfigureAwait(false);
  238. await EnsureSuccessStatusCode(response, options).ConfigureAwait(false);
  239. options.CancellationToken.ThrowIfCancellationRequested();
  240. var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
  241. return new HttpResponseInfo(response.Headers, response.Content.Headers)
  242. {
  243. Content = stream,
  244. StatusCode = response.StatusCode,
  245. ContentType = response.Content.Headers.ContentType?.MediaType,
  246. ContentLength = response.Content.Headers.ContentLength,
  247. ResponseUrl = response.Content.Headers.ContentLocation?.ToString()
  248. };
  249. }
  250. public Task<HttpResponseInfo> Post(HttpRequestOptions options)
  251. => SendAsync(options, HttpMethod.Post);
  252. private void ValidateParams(HttpRequestOptions options)
  253. {
  254. if (string.IsNullOrEmpty(options.Url))
  255. {
  256. throw new ArgumentNullException(nameof(options));
  257. }
  258. }
  259. /// <summary>
  260. /// Gets the host from URL.
  261. /// </summary>
  262. /// <param name="url">The URL.</param>
  263. /// <returns>System.String.</returns>
  264. private static string GetHostFromUrl(string url)
  265. {
  266. var index = url.IndexOf("://", StringComparison.OrdinalIgnoreCase);
  267. if (index != -1)
  268. {
  269. url = url.Substring(index + 3);
  270. var host = url.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
  271. if (!string.IsNullOrWhiteSpace(host))
  272. {
  273. return host;
  274. }
  275. }
  276. return url;
  277. }
  278. private async Task EnsureSuccessStatusCode(HttpResponseMessage response, HttpRequestOptions options)
  279. {
  280. if (response.IsSuccessStatusCode)
  281. {
  282. return;
  283. }
  284. if (options.LogErrorResponseBody)
  285. {
  286. var msg = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
  287. _logger.LogError("HTTP request failed with message: {Message}", msg);
  288. }
  289. throw new HttpException(response.ReasonPhrase)
  290. {
  291. StatusCode = response.StatusCode
  292. };
  293. }
  294. }
  295. }