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

Fix local proxy handling in REST Client module #45161

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,12 @@ public void start(List<RestClientHttpProxyBuildItem> restClientHttpProxyBuildIte

var urlKeyName = String.format("quarkus.rest-client.\"%s\".override-uri", bi.getClassName());
var urlKeyValue = String.format("http://%s:%d", createResult.host(), createResult.port());
if (baseUri.getPath() != null) {
if (!"/".equals(baseUri.getPath()) && !baseUri.getPath().isEmpty()) {
urlKeyValue = urlKeyValue + "/" + baseUri.getPath();
String basePath = baseUri.getPath();
if ((basePath != null) && !basePath.isEmpty()) {
if (basePath.startsWith("/")) {
basePath = basePath.substring(1);
}
urlKeyValue = urlKeyValue + "/" + basePath;
}

devServicePropertiesProducer.produce(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
import io.quarkus.rest.client.reactive.spi.RestClientHttpProxyBuildItem;
import io.quarkus.runtime.ResettableSystemProperties;
import io.vertx.core.Future;
import io.vertx.core.MultiMap;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.file.FileSystemOptions;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpServer;
import io.vertx.core.metrics.MetricsOptions;
import io.vertx.core.net.HostAndPort;
import io.vertx.httpproxy.HttpProxy;
import io.vertx.httpproxy.ProxyContext;
import io.vertx.httpproxy.ProxyInterceptor;
Expand Down Expand Up @@ -71,16 +71,18 @@ public CreateResult create(RestClientHttpProxyBuildItem buildItem) {
}
HttpClient proxyClient = vertx.get().createHttpClient(clientOptions);
HttpProxy proxy = HttpProxy.reverseProxy(proxyClient);
proxy.origin(determineOriginPort(baseUri), baseUri.getHost())
.addInterceptor(new HostSettingInterceptor(baseUri.getHost()));
int targetPort = determineOriginPort(baseUri);
String targetHost = baseUri.getHost();
proxy.origin(targetPort, targetHost)
.addInterceptor(new AuthoritySettingInterceptor(targetPort, targetHost));

HttpServer proxyServer = vertx.get().createHttpServer();
Integer port = findRandomPort();
proxyServer.requestHandler(proxy).listen(port);
Integer proxyPort = findRandomPort();
proxyServer.requestHandler(proxy).listen(proxyPort);

logStartup(buildItem.getClassName(), port);
logStartup(buildItem.getClassName(), proxyPort);

return new CreateResult("localhost", port, new HttpServerClosable(proxyServer));
return new CreateResult("localhost", proxyPort, new HttpServerClosable(proxyServer));
}

protected void logStartup(String className, Integer port) {
Expand Down Expand Up @@ -124,19 +126,18 @@ private Integer findRandomPort() {
* This class sets the Host HTTP Header in order to avoid having services being blocked
* for presenting a wrong value
*/
private static class HostSettingInterceptor implements ProxyInterceptor {
private static class AuthoritySettingInterceptor implements ProxyInterceptor {

private final String host;
private final HostAndPort authority;

private HostSettingInterceptor(String host) {
this.host = host;
private AuthoritySettingInterceptor(int targetPort, String host) {
this.authority = HostAndPort.authority(host, targetPort);
}

@Override
public Future<ProxyResponse> handleProxyRequest(ProxyContext context) {
ProxyRequest request = context.request();
MultiMap headers = request.headers();
headers.set("Host", host);
request.setAuthority(authority);

return context.sendRequest();
}
Expand Down
Loading