attach user agent to omdb requests

This commit is contained in:
Luke Pulverenti 2017-02-08 13:50:50 -05:00
parent 8deb423883
commit b67a106b5e
2 changed files with 22 additions and 6 deletions

View File

@ -18,6 +18,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Emby.Common.Implementations.HttpClientManager; using Emby.Common.Implementations.HttpClientManager;
using MediaBrowser.Model.IO; using MediaBrowser.Model.IO;
using MediaBrowser.Common;
namespace Emby.Common.Implementations.HttpClientManager namespace Emby.Common.Implementations.HttpClientManager
{ {
@ -43,6 +44,7 @@ namespace Emby.Common.Implementations.HttpClientManager
private readonly IFileSystem _fileSystem; private readonly IFileSystem _fileSystem;
private readonly IMemoryStreamFactory _memoryStreamProvider; private readonly IMemoryStreamFactory _memoryStreamProvider;
private readonly IApplicationHost _appHost;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="HttpClientManager" /> class. /// Initializes a new instance of the <see cref="HttpClientManager" /> class.
@ -257,6 +259,8 @@ namespace Emby.Common.Implementations.HttpClientManager
private void AddRequestHeaders(HttpWebRequest request, HttpRequestOptions options) private void AddRequestHeaders(HttpWebRequest request, HttpRequestOptions options)
{ {
var hasUserAgent = false;
foreach (var header in options.RequestHeaders.ToList()) foreach (var header in options.RequestHeaders.ToList())
{ {
if (string.Equals(header.Key, "Accept", StringComparison.OrdinalIgnoreCase)) 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)) else if (string.Equals(header.Key, "User-Agent", StringComparison.OrdinalIgnoreCase))
{ {
#if NET46 SetUserAgent(request, header.Value);
request.UserAgent = header.Value; hasUserAgent = true;
#elif NETSTANDARD1_6
request.Headers["User-Agent"] = header.Value;
#endif
} }
else else
{ {
@ -280,6 +281,20 @@ namespace Emby.Common.Implementations.HttpClientManager
#endif #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
} }
/// <summary> /// <summary>

View File

@ -347,7 +347,8 @@ namespace MediaBrowser.Providers.Omdb
Url = url, Url = url,
ResourcePool = ResourcePool, ResourcePool = ResourcePool,
CancellationToken = cancellationToken, CancellationToken = cancellationToken,
BufferContent = true BufferContent = true,
EnableDefaultUserAgent = true
}); });
} }