diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/util/CommonUtils.java b/cas-client-core/src/main/java/org/jasig/cas/client/util/CommonUtils.java index c767c56..8fb90c7 100644 --- a/cas-client-core/src/main/java/org/jasig/cas/client/util/CommonUtils.java +++ b/cas-client-core/src/main/java/org/jasig/cas/client/util/CommonUtils.java @@ -225,6 +225,21 @@ public final class CommonUtils { return serverNames[0]; } + private static boolean serverNameContainsPort(final boolean containsScheme, final String serverName) { + if (!containsScheme && serverName.contains(":")) { + return true; + } + + final int schemeIndex = serverName.indexOf(":"); + final int portIndex = serverName.lastIndexOf(":"); + return schemeIndex != portIndex; + } + + private static boolean requestIsOnStandardPort(final HttpServletRequest request) { + final int serverPort = request.getServerPort(); + return serverPort == 80 || serverPort == 443; + } + /** * Constructs a service url from the HttpServletRequest or from the given * serviceUrl. Prefers the serviceUrl provided if both a serviceUrl and a @@ -250,11 +265,19 @@ public final class CommonUtils { final String serverName = findMatchingServerName(request, serverNames); + boolean containsScheme = true; if (!serverName.startsWith("https://") && !serverName.startsWith("http://")) { buffer.append(request.isSecure() ? "https://" : "http://"); + containsScheme = false; } buffer.append(serverName); + + if (!serverNameContainsPort(containsScheme, serverName) && !requestIsOnStandardPort(request)) { + buffer.append(":"); + buffer.append(request.getServerPort()); + } + buffer.append(request.getRequestURI()); if (CommonUtils.isNotBlank(request.getQueryString())) { diff --git a/cas-client-core/src/test/java/org/jasig/cas/client/util/CommonUtilsTests.java b/cas-client-core/src/test/java/org/jasig/cas/client/util/CommonUtilsTests.java index f6e686c..43967ab 100644 --- a/cas-client-core/src/test/java/org/jasig/cas/client/util/CommonUtilsTests.java +++ b/cas-client-core/src/test/java/org/jasig/cas/client/util/CommonUtilsTests.java @@ -136,6 +136,27 @@ public final class CommonUtilsTests extends TestCase { assertEquals(CONST_MY_URL, constructedUrl); } + private void constructUrlNonStandardPortAndNoPortInConfigTest(final String serverNameList) { + final String CONST_MY_URL = "https://www.myserver.com:555/hello/hithere/"; + final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hello/hithere/"); + request.addHeader("Host", "www.myserver.com"); + request.setScheme("https"); + request.setSecure(true); + request.setServerPort(555); + final MockHttpServletResponse response = new MockHttpServletResponse(); + final String constructedUrl = CommonUtils.constructServiceUrl(request, response, null, + serverNameList, "ticket", false); + assertEquals(CONST_MY_URL, constructedUrl); + } + + public void testConstructUrlNonStandardPortAndNoScheme() { + constructUrlNonStandardPortAndNoPortInConfigTest("www.myserver.com"); + } + + public void testConstructUrlNonStandardPortAndScheme() { + constructUrlNonStandardPortAndNoPortInConfigTest("https://www.myserver.com"); + } + public void testConstructUrlWithMultipleHostsNoPortsOrProtocol() { final String CONST_MY_URL = "https://www.myserver.com/hello/hithere/"; final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hello/hithere/");