Path: redb-route/redb.Route/Core/UriMask.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Core/UriMask.cs
----
using System.Collections.Concurrent;
using System.Text.RegularExpressions;
namespace redb.Route.Core;
///
/// Endpoint URI mask shared by AdviceWith (MockEndpoints, WeaveByToUri), endpoint
/// interception and the header / property masks. Three pattern forms, checked in order:
///
/// - regex: prefix — a .NET regular expression (case-insensitive, culture-invariant,
/// one-second timeout, compiled once per pattern) matched against the URI as written, without its query
/// string, and with scheme:// collapsed to scheme:;
/// - trailing * — case-insensitive prefix match;
/// - otherwise — exact case-insensitive match.
///
/// A pattern without a query string ignores the candidate's query ("kafka://orders" matches
/// "kafka://orders?acks=all"); a pattern that carries one matches only that query
/// ("kafka://orders?acks=all" does not match "?acks=none"). Both scheme://path
/// and scheme:path spellings are tried on either side, so a pattern written one way matches an
/// endpoint written the other.
///
public static class UriMask
{
/// Prefix selecting the regular-expression pattern form.
public const string RegexPrefix = "regex:";
private static readonly ConcurrentDictionary Regexes = new(StringComparer.Ordinal);
///
/// Checks that is a usable mask and throws an
/// naming the problem otherwise (a blank mask, an invalid regular expression). Called where a mask is
/// declared, so a mistake fails there and not inside route compilation.
///
public static void Validate(string? pattern, string? paramName = null)
{
if (string.IsNullOrWhiteSpace(pattern))
throw new ArgumentException("The URI mask is blank.", paramName ?? nameof(pattern));
if (!pattern.StartsWith(RegexPrefix, StringComparison.OrdinalIgnoreCase))
return;
try
{
_ = CompiledRegex(pattern[RegexPrefix.Length..]);
}
catch (ArgumentException ex)
{
throw new ArgumentException($"The URI mask '{pattern}' is not a valid regular expression: {ex.Message}", paramName ?? nameof(pattern), ex);
}
}
/// Returns true when matches .
/// Mask: exact URI, prefix ending in *, or regex:<expression>.
/// Candidate endpoint URI.
public static bool IsMatch(string pattern, string uri)
{
ArgumentException.ThrowIfNullOrWhiteSpace(pattern);
ArgumentException.ThrowIfNullOrWhiteSpace(uri);
if (pattern.StartsWith(RegexPrefix, StringComparison.OrdinalIgnoreCase))
{
var regex = CompiledRegex(pattern[RegexPrefix.Length..]);
return RegexCandidates(uri).Any(regex.IsMatch);
}
if (pattern.EndsWith('*'))
{
// The full URI is tried, so a prefix that itself carries a query ("kafka://orders?acks=*")
// requires that query; a prefix without one matches whatever query the endpoint has.
var prefix = pattern[..^1];
return Spellings(prefix).Any(p => Spellings(uri).Any(c => c.StartsWith(p, StringComparison.OrdinalIgnoreCase)));
}
var candidate = pattern.Contains('?') ? uri : WithoutQuery(uri);
return Spellings(pattern).Any(p => Spellings(candidate).Any(c => string.Equals(c, p, StringComparison.OrdinalIgnoreCase)));
}
private static Regex CompiledRegex(string expression)
=> Regexes.GetOrAdd(expression, static e => new Regex(e, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant, TimeSpan.FromSeconds(1)));
/// What a regular expression is tried against: as written, query-stripped, and the stripped form with scheme:// collapsed.
private static IEnumerable RegexCandidates(string uri)
{
yield return uri;
var stripped = WithoutQuery(uri);
if (stripped.Length != uri.Length)
yield return stripped;
var collapsed = stripped.Replace("://", ":", StringComparison.Ordinal);
if (!string.Equals(collapsed, stripped, StringComparison.Ordinal))
yield return collapsed;
}
/// The two spellings of one URI: as written, and with scheme:// collapsed to scheme:.
private static IEnumerable Spellings(string uri)
{
yield return uri;
var collapsed = uri.Replace("://", ":", StringComparison.Ordinal);
if (!string.Equals(collapsed, uri, StringComparison.Ordinal))
yield return collapsed;
}
private static string WithoutQuery(string uri)
{
var queryIndex = uri.IndexOf('?');
return queryIndex >= 0 ? uri[..queryIndex] : uri;
}
}