Skip to content

Commit aafb92a

Browse files
ytvnrphiz71
authored andcommitted
fix: rework http client creation
1 parent 2b87d50 commit aafb92a

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

src/main/java/io/gravitee/policy/callout/CalloutHttpPolicy.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -234,24 +234,30 @@ public record Req(String url, Optional<String> body, List<HttpHeader> headerList
234234
*/
235235
private HttpClient getHttpClient(BaseExecutionContext ctx) {
236236
if (this.httpClient == null) {
237-
// Vertx only uses ssl options when https
238-
var options = new HttpClientOptions().setSsl(true).setTrustAll(true).setVerifyHost(false);
237+
synchronized (this) {
238+
if (this.httpClient == null) {
239+
// SSL options (trustAll, verifyHost) are set at client level but only applied per-request
240+
// when the URL scheme is HTTPS. Vertx's RequestOptions.setAbsoluteURI() overrides the client
241+
// SSL setting based on the actual URL scheme, so HTTP requests are not affected.
242+
var options = new HttpClientOptions().setSsl(true).setTrustAll(true).setVerifyHost(false);
239243

240-
if (configuration.isUseSystemProxy()) {
241-
Configuration config = ctx.getComponent(Configuration.class);
242-
try {
243-
options.setProxyOptions(VertxProxyOptionsUtils.buildProxyOptions(config));
244-
} catch (IllegalStateException e) {
245-
ctx
246-
.withLogger(log)
247-
.warn(
248-
"CalloutHttp requires a system proxy to be defined but some configurations are missing or not well defined: {}. Ignoring proxy",
249-
e.getMessage()
250-
);
244+
if (configuration.isUseSystemProxy()) {
245+
Configuration config = ctx.getComponent(Configuration.class);
246+
try {
247+
options.setProxyOptions(VertxProxyOptionsUtils.buildProxyOptions(config));
248+
} catch (IllegalStateException e) {
249+
ctx
250+
.withLogger(log)
251+
.warn(
252+
"CalloutHttp requires a system proxy to be defined but some configurations are missing or not well defined: {}. Ignoring proxy",
253+
e.getMessage()
254+
);
255+
}
256+
}
257+
var vertx = ctx.getComponent(Vertx.class);
258+
this.httpClient = vertx.createHttpClient(options);
251259
}
252260
}
253-
var vertx = ctx.getComponent(Vertx.class);
254-
this.httpClient = vertx.createHttpClient(options);
255261
}
256262
return this.httpClient;
257263
}

src/main/java/io/gravitee/policy/v3/callout/CalloutHttpPolicyV3.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -172,21 +172,27 @@ public void end() {
172172

173173
private HttpClient getHttpClient(ExecutionContext context) {
174174
if (this.httpClient == null) {
175-
// Vertx only uses ssl options when https
176-
var options = new HttpClientOptions().setSsl(true).setTrustAll(true).setVerifyHost(false);
177-
178-
if (configuration.isUseSystemProxy()) {
179-
Configuration config = context.getComponent(Configuration.class);
180-
try {
181-
options.setProxyOptions(VertxProxyOptionsUtils.buildProxyOptions(config));
182-
} catch (IllegalStateException e) {
183-
log.warn(
184-
"CalloutHttp requires a system proxy to be defined but some configurations are missing or not well defined: {}. Ignoring proxy",
185-
e.getMessage()
186-
);
175+
synchronized (this) {
176+
if (this.httpClient == null) {
177+
// SSL options (trustAll, verifyHost) are set at client level but only applied per-request
178+
// when the URL scheme is HTTPS. Vertx's RequestOptions.setAbsoluteURI() overrides the client
179+
// SSL setting based on the actual URL scheme, so HTTP requests are not affected.
180+
var options = new HttpClientOptions().setSsl(true).setTrustAll(true).setVerifyHost(false);
181+
182+
if (configuration.isUseSystemProxy()) {
183+
Configuration config = context.getComponent(Configuration.class);
184+
try {
185+
options.setProxyOptions(VertxProxyOptionsUtils.buildProxyOptions(config));
186+
} catch (IllegalStateException e) {
187+
log.warn(
188+
"CalloutHttp requires a system proxy to be defined but some configurations are missing or not well defined: {}. Ignoring proxy",
189+
e.getMessage()
190+
);
191+
}
192+
}
193+
this.httpClient = context.getComponent(Vertx.class).createHttpClient(options);
187194
}
188195
}
189-
this.httpClient = context.getComponent(Vertx.class).createHttpClient(options);
190196
}
191197
return this.httpClient;
192198
}

0 commit comments

Comments
 (0)