aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Extensions
diff options
context:
space:
mode:
authorcvium <clausvium@gmail.com>2020-09-10 14:16:41 +0200
committercvium <clausvium@gmail.com>2020-09-10 14:16:41 +0200
commit7576824cee0dc0d8e1729ae0a7e8e4f256b71efd (patch)
tree6ceccabcbc8555de3275ae4cf6e59e8fd013584b /MediaBrowser.Common/Extensions
parent78cab77f819e8d8b283f95f2e48f635bcf66fea5 (diff)
Standardize use of IsLocal and RemoteIp
Diffstat (limited to 'MediaBrowser.Common/Extensions')
-rw-r--r--MediaBrowser.Common/Extensions/HttpContextExtensions.cs46
1 files changed, 11 insertions, 35 deletions
diff --git a/MediaBrowser.Common/Extensions/HttpContextExtensions.cs b/MediaBrowser.Common/Extensions/HttpContextExtensions.cs
index e0cf3f9ac..8d2908882 100644
--- a/MediaBrowser.Common/Extensions/HttpContextExtensions.cs
+++ b/MediaBrowser.Common/Extensions/HttpContextExtensions.cs
@@ -1,5 +1,3 @@
-using System.Net;
-using MediaBrowser.Common.Net;
using Microsoft.AspNetCore.Http;
namespace MediaBrowser.Common.Extensions
@@ -10,54 +8,32 @@ namespace MediaBrowser.Common.Extensions
public static class HttpContextExtensions
{
/// <summary>
- /// Checks the origin of the HTTP request.
+ /// Checks the origin of the HTTP context.
/// </summary>
- /// <param name="request">The incoming HTTP request.</param>
+ /// <param name="context">The incoming HTTP context.</param>
/// <returns><c>true</c> if the request is coming from LAN, <c>false</c> otherwise.</returns>
- public static bool IsLocal(this HttpRequest request)
+ public static bool IsLocal(this HttpContext context)
{
- return (request.HttpContext.Connection.LocalIpAddress == null
- && request.HttpContext.Connection.RemoteIpAddress == null)
- || request.HttpContext.Connection.LocalIpAddress.Equals(request.HttpContext.Connection.RemoteIpAddress);
+ return (context.Connection.LocalIpAddress == null
+ && context.Connection.RemoteIpAddress == null)
+ || context.Connection.LocalIpAddress.Equals(context.Connection.RemoteIpAddress);
}
/// <summary>
- /// Extracts the remote IP address of the caller of the HTTP request.
+ /// Extracts the remote IP address of the caller of the HTTP context.
/// </summary>
- /// <param name="request">The HTTP request.</param>
+ /// <param name="context">The HTTP context.</param>
/// <returns>The remote caller IP address.</returns>
- public static string RemoteIp(this HttpRequest request)
+ public static string GetNormalizedRemoteIp(this HttpContext context)
{
- 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;
- }
- }
+ var ip = context.Connection.RemoteIpAddress;
if (ip.IsIPv4MappedToIPv6)
{
ip = ip.MapToIPv4();
}
- var normalizedIp = ip.ToString();
-
- request.HttpContext.Items["RemoteIp"] = normalizedIp;
- return normalizedIp;
+ return ip.ToString();
}
}
}