Commit c9218fbd authored by Karsten Loesing's avatar Karsten Loesing
Browse files

Allow for URLs that specify list of fingerprints.

Implements #25448.
parent 52494a7f
# Changes in version 6.2-1.16.0 - 2018-07-??
* Medium changes
- Support a comma-separated list of fingerprints in the lookup
parameter to allow for URLs that specify a list of relays or
bridges.
# Changes in version 6.1-1.15.0 - 2018-07-16
* Medium changes
......
......@@ -54,9 +54,9 @@ public class RequestHandler {
System.arraycopy(search, 0, this.search, 0, search.length);
}
private String lookup;
private String[] lookup;
public void setLookup(String lookup) {
public void setLookup(String[] lookup) {
this.lookup = lookup;
}
......@@ -340,17 +340,20 @@ public class RequestHandler {
/* Not filtering by looking up relay or bridge. */
return;
}
String fingerprint = this.lookup;
SummaryDocument relayLine = this.filteredRelays.get(fingerprint);
this.filteredRelays.clear();
if (relayLine != null) {
this.filteredRelays.put(fingerprint, relayLine);
Map<String, SummaryDocument> matchingRelays = new HashMap<>();
for (String fingerprint : this.lookup) {
if (this.filteredRelays.containsKey(fingerprint)) {
matchingRelays.put(fingerprint, this.filteredRelays.get(fingerprint));
}
}
SummaryDocument bridgeLine = this.filteredBridges.get(fingerprint);
this.filteredBridges.clear();
if (bridgeLine != null) {
this.filteredBridges.put(fingerprint, bridgeLine);
this.filteredRelays = matchingRelays;
Map<String, SummaryDocument> matchingBridges = new HashMap<>();
for (String fingerprint : this.lookup) {
if (this.filteredBridges.containsKey(fingerprint)) {
matchingBridges.put(fingerprint, this.filteredBridges.get(fingerprint));
}
}
this.filteredBridges = matchingBridges;
}
private void filterByFingerprint() {
......
......@@ -211,24 +211,22 @@ public class ResourceServlet extends HttpServlet {
rh.setRunning(runningRequested ? "true" : "false");
}
if (parameterMap.containsKey("lookup")) {
String lookupParameter = this.parseFingerprintParameter(
String[] lookupParameter = this.parseFingerprintParameter(
parameterMap.get("lookup"));
if (lookupParameter == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String fingerprint = lookupParameter.toUpperCase();
rh.setLookup(fingerprint);
rh.setLookup(lookupParameter);
}
if (parameterMap.containsKey("fingerprint")) {
String fingerprintParameter = this.parseFingerprintParameter(
String[] fingerprintParameter = this.parseFingerprintParameter(
parameterMap.get("fingerprint"));
if (fingerprintParameter == null) {
if (null == fingerprintParameter || 1 != fingerprintParameter.length) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String fingerprint = fingerprintParameter.toUpperCase();
rh.setFingerprint(fingerprint);
rh.setFingerprint(fingerprintParameter[0]);
}
if (parameterMap.containsKey("country")) {
String countryCodeParameter = this.parseCountryCodeParameter(
......@@ -360,14 +358,13 @@ public class ResourceServlet extends HttpServlet {
rh.setLimit(limitParameter);
}
if (parameterMap.containsKey("family")) {
String familyParameter = this.parseFingerprintParameter(
String[] familyParameter = this.parseFingerprintParameter(
parameterMap.get("family"));
if (familyParameter == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
if (null == familyParameter || 1 != familyParameter.length) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String family = familyParameter.toUpperCase();
rh.setFamily(family);
rh.setFamily(familyParameter[0]);
}
rh.handleRequest();
long parsedRequestMillis = System.currentTimeMillis();
......@@ -473,18 +470,14 @@ public class ResourceServlet extends HttpServlet {
}
private static Pattern fingerprintParameterPattern =
Pattern.compile("^[0-9a-zA-Z]{1,40}$");
Pattern.compile("((^|,)[0-9a-zA-Z]{40})+$");
private String parseFingerprintParameter(String parameter) {
private String[] parseFingerprintParameter(String parameter) {
if (!fingerprintParameterPattern.matcher(parameter).matches()) {
/* Fingerprint contains non-hex character(s). */
return null;
}
if (parameter.length() != 40) {
/* Only full fingerprints are accepted. */
return null;
}
return parameter;
return parameter.toUpperCase().split(",");
}
private static Pattern countryCodeParameterPattern =
......
......@@ -1041,6 +1041,20 @@ public class ResourceServletTest {
null, 0, null);
}
@Test(timeout = 100)
public void testLookupTwoFingerprints() {
this.assertSummaryDocument(
"/summary?lookup=000C5F55BD4814B917CC474BD537F1A3B33CCE2A,"
+ "1FEDE50ED8DBA1DD9F9165F78C8131E4A44AB756", 1,
new String[] { "TorkaZ" }, 1, new String[] { "gummy" });
}
@Test(timeout = 100)
public void testLookupFingerprintTrailingComma() {
this.assertErrorStatusCode(
"/summary?lookup=000C5F55BD4814B917CC474BD537F1A3B33CCE2A,", 400);
}
@Test(timeout = 100)
public void testFingerprintRelayFingerprint() {
this.assertSummaryDocument(
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment