@@ -44,17 +44,22 @@ public Http2Client() {
4444 this (HttpClient .newBuilder ().followRedirects (Redirect .ALWAYS ).version (Version .HTTP_2 ).build ());
4545 }
4646
47+ public Http2Client (Options options ) {
48+ this (newClientBuilder (options ).version (Version .HTTP_2 ).build ());
49+ }
50+
4751 public Http2Client (HttpClient client ) {
4852 this .client = Util .checkNotNull (client , "HttpClient must not be null" );
4953 }
5054
5155 @ Override
5256 public Response execute (Request request , Options options ) throws IOException {
5357 final HttpRequest httpRequest = newRequestBuilder (request , options ).build ();
58+ HttpClient clientForRequest = getOrCreateClient (options );
5459
5560 HttpResponse <byte []> httpResponse ;
5661 try {
57- httpResponse = client .send (httpRequest , BodyHandlers .ofByteArray ());
62+ httpResponse = clientForRequest .send (httpRequest , BodyHandlers .ofByteArray ());
5863 } catch (final InterruptedException e ) {
5964 Thread .currentThread ().interrupt ();
6065 throw new IOException ("Invalid uri " + request .url (), e );
@@ -75,6 +80,40 @@ public Response execute(Request request, Options options) throws IOException {
7580 return response ;
7681 }
7782
83+ private HttpClient getOrCreateClient (Options options ) {
84+ if (doesClientConfigurationDiffer (options )) {
85+ // create a new client from the existing one - but with connectTimeout and followRedirect
86+ // settings from options
87+ java .net .http .HttpClient .Builder builder =
88+ newClientBuilder (options )
89+ .sslContext (client .sslContext ())
90+ .sslParameters (client .sslParameters ())
91+ .version (client .version ());
92+ client .authenticator ().ifPresent (builder ::authenticator );
93+ client .cookieHandler ().ifPresent (builder ::cookieHandler );
94+ client .executor ().ifPresent (builder ::executor );
95+ client .proxy ().ifPresent (builder ::proxy );
96+ return builder .build ();
97+ }
98+ return client ;
99+ }
100+
101+ private boolean doesClientConfigurationDiffer (Options options ) {
102+ if ((client .followRedirects () == Redirect .ALWAYS ) != options .isFollowRedirects ()) {
103+ return true ;
104+ }
105+ return client
106+ .connectTimeout ()
107+ .map (timeout -> timeout .toMillis () != options .connectTimeoutMillis ())
108+ .orElse (true );
109+ }
110+
111+ private static java .net .http .HttpClient .Builder newClientBuilder (Options options ) {
112+ return HttpClient .newBuilder ()
113+ .followRedirects (options .isFollowRedirects () ? Redirect .ALWAYS : Redirect .NEVER )
114+ .connectTimeout (Duration .ofMillis (options .connectTimeoutMillis ()));
115+ }
116+
78117 private Builder newRequestBuilder (Request request , Options options ) throws IOException {
79118 URI uri ;
80119 try {
0 commit comments