Skip to content

Commit 43f7810

Browse files
author
Ryan Baxter
committed
Provide better error handling for invalid hostnames. Fixes spring-projects#159.
1 parent 7fcd385 commit 43f7810

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerInterceptor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.http.client.ClientHttpRequestExecution;
2424
import org.springframework.http.client.ClientHttpRequestInterceptor;
2525
import org.springframework.http.client.ClientHttpResponse;
26+
import org.springframework.util.Assert;
2627

2728
/**
2829
* @author Spencer Gibb
@@ -42,6 +43,7 @@ public ClientHttpResponse intercept(final HttpRequest request, final byte[] body
4243
final ClientHttpRequestExecution execution) throws IOException {
4344
final URI originalUri = request.getURI();
4445
String serviceName = originalUri.getHost();
46+
Assert.state(serviceName != null, "Request URI does not contain a valid hostname: " + originalUri);
4547
return this.loadBalancer.execute(serviceName,
4648
new LoadBalancerRequest<ClientHttpResponse>() {
4749
@Override

spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.springframework.retry.RetryContext;
1313
import org.springframework.retry.policy.NeverRetryPolicy;
1414
import org.springframework.retry.support.RetryTemplate;
15+
import org.springframework.util.Assert;
1516

1617
/**
1718
* @author Ryan Baxter
@@ -38,6 +39,7 @@ public ClientHttpResponse intercept(final HttpRequest request, final byte[] body
3839
final ClientHttpRequestExecution execution) throws IOException {
3940
final URI originalUri = request.getURI();
4041
final String serviceName = originalUri.getHost();
42+
Assert.state(serviceName != null, "Request URI does not contain a valid hostname: " + originalUri);
4143
LoadBalancedRetryPolicy retryPolicy = lbRetryPolicyFactory.create(serviceName,
4244
loadBalancer);
4345
retryTemplate.setRetryPolicy(

spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptorTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.springframework.retry.policy.NeverRetryPolicy;
1717
import org.springframework.retry.support.RetryTemplate;
1818

19+
import static org.bouncycastle.crypto.tls.ConnectionEnd.client;
1920
import static org.hamcrest.MatcherAssert.assertThat;
2021
import static org.hamcrest.Matchers.is;
2122
import static org.mockito.Matchers.any;
@@ -69,6 +70,25 @@ public void interceptDisableRetry() throws Throwable {
6970
verify(retryTemplate, times(1)).setRetryPolicy(any(NeverRetryPolicy.class));
7071
}
7172

73+
@Test(expected = IllegalStateException.class)
74+
public void interceptInvalidHost() throws Throwable {
75+
HttpRequest request = mock(HttpRequest.class);
76+
when(request.getURI()).thenReturn(new URI("http://foo_underscore"));
77+
ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new byte[]{}, HttpStatus.OK);
78+
LoadBalancedRetryPolicy policy = mock(LoadBalancedRetryPolicy.class);
79+
InterceptorRetryPolicy interceptorRetryPolicy = new InterceptorRetryPolicy(request, policy, client,"foo");
80+
LoadBalancedRetryPolicyFactory lbRetryPolicyFactory = mock(LoadBalancedRetryPolicyFactory.class);
81+
when(lbRetryPolicyFactory.create(eq("foo_underscore"), any(ServiceInstanceChooser.class))).thenReturn(policy);
82+
ServiceInstance serviceInstance = mock(ServiceInstance.class);
83+
when(client.choose(eq("foo_underscore"))).thenReturn(serviceInstance);
84+
when(client.execute(eq("foo_underscore"), eq(serviceInstance), any(LoadBalancerRequest.class))).thenReturn(clientHttpResponse);
85+
lbProperties.setEnabled(true);
86+
RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(client, retryTemplate, lbProperties, lbRetryPolicyFactory);
87+
byte[] body = new byte[]{};
88+
ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
89+
ClientHttpResponse rsp = interceptor.intercept(request, body, execution);
90+
}
91+
7292
@Test
7393
public void interceptNeverRetry() throws Throwable {
7494
HttpRequest request = mock(HttpRequest.class);

0 commit comments

Comments
 (0)