This commit is contained in:
James Newton-King 2018-12-21 09:12:33 +13:00
commit 1ad77cf94e
No known key found for this signature in database
GPG Key ID: 0A66B2F456BF5526
4 changed files with 4 additions and 163 deletions

View File

@ -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;

View File

@ -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;
}
}
}

View File

@ -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": []
}
]
}
}

View File

@ -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);
}
}
}