commit 11883dde69d6c936c9aadd8eb2db498de5606991 Author: Karsten Loesing karsten.loesing@gmx.net Date: Mon Jun 18 12:09:31 2012 +0200
Support searches for more than 1 search term.
Works only for search=... parameter, not for search/... URLs.
Implements #5248. --- src/org/torproject/onionoo/ResourceServlet.java | 39 ++++++++++++++++------- web/index.html | 4 ++ 2 files changed, 31 insertions(+), 12 deletions(-)
diff --git a/src/org/torproject/onionoo/ResourceServlet.java b/src/org/torproject/onionoo/ResourceServlet.java index 41f4c5a..ba1d434 100644 --- a/src/org/torproject/onionoo/ResourceServlet.java +++ b/src/org/torproject/onionoo/ResourceServlet.java @@ -241,14 +241,14 @@ public class ResourceServlet extends HttpServlet { runningRequested); } if (parameterMap.containsKey("search")) { - String searchTerm = this.parseSearchParameter( + String[] searchTerms = this.parseSearchParameters( parameterMap.get("search")); - if (searchTerm == null) { + if (searchTerms == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST); return; } - this.filterBySearchTerm(filteredRelays, filteredBridges, - searchTerm); + this.filterBySearchTerms(filteredRelays, filteredBridges, + searchTerms); } if (parameterMap.containsKey("lookup")) { String fingerprintParameter = this.parseFingerprintParameter( @@ -383,12 +383,12 @@ public class ResourceServlet extends HttpServlet { } else if (uri.equals("/" + resourceType + "/bridges")) { result.put("type", "bridges"); } else if (uri.startsWith("/" + resourceType + "/search/")) { - String searchParameter = this.parseSearchParameter(uri.substring( - ("/" + resourceType + "/search/").length())); - if (searchParameter == null) { + String[] searchParameters = this.parseSearchParameters( + uri.substring(("/" + resourceType + "/search/").length())); + if (searchParameters == null || searchParameters.length != 1) { result = null; } else { - result.put("search", searchParameter); + result.put("search", searchParameters[0]); } } else if (uri.startsWith("/" + resourceType + "/lookup/")) { String fingerprintParameter = this.parseFingerprintParameter( @@ -408,11 +408,19 @@ public class ResourceServlet extends HttpServlet { Pattern.compile("^\$?[0-9a-fA-F]{1,40}$|" /* Fingerprint. */ + "^[0-9a-zA-Z\.]{1,19}$|" /* Nickname or IPv4 address. */ + "^\[[0-9a-fA-F:\.]{1,39}\]?$"); /* IPv6 address. */ - private String parseSearchParameter(String parameter) { - if (!searchParameterPattern.matcher(parameter).matches()) { - return null; + private String[] parseSearchParameters(String parameter) { + String[] searchParameters; + if (parameter.contains(" ")) { + searchParameters = parameter.split(" "); + } else { + searchParameters = new String[] { parameter }; } - return parameter; + for (String searchParameter : searchParameters) { + if (!searchParameterPattern.matcher(searchParameter).matches()) { + return null; + } + } + return searchParameters; }
private static Pattern fingerprintParameterPattern = @@ -467,6 +475,13 @@ public class ResourceServlet extends HttpServlet { } }
+ private void filterBySearchTerms(Map<String, String> filteredRelays, + Map<String, String> filteredBridges, String[] searchTerms) { + for (String searchTerm : searchTerms) { + filterBySearchTerm(filteredRelays, filteredBridges, searchTerm); + } + } + private void filterBySearchTerm(Map<String, String> filteredRelays, Map<String, String> filteredBridges, String searchTerm) { Set<String> removeRelays = new HashSet<String>(); diff --git a/web/index.html b/web/index.html index bd44108..d9ac6e8 100755 --- a/web/index.html +++ b/web/index.html @@ -612,11 +612,15 @@ matching the beginning of a nickname, (possibly $-prefixed) fingerprint, or IP address, and bridges with the nickname or (possibly $-prefixed) hashed fingerprint. Searches are case-insensitive. +If multiple search terms are given, separated by spaces, the intersection +of all relays and bridges matching all search terms will be returned. Full fingerprints should always be hashed using SHA-1, regardless of searching for a relay or a bridge, in order to not accidentally leak non-hashed bridge fingerprints in the URL. <font color="blue">Extended search to bridge nicknames on June 14, 2012.</font> +<font color="blue">Extended search to multiple search terms on June 18, +2012.</font> </td></tr> <tr><td><b>lookup</b></td><td>Return only the relay with the parameter value matching the fingerprint or the bridge with the parameter value
tor-commits@lists.torproject.org