mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-30 13:56:31 +01:00
Merge remote-tracking branch 'upstream/master' into http-client-migrate
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using MediaBrowser.Model.Services;
|
||||
using System.Net;
|
||||
using MediaBrowser.Common.Net;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace MediaBrowser.Common.Extensions
|
||||
@@ -8,26 +9,55 @@ namespace MediaBrowser.Common.Extensions
|
||||
/// </summary>
|
||||
public static class HttpContextExtensions
|
||||
{
|
||||
private const string ServiceStackRequest = "ServiceStackRequest";
|
||||
|
||||
/// <summary>
|
||||
/// Set the ServiceStack request.
|
||||
/// Checks the origin of the HTTP request.
|
||||
/// </summary>
|
||||
/// <param name="httpContext">The HttpContext instance.</param>
|
||||
/// <param name="request">The service stack request instance.</param>
|
||||
public static void SetServiceStackRequest(this HttpContext httpContext, IRequest request)
|
||||
/// <param name="request">The incoming HTTP request.</param>
|
||||
/// <returns><c>true</c> if the request is coming from LAN, <c>false</c> otherwise.</returns>
|
||||
public static bool IsLocal(this HttpRequest request)
|
||||
{
|
||||
httpContext.Items[ServiceStackRequest] = request;
|
||||
return (request.HttpContext.Connection.LocalIpAddress == null
|
||||
&& request.HttpContext.Connection.RemoteIpAddress == null)
|
||||
|| request.HttpContext.Connection.LocalIpAddress.Equals(request.HttpContext.Connection.RemoteIpAddress);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the ServiceStack request.
|
||||
/// Extracts the remote IP address of the caller of the HTTP request.
|
||||
/// </summary>
|
||||
/// <param name="httpContext">The HttpContext instance.</param>
|
||||
/// <returns>The service stack request instance.</returns>
|
||||
public static IRequest GetServiceStackRequest(this HttpContext httpContext)
|
||||
/// <param name="request">The HTTP request.</param>
|
||||
/// <returns>The remote caller IP address.</returns>
|
||||
public static string RemoteIp(this HttpRequest request)
|
||||
{
|
||||
return (IRequest)httpContext.Items[ServiceStackRequest];
|
||||
var cachedRemoteIp = request.HttpContext.Items["RemoteIp"]?.ToString();
|
||||
if (!string.IsNullOrEmpty(cachedRemoteIp))
|
||||
{
|
||||
return cachedRemoteIp;
|
||||
}
|
||||
|
||||
IPAddress ip;
|
||||
|
||||
// "Real" remote ip might be in X-Forwarded-For of X-Real-Ip
|
||||
// (if the server is behind a reverse proxy for example)
|
||||
if (!IPAddress.TryParse(request.Headers[CustomHeaderNames.XForwardedFor].ToString(), out ip))
|
||||
{
|
||||
if (!IPAddress.TryParse(request.Headers[CustomHeaderNames.XRealIP].ToString(), out ip))
|
||||
{
|
||||
ip = request.HttpContext.Connection.RemoteIpAddress;
|
||||
|
||||
// Default to the loopback address if no RemoteIpAddress is specified (i.e. during integration tests)
|
||||
ip ??= IPAddress.Loopback;
|
||||
}
|
||||
}
|
||||
|
||||
if (ip.IsIPv4MappedToIPv6)
|
||||
{
|
||||
ip = ip.MapToIPv4();
|
||||
}
|
||||
|
||||
var normalizedIp = ip.ToString();
|
||||
|
||||
request.HttpContext.Items["RemoteIp"] = normalizedIp;
|
||||
return normalizedIp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace MediaBrowser.Common.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a nullable struct or value to/from JSON.
|
||||
/// Required - some clients send an empty string.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The struct type.</typeparam>
|
||||
public class JsonNullableStructConverter<T> : JsonConverter<T?>
|
||||
where T : struct
|
||||
{
|
||||
private readonly JsonConverter<T?> _baseJsonConverter;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JsonNullableStructConverter{T}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="baseJsonConverter">The base json converter.</param>
|
||||
public JsonNullableStructConverter(JsonConverter<T?> baseJsonConverter)
|
||||
{
|
||||
_baseJsonConverter = baseJsonConverter;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
// Handle empty string.
|
||||
if (reader.TokenType == JsonTokenType.String && ((reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return _baseJsonConverter.Read(ref reader, typeToConvert, options);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options)
|
||||
{
|
||||
_baseJsonConverter.Write(writer, value, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,8 +29,14 @@ namespace MediaBrowser.Common.Json
|
||||
NumberHandling = JsonNumberHandling.AllowReadingFromString
|
||||
};
|
||||
|
||||
// Get built-in converters for fallback converting.
|
||||
var baseNullableInt32Converter = (JsonConverter<int?>)options.GetConverter(typeof(int?));
|
||||
var baseNullableInt64Converter = (JsonConverter<long?>)options.GetConverter(typeof(long?));
|
||||
|
||||
options.Converters.Add(new JsonGuidConverter());
|
||||
options.Converters.Add(new JsonStringEnumConverter());
|
||||
options.Converters.Add(new JsonNullableStructConverter<int>(baseNullableInt32Converter));
|
||||
options.Converters.Add(new JsonNullableStructConverter<long>(baseNullableInt64Converter));
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.6" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.6" />
|
||||
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.2.8" />
|
||||
@@ -32,6 +33,15 @@
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<PublishRepositoryUrl>true</PublishRepositoryUrl>
|
||||
<EmbedUntrackedSources>true</EmbedUntrackedSources>
|
||||
<IncludeSymbols>true</IncludeSymbols>
|
||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Stability)'=='Unstable'">
|
||||
<!-- Include all symbols in the main nupkg until Azure Artifact Feed starts supporting ingesting NuGet symbol packages. -->
|
||||
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
|
||||
</PropertyGroup>
|
||||
|
||||
<!-- Code analyzers-->
|
||||
|
||||
Reference in New Issue
Block a user