Commit 718cafcd by Dave Syer

Add X-Forwarded-Port explicitly for Tomcat (and other servers)

Fixes gh-872
parent 908dcc0e
...@@ -39,7 +39,8 @@ public class PreDecorationFilter extends ZuulFilter { ...@@ -39,7 +39,8 @@ public class PreDecorationFilter extends ZuulFilter {
private UrlPathHelper urlPathHelper = new UrlPathHelper(); private UrlPathHelper urlPathHelper = new UrlPathHelper();
public PreDecorationFilter(RouteLocator routeLocator, boolean addProxyHeaders, boolean removeSemicolonContent) { public PreDecorationFilter(RouteLocator routeLocator, boolean addProxyHeaders,
boolean removeSemicolonContent) {
this.routeLocator = routeLocator; this.routeLocator = routeLocator;
this.addProxyHeaders = addProxyHeaders; this.addProxyHeaders = addProxyHeaders;
this.urlPathHelper.setRemoveSemicolonContent(removeSemicolonContent); this.urlPathHelper.setRemoveSemicolonContent(removeSemicolonContent);
...@@ -59,7 +60,8 @@ public class PreDecorationFilter extends ZuulFilter { ...@@ -59,7 +60,8 @@ public class PreDecorationFilter extends ZuulFilter {
public boolean shouldFilter() { public boolean shouldFilter() {
RequestContext ctx = RequestContext.getCurrentContext(); RequestContext ctx = RequestContext.getCurrentContext();
return !ctx.containsKey("forward.to") // another filter has already forwarded return !ctx.containsKey("forward.to") // another filter has already forwarded
&& !ctx.containsKey("serviceId"); // another filter has already determined serviceId && !ctx.containsKey("serviceId"); // another filter has already determined
// serviceId
} }
@Override @Override
...@@ -97,8 +99,9 @@ public class PreDecorationFilter extends ZuulFilter { ...@@ -97,8 +99,9 @@ public class PreDecorationFilter extends ZuulFilter {
} }
if (this.addProxyHeaders) { if (this.addProxyHeaders) {
ctx.addZuulRequestHeader("X-Forwarded-Host", ctx.addZuulRequestHeader("X-Forwarded-Host",
ctx.getRequest().getServerName() + ":" ctx.getRequest().getServerName());
+ String.valueOf(ctx.getRequest().getServerPort())); ctx.addZuulRequestHeader("X-Forwarded-Port",
String.valueOf(ctx.getRequest().getServerPort()));
ctx.addZuulRequestHeader(ZuulHeaders.X_FORWARDED_PROTO, ctx.addZuulRequestHeader(ZuulHeaders.X_FORWARDED_PROTO,
ctx.getRequest().getScheme()); ctx.getRequest().getScheme());
if (StringUtils.hasText(route.getPrefix())) { if (StringUtils.hasText(route.getPrefix())) {
......
...@@ -62,20 +62,23 @@ import com.netflix.loadbalancer.Server; ...@@ -62,20 +62,23 @@ import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList; import com.netflix.loadbalancer.ServerList;
import com.netflix.niws.client.http.RestClient; import com.netflix.niws.client.http.RestClient;
import lombok.SneakyThrows; import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import lombok.SneakyThrows;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleZuulProxyApplication.class) @SpringApplicationConfiguration(classes = SampleZuulProxyApplication.class)
@WebAppConfiguration @WebAppConfiguration
@IntegrationTest({"server.port: 0", @IntegrationTest({ "server.port: 0",
"zuul.routes.other: /test/**=http://localhost:7777/local", "zuul.routes.other: /test/**=http://localhost:7777/local",
"zuul.routes.another: /another/twolevel/**", "zuul.routes.simple: /simple/**", "zuul.routes.another: /another/twolevel/**", "zuul.routes.simple: /simple/**",
"zuul.routes.badhost: /badhost/**", "zuul.ignoredHeaders: X-Header", "zuul.routes.badhost: /badhost/**", "zuul.ignoredHeaders: X-Header",
"zuul.removeSemicolonContent: false"}) "zuul.routes.rnd: /rnd/**", "rnd.ribbon.listOfServers: ${random.value}",
"zuul.removeSemicolonContent: false" })
@DirtiesContext @DirtiesContext
public class SampleZuulProxyApplicationTests extends ZuulProxyTestBase { public class SampleZuulProxyApplicationTests extends ZuulProxyTestBase {
...@@ -140,9 +143,8 @@ public class SampleZuulProxyApplicationTests extends ZuulProxyTestBase { ...@@ -140,9 +143,8 @@ public class SampleZuulProxyApplicationTests extends ZuulProxyTestBase {
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/"); this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/");
this.endpoint.reset(); this.endpoint.reset();
ResponseEntity<String> result = new TestRestTemplate().exchange( ResponseEntity<String> result = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/self/query?foo={foo}", "http://localhost:" + this.port + "/self/query?foo={foo}", HttpMethod.GET,
HttpMethod.GET, new HttpEntity<>((Void) null), String.class, new HttpEntity<>((Void) null), String.class, "weird#chars");
"weird#chars");
assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("/query?foo=weird#chars", result.getBody()); assertEquals("/query?foo=weird#chars", result.getBody());
} }
...@@ -169,6 +171,18 @@ public class SampleZuulProxyApplicationTests extends ZuulProxyTestBase { ...@@ -169,6 +171,18 @@ public class SampleZuulProxyApplicationTests extends ZuulProxyTestBase {
"http://localhost:" + this.port + "/badhost/1", HttpMethod.GET, "http://localhost:" + this.port + "/badhost/1", HttpMethod.GET,
new HttpEntity<>((Void) null), String.class); new HttpEntity<>((Void) null), String.class);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, result.getStatusCode()); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, result.getStatusCode());
// JSON response
assertThat(result.getBody(), containsString("\"status\":500"));
}
@Test
public void ribbonCommandRandomHostFromConfig() {
ResponseEntity<String> result = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/rnd/1", HttpMethod.GET,
new HttpEntity<>((Void) null), String.class);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, result.getStatusCode());
// JSON response
assertThat(result.getBody(), containsString("\"status\":500"));
} }
@Test @Test
...@@ -211,9 +225,10 @@ class SampleZuulProxyApplication extends ZuulProxyTestBase.AbstractZuulProxyAppl ...@@ -211,9 +225,10 @@ class SampleZuulProxyApplication extends ZuulProxyTestBase.AbstractZuulProxyAppl
@RequestMapping("/matrix/{name}/{another}") @RequestMapping("/matrix/{name}/{another}")
public String matrix(@PathVariable("name") String name, public String matrix(@PathVariable("name") String name,
@MatrixVariable(value = "p", pathVar = "name") int p, @MatrixVariable(value = "p", pathVar = "name") int p,
@MatrixVariable(value = "q", pathVar = "name") int q, @MatrixVariable(value = "q", pathVar = "name") int q,
@PathVariable("another") String another, @MatrixVariable(value = "x", pathVar = "another") int x) { @PathVariable("another") String another,
@MatrixVariable(value = "x", pathVar = "another") int x) {
return name + "=" + p + "-" + q + ";" + another + "=" + x; return name + "=" + p + "-" + q + ";" + another + "=" + x;
} }
...@@ -275,7 +290,7 @@ class SampleZuulProxyApplication extends ZuulProxyTestBase.AbstractZuulProxyAppl ...@@ -275,7 +290,7 @@ class SampleZuulProxyApplication extends ZuulProxyTestBase.AbstractZuulProxyAppl
} }
} }
// Load balancer with fixed server list for "simple" pointing to localhost // Load balancer with fixed server list for "simple" pointing to bad host
@Configuration @Configuration
static class BadHostRibbonClientConfiguration { static class BadHostRibbonClientConfiguration {
@Bean @Bean
......
...@@ -90,7 +90,8 @@ public class PreDecorationFilterTests { ...@@ -90,7 +90,8 @@ public class PreDecorationFilterTests {
this.filter.run(); this.filter.run();
RequestContext ctx = RequestContext.getCurrentContext(); RequestContext ctx = RequestContext.getCurrentContext();
assertEquals("/foo/1", ctx.get("requestURI")); assertEquals("/foo/1", ctx.get("requestURI"));
assertEquals("localhost:80", ctx.getZuulRequestHeaders().get("x-forwarded-host")); assertEquals("localhost", ctx.getZuulRequestHeaders().get("x-forwarded-host"));
assertEquals("80", ctx.getZuulRequestHeaders().get("x-forwarded-port"));
assertEquals("http", ctx.getZuulRequestHeaders().get("x-forwarded-proto")); assertEquals("http", ctx.getZuulRequestHeaders().get("x-forwarded-proto"));
assertEquals("/api", ctx.getZuulRequestHeaders().get("x-forwarded-prefix")); assertEquals("/api", ctx.getZuulRequestHeaders().get("x-forwarded-prefix"));
assertEquals("foo", assertEquals("foo",
...@@ -128,7 +129,7 @@ public class PreDecorationFilterTests { ...@@ -128,7 +129,7 @@ public class PreDecorationFilterTests {
this.filter.run(); this.filter.run();
RequestContext ctx = RequestContext.getCurrentContext(); RequestContext ctx = RequestContext.getCurrentContext();
assertEquals("/1", ctx.get("requestURI")); assertEquals("/1", ctx.get("requestURI"));
assertEquals("localhost:80", ctx.getZuulRequestHeaders().get("x-forwarded-host")); assertEquals("localhost", ctx.getZuulRequestHeaders().get("x-forwarded-host"));
assertEquals("http", ctx.getZuulRequestHeaders().get("x-forwarded-proto")); assertEquals("http", ctx.getZuulRequestHeaders().get("x-forwarded-proto"));
assertEquals("/api/foo", ctx.getZuulRequestHeaders().get("x-forwarded-prefix")); assertEquals("/api/foo", ctx.getZuulRequestHeaders().get("x-forwarded-prefix"));
assertEquals("foo", assertEquals("foo",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment