diff options
| author | crobibero <cody@robibe.ro> | 2020-08-19 06:31:45 -0600 |
|---|---|---|
| committer | crobibero <cody@robibe.ro> | 2020-08-19 06:31:45 -0600 |
| commit | 076e17f35556c6d1257856896619e30eb494b2bb (patch) | |
| tree | 6078303f35f0cd2ac33a33fcf4558be88f86c248 | |
| parent | 93fe595e5e9863874c1973e4f2d7f3c85549d3f9 (diff) | |
Add default http client
| -rw-r--r-- | Jellyfin.Server/Startup.cs | 15 | ||||
| -rw-r--r-- | MediaBrowser.Common/Net/DefaultHttpClient.cs | 108 | ||||
| -rw-r--r-- | MediaBrowser.Common/Net/DefaultHttpClientHandler.cs | 18 | ||||
| -rw-r--r-- | MediaBrowser.Common/Net/UserAgentDelegatingHandler.cs | 52 |
4 files changed, 192 insertions, 1 deletions
diff --git a/Jellyfin.Server/Startup.cs b/Jellyfin.Server/Startup.cs index 108d8f881..05deaa2e0 100644 --- a/Jellyfin.Server/Startup.cs +++ b/Jellyfin.Server/Startup.cs @@ -1,13 +1,20 @@ +using System.Diagnostics; +using System.Net; using System.Net.Http; +using System.Reflection; +using Emby.Server.Implementations; using Jellyfin.Server.Extensions; +using Jellyfin.Server.Implementations; using Jellyfin.Server.Middleware; using Jellyfin.Server.Models; +using MediaBrowser.Common.Net; using MediaBrowser.Controller; using MediaBrowser.Controller.Configuration; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Http.Logging; using Prometheus; namespace Jellyfin.Server @@ -44,7 +51,13 @@ namespace Jellyfin.Server services.AddCustomAuthentication(); services.AddJellyfinApiAuthorization(); - services.AddHttpClient(); + + services + .AddTransient<UserAgentDelegatingHandler>() + .AddHttpClient<DefaultHttpClient>() + .ConfigureHttpClient((sp, options) => {}) + .ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler()) + .AddHttpMessageHandler<UserAgentDelegatingHandler>(); } /// <summary> diff --git a/MediaBrowser.Common/Net/DefaultHttpClient.cs b/MediaBrowser.Common/Net/DefaultHttpClient.cs new file mode 100644 index 000000000..cbc06eec0 --- /dev/null +++ b/MediaBrowser.Common/Net/DefaultHttpClient.cs @@ -0,0 +1,108 @@ +using System; +using System.IO; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Common.Net +{ + /// <summary> + /// Default http client. + /// </summary> + public class DefaultHttpClient + { + private readonly HttpClient _httpClient; + + /// <summary> + /// Initializes a new instance of the <see cref="DefaultHttpClient" /> class. + /// </summary> + /// <param name="httpClient">Instance of httpclient.</param> + public DefaultHttpClient(HttpClient httpClient) + { + _httpClient = httpClient; + } + + /// <summary> + /// Make GET request. + /// </summary> + /// <param name="url">Url to request.</param> + /// <returns>A <see cref="Task"/> containing the <see cref="HttpResponseMessage"/>.</returns> + public Task<HttpResponseMessage> GetAsync(string url) + { + return _httpClient.GetAsync(url); + } + + /// <summary> + /// Make GET request. + /// </summary> + /// <param name="url">Url to request.</param> + /// <returns>A <see cref="Task"/> containing the <see cref="HttpResponseMessage"/>.</returns> + public Task<HttpResponseMessage> GetAsync(Uri url) + { + return _httpClient.GetAsync(url); + } + + /// <summary> + /// Make GET request. + /// </summary> + /// <param name="url">Url to request.</param> + /// <param name="cancellationToken">The cancellation token.</param> + /// <returns>A <see cref="Task"/> containing the <see cref="HttpResponseMessage"/>.</returns> + public Task<HttpResponseMessage> GetAsync(string url, CancellationToken cancellationToken) + { + return _httpClient.GetAsync(url, cancellationToken); + } + + /// <summary> + /// Make GET request. + /// </summary> + /// <param name="url">Url to request.</param> + /// <param name="cancellationToken">The cancellation token.</param> + /// <returns>A <see cref="Task"/> containing the <see cref="HttpResponseMessage"/>.</returns> + public Task<HttpResponseMessage> GetAsync(Uri url, CancellationToken cancellationToken) + { + return _httpClient.GetAsync(url, cancellationToken); + } + + /// <summary> + /// Get stream. + /// </summary> + /// <param name="url">Url to get stream from.</param> + /// <returns>A <see cref="Task"/> containing the <see cref="Stream"/>.</returns> + public Task<Stream> GetStreamAsync(string url) + { + return _httpClient.GetStreamAsync(url); + } + + /// <summary> + /// Get stream. + /// </summary> + /// <param name="url">Url to get stream from.</param> + /// <returns>A <see cref="Task"/> containing the <see cref="Stream"/>.</returns> + public Task<Stream> GetStreamAsync(Uri url) + { + return _httpClient.GetStreamAsync(url); + } + + /// <summary> + /// Send request. + /// </summary> + /// <param name="requestMessage">The <see cref="HttpRequestMessage"/>.</param> + /// <returns>A <see cref="Task"/> containing the <see cref="HttpResponseMessage"/>.</returns> + public Task<HttpResponseMessage> SendAsync(HttpRequestMessage requestMessage) + { + return _httpClient.SendAsync(requestMessage); + } + + /// <summary> + /// Send request. + /// </summary> + /// <param name="requestMessage">The <see cref="HttpRequestMessage"/>.</param> + /// <param name="cancellationToken">The cancellation token.</param> + /// <returns>A <see cref="Task"/> containing the <see cref="HttpResponseMessage"/>.</returns> + public Task<HttpResponseMessage> SendAsync(HttpRequestMessage requestMessage, CancellationToken cancellationToken) + { + return _httpClient.SendAsync(requestMessage, cancellationToken); + } + } +} diff --git a/MediaBrowser.Common/Net/DefaultHttpClientHandler.cs b/MediaBrowser.Common/Net/DefaultHttpClientHandler.cs new file mode 100644 index 000000000..6608fad34 --- /dev/null +++ b/MediaBrowser.Common/Net/DefaultHttpClientHandler.cs @@ -0,0 +1,18 @@ +using System.Net; +using System.Net.Http; + +namespace MediaBrowser.Common.Net +{ + /// <summary> + /// Default http client handler. + /// </summary> + public class DefaultHttpClientHandler : HttpClientHandler + { + /// <inheritdoc /> + public DefaultHttpClientHandler() + { + // TODO change to DecompressionMethods.All with .NET5 + AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; + } + } +} diff --git a/MediaBrowser.Common/Net/UserAgentDelegatingHandler.cs b/MediaBrowser.Common/Net/UserAgentDelegatingHandler.cs new file mode 100644 index 000000000..f527c766f --- /dev/null +++ b/MediaBrowser.Common/Net/UserAgentDelegatingHandler.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Common.Net +{ + /// <summary> + /// User agent delegating handler. + /// Adds User-Agent header to all requests. + /// </summary> + public class UserAgentDelegatingHandler : DelegatingHandler + { + /// <inheritdoc /> + public UserAgentDelegatingHandler(IApplicationHost applicationHost) + { + UserAgentValues = new List<ProductInfoHeaderValue> + { + new ProductInfoHeaderValue(applicationHost.Name.Replace(' ', '-'), applicationHost.ApplicationVersionString), + new ProductInfoHeaderValue($"({Environment.OSVersion}; {applicationHost.ApplicationUserAgentAddress})") + }; + } + + /// <summary> + /// Gets or sets the user agent values. + /// </summary> + public List<ProductInfoHeaderValue> UserAgentValues { get; set; } + + /// <summary> + /// Send request message. + /// </summary> + /// <param name="request">The request message.</param> + /// <param name="cancellationToken">The cancellation token.</param> + /// <returns>A <see cref="Task"/> containing the <see cref="HttpResponseMessage"/>.</returns> + protected override Task<HttpResponseMessage> SendAsync( + HttpRequestMessage request, + CancellationToken cancellationToken) + { + if (request.Headers.UserAgent.Count == 0) + { + foreach (var userAgentValue in UserAgentValues) + { + request.Headers.UserAgent.Add(userAgentValue); + } + } + + return base.SendAsync(request, cancellationToken); + } + } +} |
