Skip to content

Commit 82c2116

Browse files
Merge pull request apereo#248 from SmithJosh/respect-path-in-servername-config
Include serverName path when constructing service url
2 parents ee9dcc6 + 6dbfdd2 commit 82c2116

3 files changed

Lines changed: 32 additions & 21 deletions

File tree

cas-client-core/src/main/java/org/jasig/cas/client/util/CommonUtils.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -257,17 +257,7 @@ protected static String findMatchingServerName(final HttpServletRequest request,
257257

258258
return serverNames[0];
259259
}
260-
261-
private static boolean serverNameContainsPort(final boolean containsScheme, final String serverName) {
262-
if (!containsScheme && serverName.contains(":")) {
263-
return true;
264-
}
265-
266-
final int schemeIndex = serverName.indexOf(":");
267-
final int portIndex = serverName.lastIndexOf(":");
268-
return schemeIndex != portIndex;
269-
}
270-
260+
271261
private static boolean requestIsOnStandardPort(final HttpServletRequest request) {
272262
final int serverPort = request.getServerPort();
273263
return serverPort == 80 || serverPort == 443;
@@ -325,23 +315,18 @@ public static String constructServiceUrl(final HttpServletRequest request, final
325315
originalRequestUrl.setParameters(request.getQueryString());
326316

327317
final URIBuilder builder;
328-
329-
boolean containsScheme = true;
330318
if (!serverName.startsWith("https://") && !serverName.startsWith("http://")) {
331-
builder = new URIBuilder(encode);
332-
builder.setScheme(request.isSecure() ? "https" : "http");
333-
builder.setHost(serverName);
334-
containsScheme = false;
335-
} else {
319+
String scheme = request.isSecure() ? "https://" : "http://";
320+
builder = new URIBuilder(scheme + serverName, encode);
321+
} else {
336322
builder = new URIBuilder(serverName, encode);
337323
}
338324

339-
340-
if (!serverNameContainsPort(containsScheme, serverName) && !requestIsOnStandardPort(request)) {
325+
if (builder.getPort() == -1 && !requestIsOnStandardPort(request)) {
341326
builder.setPort(request.getServerPort());
342327
}
343328

344-
builder.setEncodedPath(request.getRequestURI());
329+
builder.setEncodedPath(builder.getEncodedPath() + request.getRequestURI());
345330

346331
final List<String> serviceParameterNames = Arrays.asList(serviceParameterName.split(","));
347332
if (!serviceParameterNames.isEmpty() && !originalRequestUrl.getQueryParams().isEmpty()) {

cas-client-core/src/main/java/org/jasig/cas/client/util/URIBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,10 @@ public String getPath() {
523523
return this.path;
524524
}
525525

526+
public String getEncodedPath() {
527+
return this.encodedPath;
528+
}
529+
526530
public List<BasicNameValuePair> getQueryParams() {
527531
if (this.queryParams != null) {
528532
return new ArrayList<BasicNameValuePair>(this.queryParams);

cas-client-core/src/test/java/org/jasig/cas/client/util/CommonUtilsTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,28 @@ public void testConstructServiceUrlWithTrailingSlash() {
139139
assertEquals(CONST_MY_URL, constructedUrl);
140140
}
141141

142+
public void testConstructServiceUrlWithServerNameContainingPath() {
143+
final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hello/hithere/");
144+
request.setScheme("https");
145+
request.setSecure(true);
146+
final MockHttpServletResponse response = new MockHttpServletResponse();
147+
final String constructedUrl = CommonUtils.constructServiceUrl(request, response, null, "www.my.server.com/app",
148+
Protocol.CAS3.getServiceParameterName(), Protocol.CAS3.getArtifactParameterName(), false);
149+
150+
assertEquals("https://www.my.server.com/app/hello/hithere/", constructedUrl);
151+
}
152+
153+
public void testConstructServiceUrlWithServerNameContainingPathAndSchema() {
154+
final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hello/hithere/");
155+
request.setScheme("https");
156+
request.setSecure(true);
157+
final MockHttpServletResponse response = new MockHttpServletResponse();
158+
final String constructedUrl = CommonUtils.constructServiceUrl(request, response, null, "https://www.my.server.com/app",
159+
Protocol.CAS3.getServiceParameterName(), Protocol.CAS3.getArtifactParameterName(), false);
160+
161+
assertEquals("https://www.my.server.com/app/hello/hithere/", constructedUrl);
162+
}
163+
142164
public void testConstructServiceUrlWithParamsCas() {
143165
final String CONST_MY_URL = "https://www.myserver.com/hello/hithere/";
144166
final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hello/hithere/");

0 commit comments

Comments
 (0)