Skip to content
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

Custom transform being processed twice. #6

Open
ghstahl opened this issue Jul 9, 2014 · 1 comment
Open

Custom transform being processed twice. #6

ghstahl opened this issue Jul 9, 2014 · 1 comment

Comments

@ghstahl
Copy link

ghstahl commented Jul 9, 2014

This initially manifested itself by getting 2 calls to the IHttpModule from a single perceived request.

How to reproduce.

  1. My Rule
where I have a custom encoder, which rewrites the url to /CDNInternal/CC?remaining=abc 1. My Custom Transform public class CDNUrlEncodeTransform : IRewriteTransform { public string ApplyTransform(string input) { const string strRegex = @"(?^.+?(?=\/))\/(?.*)"; var myRegex = new Regex(strRegex, RegexOptions.None); var strTargetString = input; var matches = myRegex.Matches(strTargetString); foreach (Match myMatch in matches) { if (myMatch.Success) { var key = myMatch.Groups["key"].Value; var remaining = myMatch.Groups["remaining"].Value; var encodedRemaining = HttpUtility.UrlEncode(remaining); var finalUrl = string.Format("/CDNInternal/{0}?remaining={1}", key, encodedRemaining); return finalUrl; } }
    return input;
}

public string Name
{
    get
    {
        return "CDNUrlEncode";
    }
}

}
2. What happens.
The first request comes in:
If you check the Request you will see the following in the second request.
RequestUrl = /CDN/CC/Plugins/a.1.2.3.4/a.jpg
RawUrl = /CDN/CC/Plugins/a.1.2.3.4/a.jpg

Custom Transform transforms it to the following:
From: /CDN/CC/Plugins/a.1.2.3.4/a.jpg
To: /CDNInternal/CC?remaining=[encoded Plugins/a.1.2.3.4/a.jpg]

This results in a second request coming in reflecting the new rewrite. If you check the Request you will see the following in the second request. The RequestUrl and the RawUrl are the same. Might be a asp.net MVC 5.2 issue.

RequestUrl = /CDNInternal/CC?remaining=[encoded Plugins/a.1.2.3.4/a.jpg]
RawUrl = /CDN/CC/Plugins/a.1.2.3.4/a.jpg

This causes a rewrite to happen again because it matches the original rule. I would think this should cause an infinite look, but it stopped at 2. Did not investigate that further.

Here is the fix:
https://github.com/sethyates/urlrewriter/blob/master/src/RewriterEngine.cs

public class RewriterEngine
{
public void Rewrite()
{
string originalUrl = _httpContext.RequestUrl.PathAndQuery.Replace("+", " ");
...}
}

replace
string originalUrl = _httpContext.RawUrl.Replace("+", " ");
with
string originalUrl = _httpContext.RequestUrl.PathAndQuery.Replace("+", " ");

@sethyates
Copy link
Owner

Pull requests are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants