Save condition backcollection between runs (#10776)

This commit is contained in:
Justin Kotalik 2019-06-03 22:31:38 -07:00 committed by GitHub
parent 3bb092bdab
commit 45b0d0d0de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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.Collections.Generic;
@ -39,6 +39,8 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite
context.Logger?.ModRewriteNotMatchedRule();
return;
}
condBackReferences = condResult.BackReferences;
}
// At this point, we know our rule passed, first apply pre conditions,
@ -51,4 +53,4 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite
}
}
}
}
}

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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;
@ -308,5 +308,25 @@ namespace Microsoft.AspNetCore.Rewrite.Tests.ModRewrite
Assert.Equal("/foo", response.Headers.Location.OriginalString);
}
[Fact]
public async Task CapturedVariablesInConditionsArePreservedToRewriteRule()
{
var options = new RewriteOptions().AddApacheModRewrite(new StringReader(@"RewriteCond %{REQUEST_URI} /home
RewriteCond %{QUERY_STRING} report_id=(.+)
RewriteRule (.*) http://localhost:80/home/report/%1 [R=301,L,QSD]"));
var builder = new WebHostBuilder().Configure(app =>
{
app.UseRewriter(options);
app.Run(context => context.Response.WriteAsync(
context.Request.Path +
context.Request.QueryString));
});
var server = new TestServer(builder) { BaseAddress = new Uri("http://localhost:5000/foo") };
var response = await server.CreateClient().GetAsync("/home?report_id=123");
Assert.Equal("http://localhost:80/home/report/123", response.Headers.Location.OriginalString);
}
}
}