From b67a106b5e533f786a2261901730b2c72842770c Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 8 Feb 2017 13:50:50 -0500 Subject: [PATCH] attach user agent to omdb requests --- .../HttpClientManager/HttpClientManager.cs | 25 +++++++++++++++---- MediaBrowser.Providers/Omdb/OmdbProvider.cs | 3 ++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Emby.Common.Implementations/HttpClientManager/HttpClientManager.cs b/Emby.Common.Implementations/HttpClientManager/HttpClientManager.cs index 06af5af536..0abcaadb1f 100644 --- a/Emby.Common.Implementations/HttpClientManager/HttpClientManager.cs +++ b/Emby.Common.Implementations/HttpClientManager/HttpClientManager.cs @@ -18,6 +18,7 @@ using System.Threading; using System.Threading.Tasks; using Emby.Common.Implementations.HttpClientManager; using MediaBrowser.Model.IO; +using MediaBrowser.Common; namespace Emby.Common.Implementations.HttpClientManager { @@ -43,6 +44,7 @@ namespace Emby.Common.Implementations.HttpClientManager private readonly IFileSystem _fileSystem; private readonly IMemoryStreamFactory _memoryStreamProvider; + private readonly IApplicationHost _appHost; /// /// Initializes a new instance of the class. @@ -257,6 +259,8 @@ namespace Emby.Common.Implementations.HttpClientManager private void AddRequestHeaders(HttpWebRequest request, HttpRequestOptions options) { + var hasUserAgent = false; + foreach (var header in options.RequestHeaders.ToList()) { if (string.Equals(header.Key, "Accept", StringComparison.OrdinalIgnoreCase)) @@ -265,11 +269,8 @@ namespace Emby.Common.Implementations.HttpClientManager } else if (string.Equals(header.Key, "User-Agent", StringComparison.OrdinalIgnoreCase)) { -#if NET46 - request.UserAgent = header.Value; -#elif NETSTANDARD1_6 - request.Headers["User-Agent"] = header.Value; -#endif + SetUserAgent(request, header.Value); + hasUserAgent = true; } else { @@ -280,6 +281,20 @@ namespace Emby.Common.Implementations.HttpClientManager #endif } } + + if (!hasUserAgent && options.EnableDefaultUserAgent) + { + SetUserAgent(request, _appHost.Name + "/" + _appHost.ApplicationVersion.ToString()); + } + } + + private void SetUserAgent(HttpWebRequest request, string userAgent) + { +#if NET46 + request.UserAgent = userAgent; +#elif NETSTANDARD1_6 + request.Headers["User-Agent"] = userAgent; +#endif } /// diff --git a/MediaBrowser.Providers/Omdb/OmdbProvider.cs b/MediaBrowser.Providers/Omdb/OmdbProvider.cs index 07b35c45ae..b42dd34b21 100644 --- a/MediaBrowser.Providers/Omdb/OmdbProvider.cs +++ b/MediaBrowser.Providers/Omdb/OmdbProvider.cs @@ -347,7 +347,8 @@ namespace MediaBrowser.Providers.Omdb Url = url, ResourcePool = ResourcePool, CancellationToken = cancellationToken, - BufferContent = true + BufferContent = true, + EnableDefaultUserAgent = true }); }