Skip to content

Use UriHelper.BuildAbsolute and UriHelper.BuildRelative for RedirectRule #28903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 49 additions & 23 deletions src/Middleware/Rewrite/src/RedirectRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
using System;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Rewrite.Logging;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNetCore.Rewrite
{
Expand Down Expand Up @@ -34,13 +35,14 @@ public RedirectRule(string regex, string replacement, int statusCode)

public virtual void ApplyRule(RewriteContext context)
{
var path = context.HttpContext.Request.Path;
var pathBase = context.HttpContext.Request.PathBase;
var request = context.HttpContext.Request;
var path = request.Path;
var pathBase = request.PathBase;

Match initMatchResults;
if (path == PathString.Empty)
if (!path.HasValue)
{
initMatchResults = InitialMatch.Match(path.ToString());
initMatchResults = InitialMatch.Match(string.Empty);
}
else
{
Expand All @@ -56,31 +58,55 @@ public virtual void ApplyRule(RewriteContext context)
response.StatusCode = StatusCode;
context.Result = RuleResult.EndResponse;

if (string.IsNullOrEmpty(newPath))
{
response.Headers[HeaderNames.Location] = pathBase.HasValue ? pathBase.Value : "/";
return;
}

if (newPath.IndexOf(Uri.SchemeDelimiter, StringComparison.Ordinal) == -1 && newPath[0] != '/')
{
newPath = '/' + newPath;
}
string encodedPath;

var split = newPath.IndexOf('?');
if (split >= 0)
if (string.IsNullOrEmpty(newPath))
{
var query = context.HttpContext.Request.QueryString.Add(
QueryString.FromUriComponent(
newPath.Substring(split)));
// not using the HttpContext.Response.redirect here because status codes may be 301, 302, 307, 308
response.Headers[HeaderNames.Location] = pathBase + newPath.Substring(0, split) + query.ToUriComponent();
encodedPath = pathBase.HasValue ? pathBase.Value : "/";
}
else
{
response.Headers[HeaderNames.Location] = pathBase + newPath + context.HttpContext.Request.QueryString.ToUriComponent();
var host = default(HostString);
var schemeSplit = newPath.IndexOf(Uri.SchemeDelimiter, StringComparison.Ordinal);
if (schemeSplit >= 0)
{
schemeSplit += Uri.SchemeDelimiter.Length;
var pathSplit = newPath.IndexOf('/', schemeSplit);

if (pathSplit == -1)
{
host = new HostString(newPath.Substring(schemeSplit));
newPath = "/";
}
else
{
host = new HostString(newPath.Substring(schemeSplit, pathSplit - schemeSplit));
newPath = newPath.Substring(pathSplit);
}
}

if (newPath[0] != '/')
{
newPath = '/' + newPath;
}

var resolvedQuery = request.QueryString;
var resolvedPath = newPath;
var querySplit = newPath.IndexOf('?');
if (querySplit >= 0)
{
resolvedQuery = request.QueryString.Add(QueryString.FromUriComponent(newPath.Substring(querySplit)));
resolvedPath = newPath.Substring(0, querySplit);
}

encodedPath = host.HasValue
? UriHelper.BuildAbsolute(request.Scheme, host, pathBase, resolvedPath, resolvedQuery, default)
: UriHelper.BuildRelative(pathBase, resolvedPath, resolvedQuery, default);
}

// not using the HttpContext.Response.redirect here because status codes may be 301, 302, 307, 308
response.Headers[HeaderNames.Location] = encodedPath;

context.Logger.RedirectedRequest(newPath);
}
}
Expand Down
23 changes: 18 additions & 5 deletions src/Middleware/Rewrite/test/MiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,19 @@ public async Task CheckRewritePath()
Assert.Equal("http://example.com/foo", response);
}

[Fact]
public async Task CheckRedirectPath()
[Theory]
[InlineData("(.*)", "http://example.com/$1", null, "path", "http://example.com/path")]
[InlineData("(.*)", "http://example.com", null, "", "http://example.com/")]
[InlineData("(z*)", "$1", null, "path", "/")]
[InlineData("(z*)", "http://example.com/$1", null, "path", "http://example.com/")]
[InlineData("(z*)", "$1", "http://example.com/pathBase", "/pathBase/path", "/pathBase")]
[InlineData("path/(.*)", "path?value=$1", null, "path/value", "/path?value=value")]
[InlineData("path/(.*)", "path?param=$1", null, "path/value?param1=OtherValue", "/path?param1=OtherValue&param=value")]
[InlineData("path/(.*)", "http://example.com/pathBase/path?param=$1", "http://example.com/pathBase", "path/value?param1=OtherValue", "http://example.com/pathBase/path?param1=OtherValue&param=value")]
[InlineData("path/(.*)", "http://hoψst.com/pÂthBase/path?parãm=$1", "http://example.com/pathBase", "path/value?päram1=OtherValüe", "http://xn--host-cpd.com/p%C3%82thBase/path?p%C3%A4ram1=OtherVal%C3%BCe&parãm=value")]
public async Task CheckRedirectPath(string pattern, string replacement, string baseAddress, string requestUrl, string expectedUrl)
{
var options = new RewriteOptions().AddRedirect("(.*)", "http://example.com/$1", statusCode: StatusCodes.Status301MovedPermanently);
var options = new RewriteOptions().AddRedirect(pattern, replacement, statusCode: StatusCodes.Status301MovedPermanently);
using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
Expand All @@ -63,10 +72,14 @@ public async Task CheckRedirectPath()
await host.StartAsync();

var server = host.GetTestServer();
if (!string.IsNullOrEmpty(baseAddress))
{
server.BaseAddress = new Uri(baseAddress);
}

var response = await server.CreateClient().GetAsync("foo");
var response = await server.CreateClient().GetAsync(requestUrl);

Assert.Equal("http://example.com/foo", response.Headers.Location.OriginalString);
Assert.Equal(expectedUrl, response.Headers.Location.OriginalString);
}

[Fact]
Expand Down