473118 - HTTP/2 server does not retrieve Host header from client.

This commit is contained in:
Simone Bordet 2015-07-28 12:26:53 +02:00
parent 2e4941920b
commit 25ed10d205
2 changed files with 54 additions and 2 deletions

View File

@ -30,7 +30,10 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HostPortHttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.http.MetaData;
import org.eclipse.jetty.http2.api.Session;
import org.eclipse.jetty.http2.api.Stream;
@ -199,4 +202,41 @@ public class HTTP2Test extends AbstractTest
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
@Test
public void testHostHeader() throws Exception
{
final String host = "fooBar";
final int port = 1313;
final String authority = host + ":" + port;
start(new HttpServlet()
{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Assert.assertEquals(host, request.getServerName());
Assert.assertEquals(port, request.getServerPort());
Assert.assertEquals(authority, request.getHeader("Host"));
}
});
Session session = newClient(new Session.Listener.Adapter());
HostPortHttpField hostHeader = new HostPortHttpField(authority);
MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP, hostHeader, servletPath, HttpVersion.HTTP_2, new HttpFields());
HeadersFrame frame = new HeadersFrame(1, metaData, null, true);
final CountDownLatch latch = new CountDownLatch(1);
session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter()
{
@Override
public void onHeaders(Stream stream, HeadersFrame frame)
{
MetaData.Response response = (MetaData.Response)frame.getMetaData();
Assert.assertEquals(200, response.getStatus());
if (frame.isEndStream())
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
}

View File

@ -56,7 +56,7 @@ public class HttpChannelOverHTTP2 extends HttpChannel
{
super(connector, configuration, endPoint, transport);
}
private IStream getStream()
{
return getHttpTransport().getStream();
@ -73,6 +73,18 @@ public class HttpChannelOverHTTP2 extends HttpChannel
MetaData.Request request = (MetaData.Request)frame.getMetaData();
HttpFields fields = request.getFields();
// HTTP/2 sends the Host header as the :authority
// pseudo-header, so we need to synthesize a Host header.
if (!fields.contains(HttpHeader.HOST))
{
String authority = request.getURI().getAuthority();
if (authority != null)
{
// Lower-case to be consistent with other HTTP/2 headers.
fields.put("host", authority);
}
}
_expect100Continue = fields.contains(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString());
HttpFields response = getResponse().getHttpFields();
@ -169,7 +181,7 @@ public class HttpChannelOverHTTP2 extends HttpChannel
{
return callback.isNonBlocking();
}
@Override
public void succeeded()
{