Skip to content

Commit

Permalink
Allow for non-encoded special characters in urls
Browse files Browse the repository at this point in the history
Fixes gh-3185
  • Loading branch information
spencergibb committed Mar 6, 2024
1 parent 4ec55db commit b38499e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.cloud.gateway.server.mvc.filter.HttpHeadersFilter.RequestHttpHeadersFilter;
import org.springframework.cloud.gateway.server.mvc.filter.HttpHeadersFilter.ResponseHttpHeadersFilter;
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;
import org.springframework.web.servlet.function.HandlerFunction;
import org.springframework.web.servlet.function.ServerRequest;
import org.springframework.web.servlet.function.ServerResponse;
Expand Down Expand Up @@ -66,7 +67,7 @@ public ProxyExchangeHandlerFunction(ProxyExchange proxyExchange,
@Override
public ServerResponse handle(ServerRequest serverRequest) {
URI uri = uriResolver.apply(serverRequest);
boolean encoded = containsEncodedQuery(serverRequest.uri());
boolean encoded = containsEncodedQuery(serverRequest.uri(), serverRequest.params());
// @formatter:off
URI url = UriComponentsBuilder.fromUri(serverRequest.uri())
.scheme(uri.getScheme())
Expand Down Expand Up @@ -113,14 +114,15 @@ private <TYPE> HttpHeaders filterHeaders(Stream<HttpHeadersFilter<TYPE>> filters
return filtered;
}

private static boolean containsEncodedQuery(URI uri) {
boolean encoded = (uri.getRawQuery() != null && uri.getRawQuery().contains("%"))
private static boolean containsEncodedQuery(URI uri, MultiValueMap<String, String> params) {
String rawQuery = uri.getRawQuery();
boolean encoded = (rawQuery != null && rawQuery.contains("%"))
|| (uri.getRawPath() != null && uri.getRawPath().contains("%"));

// Verify if it is really fully encoded. Treat partial encoded as unencoded.
if (encoded) {
try {
UriComponentsBuilder.fromUri(uri).build(true);
UriComponentsBuilder.fromUri(uri).replaceQueryParams(params).build(true);
return true;
}
catch (IllegalArgumentException ignored) {
Expand All @@ -132,7 +134,7 @@ private static boolean containsEncodedQuery(URI uri) {
return false;
}

return encoded;
return false;
}

public interface URIResolver extends Function<ServerRequest, URI> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,21 @@ public void queryParamWorks() {
});
}

@SuppressWarnings("rawtypes")
@Test
public void queryParamWithSpecialCharactersWorks() {
restClient.get().uri("/get?myparam= &intlparam=æøå").exchange().expectStatus().isOk().expectBody(Map.class)
.consumeWith(result -> {
Map responseBody = result.getResponseBody();
assertThat(responseBody).containsKey("args");
Map args = getMap(responseBody, "args");
assertThat(args).containsKey("myparam");
assertThat(args.get("myparam")).isEqualTo(" ");
assertThat(args).containsKey("intlparam");
assertThat(args.get("intlparam")).isEqualTo("æøå");
});
}

@SpringBootConfiguration
@EnableAutoConfiguration
@LoadBalancerClient(name = "httpbin", configuration = TestLoadBalancerConfig.Httpbin.class)
Expand Down

0 comments on commit b38499e

Please sign in to comment.