Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions src/main/java/com/google/api/pathtemplate/PathTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,17 @@ public class PathTemplate {

/**
* A constant identifying the special variable used for endpoint bindings in the result of
* {@link #matchFromFullName(String)}.
* {@link #matchFromFullName(String)}. It may also contain protocol string, if its provided in the
* input.
*/
public static final String HOSTNAME_VAR = "$hostname";

// A regexp to match a custom verb at the end of a path.
private static final Pattern CUSTOM_VERB_PATTERN = Pattern.compile(":([^/*}{=]+)$");

// A regex to match a hostname with or without protocol.
private static final Pattern HOSTNAME_PATTERN = Pattern.compile("^(\\w+:)?//");
Comment thread
ajaaym marked this conversation as resolved.

// A splitter on slash.
private static final Splitter SLASH_SPLITTER = Splitter.on('/').trimResults();

Expand Down Expand Up @@ -533,10 +537,10 @@ private Map<String, String> match(String path, boolean forceHostName) {
path = path.substring(0, matcher.start(0));
}

// Do full match.
boolean withHostName = path.startsWith("//");
Matcher matcher = HOSTNAME_PATTERN.matcher(path);
boolean withHostName = matcher.find();
if (withHostName) {
path = path.substring(2);
path = matcher.replaceFirst("");
}
List<String> input = SLASH_SPLITTER.splitToList(path);
int inPos = 0;
Expand All @@ -548,16 +552,42 @@ private Map<String, String> match(String path, boolean forceHostName) {
String hostName = input.get(inPos++);
if (withHostName) {
// Put the // back, so we can distinguish this case from forceHostName.
hostName = "//" + hostName;
hostName = matcher.group(0) + hostName;
}
values.put(HOSTNAME_VAR, hostName);
}
if (withHostName) {
inPos = alignInputToAlignableSegment(input, inPos, segments.get(0));
}
if (!match(input, inPos, segments, 0, values)) {
return null;
}
return ImmutableMap.copyOf(values);
}

// Aligns input to start of literal value of literal or binding segment if input contains hostname.
private int alignInputToAlignableSegment(List<String> input, int inPos, Segment segment) {
switch (segment.kind()) {
case BINDING:
inPos = alignInputPositionToLiteral(input, inPos, segment.value() + "s");
return inPos + 1;
case LITERAL:
return alignInputPositionToLiteral(input, inPos, segment.value());
}
return inPos;
}

// Aligns input to start of literal value if input contains hostname.
private int alignInputPositionToLiteral(
List<String> input, int inPos, String literalSegmentValue) {
for (; inPos < input.size(); inPos++) {
if (literalSegmentValue.equals(input.get(inPos))) {
return inPos;
}
}
return inPos;
}

// Tries to match the input based on the segments at given positions. Returns a boolean
// indicating whether the match was successful.
private boolean match(
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/com/google/api/pathtemplate/PathTemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ public void matchWithHostName() {
Truth.assertThat(match.get("$1")).isEqualTo("o");
}

@Test
public void matchWithHostNameAndProtocol() {
PathTemplate template = PathTemplate.create("projects/{project}/zones/{zone}");
Map<String, String> match =
template.match(
"https://www.googleapis.com/compute/v1/projects/project-123/zones/europe-west3-c");
Truth.assertThat(match).isNotNull();
Truth.assertThat(match.get(PathTemplate.HOSTNAME_VAR)).isEqualTo("https://www.googleapis.com");
Truth.assertThat(match.get("project")).isEqualTo("project-123");
Truth.assertThat(match.get("zone")).isEqualTo("europe-west3-c");
}

@Test
public void matchWithHostNameAndProtocolWithTemplateStartWithBinding() {
PathTemplate template = PathTemplate.create("{project}/zones/{zone}");
Map<String, String> match =
template.match(
"https://www.googleapis.com/compute/v1/projects/project-123/zones/europe-west3-c");
Truth.assertThat(match).isNotNull();
Truth.assertThat(match.get(PathTemplate.HOSTNAME_VAR)).isEqualTo("https://www.googleapis.com");
Truth.assertThat(match.get("project")).isEqualTo("project-123");
Truth.assertThat(match.get("zone")).isEqualTo("europe-west3-c");
}

@Test
public void matchWithCustomMethod() {
PathTemplate template = PathTemplate.create("buckets/*/objects/*:custom");
Expand Down