@@ -461,12 +461,16 @@ public void sendPing(){
461461public void run (){
462462InputStream istream ;
463463try {
464- boolean isNewSocket = false ;
465- if (socketFactory != null ){
464+ boolean upgradeSocketToSSLSocket = false ;
465+ // Prioritise a proxy over a socket factory and apply the socketfactory later
466+ if (proxy != Proxy .NO_PROXY ){
467+ socket = new Socket (proxy );
468+ upgradeSocketToSSLSocket = true ;
469+ } else if (socketFactory != null ){
466470socket = socketFactory .createSocket ();
467471 } else if (socket == null ){
468472socket = new Socket (proxy );
469- isNewSocket = true ;
473+ upgradeSocketToSSLSocket = true ;
470474 } else if (socket .isClosed ()){
471475throw new IOException ();
472476 }
@@ -480,10 +484,17 @@ public void run(){
480484 }
481485
482486// if the socket is set by others we don't apply any TLS wrapper
483- if (isNewSocket && "wss" .equals (uri .getScheme ())){
484- SSLContext sslContext = SSLContext .getInstance ("TLSv1.2" );
485- sslContext .init (null , null , null );
486- SSLSocketFactory factory = sslContext .getSocketFactory ();
487+ if (upgradeSocketToSSLSocket && "wss" .equals (uri .getScheme ())){
488+ SSLSocketFactory factory ;
489+ // Prioritise the provided socketfactory
490+ // Helps when using web debuggers like Fiddler Classic
491+ if (socketFactory != null && (socketFactory instanceof SSLSocketFactory )){
492+ factory = (SSLSocketFactory ) socketFactory ;
493+ } else {
494+ SSLContext sslContext = SSLContext .getInstance ("TLSv1.2" );
495+ sslContext .init (null , null , null );
496+ factory = sslContext .getSocketFactory ();
497+ }
487498socket = factory .createSocket (socket , uri .getHost (), getPort (), true );
488499 }
489500
0 commit comments