diff --git a/Jellyfin.Server/Startup.cs b/Jellyfin.Server/Startup.cs index a2db7b5c1f..eb74d906f6 100644 --- a/Jellyfin.Server/Startup.cs +++ b/Jellyfin.Server/Startup.cs @@ -1,9 +1,11 @@ using System; using System.ComponentModel; +using System.Net.Http.Headers; using Jellyfin.Api.TypeConverters; using Jellyfin.Server.Extensions; using Jellyfin.Server.Middleware; using Jellyfin.Server.Models; +using MediaBrowser.Common; using MediaBrowser.Common.Net; using MediaBrowser.Controller; using MediaBrowser.Controller.Configuration; @@ -21,14 +23,17 @@ namespace Jellyfin.Server public class Startup { private readonly IServerConfigurationManager _serverConfigurationManager; + private readonly IApplicationHost _applicationHost; /// /// Initializes a new instance of the class. /// /// The server configuration manager. - public Startup(IServerConfigurationManager serverConfigurationManager) + /// The application host. + public Startup(IServerConfigurationManager serverConfigurationManager, IApplicationHost applicationHost) { _serverConfigurationManager = serverConfigurationManager; + _applicationHost = applicationHost; } /// @@ -49,10 +54,18 @@ namespace Jellyfin.Server services.AddJellyfinApiAuthorization(); services - .AddTransient() - .AddHttpClient() - .ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler()) - .AddHttpMessageHandler(); + .AddHttpClient(NamedClient.Default, c => + { + c.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(_applicationHost.Name.Replace(' ', '-'), _applicationHost.ApplicationVersionString)); + }) + .ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler()); + + services.AddHttpClient(NamedClient.MusicBrainz, c => + { + c.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(_applicationHost.Name.Replace(' ', '-'), _applicationHost.ApplicationVersionString)); + c.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(_applicationHost.ApplicationUserAgentAddress)); + }) + .ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler()); } /// diff --git a/MediaBrowser.Common/Net/DefaultHttpClient.cs b/MediaBrowser.Common/Net/DefaultHttpClient.cs deleted file mode 100644 index be18e82db8..0000000000 --- a/MediaBrowser.Common/Net/DefaultHttpClient.cs +++ /dev/null @@ -1,77 +0,0 @@ -using System; -using System.IO; -using System.Net.Http; -using System.Threading; -using System.Threading.Tasks; - -namespace MediaBrowser.Common.Net -{ - /// - /// Default http client. - /// - public class DefaultHttpClient - { - private readonly HttpClient _httpClient; - - /// - /// Initializes a new instance of the class. - /// - /// Instance of httpclient. - public DefaultHttpClient(HttpClient httpClient) - { - _httpClient = httpClient; - } - - /// - /// Make GET request. - /// - /// Url to request. - /// A containing the . - public Task GetAsync(Uri url) - { - return _httpClient.GetAsync(url); - } - - /// - /// Make GET request. - /// - /// Url to request. - /// The cancellation token. - /// A containing the . - public Task GetAsync(Uri url, CancellationToken cancellationToken) - { - return _httpClient.GetAsync(url, cancellationToken); - } - - /// - /// Get stream. - /// - /// Url to get stream from. - /// A containing the . - public Task GetStreamAsync(Uri url) - { - return _httpClient.GetStreamAsync(url); - } - - /// - /// Send request. - /// - /// The . - /// A containing the . - public Task SendAsync(HttpRequestMessage requestMessage) - { - return _httpClient.SendAsync(requestMessage); - } - - /// - /// Send request. - /// - /// The . - /// The cancellation token. - /// A containing the . - public Task SendAsync(HttpRequestMessage requestMessage, CancellationToken cancellationToken) - { - return _httpClient.SendAsync(requestMessage, cancellationToken); - } - } -} diff --git a/MediaBrowser.Common/Net/NamedClient.cs b/MediaBrowser.Common/Net/NamedClient.cs new file mode 100644 index 0000000000..0f6161c328 --- /dev/null +++ b/MediaBrowser.Common/Net/NamedClient.cs @@ -0,0 +1,18 @@ +namespace MediaBrowser.Common.Net +{ + /// + /// Registered http client names. + /// + public static class NamedClient + { + /// + /// Gets the value for the default named http client. + /// + public const string Default = nameof(Default); + + /// + /// Gets the value for the MusicBrainz named http client. + /// + public const string MusicBrainz = nameof(MusicBrainz); + } +} diff --git a/MediaBrowser.Common/Net/UserAgentDelegatingHandler.cs b/MediaBrowser.Common/Net/UserAgentDelegatingHandler.cs deleted file mode 100644 index 74dc22b7a7..0000000000 --- a/MediaBrowser.Common/Net/UserAgentDelegatingHandler.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System.Net.Http; -using System.Net.Http.Headers; -using System.Threading; -using System.Threading.Tasks; - -namespace MediaBrowser.Common.Net -{ - /// - /// User agent delegating handler. - /// Adds User-Agent header to all requests. - /// - public class UserAgentDelegatingHandler : DelegatingHandler - { - private readonly ProductInfoHeaderValue[] _userAgentValues; - - /// - /// Initializes a new instance of the class. - /// - /// Instance of the interface. - public UserAgentDelegatingHandler(IApplicationHost applicationHost) - { - _userAgentValues = new[] - { - new ProductInfoHeaderValue(applicationHost.Name.Replace(' ', '-'), applicationHost.ApplicationVersionString) - }; - } - - /// - /// Send request message. - /// - /// The request message. - /// The cancellation token. - /// A containing the . - protected override Task SendAsync( - HttpRequestMessage request, - CancellationToken cancellationToken) - { - if (request.Headers.UserAgent.Count == 0) - { - for (var i = 0; i < _userAgentValues.Length; i++) - { - request.Headers.UserAgent.Add(_userAgentValues[i]); - } - } - - return base.SendAsync(request, cancellationToken); - } - } -}