Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[net] Expose Jetty EndPoint in http requests #4092

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -24,11 +24,20 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.HttpDestination;
import org.eclipse.jetty.client.HttpExchange;
import org.eclipse.jetty.client.HttpProxy;
import org.eclipse.jetty.client.api.Connection;
import org.eclipse.jetty.client.http.HttpChannelOverHTTP;
import org.eclipse.jetty.client.http.HttpClientTransportOverHTTP;
import org.eclipse.jetty.client.http.HttpConnectionOverHTTP;
import org.eclipse.jetty.client.http.HttpReceiverOverHTTP;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.http.PreEncodedHttpField;
import org.eclipse.jetty.http2.client.HTTP2Client;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.util.Promise;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.websocket.client.WebSocketClient;
Expand Down Expand Up @@ -235,7 +244,7 @@ private HttpClient createHttpClientInternal(String consumerName, @Nullable SslCo
try {
logger.debug("creating http client for consumer {}", consumerName);

HttpClient httpClient = new HttpClient(
HttpClient httpClient = new HttpClient(new CustomHttpClientTransportOverHTTP(),
sslContextFactory != null ? sslContextFactory : createSslContextFactory());

// If proxy is set as property (standard Java property), provide the proxy information to Jetty HTTP
Expand Down Expand Up @@ -413,4 +422,48 @@ private HTTP2Client createHttp2ClientInternal(String consumerName, @Nullable Ssl
"unexpected checked exception during initialization of the Jetty HTTP/2 client", e);
}
}

/**
* Extends the default {@link HttpClientTransportOverHTTP) but exposes the underling {@link EndPoint} of each
* request/response.
* It mimics the way it's done in higher Jetty Http client versions.
*/
private static class CustomHttpClientTransportOverHTTP extends HttpClientTransportOverHTTP {
@Override
@NonNullByDefault({})
protected HttpConnectionOverHTTP newHttpConnection(EndPoint endPoint, HttpDestination destination,
Promise<Connection> promise) {
return new HttpConnectionOverHTTP(endPoint, destination, promise) {
@Override
protected HttpChannelOverHTTP newHttpChannel() {
return new HttpChannelOverHTTP(this) {
@Override
protected HttpReceiverOverHTTP newHttpReceiver() {
return new CustomHttpReceiverOverHTTP(this);
}
};
}
};
}

private static class CustomHttpReceiverOverHTTP extends HttpReceiverOverHTTP {
private final HttpChannelOverHTTP channel;

public CustomHttpReceiverOverHTTP(HttpChannelOverHTTP channel) {
super(channel);
this.channel = channel;
}

@Override
public boolean headerComplete() {
HttpExchange exchange = getHttpExchange();
if (exchange != null) {
// Store the EndPoint is case of upgrades
exchange.getRequest().getConversation().setAttribute(EndPoint.class.getName(),
channel.getHttpConnection().getEndPoint());
}
return super.headerComplete();
}
}
}
}