jetty / jetty.project

Eclipse Jetty® - Web Container & Clients - supports HTTP/2, HTTP/1.1, HTTP/1.0, websocket, servlets, and more

Home Page:https://eclipse.dev/jetty

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Setting :authority with HTTP2 transport

moscicky opened this issue · comments

Jetty Version
12

Jetty Environment
core

Java Version
17

Question
In #5633 a support for setting :authority header with HttpClient was introduced. The original issue mentions a fluent builder liker API (authority()) for a Request but the linked PR is using sth called HTTPDestination.

Could you point me to a piece of docs or guide me how to properly use the API? Say I am sending an HTTP2 request to an Envoy proxy running on localhost:5000 and want to include :authority header set to: foo.bar.baz

The Jetty 12 version of the same testcase is ...

@ParameterizedTest
@MethodSource("transports")
public void testRequestWithDifferentDestination(Transport transport) throws Exception
{
String requestScheme = newURI(transport).getScheme();
String requestHost = "otherHost.com";
int requestPort = 8888;
start(transport, new Handler.Abstract()
{
@Override
public boolean handle(Request request, org.eclipse.jetty.server.Response response, Callback callback)
{
HttpURI uri = request.getHttpURI();
assertEquals(requestHost, uri.getHost());
assertEquals(requestPort, uri.getPort());
if (transport == Transport.H2C || transport == Transport.H2)
assertEquals(requestScheme, uri.getScheme());
callback.succeeded();
return true;
}
});
if (transport.isSecure())
httpConfig.getCustomizer(SecureRequestCustomizer.class).setSniHostCheck(false);
Origin origin = new Origin(requestScheme, "localhost", ((NetworkConnector)connector).getLocalPort());
Destination destination = client.resolveDestination(origin);
var request = client.newRequest(requestHost, requestPort)
.scheme(requestScheme)
.path("/path");
CountDownLatch resultLatch = new CountDownLatch(1);
destination.send(request, result ->
{
assertTrue(result.isSucceeded());
assertEquals(HttpStatus.OK_200, result.getResponse().getStatus());
resultLatch.countDown();
});
assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
}

What's the key piece of information is ...

// Setup the destination to physically connect to.
Origin origin = new Origin(scheme, destHost, destPort);
Destination destination = client.resolveDestination(origin);

// What the HTTP request should contain for Authority
var request = client.newRequest(requestHost, requestPort)
    .scheme(requestScheme)
    .path("/path");

// Initiate the request to the specified destination.
destination.send(request, result -> {
    // process the results
});

Great, thanks!