diff options
-rw-r--r-- | jdisc_core/src/main/java/com/yahoo/jdisc/application/UriPattern.java | 17 | ||||
-rw-r--r-- | jdisc_core/src/test/java/com/yahoo/jdisc/application/UriPatternTestCase.java | 9 |
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); |