Skip to content

Commit

Permalink
chore align route handling with upstream (#3057)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Nov 19, 2024
1 parent 7797511 commit 497b7b0
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 133 deletions.
106 changes: 54 additions & 52 deletions src/Playwright/Core/BrowserContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,28 +487,28 @@ public async Task<IPage> NewPageAsync()
}

[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteAsync(string url, Action<IRoute> handler, BrowserContextRouteOptions options = default)
=> RouteAsync(new Regex(CombineUrlWithBase(url).GlobToRegex()), null, handler, options);
public Task RouteAsync(string globMatch, Func<IRoute, Task> handler, BrowserContextRouteOptions options = null)
=> RouteAsync(globMatch, null, null, handler, options);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteAsync(string url, Func<IRoute, Task> handler, BrowserContextRouteOptions options = null)
=> RouteAsync(new Regex(CombineUrlWithBase(url).GlobToRegex()), null, handler, options);
public Task RouteAsync(string globMatch, Action<IRoute> handler, BrowserContextRouteOptions options = null)
=> RouteAsync(globMatch, null, null, handler, options);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteAsync(Regex url, Action<IRoute> handler, BrowserContextRouteOptions options = default)
=> RouteAsync(url, null, handler, options);
public Task RouteAsync(Regex reMatch, Action<IRoute> handler, BrowserContextRouteOptions options = null)
=> RouteAsync(null, reMatch, null, handler, options);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteAsync(Regex url, Func<IRoute, Task> handler, BrowserContextRouteOptions options = default)
=> RouteAsync(url, null, handler, options);
public Task RouteAsync(Regex reMatch, Func<IRoute, Task> handler, BrowserContextRouteOptions options = null)
=> RouteAsync(null, reMatch, null, handler, options);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteAsync(Func<string, bool> url, Action<IRoute> handler, BrowserContextRouteOptions options = default)
=> RouteAsync(null, url, handler, options);
public Task RouteAsync(Func<string, bool> funcMatch, Action<IRoute> handler, BrowserContextRouteOptions options = null)
=> RouteAsync(null, null, funcMatch, handler, options);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteAsync(Func<string, bool> url, Func<IRoute, Task> handler, BrowserContextRouteOptions options = default)
=> RouteAsync(null, url, handler, options);
public Task RouteAsync(Func<string, bool> funcMatch, Func<IRoute, Task> handler, BrowserContextRouteOptions options = null)
=> RouteAsync(null, null, funcMatch, handler, options);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task SetExtraHTTPHeadersAsync(IEnumerable<KeyValuePair<string, string>> headers)
Expand Down Expand Up @@ -558,28 +558,28 @@ public async Task UnrouteAllAsync(BrowserContextUnrouteAllOptions options = defa
}

[MethodImpl(MethodImplOptions.NoInlining)]
public Task UnrouteAsync(string urlString, Action<IRoute> handler = default)
=> UnrouteAsync(new Regex(CombineUrlWithBase(urlString).GlobToRegex()), null, handler);
public Task UnrouteAsync(string globMatch, Action<IRoute> handler)
=> UnrouteAsync(globMatch, null, null, handler);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task UnrouteAsync(string urlString, Func<IRoute, Task> handler = null)
=> UnrouteAsync(new Regex(CombineUrlWithBase(urlString).GlobToRegex()), null, handler);
public Task UnrouteAsync(string globMatch, Func<IRoute, Task> handler)
=> UnrouteAsync(globMatch, null, null, handler);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task UnrouteAsync(Regex urlRegex, Action<IRoute> handler = default)
=> UnrouteAsync(urlRegex, null, handler);
public Task UnrouteAsync(Regex reMatch, Action<IRoute> handler)
=> UnrouteAsync(null, reMatch, null, handler);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task UnrouteAsync(Regex urlRegex, Func<IRoute, Task> handler = default)
=> UnrouteAsync(urlRegex, null, handler);
public Task UnrouteAsync(Regex reMatch, Func<IRoute, Task> handler)
=> UnrouteAsync(null, reMatch, null, handler);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task UnrouteAsync(Func<string, bool> urlFunc, Action<IRoute> handler = default)
=> UnrouteAsync(null, urlFunc, handler);
public Task UnrouteAsync(Func<string, bool> funcMatch, Action<IRoute> handler)
=> UnrouteAsync(null, null, funcMatch, handler);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task UnrouteAsync(Func<string, bool> urlFunc, Func<IRoute, Task> handler = default)
=> UnrouteAsync(null, urlFunc, handler);
public Task UnrouteAsync(Func<string, bool> funcMatch, Func<IRoute, Task> handler)
=> UnrouteAsync(null, null, funcMatch, handler);

internal string _effectiveCloseReason()
{
Expand Down Expand Up @@ -674,9 +674,7 @@ internal async Task OnRouteAsync(Route route)
{
return;
}
var matches = routeHandler.Regex?.IsMatch(route.Request.Url) == true ||
routeHandler.Function?.Invoke(route.Request.Url) == true;
if (!matches)
if (!routeHandler.Matches(route.Request.Url))
{
continue;
}
Expand Down Expand Up @@ -731,11 +729,16 @@ internal string CombineUrlWithBase(string url)
return URLMatch.JoinWithBaseURL(Options?.BaseURL, url);
}

private Task RouteAsync(Regex urlRegex, Func<string, bool> urlFunc, Delegate handler, BrowserContextRouteOptions options)
private Task RouteAsync(string globMatch, Regex reMatch, Func<string, bool> funcMatch, Delegate handler, BrowserContextRouteOptions options)
=> RouteAsync(new()
{
Regex = urlRegex,
Function = urlFunc,
urlMatcher = new URLMatch()
{
glob = globMatch,
re = reMatch,
func = funcMatch,
baseURL = Options.BaseURL,
},
Handler = handler,
Times = options?.Times,
});
Expand All @@ -746,23 +749,22 @@ private Task RouteAsync(RouteHandler setting)
return UpdateInterceptionAsync();
}

private Task UnrouteAsync(Regex urlRegex, Func<string, bool> urlFunc, Delegate handler = null)
=> UnrouteAsync(new()
{
Function = urlFunc,
Regex = urlRegex,
Handler = handler,
});

private Task UnrouteAsync(RouteHandler setting)
private async Task UnrouteAsync(string globMatch, Regex reMatch, Func<string, bool> funcMatch, Delegate handler)
{
var newRoutes = new List<RouteHandler>();
newRoutes.AddRange(_routes.Where(r =>
(setting.Regex != null && !(r.Regex == setting.Regex || (r.Regex.ToString() == setting.Regex.ToString() && r.Regex.Options == setting.Regex.Options))) ||
(setting.Function != null && r.Function != setting.Function) ||
(setting.Handler != null && r.Handler != setting.Handler)));
_routes = newRoutes;
return UpdateInterceptionAsync();
var removed = new List<RouteHandler>();
var remaining = new List<RouteHandler>();
foreach (var routeHandler in _routes)
{
if (routeHandler.urlMatcher.Equals(globMatch, reMatch, funcMatch, Options.BaseURL) && (handler == null || routeHandler.Handler == handler))
{
removed.Add(routeHandler);
}
else
{
remaining.Add(routeHandler);
}
}
await UnrouteInternalAsync(removed, remaining, UnrouteBehavior.Default).ConfigureAwait(false);
}

private async Task UnrouteInternalAsync(List<RouteHandler> removed, List<RouteHandler> remaining, UnrouteBehavior? behavior)
Expand All @@ -773,7 +775,7 @@ private async Task UnrouteInternalAsync(List<RouteHandler> removed, List<RouteHa
{
return;
}
var tasks = removed.Select(routeHandler => routeHandler.StopAsync((UnrouteBehavior)behavior));
var tasks = removed.Select(routeHandler => routeHandler.StopAsync(behavior.Value));
await Task.WhenAll(tasks).ConfigureAwait(false);
}

Expand Down Expand Up @@ -916,12 +918,12 @@ private Task RouteWebSocketAsync(string globMatch, Regex reMatch, Func<string, b
{
_webSocketRoutes.Insert(0, new WebSocketRouteHandler()
{
URL = new URLMatch()
urlMatcher = new URLMatch()
{
BaseURL = Options.BaseURL,
globMatch = globMatch,
reMatch = reMatch,
funcMatch = funcMatch,
baseURL = Options.BaseURL,
glob = globMatch,
re = reMatch,
func = funcMatch,
},
Handler = handler,
});
Expand Down
113 changes: 57 additions & 56 deletions src/Playwright/Core/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -932,59 +932,59 @@ public Task AddInitScriptAsync(string script, string scriptPath)
});

[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteAsync(string url, Func<IRoute, Task> handler, PageRouteOptions options = null)
=> RouteAsync(new Regex(Context.CombineUrlWithBase(url).GlobToRegex()), null, handler, options);
public Task RouteAsync(string globMatch, Func<IRoute, Task> handler, PageRouteOptions options = null)
=> RouteAsync(globMatch, null, null, handler, options);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteAsync(string url, Action<IRoute> handler, PageRouteOptions options = null)
=> RouteAsync(new Regex(Context.CombineUrlWithBase(url).GlobToRegex()), null, handler, options);
public Task RouteAsync(string globMatch, Action<IRoute> handler, PageRouteOptions options = null)
=> RouteAsync(globMatch, null, null, handler, options);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteAsync(Regex url, Action<IRoute> handler, PageRouteOptions options = null)
=> RouteAsync(url, null, handler, options);
public Task RouteAsync(Regex reMatch, Action<IRoute> handler, PageRouteOptions options = null)
=> RouteAsync(null, reMatch, null, handler, options);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteAsync(Regex url, Func<IRoute, Task> handler, PageRouteOptions options = null)
=> RouteAsync(url, null, handler, options);
public Task RouteAsync(Regex reMatch, Func<IRoute, Task> handler, PageRouteOptions options = null)
=> RouteAsync(null, reMatch, null, handler, options);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteAsync(Func<string, bool> url, Action<IRoute> handler, PageRouteOptions options = null)
=> RouteAsync(null, url, handler, options);
public Task RouteAsync(Func<string, bool> funcMatch, Action<IRoute> handler, PageRouteOptions options = null)
=> RouteAsync(null, null, funcMatch, handler, options);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteAsync(Func<string, bool> url, Func<IRoute, Task> handler, PageRouteOptions options = null)
=> RouteAsync(null, url, handler, options);
public Task RouteAsync(Func<string, bool> funcMatch, Func<IRoute, Task> handler, PageRouteOptions options = null)
=> RouteAsync(null, null, funcMatch, handler, options);

[MethodImpl(MethodImplOptions.NoInlining)]
public async Task UnrouteAllAsync(PageUnrouteAllOptions options = default)
{
await UnrouteInternalAsync(_routes, new(), options?.Behavior).ConfigureAwait(false);
await UnrouteInternalAsync(_routes, [], options?.Behavior).ConfigureAwait(false);
DisposeHarRouters();
}

[MethodImpl(MethodImplOptions.NoInlining)]
public Task UnrouteAsync(string urlString, Action<IRoute> handler)
=> UnrouteAsync(new Regex(Context.CombineUrlWithBase(urlString).GlobToRegex()), null, handler);
public Task UnrouteAsync(string globMatch, Action<IRoute> handler)
=> UnrouteAsync(globMatch, null, null, handler);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task UnrouteAsync(string urlString, Func<IRoute, Task> handler)
=> UnrouteAsync(new Regex(Context.CombineUrlWithBase(urlString).GlobToRegex()), null, handler);
public Task UnrouteAsync(string globMatch, Func<IRoute, Task> handler)
=> UnrouteAsync(globMatch, null, null, handler);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task UnrouteAsync(Regex urlString, Action<IRoute> handler)
=> UnrouteAsync(urlString, null, handler);
public Task UnrouteAsync(Regex reMatch, Action<IRoute> handler)
=> UnrouteAsync(null, reMatch, null, handler);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task UnrouteAsync(Regex urlString, Func<IRoute, Task> handler)
=> UnrouteAsync(urlString, null, handler);
public Task UnrouteAsync(Regex reMatch, Func<IRoute, Task> handler)
=> UnrouteAsync(null, reMatch, null, handler);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task UnrouteAsync(Func<string, bool> urlFunc, Action<IRoute> handler)
=> UnrouteAsync(null, urlFunc, handler);
public Task UnrouteAsync(Func<string, bool> funcMatch, Action<IRoute> handler)
=> UnrouteAsync(null, null, funcMatch, handler);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task UnrouteAsync(Func<string, bool> urlFunc, Func<IRoute, Task> handler)
=> UnrouteAsync(null, urlFunc, handler);
public Task UnrouteAsync(Func<string, bool> funcMatch, Func<IRoute, Task> handler)
=> UnrouteAsync(null, null, funcMatch, handler);

[MethodImpl(MethodImplOptions.NoInlining)]
public Task WaitForLoadStateAsync(LoadState? state = default, PageWaitForLoadStateOptions options = default)
Expand Down Expand Up @@ -1235,11 +1235,16 @@ internal void OnFrameNavigated(Frame frame)

internal void FirePageError(string error) => PageError?.Invoke(this, error);

private Task RouteAsync(Regex urlRegex, Func<string, bool> urlFunc, Delegate handler, PageRouteOptions options)
private Task RouteAsync(string globMatch, Regex reMatch, Func<string, bool> funcMatch, Delegate handler, PageRouteOptions options)
=> RouteAsync(new()
{
Regex = urlRegex,
Function = urlFunc,
urlMatcher = new URLMatch()
{
glob = globMatch,
re = reMatch,
func = funcMatch,
baseURL = Context.Options.BaseURL,
},
Handler = handler,
Times = options?.Times,
});
Expand All @@ -1250,23 +1255,22 @@ private Task RouteAsync(RouteHandler setting)
return UpdateInterceptionAsync();
}

private Task UnrouteAsync(Regex urlRegex, Func<string, bool> urlFunc, Delegate handler = null)
=> UnrouteAsync(new()
{
Function = urlFunc,
Regex = urlRegex,
Handler = handler,
});

private Task UnrouteAsync(RouteHandler setting)
private async Task UnrouteAsync(string globMatch, Regex reMatch, Func<string, bool> funcMatch, Delegate handler)
{
var newRoutes = new List<RouteHandler>();
newRoutes.AddRange(_routes.Where(r =>
(setting.Regex != null && !(r.Regex == setting.Regex || (r.Regex.ToString() == setting.Regex.ToString() && r.Regex.Options == setting.Regex.Options))) ||
(setting.Function != null && r.Function != setting.Function) ||
(setting.Handler != null && r.Handler != setting.Handler)));
_routes = newRoutes;
return UpdateInterceptionAsync();
var removed = new List<RouteHandler>();
var remaining = new List<RouteHandler>();
foreach (var routeHandler in _routes)
{
if (routeHandler.urlMatcher.Equals(globMatch, reMatch, funcMatch, Context.Options.BaseURL) && (handler == null || routeHandler.Handler == handler))
{
removed.Add(routeHandler);
}
else
{
remaining.Add(routeHandler);
}
}
await UnrouteInternalAsync(removed, remaining, UnrouteBehavior.Default).ConfigureAwait(false);
}

private async Task UnrouteInternalAsync(List<RouteHandler> removed, List<RouteHandler> remaining, UnrouteBehavior? behavior)
Expand All @@ -1277,7 +1281,7 @@ private async Task UnrouteInternalAsync(List<RouteHandler> removed, List<RouteHa
{
return;
}
var tasks = removed.Select(routeHandler => routeHandler.StopAsync((UnrouteBehavior)behavior));
var tasks = removed.Select(routeHandler => routeHandler.StopAsync(behavior.Value));
await Task.WhenAll(tasks).ConfigureAwait(false);
}

Expand Down Expand Up @@ -1317,17 +1321,14 @@ private void Channel_BindingCall(object sender, BindingCall bindingCall)
private async Task OnRouteAsync(Route route)
{
route._context = Context;
var routeHandlers = _routes.ToArray();
foreach (var routeHandler in routeHandlers)
foreach (var routeHandler in _routes.ToArray())
{
// If the page was closed we stall all requests right away.
if (CloseWasCalled || Context.CloseWasCalled)
{
return;
}
var matches = (routeHandler.Regex?.IsMatch(route.Request.Url) == true) ||
(routeHandler.Function?.Invoke(route.Request.Url) == true);
if (!matches)
if (!routeHandler.Matches(route.Request.Url))
{
continue;
}
Expand Down Expand Up @@ -1578,7 +1579,7 @@ public async Task RemoveLocatorHandlerAsync(ILocator locator)
["uid"] = uid,
}).ConfigureAwait(false);
}
catch (System.Exception)
catch (Exception)
{
// Ignore
}
Expand All @@ -1602,12 +1603,12 @@ private Task RouteWebSocketAsync(string globMatch, Regex urlRegex, Func<string,
{
_webSocketRoutes.Insert(0, new WebSocketRouteHandler()
{
URL = new URLMatch()
urlMatcher = new URLMatch()
{
BaseURL = Context.Options.BaseURL,
globMatch = globMatch,
reMatch = urlRegex,
funcMatch = urlFunc,
baseURL = Context.Options.BaseURL,
glob = globMatch,
re = urlRegex,
func = urlFunc,
},
Handler = handler,
});
Expand Down
Loading

0 comments on commit 497b7b0

Please sign in to comment.