Add default http client

This commit is contained in:
crobibero 2020-08-19 06:31:45 -06:00
parent 93fe595e5e
commit 076e17f355
4 changed files with 192 additions and 1 deletions

View File

@ -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>

View File

@ -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);
}
}
}

View File

@ -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;
}
}
}

View File

@ -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);
}
}
}