aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core
diff options
context:
space:
mode:
authorBjørn Christian Seime <bjorncs@oath.com>2018-11-19 14:53:00 +0100
committerBjørn Christian Seime <bjorncs@oath.com>2018-11-19 15:00:50 +0100
commitd0af4c555289ca73a3c7119c60d87ecfd6d4676e (patch)
tree14a1b25555a332693c043eb24414bbc5f9cfd2d3 /jdisc_core
parent5a591f4c70ae2ec28a3bc32e32434ccd36694b40 (diff)
Match uris with implicit port given from scheme
Diffstat (limited to 'jdisc_core')
-rw-r--r--jdisc_core/src/main/java/com/yahoo/jdisc/application/UriPattern.java17
-rw-r--r--jdisc_core/src/test/java/com/yahoo/jdisc/application/UriPatternTestCase.java9
2 files changed, 25 insertions, 1 deletions
diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/application/UriPattern.java b/jdisc_core/src/main/java/com/yahoo/jdisc/application/UriPattern.java
index 3481e0612fc..89cb6412181 100644
--- a/jdisc_core/src/main/java/com/yahoo/jdisc/application/UriPattern.java
+++ b/jdisc_core/src/main/java/com/yahoo/jdisc/application/UriPattern.java
@@ -25,6 +25,7 @@ import java.util.regex.Pattern;
* </ul>
*
* @author Simon Thoresen Hult
+ * @author bjorncs
*/
public class UriPattern implements Comparable<UriPattern> {
@@ -86,7 +87,7 @@ public class UriPattern implements Comparable<UriPattern> {
if (pathMatch == null) {
return null;
}
- if (port > 0 && port != uri.getPort()) {
+ if (port > 0 && port != resolvePortComponent(uri)) {
return null;
}
// Match scheme before host because it has a higher chance of differing (e.g. http versus https)
@@ -157,6 +158,20 @@ public class UriPattern implements Comparable<UriPattern> {
return Integer.parseInt(str);
}
+ private static int resolvePortComponent(URI uri) {
+ int rawPort = uri.getPort();
+ return rawPort != -1 ? rawPort : resolvePortFromScheme(uri.getScheme());
+ }
+
+ private static int resolvePortFromScheme(String scheme) {
+ if (scheme == null) return -1;
+ switch (scheme) {
+ case "http": return 80;
+ case "https": return 443;
+ default: return -1;
+ }
+ }
+
/**
* <p>This class holds the result of a {@link UriPattern#match(URI)} operation. It contains methods to inspect the
* groups captured during matching, where a <em>group</em> is defined as a sequence of characters matches by a
diff --git a/jdisc_core/src/test/java/com/yahoo/jdisc/application/UriPatternTestCase.java b/jdisc_core/src/test/java/com/yahoo/jdisc/application/UriPatternTestCase.java
index 7ee5b038d6a..c91a7134c3a 100644
--- a/jdisc_core/src/test/java/com/yahoo/jdisc/application/UriPatternTestCase.java
+++ b/jdisc_core/src/test/java/com/yahoo/jdisc/application/UriPatternTestCase.java
@@ -286,6 +286,15 @@ public class UriPatternTestCase {
"scheme://host:69/foo/bar");
}
+ @Test
+ public void requireThatUrisWithImplicitPortFromSchemeMatchesBindingWithExplicitPort() {
+ UriPattern httpPattern = new UriPattern("http://host:80/path");
+ assertMatch(httpPattern, "http://host/path", NO_GROUPS);
+
+ UriPattern httpsPattern = new UriPattern("https://host:443/path");
+ assertMatch(httpsPattern, "https://host/path", NO_GROUPS);
+ }
+
private static void assertIllegalPattern(String uri) {
try {
new UriPattern(uri);