diff options
| author | crobibero <cody@robibe.ro> | 2020-09-26 16:57:53 -0600 |
|---|---|---|
| committer | crobibero <cody@robibe.ro> | 2020-09-26 16:57:53 -0600 |
| commit | b7022e8dc17a50012418e0cb7b3066fb842758dd (patch) | |
| tree | 791478976b3223765be62b8557392fdf5776144d /MediaBrowser.Common/Extensions/HttpContextExtensions.cs | |
| parent | f443c534bfac9feea4c92033e85a9aa5097ec4f5 (diff) | |
| parent | 800c03961281d4f2ee6d3d7c9d9c0db6f45f506a (diff) | |
Merge remote-tracking branch 'upstream/master' into package-install-repo
Diffstat (limited to 'MediaBrowser.Common/Extensions/HttpContextExtensions.cs')
| -rw-r--r-- | MediaBrowser.Common/Extensions/HttpContextExtensions.cs | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/MediaBrowser.Common/Extensions/HttpContextExtensions.cs b/MediaBrowser.Common/Extensions/HttpContextExtensions.cs index d746207c7..19fa95480 100644 --- a/MediaBrowser.Common/Extensions/HttpContextExtensions.cs +++ b/MediaBrowser.Common/Extensions/HttpContextExtensions.cs @@ -1,4 +1,4 @@ -using MediaBrowser.Model.Services; +using System.Net; using Microsoft.AspNetCore.Http; namespace MediaBrowser.Common.Extensions @@ -8,26 +8,34 @@ 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 context. /// </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="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 HttpContext context) { - httpContext.Items[ServiceStackRequest] = request; + return (context.Connection.LocalIpAddress == null + && context.Connection.RemoteIpAddress == null) + || context.Connection.LocalIpAddress.Equals(context.Connection.RemoteIpAddress); } /// <summary> - /// Get the ServiceStack request. + /// Extracts the remote IP address of the caller of the HTTP context. /// </summary> - /// <param name="httpContext">The HttpContext instance.</param> - /// <returns>The service stack request instance.</returns> - public static IRequest GetServiceStackRequest(this HttpContext httpContext) + /// <param name="context">The HTTP context.</param> + /// <returns>The remote caller IP address.</returns> + public static string GetNormalizedRemoteIp(this HttpContext context) { - return (IRequest)httpContext.Items[ServiceStackRequest]; + // 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.ToString(); } } } |
