summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorThomas Becker2012-02-23 09:05:59 (EST)
committer Thomas Becker2012-03-01 09:04:31 (EST)
commit492cc7508a2d0b018d971ae2363ee2c2b2afecff (patch)
treeb2a886a13112e806b37d9208172f3031089de88d
parent5c0c8c7e73c86dd334e856cc28d1cefae3a8f462 (diff)
downloadorg.eclipse.jetty.project-492cc7508a2d0b018d971ae2363ee2c2b2afecff.zip
org.eclipse.jetty.project-492cc7508a2d0b018d971ae2363ee2c2b2afecff.tar.gz
org.eclipse.jetty.project-492cc7508a2d0b018d971ae2363ee2c2b2afecff.tar.bz2
370842: X-Forwarded-Host header is now set by request.getHeader(Host) which doesn't ommit the port informationrefs/changes/01/5201/1
-rw-r--r--jetty-servlets/src/main/java/org/eclipse/jetty/servlets/ProxyServlet.java4
-rw-r--r--jetty-servlets/src/test/java/org/eclipse/jetty/servlets/ProxyServletTest.java65
2 files changed, 50 insertions, 19 deletions
diff --git a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/ProxyServlet.java b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/ProxyServlet.java
index 782d167..445a1bf 100644
--- a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/ProxyServlet.java
+++ b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/ProxyServlet.java
@@ -597,12 +597,14 @@ public class ProxyServlet implements Servlet
{
exchange.addRequestHeader("X-Forwarded-For",request.getRemoteAddr());
exchange.addRequestHeader("X-Forwarded-Proto",request.getScheme());
- exchange.addRequestHeader("X-Forwarded-Host",request.getServerName());
+ exchange.addRequestHeader("X-Forwarded-Host",request.getHeader("Host"));
exchange.addRequestHeader("X-Forwarded-Server",request.getLocalName());
}
if (hasContent)
+ {
exchange.setRequestContentSource(in);
+ }
customizeExchange(exchange, request);
diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/ProxyServletTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/ProxyServletTest.java
index 68a3b97..80e1fe3 100644
--- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/ProxyServletTest.java
+++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/ProxyServletTest.java
@@ -4,6 +4,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
@@ -12,7 +13,6 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-
import junit.framework.Assert;
import org.eclipse.jetty.client.ContentExchange;
import org.eclipse.jetty.client.HttpClient;
@@ -30,21 +30,24 @@ import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.junit.After;
import org.junit.Test;
+import static org.junit.Assert.*;
+import static org.hamcrest.Matchers.*;
+
public class ProxyServletTest
{
- private Server server;
- private Connector connector;
- private HttpClient client;
+ private Server _server;
+ private Connector _connector;
+ private HttpClient _client;
public void init(HttpServlet servlet) throws Exception
{
- server = new Server();
+ _server = new Server();
- connector = new SelectChannelConnector();
- server.addConnector(connector);
+ _connector = new SelectChannelConnector();
+ _server.addConnector(_connector);
HandlerCollection handlers = new HandlerCollection();
- server.setHandler(handlers);
+ _server.setHandler(handlers);
ServletContextHandler proxyCtx = new ServletContextHandler(handlers, "/proxy", ServletContextHandler.NO_SESSIONS);
ServletHolder proxyServletHolder = new ServletHolder(new ProxyServlet()
@@ -66,25 +69,49 @@ public class ProxyServletTest
handlers.addHandler(proxyCtx);
handlers.addHandler(appCtx);
- server.start();
+ _server.start();
- client = new HttpClient();
- client.start();
+ _client = new HttpClient();
+ _client.start();
}
@After
public void destroy() throws Exception
{
- if (client != null)
- client.stop();
+ if (_client != null)
+ _client.stop();
- if (server != null)
+ if (_server != null)
{
- server.stop();
- server.join();
+ _server.stop();
+ _server.join();
}
}
+ @Test
+ public void testXForwardedHostHeader() throws Exception
+ {
+ init(new HttpServlet()
+ {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+ {
+ PrintWriter writer = resp.getWriter();
+ writer.write(req.getHeader("X-Forwarded-Host"));
+ writer.flush();
+ }
+ });
+
+ String url = "http://localhost:" + _connector.getLocalPort() + "/proxy/test";
+ ContentExchange exchange = new ContentExchange();
+ exchange.setURL(url);
+ _client.send(exchange);
+ exchange.waitForDone();
+ assertThat("Response expected to contain content of X-Forwarded-Host Header from the request",exchange.getResponseContent(),equalTo("localhost:"
+ + _connector.getLocalPort()));
+ }
@Test
public void testBigDownloadWithSlowReader() throws Exception
@@ -101,6 +128,8 @@ public class ProxyServletTest
init(new HttpServlet()
{
+ private static final long serialVersionUID = 1L;
+
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
@@ -114,7 +143,7 @@ public class ProxyServletTest
}
});
- String url = "http://localhost:" + connector.getLocalPort() + "/proxy" + "/test";
+ String url = "http://localhost:" + _connector.getLocalPort() + "/proxy/test";
ContentExchange exchange = new ContentExchange(true)
{
@Override
@@ -134,7 +163,7 @@ public class ProxyServletTest
};
exchange.setURL(url);
long start = System.nanoTime();
- client.send(exchange);
+ _client.send(exchange);
Assert.assertEquals(HttpExchange.STATUS_COMPLETED, exchange.waitForDone());
long elapsed = System.nanoTime() - start;
Assert.assertEquals(HttpStatus.OK_200, exchange.getResponseStatus());