From 174b77b032b16d9571012cd8f2050c1f4ed576c6 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Tue, 19 Jul 2022 14:19:44 +0200 Subject: [PATCH] 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 --- .../graphql/server/webmvc/GraphiQlHandler.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphiQlHandler.java b/spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphiQlHandler.java index b59f532c..c07cea0e 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphiQlHandler.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphiQlHandler.java @@ -78,23 +78,21 @@ public class GraphiQlHandler { } private URI getRedirectUrl(ServerRequest request) { - String contextPath = request.requestPath().contextPath().toString(); - String path = request.requestPath().pathWithinApplication().toString(); - UriBuilder builder = request.uriBuilder().replacePath(contextPath).path(path); - - String pathQueryParam = applyContextPath(request, this.graphQlPath); + UriBuilder builder = request.uriBuilder(); + String pathQueryParam = applyPathPrefix(request, this.graphQlPath); builder.queryParam("path", pathQueryParam); - if (StringUtils.hasText(this.graphQlWsPath)) { - String wsPathQueryParam = applyContextPath(request, this.graphQlWsPath); + String wsPathQueryParam = applyPathPrefix(request, this.graphQlWsPath); builder.queryParam("wsPath", wsPathQueryParam); } return builder.build(); } - private String applyContextPath(ServerRequest request, String path) { - String contextPath = request.requestPath().contextPath().toString(); - return StringUtils.hasText(contextPath) ? contextPath + path : path; + private String applyPathPrefix(ServerRequest request, String path) { + String fullPath = request.requestPath().value(); + String pathWithinApplication = request.requestPath().pathWithinApplication().toString(); + int pathWithinApplicationIndex = fullPath.indexOf(pathWithinApplication); + return (pathWithinApplicationIndex != -1) ? fullPath.substring(0, pathWithinApplicationIndex) + path : path; } }