Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ public class CustomRedirectCollection : IEnumerable<CustomRedirect>
/// </summary>
private readonly Dictionary<string, CustomRedirect> _quickLookupTable = new(StringComparer.OrdinalIgnoreCase);

/// <summary>
/// Cache of URLs sorted ZA for look up of partially matched URLs
/// </summary>
private KeyValuePair<string, CustomRedirect>[] _redirectsZACache;

public CustomRedirectCollection()
{
}
Expand Down Expand Up @@ -87,74 +82,13 @@ public void Add(CustomRedirect customRedirect)

// Add to quick look up table too
_quickLookupTable.Add(oldUrl, customRedirect);

// clean cache
_redirectsZACache = null;
}

private CustomRedirect FindInternal(string url)
{
url = HttpUtility.UrlDecode(url) ?? string.Empty;
if (_quickLookupTable.TryGetValue(url, out var redirect))
{
return redirect;
}

// working with local copy to avoid multi-threading issues
var redirectsZA = _redirectsZACache;
if (redirectsZA == null)
{
redirectsZA = _quickLookupTable.OrderByDescending(x => x.Key, StringComparer.OrdinalIgnoreCase).ToArray();
_redirectsZACache = redirectsZA;
}

var path = url.AsPathSpan();
var query = url.AsQuerySpan();

// No exact match could be done, so we'll check if the 404 url
// starts with one of the urls we're matching against. This
// will be kind of a wild card match (even though we only check
// for the start of the url
// Example: http://www.mysite.com/news/mynews.html is not found
// We have defined an "<old>/news</old>" entry in the config
// file. We will get a match on the /news part of /news/myne...
// Depending on the skip wild card append setting, we will either
// redirect using the <new> url as is, or we'll append the 404
// url to the <new> url.
foreach (var redirectPair in redirectsZA)
{
var oldUrl = redirectPair.Key;
if (string.IsNullOrWhiteSpace(oldUrl))
{
continue;
}

var oldPath = oldUrl.AsPathSpan();
var oldQuery = oldUrl.AsQuerySpan();

// See if this "old" url (the one that cannot be found) starts with one
if (path.UrlPathMatch(oldPath) && query.StartsWith(oldQuery, StringComparison.InvariantCultureIgnoreCase))
{
var cr = redirectPair.Value;
if (cr.State == (int)RedirectState.Ignored)
{
return null;
}

if (cr.WildCardSkipAppend)
{
// Remove path from original url but keep query string.
return CreateSubSegmentRedirect(path, query, cr, oldPath, true);
}

if (UrlIsOldUrlsSubSegment(path, oldUrl))
{
return CreateSubSegmentRedirect(path, query, cr, oldPath);
}
}
}

return null;

return _quickLookupTable.GetValueOrDefault(url);
}
Comment on lines 87 to 92
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The removal of the wildcard matching logic in FindInternal renders several private methods in this class (CreateSubSegmentRedirect, BuildPath, BuildQuery, UrlIsOldUrlsSubSegment) as dead code. Additionally, many unit tests in CustomRedirectCollectionTests.cs (such as Find_handles_path_and_query_parameters_when_matching_sub_segment and Find_finds_redirect_and_appends_relative_path_when_not_found_url_starts_with_stored_url) will now fail because they rely on the removed wildcard/sub-segment matching functionality. Please ensure that the unused methods are removed and the test suite is updated to reflect the new simplified redirect logic.


private static CustomRedirect CreateSubSegmentRedirect(
Expand Down