From 52bc3919e226c7cf97166eb29e035c695e448e1c Mon Sep 17 00:00:00 2001 From: TrianguloY Date: Mon, 19 Aug 2024 18:42:14 +0200 Subject: [PATCH] check host subdomains too fixes #370 --- .../urlchecker/modules/companions/Hosts.java | 47 +++++++------------ 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/trianguloy/urlchecker/modules/companions/Hosts.java b/app/src/main/java/com/trianguloy/urlchecker/modules/companions/Hosts.java index 705311c..5af75d8 100644 --- a/app/src/main/java/com/trianguloy/urlchecker/modules/companions/Hosts.java +++ b/app/src/main/java/com/trianguloy/urlchecker/modules/companions/Hosts.java @@ -9,7 +9,6 @@ import com.trianguloy.urlchecker.R; import com.trianguloy.urlchecker.utilities.generics.JsonCatalog; import com.trianguloy.urlchecker.utilities.methods.HttpUtils; import com.trianguloy.urlchecker.utilities.methods.JavaUtils; -import com.trianguloy.urlchecker.utilities.methods.StreamUtils; import com.trianguloy.urlchecker.utilities.wrappers.InternalFile; import com.trianguloy.urlchecker.utilities.wrappers.ProgressDialog; @@ -19,9 +18,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; -/** - * Represents and manages the hosts data - */ +/** Represents and manages the hosts data */ public class Hosts { private final HostsCatalog data; @@ -39,9 +36,7 @@ public class Hosts { data = new HostsCatalog(cntx); } - /** - * Builds the hosts database (asks first) - */ + /** Builds the hosts database (asks first) */ public void build(boolean showEditor, Runnable onFinished) { var builder = new AlertDialog.Builder(cntx) .setTitle(R.string.mHosts_buildTitle) @@ -60,16 +55,12 @@ public class Hosts { builder.show(); } - /** - * @see JsonCatalog#showEditor - */ + /** @see JsonCatalog#showEditor */ public void showEditor() { data.showEditor(); } - /** - * Background thread that builds the database (and notifies progress) - */ + /** Background thread that builds the database (and notifies progress) */ private void _build(ProgressDialog progress, Runnable onFinished) { var catalog = data.getCatalog(); @@ -149,17 +140,21 @@ public class Hosts { }); } - /** - * return true if the database is built - */ + /** return true if the database is built */ public boolean isUninitialized() { return buckets.isEmpty() && getFileNames().isEmpty(); } - /** - * Returns the label and color for the host, null if not in the database - */ + /** Returns the label and color for the host, or bigger partial host (no subdomain). null if not in the database */ public Pair contains(String host) { + var contains = containsExact(host); + if (contains != null) return contains; + if (!host.contains(".")) return null; // no more subdomains to check + return contains(host.split("\\.", 2)[1]); // remove leftmost subdomain + } + + /** Returns the label and color for the exact host, null if not in the database */ + public Pair containsExact(String host) { return getBucket(host, key -> { var values = new HashMap>(); new InternalFile(PREFIX + key, cntx).stream(line -> { @@ -173,9 +168,7 @@ public class Hosts { /* ------------------- internal ------------------- */ - /** - * returns all the files from this database - */ + /** returns all the files from this database */ private ArrayList getFileNames() { var files = new ArrayList(); for (var fileName : cntx.fileList()) { @@ -184,11 +177,9 @@ public class Hosts { return files; } - /** - * The number of hosts. - * I miss streams :( - */ + /** The number of hosts. */ public int size() { + // I miss streams :( var s = 0; for (var value : buckets.values()) { s += value.size(); @@ -205,9 +196,7 @@ public class Hosts { if (replace || !bucket.containsKey(host)) bucket.put(host, data); } - /** - * returns the bucket of a value, if not ready computes it - */ + /** returns the bucket of a value, if not ready computes it */ private HashMap> getBucket(String value, JavaUtils.Function>> compute) { // HASHING var hash = Math.floorMod(value.hashCode(), FILES);