Skip to content

Commit f881486

Browse files
authored
Merge pull request #3770 from SebHeuze/gh-3769-prefix-path-double-encoded
Fix prefixPath double encoding
2 parents a4a4002 + 2a1d831 commit f881486

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/BeforeFilterFunctions.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ public static Function<ServerRequest, ServerRequest> prefixPath(String prefix) {
195195

196196
String newPath = uri.getRawPath() + request.uri().getRawPath();
197197

198-
URI prefixedUri = UriComponentsBuilder.fromUri(request.uri()).replacePath(newPath).build().toUri();
198+
URI prefixedUri = UriComponentsBuilder.fromUri(request.uri())
199+
.replacePath(newPath).build(true).toUri();
199200
return ServerRequest.from(request).uri(prefixedUri).build();
200201
};
201202
}

spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/filter/BeforeFilterFunctionsTests.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,32 @@ void rewritePathWithEnDashAndSpace() {
243243
.isEqualTo("/modified/path/with%E2%80%93en%E2%80%93dashes%20and%20spaces");
244244
}
245245

246+
@Test
247+
void prefixPath() {
248+
MockHttpServletRequest servletRequest = MockMvcRequestBuilders
249+
.get("http://localhost/get").buildRequest(null);
250+
251+
ServerRequest request = ServerRequest.create(servletRequest,
252+
Collections.emptyList());
253+
254+
ServerRequest modified = BeforeFilterFunctions.prefixPath("/prefix")
255+
.apply(request);
256+
257+
assertThat(modified.uri().getRawPath()).isEqualTo("/prefix/get");
258+
}
259+
260+
@Test
261+
void prefixEncodedPath() {
262+
MockHttpServletRequest servletRequest = MockMvcRequestBuilders
263+
.get("http://localhost/get/é").buildRequest(null);
264+
265+
ServerRequest request = ServerRequest.create(servletRequest,
266+
Collections.emptyList());
267+
268+
ServerRequest modified = BeforeFilterFunctions.prefixPath("/pre fix")
269+
.apply(request);
270+
271+
assertThat(modified.uri().getRawPath()).isEqualTo("/pre%20fix/get/%C3%A9");
272+
}
273+
246274
}

0 commit comments

Comments
 (0)