Skip to content

Commit bfbe360

Browse files
Fixes processing hosts and URLs to watch (#1107)
1 parent 0c2a6a7 commit bfbe360

File tree

5 files changed

+18
-12
lines changed

5 files changed

+18
-12
lines changed

dev-proxy-abstractions/ProxyUtils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,9 @@ public static void MergeHeaders(IList<MockResponseHeader> allHeaders, IList<Mock
356356

357357
public static JsonSerializerOptions JsonSerializerOptions => jsonSerializerOptions;
358358

359-
public static bool MatchesUrlToWatch(ISet<UrlToWatch> watchedUrls, string url)
359+
public static bool MatchesUrlToWatch(ISet<UrlToWatch> watchedUrls, string url, bool evaluateWildcards = false)
360360
{
361-
if (url.Contains('*'))
361+
if (evaluateWildcards && url.Contains('*'))
362362
{
363363
// url contains a wildcard, so convert it to regex and compare
364364
var match = watchedUrls.FirstOrDefault(r => {

dev-proxy-plugins/Mocks/CrudApiPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public override async Task RegisterAsync()
101101
_configuration.Auth = CrudApiAuthType.None;
102102
}
103103

104-
if (!ProxyUtils.MatchesUrlToWatch(UrlsToWatch, _configuration.BaseUrl))
104+
if (!ProxyUtils.MatchesUrlToWatch(UrlsToWatch, _configuration.BaseUrl, true))
105105
{
106106
Logger.LogWarning(
107107
"The base URL of the API {baseUrl} does not match any URL to watch. The {plugin} plugin will be disabled. To enable it, add {url}* to the list of URLs to watch and restart Dev Proxy.",

dev-proxy-plugins/Mocks/MockResponsePlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private void ValidateMocks()
137137
continue;
138138
}
139139

140-
if (!ProxyUtils.MatchesUrlToWatch(UrlsToWatch, mock.Request.Url))
140+
if (!ProxyUtils.MatchesUrlToWatch(UrlsToWatch, mock.Request.Url, true))
141141
{
142142
unmatchedMockUrls.Add(mock.Request.Url);
143143
}

dev-proxy-plugins/RandomErrors/GenericRandomErrorPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ private void ValidateErrors()
268268
continue;
269269
}
270270

271-
if (!ProxyUtils.MatchesUrlToWatch(UrlsToWatch, error.Request.Url))
271+
if (!ProxyUtils.MatchesUrlToWatch(UrlsToWatch, error.Request.Url, true))
272272
{
273273
unmatchedErrorUrls.Add(error.Request.Url);
274274
}

dev-proxy/ProxyEngine.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,6 @@ private void LoadHostNamesFromUrls()
293293
{
294294
foreach (var urlToWatch in _urlsToWatch)
295295
{
296-
if (urlToWatch.Exclude)
297-
{
298-
continue;
299-
}
300-
301296
// extract host from the URL
302297
string urlToWatchPattern = Regex.Unescape(urlToWatch.Url.ToString())
303298
.Trim('^', '$')
@@ -329,7 +324,7 @@ private void LoadHostNamesFromUrls()
329324
// don't add the same host twice
330325
if (!_hostsToWatch.Any(h => h.Url.ToString() == hostRegex.ToString()))
331326
{
332-
_hostsToWatch.Add(new UrlToWatch(hostRegex));
327+
_hostsToWatch.Add(new UrlToWatch(hostRegex, urlToWatch.Exclude));
333328
}
334329
}
335330
}
@@ -520,7 +515,18 @@ private async Task HandleRequestAsync(SessionEventArgs e, ProxyRequestArgs proxy
520515

521516
private static void AddProxyHeader(Request r) => r.Headers?.AddHeader("Via", $"{r.HttpVersion} dev-proxy/{ProxyUtils.ProductVersion}");
522517

523-
private bool IsProxiedHost(string hostName) => _hostsToWatch.Any(h => h.Url.IsMatch(hostName));
518+
private bool IsProxiedHost(string hostName)
519+
{
520+
var urlMatch = _hostsToWatch.FirstOrDefault(h => h.Url.IsMatch(hostName));
521+
if (urlMatch is null)
522+
{
523+
return false;
524+
}
525+
else
526+
{
527+
return !urlMatch.Exclude;
528+
}
529+
}
524530

525531
private bool IsIncludedByHeaders(HeaderCollection requestHeaders)
526532
{

0 commit comments

Comments
 (0)