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 @@
- Add "as" field as a copy of the "as_number" field in preparation
of removing the "as_number" field in the future.
- 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
......
......@@ -72,9 +72,9 @@ public class RequestHandler {
this.country = country;
}
private String as;
private String[] as;
public void setAs(String as) {
public void setAs(String[] as) {
this.as = as;
}
......@@ -408,29 +408,23 @@ public class RequestHandler {
}
private void filterByAsNumber() {
if (this.as == null) {
if (this.as == null || 0 == this.as.length) {
/* Not filtering by AS number. */
return;
}
String asNumber = this.as.toUpperCase();
if (!asNumber.startsWith("AS")) {
asNumber = "AS" + asNumber;
}
if (!this.nodeIndex.getRelaysByAsNumber().containsKey(asNumber)) {
this.filteredRelays.clear();
} else {
Set<String> relaysWithAsNumber =
this.nodeIndex.getRelaysByAsNumber().get(asNumber);
Set<String> removeRelays = new HashSet<>();
for (String fingerprint : this.filteredRelays.keySet()) {
if (!relaysWithAsNumber.contains(fingerprint)) {
removeRelays.add(fingerprint);
Map<String, SummaryDocument> matchingRelays = new HashMap<>();
for (String asNumber : this.as) {
if (this.nodeIndex.getRelaysByAsNumber().containsKey(asNumber)) {
for (String fingerprint
: this.nodeIndex.getRelaysByAsNumber().get(asNumber)) {
if (this.filteredRelays.containsKey(fingerprint)) {
matchingRelays.put(fingerprint,
this.filteredRelays.get(fingerprint));
}
}
}
for (String fingerprint : removeRelays) {
this.filteredRelays.remove(fingerprint);
}
}
this.filteredRelays = matchingRelays;
this.filteredBridges.clear();
}
......
......@@ -238,7 +238,7 @@ public class ResourceServlet extends HttpServlet {
rh.setCountry(countryCodeParameter);
}
if (parameterMap.containsKey("as")) {
String asNumberParameter = this.parseAsNumberParameter(
String[] asNumberParameter = this.parseAsNumberParameter(
parameterMap.get("as"));
if (asNumberParameter == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
......@@ -502,14 +502,20 @@ public class ResourceServlet extends HttpServlet {
}
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()) {
/* AS number contains illegal character(s). */
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) {
......
......@@ -1173,6 +1173,12 @@ public class ResourceServletTest {
"/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)
public void testAsNameComcast() {
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