Skip to content

Commit

Permalink
chore: refactor UrlMatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Dec 19, 2024
1 parent eb08046 commit d564bc4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.regex.Pattern;
Expand All @@ -30,20 +32,14 @@
import static com.microsoft.playwright.impl.Utils.toJsRegexFlags;

class UrlMatcher {
final Object rawSource;
private final Predicate<String> predicate;

private static Predicate<String> toPredicate(Pattern pattern) {
return s -> pattern.matcher(s).find();
}

static UrlMatcher any() {
return new UrlMatcher((Object) null, null);
}
private final URL baseURL;
public final String glob;
public final Pattern pattern;
public final Predicate<String> predicate;

static UrlMatcher forOneOf(URL baseUrl, Object object) {
if (object == null) {
return UrlMatcher.any();
return new UrlMatcher((Predicate<String>) s -> true);
}
if (object instanceof String) {
return new UrlMatcher(baseUrl, (String) object);
Expand All @@ -68,50 +64,79 @@ static String resolveUrl(URL baseUrl, String spec) {
}
}

UrlMatcher(URL base, String url) {
this(url, toPredicate(Pattern.compile(globToRegex(resolveUrl(base, url)))).or(s -> url == null || url.equals(s)));
UrlMatcher(URL baseURL, String glob) {
this(baseURL, glob, null, null);
}

UrlMatcher(Pattern pattern) {
this(pattern, toPredicate(pattern));
this(null, null, pattern, null);
}
UrlMatcher(Predicate<String> predicate) {
this(predicate, predicate);
this(null, null, null, predicate);
}

private UrlMatcher(Object rawSource, Predicate<String> predicate) {
this.rawSource = rawSource;
private UrlMatcher(URL baseURL, String glob, Pattern pattern, Predicate<String> predicate) {
this.baseURL = baseURL;
this.glob = glob;
this.pattern = pattern;
this.predicate = predicate;
}

boolean test(String value) {
return predicate == null || predicate.test(value);
return testImpl(baseURL, pattern, predicate, glob, value);
}

private static boolean testImpl(URL baseURL, Pattern pattern, Predicate<String> predicate, String glob, String value) {
if (pattern != null) {
return pattern.matcher(value).find();
}
if (predicate != null) {
return predicate.test(value);
}
if (glob != null) {
return Pattern.compile(globToRegex(resolveUrl(baseURL, glob))).matcher(value).find();
}
return true;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UrlMatcher that = (UrlMatcher) o;
if (rawSource instanceof Pattern && that.rawSource instanceof Pattern) {
Pattern a = (Pattern) rawSource;
Pattern b = (Pattern) that.rawSource;
return a.pattern().equals(b.pattern()) && a.flags() == b.flags();
List<Boolean> matches = new ArrayList<>();
if (baseURL != null && that.baseURL != null) {
matches.add(baseURL.equals(that.baseURL));
}
if (pattern != null && that.pattern != null) {
matches.add(pattern.pattern().equals(that.pattern.pattern()) && pattern.flags() == that.pattern.flags());
}
if (predicate != null && that.predicate != null) {
matches.add(predicate.equals(that.predicate));
}
return Objects.equals(rawSource, that.rawSource);
if (glob != null && that.glob != null) {
matches.add(glob.equals(that.glob));
}
return matches.stream().allMatch(m -> m);
}

@Override
public int hashCode() {
return Objects.hash(rawSource);
if (pattern != null) {
return pattern.hashCode();
}
if (predicate != null) {
return predicate.hashCode();
}
return glob.hashCode();
}

@Override
public String toString() {
if (rawSource == null)
return "<any>";
if (rawSource instanceof Predicate)
return "matching predicate";
return rawSource.toString();
if (pattern != null)
return String.format("<regex pattern=\"%s\" flags=\"%s\">", pattern.pattern(), toJsRegexFlags(pattern));
if (this.predicate != null)
return "<predicate>";
return String.format("<glob pattern=\"%s\">", glob);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -462,13 +462,11 @@ static JsonObject interceptionPatterns(List<UrlMatcher> matchers) {
JsonArray jsonPatterns = new JsonArray();
for (UrlMatcher matcher: matchers) {
JsonObject jsonPattern = new JsonObject();
Object urlFilter = matcher.rawSource;
if (urlFilter instanceof String) {
jsonPattern.addProperty("glob", (String) urlFilter);
} else if (urlFilter instanceof Pattern) {
Pattern pattern = (Pattern) urlFilter;
jsonPattern.addProperty("regexSource", pattern.pattern());
jsonPattern.addProperty("regexFlags", toJsRegexFlags(pattern));
if (matcher.glob != null) {
jsonPattern.addProperty("glob", matcher.glob);
} else if (matcher.pattern != null) {
jsonPattern.addProperty("regexSource", matcher.pattern.pattern());
jsonPattern.addProperty("regexFlags", toJsRegexFlags(matcher.pattern));
} else {
// Match all requests.
jsonPattern.addProperty("glob", "**/*");
Expand Down

0 comments on commit d564bc4

Please sign in to comment.