Files
pgsql-jellyfin/MediaBrowser.Common/Extensions/HttpContextExtensions.cs
wjones af1152b001 Refactor: standardize namespace and using directive style
Refactored all C# test files to use explicit namespace declarations and moved all using directives inside the namespace block for consistency. Updated assembly info and cache files to reflect the new build. Adjusted MvcTestingAppManifest.json to use fully qualified assembly names. No functional changes; all updates are related to code style, organization, and project metadata.
2026-02-20 16:26:53 -05:00

46 lines
1.7 KiB
C#

// <copyright file="HttpContextExtensions.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace MediaBrowser.Common.Extensions
{
using System.Net;
using Microsoft.AspNetCore.Http;
/// <summary>
/// Static class containing extension methods for <see cref="HttpContext"/>.
/// </summary>
public static class HttpContextExtensions
{
/// <summary>
/// Checks the origin of the HTTP context.
/// </summary>
/// <param name="context">The incoming HTTP context.</param>
/// <returns><c>true</c> if the request is coming from the same machine as is running the server, <c>false</c> otherwise.</returns>
public static bool IsLocal(this HttpContext context)
{
return (context.Connection.LocalIpAddress is null
&& context.Connection.RemoteIpAddress is null)
|| Equals(context.Connection.LocalIpAddress, context.Connection.RemoteIpAddress);
}
/// <summary>
/// Extracts the remote IP address of the caller of the HTTP context.
/// </summary>
/// <param name="context">The HTTP context.</param>
/// <returns>The remote caller IP address.</returns>
public static IPAddress GetNormalizedRemoteIP(this HttpContext context)
{
// Default to the loopback address if no RemoteIpAddress is specified (i.e. during integration tests)
var ip = context.Connection.RemoteIpAddress ?? IPAddress.Loopback;
if (ip.IsIPv4MappedToIPv6)
{
ip = ip.MapToIPv4();
}
return ip;
}
}
}