use of org.eclipse.jetty.http.HostPortHttpField in project jetty.project by eclipse.
the class HTTP2Test method testHostHeader.
@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(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));
}
use of org.eclipse.jetty.http.HostPortHttpField in project jetty.project by eclipse.
the class ProxyTest method newRequest.
private MetaData.Request newRequest(String method, String path, HttpFields fields) {
String host = "localhost";
int port = proxyConnector.getLocalPort();
String authority = host + ":" + port;
return new MetaData.Request(method, HttpScheme.HTTP, new HostPortHttpField(authority), path, HttpVersion.HTTP_2, fields);
}
use of org.eclipse.jetty.http.HostPortHttpField in project jetty.project by eclipse.
the class ForwardedRequestCustomizer method customize.
@Override
public void customize(Connector connector, HttpConfiguration config, Request request) {
HttpFields httpFields = request.getHttpFields();
RFC7239 rfc7239 = null;
String forwardedHost = null;
String forwardedServer = null;
String forwardedFor = null;
String forwardedProto = null;
String forwardedHttps = null;
// Do a single pass through the header fields as it is a more efficient single iteration.
for (HttpField field : httpFields) {
String name = field.getName();
if (getForwardedCipherSuiteHeader() != null && getForwardedCipherSuiteHeader().equalsIgnoreCase(name)) {
request.setAttribute("javax.servlet.request.cipher_suite", field.getValue());
if (isSslIsSecure()) {
request.setSecure(true);
request.setScheme(config.getSecureScheme());
}
}
if (getForwardedSslSessionIdHeader() != null && getForwardedSslSessionIdHeader().equalsIgnoreCase(name)) {
request.setAttribute("javax.servlet.request.ssl_session_id", field.getValue());
if (isSslIsSecure()) {
request.setSecure(true);
request.setScheme(config.getSecureScheme());
}
}
if (forwardedHost == null && _forwardedHostHeader != null && _forwardedHostHeader.equalsIgnoreCase(name))
forwardedHost = getLeftMost(field.getValue());
if (forwardedServer == null && _forwardedServerHeader != null && _forwardedServerHeader.equalsIgnoreCase(name))
forwardedServer = getLeftMost(field.getValue());
if (forwardedFor == null && _forwardedForHeader != null && _forwardedForHeader.equalsIgnoreCase(name))
forwardedFor = getLeftMost(field.getValue());
if (forwardedProto == null && _forwardedProtoHeader != null && _forwardedProtoHeader.equalsIgnoreCase(name))
forwardedProto = getLeftMost(field.getValue());
if (forwardedHttps == null && _forwardedHttpsHeader != null && _forwardedHttpsHeader.equalsIgnoreCase(name))
forwardedHttps = getLeftMost(field.getValue());
if (_forwardedHeader != null && _forwardedHeader.equalsIgnoreCase(name)) {
if (rfc7239 == null)
rfc7239 = new RFC7239();
rfc7239.addValue(field.getValue());
}
}
// Handle host header if if not available any RFC7230.by or X-ForwardedServer header
if (_forcedHost != null) {
// Update host header
httpFields.put(_forcedHost);
request.setAuthority(_forcedHost.getHost(), _forcedHost.getPort());
} else if (rfc7239 != null && rfc7239._host != null) {
HostPortHttpField auth = rfc7239._host;
httpFields.put(auth);
request.setAuthority(auth.getHost(), auth.getPort());
} else if (forwardedHost != null) {
HostPortHttpField auth = new HostPortHttpField(forwardedHost);
httpFields.put(auth);
request.setAuthority(auth.getHost(), auth.getPort());
} else if (_proxyAsAuthority) {
if (rfc7239 != null && rfc7239._by != null) {
HostPortHttpField auth = rfc7239._by;
httpFields.put(auth);
request.setAuthority(auth.getHost(), auth.getPort());
} else if (forwardedServer != null) {
request.setAuthority(forwardedServer, request.getServerPort());
}
}
// handle remote end identifier
if (rfc7239 != null && rfc7239._for != null) {
request.setRemoteAddr(InetSocketAddress.createUnresolved(rfc7239._for.getHost(), rfc7239._for.getPort()));
} else if (forwardedFor != null) {
request.setRemoteAddr(InetSocketAddress.createUnresolved(forwardedFor, request.getRemotePort()));
}
// handle protocol identifier
if (rfc7239 != null && rfc7239._proto != null) {
request.setScheme(rfc7239._proto);
if (rfc7239._proto.equals(config.getSecureScheme()))
request.setSecure(true);
} else if (forwardedProto != null) {
request.setScheme(forwardedProto);
if (forwardedProto.equals(config.getSecureScheme()))
request.setSecure(true);
} else if (forwardedHttps != null && ("on".equalsIgnoreCase(forwardedHttps) || "true".equalsIgnoreCase(forwardedHttps))) {
request.setScheme(HttpScheme.HTTPS.asString());
if (HttpScheme.HTTPS.asString().equals(config.getSecureScheme()))
request.setSecure(true);
}
}
use of org.eclipse.jetty.http.HostPortHttpField in project jetty.project by eclipse.
the class HttpChannelOverHttp method parsedHeader.
@Override
public void parsedHeader(HttpField field) {
HttpHeader header = field.getHeader();
String value = field.getValue();
if (header != null) {
switch(header) {
case CONNECTION:
_connection = field;
break;
case HOST:
if (!_metadata.getURI().isAbsolute() && field instanceof HostPortHttpField) {
HostPortHttpField hp = (HostPortHttpField) field;
_metadata.getURI().setAuthority(hp.getHost(), hp.getPort());
}
break;
case EXPECT:
{
if (_metadata.getHttpVersion() == HttpVersion.HTTP_1_1) {
HttpHeaderValue expect = HttpHeaderValue.CACHE.get(value);
switch(expect == null ? HttpHeaderValue.UNKNOWN : expect) {
case CONTINUE:
_expect100Continue = true;
break;
case PROCESSING:
_expect102Processing = true;
break;
default:
String[] values = field.getValues();
for (int i = 0; values != null && i < values.length; i++) {
expect = HttpHeaderValue.CACHE.get(values[i].trim());
if (expect == null)
_unknownExpectation = true;
else {
switch(expect) {
case CONTINUE:
_expect100Continue = true;
break;
case PROCESSING:
_expect102Processing = true;
break;
default:
_unknownExpectation = true;
}
}
}
}
}
break;
}
case UPGRADE:
_upgrade = field;
break;
default:
break;
}
}
_fields.add(field);
}
use of org.eclipse.jetty.http.HostPortHttpField in project jetty.project by eclipse.
the class AbstractServerTest method newRequest.
protected MetaData.Request newRequest(String method, HttpFields fields) {
String host = "localhost";
int port = connector.getLocalPort();
String authority = host + ":" + port;
return new MetaData.Request(method, HttpScheme.HTTP, new HostPortHttpField(authority), path, HttpVersion.HTTP_2, fields);
}
Aggregations