Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Commit

Permalink
Remove splitting of path and path base (#1050).
Browse files Browse the repository at this point in the history
  • Loading branch information
Cesar Blum Silveira committed Mar 15, 2017
1 parent 199329b commit 5c2af40
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 165 deletions.
55 changes: 2 additions & 53 deletions src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Frame.cs
Expand Up @@ -68,8 +68,6 @@ public abstract partial class Frame : IFrameControl, IHttpRequestLineHandler, IH

protected HttpVersion _httpVersion;

private readonly string _pathBase;

private int _remainingRequestHeadersBytesAllowed;
private int _requestHeadersParsed;

Expand All @@ -88,7 +86,6 @@ public Frame(ConnectionContext context)

ServerOptions = context.ListenerContext.ServiceContext.ServerOptions;

_pathBase = context.ListenerContext.ListenOptions.PathBase;
_parser = context.ListenerContext.ServiceContext.HttpParserFactory(this);

FrameControl = this;
Expand Down Expand Up @@ -1041,38 +1038,6 @@ public bool TakeStartLine(ReadableBuffer buffer, out ReadCursor consumed, out Re
return result;
}

private bool RequestUrlStartsWithPathBase(string requestUrl, out bool caseMatches)
{
caseMatches = true;

if (string.IsNullOrEmpty(_pathBase))
{
return false;
}

if (requestUrl.Length < _pathBase.Length || (requestUrl.Length > _pathBase.Length && requestUrl[_pathBase.Length] != '/'))
{
return false;
}

for (var i = 0; i < _pathBase.Length; i++)
{
if (requestUrl[i] != _pathBase[i])
{
if (char.ToLowerInvariant(requestUrl[i]) == char.ToLowerInvariant(_pathBase[i]))
{
caseMatches = false;
}
else
{
return false;
}
}
}

return true;
}

public bool TakeMessageHeaders(ReadableBuffer buffer, out ReadCursor consumed, out ReadCursor examined)
{
// Make sure the buffer is limited
Expand Down Expand Up @@ -1321,7 +1286,7 @@ private void OnOriginFormTarget(HttpMethod method, HttpVersion version, Span<byt

QueryString = query.GetAsciiStringNonNullCharacters();
RawTarget = rawTarget;
SetNormalizedPath(requestUrlPath);
Path = PathNormalizer.RemoveDotSegments(requestUrlPath);
}

private void OnAuthorityFormTarget(HttpMethod method, Span<byte> target)
Expand Down Expand Up @@ -1356,7 +1321,6 @@ private void OnAuthorityFormTarget(HttpMethod method, Span<byte> target)
// See https://tools.ietf.org/html/rfc3986#section-3.2
RawTarget = target.GetAsciiStringNonNullCharacters();
Path = string.Empty;
PathBase = string.Empty;
QueryString = string.Empty;
}

Expand All @@ -1371,7 +1335,6 @@ private void OnAsteriskFormTarget(HttpMethod method)

RawTarget = Asterisk;
Path = string.Empty;
PathBase = string.Empty;
QueryString = string.Empty;
}

Expand All @@ -1397,25 +1360,11 @@ private void OnAbsoluteFormTarget(Span<byte> target, Span<byte> query)
RejectRequestTarget(target);
}

SetNormalizedPath(uri.LocalPath);
Path = PathNormalizer.RemoveDotSegments(uri.LocalPath);
// don't use uri.Query because we need the unescaped version
QueryString = query.GetAsciiStringNonNullCharacters();
}

private void SetNormalizedPath(string requestPath)
{
var normalizedTarget = PathNormalizer.RemoveDotSegments(requestPath);
if (RequestUrlStartsWithPathBase(normalizedTarget, out bool caseMatches))
{
PathBase = caseMatches ? _pathBase : normalizedTarget.Substring(0, _pathBase.Length);
Path = normalizedTarget.Substring(_pathBase.Length);
}
else
{
Path = normalizedTarget;
}
}

private unsafe static string GetUtf8String(Span<byte> path)
{
// .NET 451 doesn't have pointer overloads for Encoding.GetString so we
Expand Down

This file was deleted.

Expand Up @@ -18,6 +18,7 @@
using Microsoft.AspNetCore.Server.Kestrel.Internal.Networking;
using Microsoft.AspNetCore.Testing;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Newtonsoft.Json;
Expand Down Expand Up @@ -520,6 +521,53 @@ public async Task CanHandleRequestsWithUrlInAbsoluteForm(string requestUrl, stri
}
}

[Theory]
[InlineData("/base", "/base")]
[InlineData("/base", "/base/")]
[InlineData("/base", "/base/something")]
[InlineData("/base", "/base/something/")]
[InlineData("/base/something", "/base/something")]
[InlineData("/base/something", "/base/something/")]
[InlineData("/base/something", "/base/something/more")]
[InlineData("/base/something", "/base/something/more/")]
public async Task DoesNotSplitPathBase(string registerPathBase, string requestPath)
{
var done = new SemaphoreSlim(0);

string contextPathBase = null;
string contextPath = null;

var builder = new WebHostBuilder()
.UseKestrel()
.UseUrls($"http://127.0.0.1:0{registerPathBase}")
.Configure(app =>
{
app.Run(context =>
{
contextPathBase = context.Request.PathBase;
contextPath = context.Request.Path;
done.Release();
return TaskCache.CompletedTask;
});
});

using (var host = builder.Build())
{
host.Start();

using (var connection = new TestConnection(host.GetPort()))
{
await connection.Send($"GET {requestPath} HTTP/1.1\r\n\r\n");
await done.WaitAsync(TimeSpan.FromSeconds(10));

Assert.Equal("", contextPathBase);
Assert.Equal(requestPath, contextPath);
}
}
}

private async Task TestRemoteIPAddress(string registerAddress, string requestAddress, string expectAddress)
{
var builder = new WebHostBuilder()
Expand Down
Expand Up @@ -19,18 +19,13 @@ public class RequestTargetProcessingTests
public async Task RequestPathIsNotNormalized()
{
var testContext = new TestServiceContext();

var listenOptions = new ListenOptions(new IPEndPoint(IPAddress.Loopback, 0))
{
PathBase = "/\u0041\u030A"
};
var listenOptions = new ListenOptions(new IPEndPoint(IPAddress.Loopback, 0));

using (var server = new TestServer(async context =>
{
Assert.Equal("/\u0041\u030A", context.Request.PathBase.Value);
Assert.Equal("/B/\u0041\u030A", context.Request.Path.Value);
Assert.Equal("/\u0041\u030A/B/\u0041\u030A", context.Request.Path.Value);
context.Response.Headers["Content-Length"] = new[] { "11" };
context.Response.Headers.ContentLength = 11;
await context.Response.WriteAsync("Hello World");
}, testContext, listenOptions))
{
Expand Down

0 comments on commit 5c2af40

Please sign in to comment.