Skip to content

Commit 174b77b

Browse files
committed
Redirect GraphiQL clients with servlet path
This commit ensures that both servlet context and servlet paths are taken into account when generating URL paths to the GraphQL HTTP and WS endpoints. Fixes gh-402
1 parent b947554 commit 174b77b

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphiQlHandler.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,21 @@ public ServerResponse handleRequest(ServerRequest request) {
7878
}
7979

8080
private URI getRedirectUrl(ServerRequest request) {
81-
String contextPath = request.requestPath().contextPath().toString();
82-
String path = request.requestPath().pathWithinApplication().toString();
83-
UriBuilder builder = request.uriBuilder().replacePath(contextPath).path(path);
84-
85-
String pathQueryParam = applyContextPath(request, this.graphQlPath);
81+
UriBuilder builder = request.uriBuilder();
82+
String pathQueryParam = applyPathPrefix(request, this.graphQlPath);
8683
builder.queryParam("path", pathQueryParam);
87-
8884
if (StringUtils.hasText(this.graphQlWsPath)) {
89-
String wsPathQueryParam = applyContextPath(request, this.graphQlWsPath);
85+
String wsPathQueryParam = applyPathPrefix(request, this.graphQlWsPath);
9086
builder.queryParam("wsPath", wsPathQueryParam);
9187
}
9288
return builder.build();
9389
}
9490

95-
private String applyContextPath(ServerRequest request, String path) {
96-
String contextPath = request.requestPath().contextPath().toString();
97-
return StringUtils.hasText(contextPath) ? contextPath + path : path;
91+
private String applyPathPrefix(ServerRequest request, String path) {
92+
String fullPath = request.requestPath().value();
93+
String pathWithinApplication = request.requestPath().pathWithinApplication().toString();
94+
int pathWithinApplicationIndex = fullPath.indexOf(pathWithinApplication);
95+
return (pathWithinApplicationIndex != -1) ? fullPath.substring(0, pathWithinApplicationIndex) + path : path;
9896
}
9997

10098
}

0 commit comments

Comments
 (0)