3232import java .util .concurrent .Future ;
3333import java .util .concurrent .TimeUnit ;
3434
35+ import javax .net .ssl .SSLEngine ;
36+ import javax .net .ssl .SSLParameters ;
37+
3538import javax .ws .rs .ProcessingException ;
3639import javax .ws .rs .client .Client ;
3740import javax .ws .rs .core .Configuration ;
6063import io .netty .handler .proxy .HttpProxyHandler ;
6164import io .netty .handler .ssl .ClientAuth ;
6265import io .netty .handler .ssl .JdkSslContext ;
66+ import io .netty .handler .ssl .SslHandler ;
6367import io .netty .handler .stream .ChunkedWriteHandler ;
6468import io .netty .handler .timeout .IdleState ;
6569import io .netty .handler .timeout .IdleStateEvent ;
@@ -216,15 +220,9 @@ protected CompletableFuture<ClientResponse> execute(final ClientRequest jerseyRe
216220 protected void initChannel (SocketChannel ch ) throws Exception {
217221 ChannelPipeline p = ch .pipeline ();
218222
219- // Enable HTTPS if necessary.
220- if ("https" .equals (requestUri .getScheme ())) {
221- // making client authentication optional for now; it could be extracted to configurable property
222- JdkSslContext jdkSslContext = new JdkSslContext (client .getSslContext (), true , ClientAuth .NONE );
223- p .addLast (jdkSslContext .newHandler (ch .alloc ()));
224- }
223+ Configuration config = jerseyRequest .getConfiguration ();
225224
226225 // http proxy
227- Configuration config = jerseyRequest .getConfiguration ();
228226 final Object proxyUri = config .getProperties ().get (ClientProperties .PROXY_URI );
229227 if (proxyUri != null ) {
230228 final URI u = getProxyUri (proxyUri );
@@ -234,9 +232,28 @@ protected void initChannel(SocketChannel ch) throws Exception {
234232 final String password = ClientProperties .getValue (
235233 config .getProperties (), ClientProperties .PROXY_PASSWORD , String .class );
236234
237- p .addLast (new HttpProxyHandler (new InetSocketAddress (u .getHost (),
238- u .getPort () == -1 ? 8080 : u .getPort ()),
239- userName , password ));
235+ InetSocketAddress proxyAddr = new InetSocketAddress (u .getHost (),
236+ u .getPort () == -1 ? 8080 : u .getPort ());
237+ p .addLast (userName == null ? new HttpProxyHandler (proxyAddr )
238+ : new HttpProxyHandler (proxyAddr , userName , password ));
239+ }
240+
241+ // Enable HTTPS if necessary.
242+ if ("https" .equals (requestUri .getScheme ())) {
243+ // making client authentication optional for now; it could be extracted to configurable property
244+ JdkSslContext jdkSslContext = new JdkSslContext (client .getSslContext (), true , ClientAuth .NONE );
245+ int port = requestUri .getPort ();
246+ SslHandler sslHandler = jdkSslContext .newHandler (ch .alloc (), requestUri .getHost (),
247+ port <= 0 ? 443 : port , executorService );
248+ if (ClientProperties .getValue (config .getProperties (),
249+ NettyClientProperties .ENABLE_SSL_HOSTNAME_VERIFICATION , true )) {
250+ SSLEngine sslEngine = sslHandler .engine ();
251+ SSLParameters sslParameters = sslEngine .getSSLParameters ();
252+ sslParameters .setEndpointIdentificationAlgorithm ("HTTPS" );
253+ sslEngine .setSSLParameters (sslParameters );
254+ }
255+
256+ p .addLast (sslHandler );
240257 }
241258
242259 p .addLast (new HttpClientCodec ());
0 commit comments