using System.Net;
using Microsoft.AspNetCore.Http;
namespace MediaBrowser.Common.Extensions
{
///
/// Static class containing extension methods for .
///
public static class HttpContextExtensions
{
///
/// Checks the origin of the HTTP context.
///
/// The incoming HTTP context.
/// true if the request is coming from LAN, false otherwise.
public static bool IsLocal(this HttpContext context)
{
return (context.Connection.LocalIpAddress == null
&& context.Connection.RemoteIpAddress == null)
|| context.Connection.LocalIpAddress.Equals(context.Connection.RemoteIpAddress);
}
///
/// Extracts the remote IP address of the caller of the HTTP context.
///
/// The HTTP context.
/// The remote caller IP address.
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;
}
}
}