checks if query parameters are encoded then sets encoding appropriately.
fixes gh-139
This commit is contained in:
@@ -26,11 +26,11 @@ import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
|
||||
import org.springframework.cloud.gateway.support.NotFoundException;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.addOriginalRequestUrl;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedQuery;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@@ -72,11 +72,12 @@ public class LoadBalancerClientFilter implements GlobalFilter, Ordered {
|
||||
|
||||
/*URI uri = exchange.getRequest().getURI();
|
||||
URI requestUrl = loadBalancer.reconstructURI(instance, uri);*/
|
||||
boolean encoded = containsEncodedQuery(url);
|
||||
URI requestUrl = UriComponentsBuilder.fromUri(url)
|
||||
.scheme(instance.isSecure()? "https" : "http") //TODO: support websockets
|
||||
.host(instance.getHost())
|
||||
.port(instance.getPort())
|
||||
.build(false)
|
||||
.build(encoded)
|
||||
.toUri();
|
||||
log.trace("LoadBalancerClientFilter url chosen: " + requestUrl);
|
||||
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, requestUrl);
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedQuery;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@@ -51,9 +52,11 @@ public class RouteToRequestUrlFilter implements GlobalFilter, Ordered {
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
log.trace("RouteToRequestUrlFilter start");
|
||||
URI requestUrl = UriComponentsBuilder.fromHttpRequest(exchange.getRequest())
|
||||
URI uri = exchange.getRequest().getURI();
|
||||
boolean encoded = containsEncodedQuery(uri);
|
||||
URI requestUrl = UriComponentsBuilder.fromUri(uri)
|
||||
.uri(route.getUri())
|
||||
.build(false)
|
||||
.build(encoded)
|
||||
.toUri();
|
||||
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, requestUrl);
|
||||
return chain.filter(exchange);
|
||||
|
||||
@@ -67,6 +67,13 @@ public class ServerWebExchangeUtils {
|
||||
return response;
|
||||
}
|
||||
|
||||
public static boolean containsEncodedQuery(URI uri) {
|
||||
if (uri.getRawQuery() == null) {
|
||||
return false;
|
||||
}
|
||||
return uri.getRawQuery().contains("%");
|
||||
}
|
||||
|
||||
public static HttpStatus parse(String statusString) {
|
||||
HttpStatus httpStatus;
|
||||
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* Copyright 2013-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.gateway.filter;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.mock.web.server.MockServerWebExchange;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class LoadBalancerClientFilterTests {
|
||||
|
||||
@Test
|
||||
public void happyPath() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest
|
||||
.get("http://localhost/get?a=b")
|
||||
.build();
|
||||
|
||||
URI lbUri = URI.create("lb://service1?a=b");
|
||||
ServerWebExchange webExchange = testFilter(request, lbUri);
|
||||
URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
|
||||
assertThat(uri).hasScheme("http").hasHost("service1-host1")
|
||||
.hasParameter("a", "b");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noQueryParams() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest
|
||||
.get("http://localhost/get")
|
||||
.build();
|
||||
|
||||
ServerWebExchange webExchange = testFilter(request, URI.create("lb://service1"));
|
||||
URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
|
||||
assertThat(uri).hasScheme("http").hasHost("service1-host1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodedParameters() {
|
||||
URI url = UriComponentsBuilder.fromUriString("http://localhost/get?a=b&c=d[]").buildAndExpand().encode().toUri();
|
||||
|
||||
MockServerHttpRequest request = MockServerHttpRequest
|
||||
.method(HttpMethod.GET, url)
|
||||
.build();
|
||||
|
||||
URI lbUrl = UriComponentsBuilder.fromUriString("lb://service1?a=b&c=d[]").buildAndExpand().encode().toUri();
|
||||
|
||||
// prove that it is encoded
|
||||
assertThat(lbUrl.getRawQuery()).isEqualTo("a=b&c=d%5B%5D");
|
||||
|
||||
assertThat(lbUrl).hasParameter("c", "d[]");
|
||||
|
||||
ServerWebExchange webExchange = testFilter(request, lbUrl);
|
||||
URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
|
||||
assertThat(uri).hasScheme("http").hasHost("service1-host1")
|
||||
.hasParameter("a", "b")
|
||||
.hasParameter("c", "d[]");
|
||||
|
||||
// prove that it is not double encoded
|
||||
assertThat(uri.getRawQuery()).isEqualTo("a=b&c=d%5B%5D");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unencodedParameters() {
|
||||
URI url = URI.create("http://localhost/get?a=b&c=d[]");
|
||||
|
||||
MockServerHttpRequest request = MockServerHttpRequest
|
||||
.method(HttpMethod.GET, url)
|
||||
.build();
|
||||
|
||||
URI lbUrl = URI.create("lb://service1?a=b&c=d[]");
|
||||
|
||||
// prove that it is unencoded
|
||||
assertThat(lbUrl.getRawQuery()).isEqualTo("a=b&c=d[]");
|
||||
|
||||
ServerWebExchange webExchange = testFilter(request, lbUrl);
|
||||
|
||||
URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
|
||||
assertThat(uri).hasScheme("http").hasHost("service1-host1")
|
||||
.hasParameter("a", "b")
|
||||
.hasParameter("c", "d[]");
|
||||
|
||||
// prove that it is NOT encoded
|
||||
assertThat(uri.getRawQuery()).isEqualTo("a=b&c=d[]");
|
||||
}
|
||||
|
||||
private ServerWebExchange testFilter(MockServerHttpRequest request, URI uri) {
|
||||
ServerWebExchange exchange = MockServerWebExchange.from(request);
|
||||
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, uri);
|
||||
|
||||
GatewayFilterChain filterChain = mock(GatewayFilterChain.class);
|
||||
|
||||
ArgumentCaptor<ServerWebExchange> captor = ArgumentCaptor.forClass(ServerWebExchange.class);
|
||||
when(filterChain.filter(captor.capture())).thenReturn(Mono.empty());
|
||||
|
||||
LoadBalancerClient loadBalancerClient = mock(LoadBalancerClient.class);
|
||||
when(loadBalancerClient.choose("service1")).
|
||||
thenReturn(new DefaultServiceInstance("service1", "service1-host1", 8081,
|
||||
false, Collections.emptyMap()));
|
||||
|
||||
LoadBalancerClientFilter filter = new LoadBalancerClientFilter(loadBalancerClient);
|
||||
filter.filter(exchange, filterChain);
|
||||
|
||||
return captor.getValue();
|
||||
}
|
||||
}
|
||||
@@ -54,6 +54,17 @@ public class RouteToRequestUrlFilterTests {
|
||||
.hasParameter("a", "b");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noQueryParams() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest
|
||||
.get("http://localhost/get")
|
||||
.build();
|
||||
|
||||
ServerWebExchange webExchange = testFilter(request, "http://myhost");
|
||||
URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
|
||||
assertThat(uri).hasScheme("http").hasHost("myhost");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodedParameters() {
|
||||
URI url = UriComponentsBuilder.fromUriString("http://localhost/get?a=b&c=d[]").buildAndExpand().encode().toUri();
|
||||
@@ -72,6 +83,9 @@ public class RouteToRequestUrlFilterTests {
|
||||
assertThat(uri).hasScheme("http").hasHost("myhost")
|
||||
.hasParameter("a", "b")
|
||||
.hasParameter("c", "d[]");
|
||||
|
||||
// prove that it is not double encoded
|
||||
assertThat(uri.getRawQuery()).isEqualTo("a=b&c=d%5B%5D");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -91,6 +105,9 @@ public class RouteToRequestUrlFilterTests {
|
||||
assertThat(uri).hasScheme("http").hasHost("myhost")
|
||||
.hasParameter("a", "b")
|
||||
.hasParameter("c", "d[]");
|
||||
|
||||
// prove that it is NOT encoded
|
||||
assertThat(uri.getRawQuery()).isEqualTo("a=b&c=d[]");
|
||||
}
|
||||
|
||||
private ServerWebExchange testFilter(MockServerHttpRequest request, String url) {
|
||||
|
||||
Reference in New Issue
Block a user