Skip to content

Commit 204d316

Browse files
committed
use globToRegex
1 parent f7b7d79 commit 204d316

File tree

6 files changed

+65
-148
lines changed

6 files changed

+65
-148
lines changed

playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -480,17 +480,17 @@ public APIRequestContextImpl request() {
480480

481481
@Override
482482
public void route(String url, Consumer<Route> handler, RouteOptions options) {
483-
route(new UrlMatcher(baseUrl, url), handler, options);
483+
route(new UrlMatcher(this.connection.localUtils, baseUrl, url), handler, options);
484484
}
485485

486486
@Override
487487
public void route(Pattern url, Consumer<Route> handler, RouteOptions options) {
488-
route(new UrlMatcher(url), handler, options);
488+
route(new UrlMatcher(this.connection.localUtils, url), handler, options);
489489
}
490490

491491
@Override
492492
public void route(Predicate<String> url, Consumer<Route> handler, RouteOptions options) {
493-
route(new UrlMatcher(url), handler, options);
493+
route(new UrlMatcher(this.connection.localUtils, url), handler, options);
494494
}
495495

496496
@Override
@@ -502,7 +502,7 @@ public void routeFromHAR(Path har, RouteFromHAROptions options) {
502502
recordIntoHar(null, har, options);
503503
return;
504504
}
505-
UrlMatcher matcher = UrlMatcher.forOneOf(baseUrl, options.url);
505+
UrlMatcher matcher = UrlMatcher.forOneOf(this.connection.localUtils, baseUrl, options.url);
506506
HARRouter harRouter = new HARRouter(connection.localUtils, har, options.notFound);
507507
onClose(context -> harRouter.dispose());
508508
route(matcher, route -> harRouter.handle(route), null);
@@ -517,17 +517,17 @@ private void route(UrlMatcher matcher, Consumer<Route> handler, RouteOptions opt
517517

518518
@Override
519519
public void routeWebSocket(String url, Consumer<WebSocketRoute> handler) {
520-
routeWebSocketImpl(new UrlMatcher(baseUrl, url), handler);
520+
routeWebSocketImpl(new UrlMatcher(this.connection.localUtils, baseUrl, url), handler);
521521
}
522522

523523
@Override
524524
public void routeWebSocket(Pattern pattern, Consumer<WebSocketRoute> handler) {
525-
routeWebSocketImpl(new UrlMatcher(pattern), handler);
525+
routeWebSocketImpl(new UrlMatcher(this.connection.localUtils, pattern), handler);
526526
}
527527

528528
@Override
529529
public void routeWebSocket(Predicate<String> predicate, Consumer<WebSocketRoute> handler) {
530-
routeWebSocketImpl(new UrlMatcher(predicate), handler);
530+
routeWebSocketImpl(new UrlMatcher(this.connection.localUtils, predicate), handler);
531531
}
532532

533533
private void routeWebSocketImpl(UrlMatcher matcher, Consumer<WebSocketRoute> handler) {
@@ -656,17 +656,17 @@ public void unrouteAll() {
656656

657657
@Override
658658
public void unroute(String url, Consumer<Route> handler) {
659-
unroute(new UrlMatcher(this.baseUrl, url), handler);
659+
unroute(new UrlMatcher(this.connection.localUtils, this.baseUrl, url), handler);
660660
}
661661

662662
@Override
663663
public void unroute(Pattern url, Consumer<Route> handler) {
664-
unroute(new UrlMatcher(url), handler);
664+
unroute(new UrlMatcher(this.connection.localUtils, url), handler);
665665
}
666666

667667
@Override
668668
public void unroute(Predicate<String> url, Consumer<Route> handler) {
669-
unroute(new UrlMatcher(url), handler);
669+
unroute(new UrlMatcher(this.connection.localUtils, url), handler);
670670
}
671671

672672
@Override

playwright/src/main/java/com/microsoft/playwright/impl/FrameImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ private Response waitForNavigationImpl(Logger logger, Runnable code, WaitForNavi
10311031

10321032
List<Waitable<Response>> waitables = new ArrayList<>();
10331033
if (matcher == null) {
1034-
matcher = UrlMatcher.forOneOf(page.context().baseUrl, options.url);
1034+
matcher = UrlMatcher.forOneOf(this.connection.localUtils, page.context().baseUrl, options.url);
10351035
}
10361036
logger.log("waiting for navigation " + matcher);
10371037
waitables.add(new WaitForNavigationHelper(matcher, options.waitUntil, logger));
@@ -1078,17 +1078,17 @@ void waitForTimeoutImpl(double timeout) {
10781078

10791079
@Override
10801080
public void waitForURL(String url, WaitForURLOptions options) {
1081-
waitForURL(new UrlMatcher(page.context().baseUrl, url), options);
1081+
waitForURL(new UrlMatcher(this.connection.localUtils, page.context().baseUrl, url), options);
10821082
}
10831083

10841084
@Override
10851085
public void waitForURL(Pattern url, WaitForURLOptions options) {
1086-
waitForURL(new UrlMatcher(url), options);
1086+
waitForURL(new UrlMatcher(this.connection.localUtils, url), options);
10871087
}
10881088

10891089
@Override
10901090
public void waitForURL(Predicate<String> url, WaitForURLOptions options) {
1091-
waitForURL(new UrlMatcher(url), options);
1091+
waitForURL(new UrlMatcher(this.connection.localUtils, url), options);
10921092
}
10931093

10941094
private void waitForURL(UrlMatcher matcher, WaitForURLOptions options) {

playwright/src/main/java/com/microsoft/playwright/impl/LocalUtils.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.nio.file.Path;
2323
import java.util.List;
24+
import java.util.regex.Pattern;
2425

2526
import static com.microsoft.playwright.impl.Serialization.gson;
2627

@@ -59,4 +60,17 @@ String tracingStarted(String tracesDir, String traceName) {
5960
JsonObject json = connection.localUtils().sendMessage("tracingStarted", params).getAsJsonObject();
6061
return json.get("stacksId").getAsString();
6162
}
63+
64+
Pattern globToRegex(String glob, String baseUrl, String websocketUrl) {
65+
JsonObject params = new JsonObject();
66+
params.addProperty("glob", glob);
67+
if (baseUrl != null) {
68+
params.addProperty("baseUrl", baseUrl);
69+
}
70+
if (websocketUrl != null) {
71+
params.addProperty("websocketUrl", websocketUrl);
72+
}
73+
JsonObject json = connection.localUtils().sendMessage("globToRegex", params).getAsJsonObject();
74+
return Pattern.compile(json.get("regex").getAsString());
75+
}
6276
}

playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -785,17 +785,17 @@ public Frame frame(String name) {
785785

786786
@Override
787787
public Frame frameByUrl(String glob) {
788-
return frameFor(new UrlMatcher(browserContext.baseUrl, glob));
788+
return frameFor(new UrlMatcher(this.connection.localUtils, browserContext.baseUrl, glob));
789789
}
790790

791791
@Override
792792
public Frame frameByUrl(Pattern pattern) {
793-
return frameFor(new UrlMatcher(pattern));
793+
return frameFor(new UrlMatcher(this.connection.localUtils, pattern));
794794
}
795795

796796
@Override
797797
public Frame frameByUrl(Predicate<String> predicate) {
798-
return frameFor(new UrlMatcher(predicate));
798+
return frameFor(new UrlMatcher(this.connection.localUtils, predicate));
799799
}
800800

801801
@Override
@@ -1105,17 +1105,17 @@ private Response reloadImpl(ReloadOptions options) {
11051105

11061106
@Override
11071107
public void route(String url, Consumer<Route> handler, RouteOptions options) {
1108-
route(new UrlMatcher(browserContext.baseUrl, url), handler, options);
1108+
route(new UrlMatcher(this.connection.localUtils, browserContext.baseUrl, url), handler, options);
11091109
}
11101110

11111111
@Override
11121112
public void route(Pattern url, Consumer<Route> handler, RouteOptions options) {
1113-
route(new UrlMatcher(url), handler, options);
1113+
route(new UrlMatcher(this.connection.localUtils, url), handler, options);
11141114
}
11151115

11161116
@Override
11171117
public void route(Predicate<String> url, Consumer<Route> handler, RouteOptions options) {
1118-
route(new UrlMatcher(url), handler, options);
1118+
route(new UrlMatcher(this.connection.localUtils, url), handler, options);
11191119
}
11201120

11211121
@Override
@@ -1127,7 +1127,7 @@ public void routeFromHAR(Path har, RouteFromHAROptions options) {
11271127
browserContext.recordIntoHar(this, har, convertType(options, BrowserContext.RouteFromHAROptions.class));
11281128
return;
11291129
}
1130-
UrlMatcher matcher = UrlMatcher.forOneOf(browserContext.baseUrl, options.url);
1130+
UrlMatcher matcher = UrlMatcher.forOneOf(this.connection.localUtils, browserContext.baseUrl, options.url);
11311131
HARRouter harRouter = new HARRouter(connection.localUtils, har, options.notFound);
11321132
onClose(context -> harRouter.dispose());
11331133
route(matcher, route -> harRouter.handle(route), null);
@@ -1142,17 +1142,17 @@ private void route(UrlMatcher matcher, Consumer<Route> handler, RouteOptions opt
11421142

11431143
@Override
11441144
public void routeWebSocket(String url, Consumer<WebSocketRoute> handler) {
1145-
routeWebSocketImpl(new UrlMatcher(browserContext.baseUrl, url), handler);
1145+
routeWebSocketImpl(new UrlMatcher(this.connection.localUtils, browserContext.baseUrl, url), handler);
11461146
}
11471147

11481148
@Override
11491149
public void routeWebSocket(Pattern pattern, Consumer<WebSocketRoute> handler) {
1150-
routeWebSocketImpl(new UrlMatcher(pattern), handler);
1150+
routeWebSocketImpl(new UrlMatcher(this.connection.localUtils, pattern), handler);
11511151
}
11521152

11531153
@Override
11541154
public void routeWebSocket(Predicate<String> predicate, Consumer<WebSocketRoute> handler) {
1155-
routeWebSocketImpl(new UrlMatcher(predicate), handler);
1155+
routeWebSocketImpl(new UrlMatcher(this.connection.localUtils, predicate), handler);
11561156
}
11571157

11581158
private void routeWebSocketImpl(UrlMatcher matcher, Consumer<WebSocketRoute> handler) {
@@ -1365,17 +1365,17 @@ public void unrouteAll() {
13651365

13661366
@Override
13671367
public void unroute(String url, Consumer<Route> handler) {
1368-
unroute(new UrlMatcher(browserContext.baseUrl, url), handler);
1368+
unroute(new UrlMatcher(this.connection.localUtils, browserContext.baseUrl, url), handler);
13691369
}
13701370

13711371
@Override
13721372
public void unroute(Pattern url, Consumer<Route> handler) {
1373-
unroute(new UrlMatcher(url), handler);
1373+
unroute(new UrlMatcher(this.connection.localUtils, url), handler);
13741374
}
13751375

13761376
@Override
13771377
public void unroute(Predicate<String> url, Consumer<Route> handler) {
1378-
unroute(new UrlMatcher(url), handler);
1378+
unroute(new UrlMatcher(this.connection.localUtils, url), handler);
13791379
}
13801380

13811381
private void unroute(UrlMatcher matcher, Consumer<Route> handler) {
@@ -1508,12 +1508,12 @@ public T get() {
15081508

15091509
@Override
15101510
public Request waitForRequest(String urlGlob, WaitForRequestOptions options, Runnable code) {
1511-
return waitForRequest(new UrlMatcher(browserContext.baseUrl, urlGlob), null, options, code);
1511+
return waitForRequest(new UrlMatcher(this.connection.localUtils, browserContext.baseUrl, urlGlob), null, options, code);
15121512
}
15131513

15141514
@Override
15151515
public Request waitForRequest(Pattern urlPattern, WaitForRequestOptions options, Runnable code) {
1516-
return waitForRequest(new UrlMatcher(urlPattern), null, options, code);
1516+
return waitForRequest(new UrlMatcher(this.connection.localUtils, urlPattern), null, options, code);
15171517
}
15181518

15191519
@Override
@@ -1553,12 +1553,12 @@ private Request waitForRequestFinishedImpl(WaitForRequestFinishedOptions options
15531553

15541554
@Override
15551555
public Response waitForResponse(String urlGlob, WaitForResponseOptions options, Runnable code) {
1556-
return waitForResponse(new UrlMatcher(browserContext.baseUrl, urlGlob), null, options, code);
1556+
return waitForResponse(new UrlMatcher(this.connection.localUtils, browserContext.baseUrl, urlGlob), null, options, code);
15571557
}
15581558

15591559
@Override
15601560
public Response waitForResponse(Pattern urlPattern, WaitForResponseOptions options, Runnable code) {
1561-
return waitForResponse(new UrlMatcher(urlPattern), null, options, code);
1561+
return waitForResponse(new UrlMatcher(this.connection.localUtils, urlPattern), null, options, code);
15621562
}
15631563

15641564
@Override
@@ -1606,17 +1606,17 @@ public void waitForTimeout(double timeout) {
16061606

16071607
@Override
16081608
public void waitForURL(String url, WaitForURLOptions options) {
1609-
waitForURL(new UrlMatcher(browserContext.baseUrl, url), options);
1609+
waitForURL(new UrlMatcher(this.connection.localUtils, browserContext.baseUrl, url), options);
16101610
}
16111611

16121612
@Override
16131613
public void waitForURL(Pattern url, WaitForURLOptions options) {
1614-
waitForURL(new UrlMatcher(url), options);
1614+
waitForURL(new UrlMatcher(this.connection.localUtils, url), options);
16151615
}
16161616

16171617
@Override
16181618
public void waitForURL(Predicate<String> url, WaitForURLOptions options) {
1619-
waitForURL(new UrlMatcher(url), options);
1619+
waitForURL(new UrlMatcher(this.connection.localUtils, url), options);
16201620
}
16211621

16221622
private void waitForURL(UrlMatcher matcher, WaitForURLOptions options) {

playwright/src/main/java/com/microsoft/playwright/impl/UrlMatcher.java

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,30 @@
2121
import java.net.URI;
2222
import java.net.URISyntaxException;
2323
import java.net.URL;
24-
import java.util.Arrays;
2524
import java.util.function.Predicate;
2625
import java.util.regex.Pattern;
2726

28-
import static com.microsoft.playwright.impl.Utils.globToRegex;
2927
import static com.microsoft.playwright.impl.Utils.toJsRegexFlags;
3028

3129
class UrlMatcher {
30+
private final LocalUtils localUtils;
3231
private final String baseURL;
3332
public final String glob;
3433
public final Pattern pattern;
3534
public final Predicate<String> predicate;
3635

37-
static UrlMatcher forOneOf(URL baseUrl, Object object) {
36+
static UrlMatcher forOneOf(LocalUtils localUtils, URL baseUrl, Object object) {
3837
if (object == null) {
39-
return new UrlMatcher(null, null, null, null);
38+
return new UrlMatcher(localUtils, null, null, null, null);
4039
}
4140
if (object instanceof String) {
42-
return new UrlMatcher(baseUrl, (String) object);
41+
return new UrlMatcher(localUtils, baseUrl, (String) object);
4342
}
4443
if (object instanceof Pattern) {
45-
return new UrlMatcher((Pattern) object);
44+
return new UrlMatcher(localUtils, (Pattern) object);
4645
}
4746
if (object instanceof Predicate) {
48-
return new UrlMatcher((Predicate<String>) object);
47+
return new UrlMatcher(localUtils, (Predicate<String>) object);
4948
}
5049
throw new PlaywrightException("Url must be String, Pattern or Predicate<String>, found: " + object.getClass().getTypeName());
5150
}
@@ -66,60 +65,39 @@ private static String resolveUrl(String baseUrl, String spec) {
6665
}
6766
}
6867

69-
private static String normaliseUrl(String spec) {
70-
try {
71-
// Align with the Node.js URL parser which automatically adds a slash to the path if it is empty.
72-
URI url = new URI(spec);
73-
if (url.getScheme() != null &&
74-
Arrays.asList("http", "https", "ws", "wss").contains(url.getScheme()) &&
75-
url.getPath().isEmpty()) {
76-
return new URI(url.getScheme(), url.getAuthority(), "/", url.getQuery(), url.getFragment()).toString();
77-
}
78-
return url.toString();
79-
} catch (URISyntaxException e) {
80-
return spec;
81-
}
82-
}
83-
84-
UrlMatcher(URL baseURL, String glob) {
85-
this(baseURL, glob, null, null);
68+
UrlMatcher(LocalUtils localUtils, URL baseURL, String glob) {
69+
this(localUtils, baseURL, glob, null, null);
8670
}
8771

88-
UrlMatcher(Pattern pattern) {
89-
this(null, null, pattern, null);
72+
UrlMatcher(LocalUtils localUtils, Pattern pattern) {
73+
this(localUtils, null, null, pattern, null);
9074
}
9175

92-
UrlMatcher(Predicate<String> predicate) {
93-
this(null, null, null, predicate);
76+
UrlMatcher(LocalUtils localUtils, Predicate<String> predicate) {
77+
this(localUtils, null, null, null, predicate);
9478
}
9579

96-
private UrlMatcher(URL baseURL, String glob, Pattern pattern, Predicate<String> predicate) {
80+
private UrlMatcher(LocalUtils localUtils, URL baseURL, String glob, Pattern pattern, Predicate<String> predicate) {
9781
this.baseURL = baseURL != null ? baseURL.toString() : null;
9882
this.glob = glob;
9983
this.pattern = pattern;
10084
this.predicate = predicate;
85+
this.localUtils = localUtils;
10186
}
10287

10388
boolean test(String value) {
104-
return testImpl(baseURL, pattern, predicate, glob, value);
89+
return testImpl(localUtils, baseURL, pattern, predicate, glob, value);
10590
}
10691

107-
private static boolean testImpl(String baseURL, Pattern pattern, Predicate<String> predicate, String glob, String value) {
92+
private static boolean testImpl(LocalUtils localUtils, String baseURL, Pattern pattern, Predicate<String> predicate, String glob, String value) {
10893
if (pattern != null) {
10994
return pattern.matcher(value).find();
11095
}
11196
if (predicate != null) {
11297
return predicate.test(value);
11398
}
11499
if (glob != null) {
115-
if (!glob.startsWith("*")) {
116-
// Allow http(s) baseURL to match ws(s) urls.
117-
if (baseURL != null && Pattern.compile("^https?://").matcher(baseURL).find() && Pattern.compile("^wss?://").matcher(value).find()) {
118-
baseURL = baseURL.replaceFirst("^http", "ws");
119-
}
120-
glob = normaliseUrl(resolveUrl(baseURL, glob));
121-
}
122-
return Pattern.compile(globToRegex(glob)).matcher(value).find();
100+
return localUtils.globToRegex(glob, baseURL, null).matcher(value).find();
123101
}
124102
return true;
125103
}

0 commit comments

Comments
 (0)