< Summary - Jellyfin

Information
Class: MediaBrowser.Common.Extensions.HttpContextExtensions
Assembly: MediaBrowser.Common
File(s): /srv/git/jellyfin/MediaBrowser.Common/Extensions/HttpContextExtensions.cs
Line coverage
85%
Covered lines: 6
Uncovered lines: 1
Coverable lines: 7
Total lines: 41
Line coverage: 85.7%
Branch coverage
62%
Covered branches: 5
Total branches: 8
Branch coverage: 62.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsLocal(...)50%44100%
GetNormalizedRemoteIP(...)75%4.25475%

File(s)

/srv/git/jellyfin/MediaBrowser.Common/Extensions/HttpContextExtensions.cs

#LineLine coverage
 1using System.Net;
 2using Microsoft.AspNetCore.Http;
 3
 4namespace MediaBrowser.Common.Extensions
 5{
 6    /// <summary>
 7    /// Static class containing extension methods for <see cref="HttpContext"/>.
 8    /// </summary>
 9    public static class HttpContextExtensions
 10    {
 11        /// <summary>
 12        /// Checks the origin of the HTTP context.
 13        /// </summary>
 14        /// <param name="context">The incoming HTTP context.</param>
 15        /// <returns><c>true</c> if the request is coming from LAN, <c>false</c> otherwise.</returns>
 16        public static bool IsLocal(this HttpContext context)
 17        {
 18018            return (context.Connection.LocalIpAddress is null
 18019                    && context.Connection.RemoteIpAddress is null)
 18020                   || Equals(context.Connection.LocalIpAddress, context.Connection.RemoteIpAddress);
 21        }
 22
 23        /// <summary>
 24        /// Extracts the remote IP address of the caller of the HTTP context.
 25        /// </summary>
 26        /// <param name="context">The HTTP context.</param>
 27        /// <returns>The remote caller IP address.</returns>
 28        public static IPAddress GetNormalizedRemoteIP(this HttpContext context)
 29        {
 30            // Default to the loopback address if no RemoteIpAddress is specified (i.e. during integration tests)
 13531            var ip = context.Connection.RemoteIpAddress ?? IPAddress.Loopback;
 32
 13533            if (ip.IsIPv4MappedToIPv6)
 34            {
 035                ip = ip.MapToIPv4();
 36            }
 37
 13538            return ip;
 39        }
 40    }
 41}