aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Extensions/HttpContextExtensions.cs
diff options
context:
space:
mode:
authorferferga <ferferga.fer@gmail.com>2020-10-19 17:28:07 +0200
committerferferga <ferferga.fer@gmail.com>2020-10-19 17:28:07 +0200
commit9fd01fade6ac971ba72a2e7dd54e2295f23839bc (patch)
tree20a5ff4a1621e765fc93c0e53b149edb61ff3654 /MediaBrowser.Common/Extensions/HttpContextExtensions.cs
parentba03ed65fe64b724b3e8b5b94b9cbe1075c61da2 (diff)
parent49ac4c4044b1777dc3a25544aead7b0b15b953e8 (diff)
Remove "download images in advance" option
Diffstat (limited to 'MediaBrowser.Common/Extensions/HttpContextExtensions.cs')
-rw-r--r--MediaBrowser.Common/Extensions/HttpContextExtensions.cs41
1 files changed, 41 insertions, 0 deletions
diff --git a/MediaBrowser.Common/Extensions/HttpContextExtensions.cs b/MediaBrowser.Common/Extensions/HttpContextExtensions.cs
new file mode 100644
index 000000000..19fa95480
--- /dev/null
+++ b/MediaBrowser.Common/Extensions/HttpContextExtensions.cs
@@ -0,0 +1,41 @@
+using System.Net;
+using Microsoft.AspNetCore.Http;
+
+namespace MediaBrowser.Common.Extensions
+{
+ /// <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 LAN, <c>false</c> otherwise.</returns>
+ public static bool IsLocal(this HttpContext context)
+ {
+ 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 context.
+ /// </summary>
+ /// <param name="context">The HTTP context.</param>
+ /// <returns>The remote caller IP address.</returns>
+ public static string 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.ToString();
+ }
+ }
+}