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

Support comma-separated list in "as" parameter.

Implements the last part of #23713.
parent a7d03e23
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
- Add "as" field as a copy of the "as_number" field in preparation - Add "as" field as a copy of the "as_number" field in preparation
of removing the "as_number" field in the future. of removing the "as_number" field in the future.
- Add new "as_name" parameter to search relays by AS name. - Add new "as_name" parameter to search relays by AS name.
- Support a comma-separated list of AS numbers in the "as"
parameter.
# Changes in version 6.1-1.15.0 - 2018-07-16 # Changes in version 6.1-1.15.0 - 2018-07-16
......
...@@ -72,9 +72,9 @@ public class RequestHandler { ...@@ -72,9 +72,9 @@ public class RequestHandler {
this.country = country; this.country = country;
} }
private String as; private String[] as;
public void setAs(String as) { public void setAs(String[] as) {
this.as = as; this.as = as;
} }
...@@ -408,29 +408,23 @@ public class RequestHandler { ...@@ -408,29 +408,23 @@ public class RequestHandler {
} }
private void filterByAsNumber() { private void filterByAsNumber() {
if (this.as == null) { if (this.as == null || 0 == this.as.length) {
/* Not filtering by AS number. */ /* Not filtering by AS number. */
return; return;
} }
String asNumber = this.as.toUpperCase(); Map<String, SummaryDocument> matchingRelays = new HashMap<>();
if (!asNumber.startsWith("AS")) { for (String asNumber : this.as) {
asNumber = "AS" + asNumber; if (this.nodeIndex.getRelaysByAsNumber().containsKey(asNumber)) {
} for (String fingerprint
if (!this.nodeIndex.getRelaysByAsNumber().containsKey(asNumber)) { : this.nodeIndex.getRelaysByAsNumber().get(asNumber)) {
this.filteredRelays.clear(); if (this.filteredRelays.containsKey(fingerprint)) {
} else { matchingRelays.put(fingerprint,
Set<String> relaysWithAsNumber = this.filteredRelays.get(fingerprint));
this.nodeIndex.getRelaysByAsNumber().get(asNumber); }
Set<String> removeRelays = new HashSet<>();
for (String fingerprint : this.filteredRelays.keySet()) {
if (!relaysWithAsNumber.contains(fingerprint)) {
removeRelays.add(fingerprint);
} }
} }
for (String fingerprint : removeRelays) {
this.filteredRelays.remove(fingerprint);
}
} }
this.filteredRelays = matchingRelays;
this.filteredBridges.clear(); this.filteredBridges.clear();
} }
......
...@@ -238,7 +238,7 @@ public class ResourceServlet extends HttpServlet { ...@@ -238,7 +238,7 @@ public class ResourceServlet extends HttpServlet {
rh.setCountry(countryCodeParameter); rh.setCountry(countryCodeParameter);
} }
if (parameterMap.containsKey("as")) { if (parameterMap.containsKey("as")) {
String asNumberParameter = this.parseAsNumberParameter( String[] asNumberParameter = this.parseAsNumberParameter(
parameterMap.get("as")); parameterMap.get("as"));
if (asNumberParameter == null) { if (asNumberParameter == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST); response.sendError(HttpServletResponse.SC_BAD_REQUEST);
...@@ -502,14 +502,20 @@ public class ResourceServlet extends HttpServlet { ...@@ -502,14 +502,20 @@ public class ResourceServlet extends HttpServlet {
} }
private static Pattern asNumberParameterPattern = private static Pattern asNumberParameterPattern =
Pattern.compile("^[asAS]{0,2}[0-9]{1,10}$"); Pattern.compile("((^|,)([aA][sS])?[1-9][0-9]{0,9})+$");
private String parseAsNumberParameter(String parameter) { private String[] parseAsNumberParameter(String parameter) {
if (!asNumberParameterPattern.matcher(parameter).matches()) { if (!asNumberParameterPattern.matcher(parameter).matches()) {
/* AS number contains illegal character(s). */ /* AS number contains illegal character(s). */
return null; return null;
} }
return parameter; String[] parameterParts = parameter.toUpperCase().split(",");
String[] parsedParameter = new String[parameterParts.length];
for (int i = 0; i < parameterParts.length; i++) {
parsedParameter[i] = (!parameterParts[i].startsWith("AS") ? "AS" : "")
+ parameterParts[i];
}
return parsedParameter;
} }
private String[] parseAsNameParameter(String parameter) { private String[] parseAsNameParameter(String parameter) {
......
...@@ -1173,6 +1173,12 @@ public class ResourceServletTest { ...@@ -1173,6 +1173,12 @@ public class ResourceServletTest {
"/summary?as=AS 8767", 400); "/summary?as=AS 8767", 400);
} }
@Test(timeout = 100)
public void testAs8767Or7922() {
this.assertSummaryDocument("/summary?as=8767,7922", 2,
new String[] { "TorkaZ", "Ferrari458" }, 0, null);
}
@Test(timeout = 100) @Test(timeout = 100)
public void testAsNameComcast() { public void testAsNameComcast() {
this.assertSummaryDocument("/summary?as_name=Comcast", 1, null, 0, null); this.assertSummaryDocument("/summary?as_name=Comcast", 1, null, 0, null);
......
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