@@ -47,17 +47,24 @@ public Http2Client() {
4747 .build ());
4848 }
4949
50+ public Http2Client (Options options ) {
51+ this (newClientBuilder (options )
52+ .version (Version .HTTP_2 )
53+ .build ());
54+ }
55+
5056 public Http2Client (HttpClient client ) {
5157 this .client = Util .checkNotNull (client , "HttpClient must not be null" );
5258 }
5359
5460 @ Override
5561 public Response execute (Request request , Options options ) throws IOException {
5662 final HttpRequest httpRequest = newRequestBuilder (request , options ).build ();
63+ HttpClient clientForRequest = getOrCreateClient (options );
5764
5865 HttpResponse <byte []> httpResponse ;
5966 try {
60- httpResponse = client .send (httpRequest , BodyHandlers .ofByteArray ());
67+ httpResponse = clientForRequest .send (httpRequest , BodyHandlers .ofByteArray ());
6168 } catch (final InterruptedException e ) {
6269 Thread .currentThread ().interrupt ();
6370 throw new IOException ("Invalid uri " + request .url (), e );
@@ -76,6 +83,39 @@ public Response execute(Request request, Options options) throws IOException {
7683 return response ;
7784 }
7885
86+ private HttpClient getOrCreateClient (Options options ) {
87+ if (doesClientConfigurationDiffer (options )) {
88+ // create a new client from the existing one - but with connectTimeout and followRedirect
89+ // settings from options
90+ java .net .http .HttpClient .Builder builder = newClientBuilder (options )
91+ .sslContext (client .sslContext ())
92+ .sslParameters (client .sslParameters ())
93+ .version (client .version ());
94+ client .authenticator ().ifPresent (builder ::authenticator );
95+ client .cookieHandler ().ifPresent (builder ::cookieHandler );
96+ client .executor ().ifPresent (builder ::executor );
97+ client .proxy ().ifPresent (builder ::proxy );
98+ return builder .build ();
99+ }
100+ return client ;
101+ }
102+
103+ private boolean doesClientConfigurationDiffer (Options options ) {
104+ if ((client .followRedirects () == Redirect .ALWAYS ) != options .isFollowRedirects ()) {
105+ return true ;
106+ }
107+ return client .connectTimeout ()
108+ .map (timeout -> timeout .toMillis () != options .connectTimeoutMillis ())
109+ .orElse (true );
110+ }
111+
112+ private static java .net .http .HttpClient .Builder newClientBuilder (Options options ) {
113+ return HttpClient
114+ .newBuilder ()
115+ .followRedirects (options .isFollowRedirects () ? Redirect .ALWAYS : Redirect .NEVER )
116+ .connectTimeout (Duration .ofMillis (options .connectTimeoutMillis ()));
117+ }
118+
79119 private Builder newRequestBuilder (Request request , Options options ) throws IOException {
80120 URI uri ;
81121 try {
0 commit comments