Skip to content

Commit 0cd4fe7

Browse files
committed
strip redundant port numbers to fix API Explorer
API Explorer does not allow http://host:80 or https://host:443, which is what we get from the servlet container. This change removes the port numbers in these cases.
1 parent 6ead7f5 commit 0cd4fe7

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

endpoints-framework/src/main/java/com/google/api/server/spi/handlers/ExplorerHandler.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void handle(EndpointsContext context) throws IOException {
3535
}
3636

3737
private String getExplorerUrl(HttpServletRequest req, String path) {
38-
String url = Strings.stripTrailingSlash(req.getRequestURL().toString());
38+
String url = stripRedundantPorts(Strings.stripTrailingSlash(req.getRequestURL().toString()));
3939
// This will convert http://localhost:8080/_ah/api/explorer to
4040
// http://apis-explorer.appspot.com/apis-explorer/?base=http://localhost:8080/_ah/api&
4141
// root=http://localhost:8080/_ah/api
@@ -45,4 +45,15 @@ private String getExplorerUrl(HttpServletRequest req, String path) {
4545
String apiRoot = url.substring(0, url.length() - path.length() - 1);
4646
return EXPLORER_URL + "?base=" + apiRoot + "&root=" + apiRoot;
4747
}
48+
49+
private static String stripRedundantPorts(String url) {
50+
if (url == null) {
51+
return null;
52+
} else if (url.startsWith("http:") && url.contains(":80/")) {
53+
return url.replace(":80/", "/");
54+
} else if (url.startsWith("https:") && url.contains(":443/")) {
55+
return url.replace(":443/", "/");
56+
}
57+
return url;
58+
}
4859
}

endpoints-framework/src/test/java/com/google/api/server/spi/handlers/ExplorerHandlerTest.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,34 @@
3131
public class ExplorerHandlerTest {
3232
@Test
3333
public void testHandle() throws Exception {
34+
testHandle("http", 8080, "http://apis-explorer.appspot.com/apis-explorer/"
35+
+ "?base=http://localhost:8080/_ah/api&root=http://localhost:8080/_ah/api");
36+
}
37+
38+
@Test
39+
public void testHandle_explicitHttpPort() throws Exception {
40+
testHandle("http", 80, "http://apis-explorer.appspot.com/apis-explorer/"
41+
+ "?base=http://localhost/_ah/api&root=http://localhost/_ah/api");
42+
}
43+
44+
@Test
45+
public void testHandle_explicitHttpsPort() throws Exception {
46+
testHandle("https", 443, "http://apis-explorer.appspot.com/apis-explorer/"
47+
+ "?base=https://localhost/_ah/api&root=https://localhost/_ah/api");
48+
}
49+
50+
private void testHandle(String scheme, int port, String expectedLocation) throws Exception {
3451
MockHttpServletRequest request = new MockHttpServletRequest();
52+
request.setScheme(scheme);
3553
request.setServerName("localhost");
36-
request.setServerPort(8080);
54+
request.setServerPort(port);
3755
request.setRequestURI("/_ah/api/explorer/");
3856
MockHttpServletResponse response = new MockHttpServletResponse();
3957
ExplorerHandler handler = new ExplorerHandler();
4058
EndpointsContext context = new EndpointsContext("GET", "explorer", request, response);
4159
handler.handle(context);
4260

4361
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_FOUND);
44-
assertThat(response.getHeader("Location")).isEqualTo(
45-
"http://apis-explorer.appspot.com/apis-explorer/?base=http://localhost:8080/_ah/api"
46-
+ "&root=http://localhost:8080/_ah/api");
62+
assertThat(response.getHeader("Location")).isEqualTo(expectedLocation);
4763
}
4864
}

0 commit comments

Comments
 (0)