diff --git a/src/Middleware/HttpOverrides/src/ForwardedHeadersMiddleware.cs b/src/Middleware/HttpOverrides/src/ForwardedHeadersMiddleware.cs index bafd403c35..f22fd90588 100644 --- a/src/Middleware/HttpOverrides/src/ForwardedHeadersMiddleware.cs +++ b/src/Middleware/HttpOverrides/src/ForwardedHeadersMiddleware.cs @@ -9,7 +9,6 @@ using System.Runtime.CompilerServices; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.HttpOverrides.Internal; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.Extensions.Primitives; @@ -181,7 +180,7 @@ namespace Microsoft.AspNetCore.HttpOverrides _logger.LogWarning(1, "Parameter count mismatch between X-Forwarded-Host and X-Forwarded-For or X-Forwarded-Proto."); return; } - entryCount = Math.Max(forwardedHost.Length, entryCount); + entryCount = Math.Max(forwardedHost.Length, entryCount); } // Apply ForwardLimit, if any @@ -224,7 +223,7 @@ namespace Microsoft.AspNetCore.HttpOverrides bool applyChanges = false; int entriesConsumed = 0; - for ( ; entriesConsumed < sets.Length; entriesConsumed++) + for (; entriesConsumed < sets.Length; entriesConsumed++) { var set = sets[entriesConsumed]; if (checkFor) @@ -237,8 +236,7 @@ namespace Microsoft.AspNetCore.HttpOverrides break; } - IPEndPoint parsedEndPoint; - if (IPEndPointParser.TryParse(set.IpAndPortText, out parsedEndPoint)) + if (IPEndPoint.TryParse(set.IpAndPortText, out var parsedEndPoint)) { applyChanges = true; set.RemoteIpAndPort = parsedEndPoint; diff --git a/src/Middleware/HttpOverrides/src/Internal/IPEndPointParser.cs b/src/Middleware/HttpOverrides/src/Internal/IPEndPointParser.cs deleted file mode 100644 index a550ee70e4..0000000000 --- a/src/Middleware/HttpOverrides/src/Internal/IPEndPointParser.cs +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Globalization; -using System.Net; - -namespace Microsoft.AspNetCore.HttpOverrides.Internal -{ - public static class IPEndPointParser - { - public static bool TryParse(string addressWithPort, out IPEndPoint endpoint) - { - string addressPart = null; - string portPart = null; - IPAddress address; - endpoint = null; - - if (string.IsNullOrEmpty(addressWithPort)) - { - return false; - } - - var lastColonIndex = addressWithPort.LastIndexOf(':'); - if (lastColonIndex > 0) - { - // IPv4 with port or IPv6 - var closingIndex = addressWithPort.LastIndexOf(']'); - if (closingIndex > 0) - { - // IPv6 with brackets - addressPart = addressWithPort.Substring(1, closingIndex - 1); - if (closingIndex < lastColonIndex) - { - // IPv6 with port [::1]:80 - portPart = addressWithPort.Substring(lastColonIndex + 1); - } - } - else - { - // IPv6 without port or IPv4 - var firstColonIndex = addressWithPort.IndexOf(':'); - if (firstColonIndex != lastColonIndex) - { - // IPv6 ::1 - addressPart = addressWithPort; - } - else - { - // IPv4 with port 127.0.0.1:123 - addressPart = addressWithPort.Substring(0, firstColonIndex); - portPart = addressWithPort.Substring(firstColonIndex + 1); - } - } - } - else - { - // IPv4 without port - addressPart = addressWithPort; - } - - if (IPAddress.TryParse(addressPart, out address)) - { - if (portPart != null) - { - int port; - if (int.TryParse(portPart, NumberStyles.None, CultureInfo.InvariantCulture, out port)) - { - endpoint = new IPEndPoint(address, port); - return true; - } - return false; - } - endpoint = new IPEndPoint(address, 0); - return true; - } - return false; - } - } -} \ No newline at end of file diff --git a/src/Middleware/HttpOverrides/src/baseline.net45.json b/src/Middleware/HttpOverrides/src/baseline.net45.json index 4a5e5a6e3e..7182bc500f 100644 --- a/src/Middleware/HttpOverrides/src/baseline.net45.json +++ b/src/Middleware/HttpOverrides/src/baseline.net45.json @@ -416,37 +416,6 @@ } ], "GenericParameters": [] - }, - { - "Name": "Microsoft.AspNetCore.HttpOverrides.Internal.IPEndPointParser", - "Visibility": "Public", - "Kind": "Class", - "Abstract": true, - "Static": true, - "Sealed": true, - "ImplementedInterfaces": [], - "Members": [ - { - "Kind": "Method", - "Name": "TryParse", - "Parameters": [ - { - "Name": "addressWithPort", - "Type": "System.String" - }, - { - "Name": "endpoint", - "Type": "System.Net.IPEndPoint", - "Direction": "Out" - } - ], - "ReturnType": "System.Boolean", - "Static": true, - "Visibility": "Public", - "GenericParameter": [] - } - ], - "GenericParameters": [] } ] -} \ No newline at end of file +} diff --git a/src/Middleware/HttpOverrides/test/IPEndPointParserTest.cs b/src/Middleware/HttpOverrides/test/IPEndPointParserTest.cs deleted file mode 100644 index 11d13c401d..0000000000 --- a/src/Middleware/HttpOverrides/test/IPEndPointParserTest.cs +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Net; -using Xunit; - -namespace Microsoft.AspNetCore.HttpOverrides.Internal -{ - public class IPEndPointParserTests - { - [Theory] - [InlineData("127.0.0.1", "127.0.0.1", 0)] - [InlineData("127.0.0.1:1", "127.0.0.1", 1)] - [InlineData("1", "0.0.0.1", 0)] - [InlineData("1:1", "0.0.0.1", 1)] - [InlineData("::1", "::1", 0)] - [InlineData("[::1]", "::1", 0)] - [InlineData("[::1]:1", "::1", 1)] - public void ParsesCorrectly(string input, string expectedAddress, int expectedPort) - { - IPEndPoint endpoint; - var success = IPEndPointParser.TryParse(input, out endpoint); - Assert.True(success); - Assert.Equal(expectedAddress, endpoint.Address.ToString()); - Assert.Equal(expectedPort, endpoint.Port); - } - - [Theory] - [InlineData(null)] - [InlineData("[::1]:")] - [InlineData("[::1:")] - [InlineData("::1:")] - [InlineData("127:")] - [InlineData("127.0.0.1:")] - [InlineData("")] - [InlineData("[]")] - [InlineData("]")] - [InlineData("]:1")] - public void ShouldNotParse(string input) - { - IPEndPoint endpoint; - var success = IPEndPointParser.TryParse(input, out endpoint); - Assert.False(success); - Assert.Null(endpoint); - } - } -}