Skip to content

Commit

Permalink
[net] Expose Jetty EndPoint in http requests (#4092)
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Álvarez <miguelwork92@gmail.com>
  • Loading branch information
GiviMAD authored Mar 2, 2024
1 parent 97d64a1 commit a94dd51
Showing 1 changed file with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
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();
}
}
}
}

0 comments on commit a94dd51

Please sign in to comment.