Skip to content
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ dependencies {
implementation 'com.kjetland:mbknor-jackson-jsonschema_2.13:1.0.39'
implementation 'com.jayway.jsonpath:json-path:2.9.0'
implementation 'org.apache.httpcomponents.client5:httpclient5:5.5'
implementation 'io.projectreactor.netty:reactor-netty-http:1.2.8'
implementation 'io.projectreactor.netty:reactor-netty-http:1.2.9'
implementation 'org.apache.maven:maven-artifact:3.9.11'
implementation 'commons-codec:commons-codec:1.19.0'
// for RFC5987 parsing of content-disposition filename*
Expand Down
54 changes: 35 additions & 19 deletions src/main/java/me/itzg/helpers/http/SharedFetch.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,40 @@ public SharedFetch(String forCommand, Options options) {
.pendingAcquireTimeout(options.getPendingAcquireTimeout())
.build();

reactiveClient = HttpClient.create(connectionProvider)
.proxyWithSystemProperties()
.headers(headers -> {
headers
.set(HttpHeaderNames.USER_AGENT.toString(), userAgent)
.set("x-fetch-session", fetchSessionId);
if (options.getExtraHeaders() != null) {
options.getExtraHeaders().forEach(headers::set);
}
}
)
// Reference https://projectreactor.io/docs/netty/release/reference/index.html#response-timeout
.responseTimeout(options.getResponseTimeout());
reactiveClient =
applyWiretap(
applyUseHttp2(
HttpClient.create(connectionProvider)
.proxyWithSystemProperties()
.headers(headers -> {
headers
.set(HttpHeaderNames.USER_AGENT.toString(), userAgent)
.set("x-fetch-session", fetchSessionId);
if (options.getExtraHeaders() != null) {
options.getExtraHeaders().forEach(headers::set);
}
}
)
// Reference https://projectreactor.io/docs/netty/release/reference/index.html#response-timeout
.responseTimeout(options.getResponseTimeout()),
options
),
options
);

headers.put("x-fetch-session", fetchSessionId);

this.filesViaUrl = options.getFilesViaUrl();
}

private HttpClient applyWiretap(HttpClient c, Options options) {
return options.isWiretap() ? c.wiretap(true) : c;
}

private HttpClient applyUseHttp2(HttpClient c, Options options) {
if (options.isUseHttp2()) {
log.debug("Using HTTP/2");
reactiveClient
return c
// https://projectreactor.io/docs/netty/release/reference/http-client.html#HTTP2
.protocol(HttpProtocol.HTTP11, HttpProtocol.H2)
.secure(spec ->
Expand All @@ -87,17 +104,14 @@ public SharedFetch(String forCommand, Options options) {
}
else {
log.debug("Using HTTP/1.1");
reactiveClient
return c
.secure(spec ->
spec.sslContext((GenericSslContextSpec<?>) Http11SslContextSpec.forClient())
// Reference https://projectreactor.io/docs/netty/release/reference/index.html#ssl-tls-timeout
.handshakeTimeout(options.getTlsHandshakeTimeout())
);
}

headers.put("x-fetch-session", fetchSessionId);

this.filesViaUrl = options.getFilesViaUrl();
}

public FetchBuilderBase<?> fetch(URI uri) {
Expand Down Expand Up @@ -151,14 +165,16 @@ public static class Options {
@Default
private final boolean useHttp2 = true;

private final boolean wiretap;

public Options withHeader(String key, String value) {
final Map<String, String> newHeaders = extraHeaders != null ?
new HashMap<>(extraHeaders) : new HashMap<>();
newHeaders.put(key, value);

return new Options(
responseTimeout, tlsHandshakeTimeout, maxIdleTimeout, pendingAcquireTimeout,
newHeaders, filesViaUrl, useHttp2
newHeaders, filesViaUrl, useHttp2, wiretap
);
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/me/itzg/helpers/http/SharedFetchArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ public void setUseHttp2(boolean useHttp2) {
optionsBuilder.useHttp2(useHttp2);
}

@Option(names = "--wiretap", defaultValue = "${env:FETCH_WIRETAP:-false}",
description = "Whether to enable Reactor Netty wiretap logging. Default: ${DEFAULT-VALUE}"
)
public void setWiretap(boolean wiretap) {
optionsBuilder.wiretap(wiretap);
}

public Options options() {
return optionsBuilder.build();
}
Expand Down