From 43706316548ce90bad8454337c2531cd90e79f6c Mon Sep 17 00:00:00 2001 From: TheSeeker Date: Sun, 22 Oct 2023 00:42:53 -0700 Subject: [PATCH 1/8] Bookmarks may not be at root of site, so use regex to construct the activelink. --- src/freenet/clients/http/WelcomeToadlet.java | 35 +++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/freenet/clients/http/WelcomeToadlet.java b/src/freenet/clients/http/WelcomeToadlet.java index 944ba16bb67..18bebc6909e 100644 --- a/src/freenet/clients/http/WelcomeToadlet.java +++ b/src/freenet/clients/http/WelcomeToadlet.java @@ -15,6 +15,8 @@ import java.net.URI; import java.util.HashSet; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import freenet.client.ClientMetadata; import freenet.client.HighLevelSimpleClient; @@ -40,6 +42,12 @@ public class WelcomeToadlet extends Toadlet { /** Suffix {@link #path()} with "#" + BOOKMARKS_ANCHOR to deep link to the bookmark list */ public static final String BOOKMARKS_ANCHOR = "bookmarks"; + // Regex Patterns to use when generating keys for activelinks. + // For USK and SSK we want to match from the start of the key to the first instance of -#/ or /#/. + protected static final Pattern PATTERN_USK_SSK = Pattern.compile("^.*[\/\-][0-9]+\/"); + // For CHK and KSK we match from the start of the key to the first slash and append the activelink there. + protected static final Pattern PATTERN_CHK_KSK = Pattern.compile("^.*\/"); + final Node node; private static volatile boolean logMINOR; @@ -80,7 +88,32 @@ private void addCategoryToList(BookmarkCategory cat, HTMLNode list, boolean noAc HTMLNode cell = row.addChild("td", "style", "border: none;"); if (item.hasAnActivelink() && !noActiveLinks) { String initialKey = item.getKey(); - String key = '/' + initialKey + (initialKey.endsWith("/") ? "" : "/") + "activelink.png"; + // extract the key type + String keyType = initialKey.substring(1,3); + String keyType = '/' + initialKey + (initialKey.endsWith("/") ? "" : "/"); + Switch prefix { + Case "USK": + Case "SSK": + Matcher match = PATTERN_USK_SSK.matcher(key); + if match.matches() { + key = match.group(1) + "activelink.png"; + } + else { // If no edition # exists, assume bare SSK and just append the activelink. + key = key + "activelink.png"; + } + Case "CHK": + Case "KSK": // This assumes the activelink is in the root of any one-shot directory upload. + Matcher match = PATTERN_CHK_KSK.matcher(key); + if match.matches() { + key = match.group(1) + "activelink.png"; + } + else { // we append '/' to key at the start if it isn't already there, so this should never be reachable unless the regex is broken. + Logger.minor(this, "Impossible: Regex for CHK/KSK didn't match. Key: "+ key); + } + default: + Logger.minor(this, "Unknown key type: " + prefix + "! Activelink regex needs updating."); + } + cell.addChild("div", "style", "height: 36px; width: 108px;").addChild("a", "href", '/' + item.getKey()).addChild("img", new String[]{"src", "alt", "style", "title"}, new String[]{ key, "activelink", "height: 36px; width: 108px", item.getDescription()}); } else { From ab5074ba25dade1adb51df3a579e3d8e48d14f43 Mon Sep 17 00:00:00 2001 From: TheSeeker Date: Sun, 22 Oct 2023 00:56:14 -0700 Subject: [PATCH 2/8] fix refactoring typo --- src/freenet/clients/http/WelcomeToadlet.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/freenet/clients/http/WelcomeToadlet.java b/src/freenet/clients/http/WelcomeToadlet.java index 18bebc6909e..baf80ceb3ef 100644 --- a/src/freenet/clients/http/WelcomeToadlet.java +++ b/src/freenet/clients/http/WelcomeToadlet.java @@ -90,8 +90,8 @@ private void addCategoryToList(BookmarkCategory cat, HTMLNode list, boolean noAc String initialKey = item.getKey(); // extract the key type String keyType = initialKey.substring(1,3); - String keyType = '/' + initialKey + (initialKey.endsWith("/") ? "" : "/"); - Switch prefix { + String key = '/' + initialKey + (initialKey.endsWith("/") ? "" : "/"); + Switch keyType { Case "USK": Case "SSK": Matcher match = PATTERN_USK_SSK.matcher(key); From 24032c62b521edb2fd8be528e6cbbef8e494bb06 Mon Sep 17 00:00:00 2001 From: TheSeeker Date: Sun, 22 Oct 2023 00:59:20 -0700 Subject: [PATCH 3/8] Missed one. --- src/freenet/clients/http/WelcomeToadlet.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/freenet/clients/http/WelcomeToadlet.java b/src/freenet/clients/http/WelcomeToadlet.java index baf80ceb3ef..351850365ca 100644 --- a/src/freenet/clients/http/WelcomeToadlet.java +++ b/src/freenet/clients/http/WelcomeToadlet.java @@ -111,7 +111,7 @@ private void addCategoryToList(BookmarkCategory cat, HTMLNode list, boolean noAc Logger.minor(this, "Impossible: Regex for CHK/KSK didn't match. Key: "+ key); } default: - Logger.minor(this, "Unknown key type: " + prefix + "! Activelink regex needs updating."); + Logger.minor(this, "Unknown key type: " + keyType + "! Activelink regex needs updating."); } cell.addChild("div", "style", "height: 36px; width: 108px;").addChild("a", "href", '/' + item.getKey()).addChild("img", new String[]{"src", "alt", "style", "title"}, From 96408c69069c7a9b252609ddf5fc6d425460235a Mon Sep 17 00:00:00 2001 From: TheSeeker Date: Sun, 22 Oct 2023 01:02:36 -0700 Subject: [PATCH 4/8] Switches need breaks. --- src/freenet/clients/http/WelcomeToadlet.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/freenet/clients/http/WelcomeToadlet.java b/src/freenet/clients/http/WelcomeToadlet.java index 351850365ca..fec22de6ce3 100644 --- a/src/freenet/clients/http/WelcomeToadlet.java +++ b/src/freenet/clients/http/WelcomeToadlet.java @@ -101,6 +101,7 @@ private void addCategoryToList(BookmarkCategory cat, HTMLNode list, boolean noAc else { // If no edition # exists, assume bare SSK and just append the activelink. key = key + "activelink.png"; } + break; Case "CHK": Case "KSK": // This assumes the activelink is in the root of any one-shot directory upload. Matcher match = PATTERN_CHK_KSK.matcher(key); @@ -110,8 +111,10 @@ private void addCategoryToList(BookmarkCategory cat, HTMLNode list, boolean noAc else { // we append '/' to key at the start if it isn't already there, so this should never be reachable unless the regex is broken. Logger.minor(this, "Impossible: Regex for CHK/KSK didn't match. Key: "+ key); } + break; default: Logger.minor(this, "Unknown key type: " + keyType + "! Activelink regex needs updating."); + break; } cell.addChild("div", "style", "height: 36px; width: 108px;").addChild("a", "href", '/' + item.getKey()).addChild("img", new String[]{"src", "alt", "style", "title"}, From 5d68076f21ad03590678d128e3908e19c1e6596b Mon Sep 17 00:00:00 2001 From: TheSeeker Date: Thu, 26 Oct 2023 10:59:09 -0700 Subject: [PATCH 5/8] Update WelcomeToadlet.java Regex was too greedy, matched last instance of what looks like an edition isntead of the first. --- src/freenet/clients/http/WelcomeToadlet.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/freenet/clients/http/WelcomeToadlet.java b/src/freenet/clients/http/WelcomeToadlet.java index fec22de6ce3..9afea194b10 100644 --- a/src/freenet/clients/http/WelcomeToadlet.java +++ b/src/freenet/clients/http/WelcomeToadlet.java @@ -44,9 +44,9 @@ public class WelcomeToadlet extends Toadlet { // Regex Patterns to use when generating keys for activelinks. // For USK and SSK we want to match from the start of the key to the first instance of -#/ or /#/. - protected static final Pattern PATTERN_USK_SSK = Pattern.compile("^.*[\/\-][0-9]+\/"); - // For CHK and KSK we match from the start of the key to the first slash and append the activelink there. - protected static final Pattern PATTERN_CHK_KSK = Pattern.compile("^.*\/"); + protected static final Pattern PATTERN_USK_SSK = Pattern.compile("^[^/]*/[^/]+/?[/-]?[0-9]+/"); + // For bare SSK, CHK, and KSK we match from the start of the key to the first slash and append the activelink there. + protected static final Pattern PATTERN_BARE_SSK_CHK_KSK = Pattern.compile("^[^/]*/"); final Node node; @@ -99,12 +99,18 @@ private void addCategoryToList(BookmarkCategory cat, HTMLNode list, boolean noAc key = match.group(1) + "activelink.png"; } else { // If no edition # exists, assume bare SSK and just append the activelink. - key = key + "activelink.png"; + match = PATTERN_BARE_SSK_CHK_KSK.matcher(key); + if match.matches() { + key = match.group(1) + "activelink.png"; + } + else { + Logger.minor(this, "Impossible: Regex for USK/SSK didn't match. Key: "+ key); + } } break; Case "CHK": Case "KSK": // This assumes the activelink is in the root of any one-shot directory upload. - Matcher match = PATTERN_CHK_KSK.matcher(key); + Matcher match = PATTERN_BARE_SSK_CHK_KSK.matcher(key); if match.matches() { key = match.group(1) + "activelink.png"; } From 2d9d9e967c9ca2bfb3636c78aa1da625563e9236 Mon Sep 17 00:00:00 2001 From: TheSeeker Date: Fri, 27 Oct 2023 16:52:14 -0700 Subject: [PATCH 6/8] Update WelcomeToadlet.java fix Switch/Case -> switch/case --- src/freenet/clients/http/WelcomeToadlet.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/freenet/clients/http/WelcomeToadlet.java b/src/freenet/clients/http/WelcomeToadlet.java index 9afea194b10..16be0268956 100644 --- a/src/freenet/clients/http/WelcomeToadlet.java +++ b/src/freenet/clients/http/WelcomeToadlet.java @@ -91,9 +91,9 @@ private void addCategoryToList(BookmarkCategory cat, HTMLNode list, boolean noAc // extract the key type String keyType = initialKey.substring(1,3); String key = '/' + initialKey + (initialKey.endsWith("/") ? "" : "/"); - Switch keyType { - Case "USK": - Case "SSK": + switch keyType { + case "USK": + case "SSK": Matcher match = PATTERN_USK_SSK.matcher(key); if match.matches() { key = match.group(1) + "activelink.png"; @@ -108,8 +108,8 @@ private void addCategoryToList(BookmarkCategory cat, HTMLNode list, boolean noAc } } break; - Case "CHK": - Case "KSK": // This assumes the activelink is in the root of any one-shot directory upload. + case "CHK": + case "KSK": // This assumes the activelink is in the root of any one-shot directory upload. Matcher match = PATTERN_BARE_SSK_CHK_KSK.matcher(key); if match.matches() { key = match.group(1) + "activelink.png"; From 285262d22a6fbdf9e3748a7f6d0530e448796a0c Mon Sep 17 00:00:00 2001 From: TheSeeker Date: Fri, 27 Oct 2023 16:58:51 -0700 Subject: [PATCH 7/8] Update WelcomeToadlet.java fix missing parens. --- src/freenet/clients/http/WelcomeToadlet.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/freenet/clients/http/WelcomeToadlet.java b/src/freenet/clients/http/WelcomeToadlet.java index 16be0268956..30269aace5e 100644 --- a/src/freenet/clients/http/WelcomeToadlet.java +++ b/src/freenet/clients/http/WelcomeToadlet.java @@ -91,16 +91,16 @@ private void addCategoryToList(BookmarkCategory cat, HTMLNode list, boolean noAc // extract the key type String keyType = initialKey.substring(1,3); String key = '/' + initialKey + (initialKey.endsWith("/") ? "" : "/"); - switch keyType { + switch (keyType) { case "USK": case "SSK": Matcher match = PATTERN_USK_SSK.matcher(key); - if match.matches() { + if (match.matches()) { key = match.group(1) + "activelink.png"; } else { // If no edition # exists, assume bare SSK and just append the activelink. match = PATTERN_BARE_SSK_CHK_KSK.matcher(key); - if match.matches() { + if (match.matches()) { key = match.group(1) + "activelink.png"; } else { @@ -111,7 +111,7 @@ private void addCategoryToList(BookmarkCategory cat, HTMLNode list, boolean noAc case "CHK": case "KSK": // This assumes the activelink is in the root of any one-shot directory upload. Matcher match = PATTERN_BARE_SSK_CHK_KSK.matcher(key); - if match.matches() { + if (match.matches()) { key = match.group(1) + "activelink.png"; } else { // we append '/' to key at the start if it isn't already there, so this should never be reachable unless the regex is broken. From 3eb09ed272a7f83ed614c62144b21cf6d7e5e542 Mon Sep 17 00:00:00 2001 From: TheSeeker Date: Fri, 27 Oct 2023 17:15:15 -0700 Subject: [PATCH 8/8] Update WelcomeToadlet.java fix scope issue. --- src/freenet/clients/http/WelcomeToadlet.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/freenet/clients/http/WelcomeToadlet.java b/src/freenet/clients/http/WelcomeToadlet.java index 30269aace5e..edab713627b 100644 --- a/src/freenet/clients/http/WelcomeToadlet.java +++ b/src/freenet/clients/http/WelcomeToadlet.java @@ -91,10 +91,11 @@ private void addCategoryToList(BookmarkCategory cat, HTMLNode list, boolean noAc // extract the key type String keyType = initialKey.substring(1,3); String key = '/' + initialKey + (initialKey.endsWith("/") ? "" : "/"); + Matcher match = null; switch (keyType) { case "USK": case "SSK": - Matcher match = PATTERN_USK_SSK.matcher(key); + match = PATTERN_USK_SSK.matcher(key); if (match.matches()) { key = match.group(1) + "activelink.png"; } @@ -110,7 +111,7 @@ private void addCategoryToList(BookmarkCategory cat, HTMLNode list, boolean noAc break; case "CHK": case "KSK": // This assumes the activelink is in the root of any one-shot directory upload. - Matcher match = PATTERN_BARE_SSK_CHK_KSK.matcher(key); + match = PATTERN_BARE_SSK_CHK_KSK.matcher(key); if (match.matches()) { key = match.group(1) + "activelink.png"; }