@@ -461,12 +461,16 @@ public void sendPing() {
461
461
public void run () {
462
462
InputStream istream ;
463
463
try {
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 ) {
466
470
socket = socketFactory .createSocket ();
467
471
} else if (socket == null ) {
468
472
socket = new Socket (proxy );
469
- isNewSocket = true ;
473
+ upgradeSocketToSSLSocket = true ;
470
474
} else if (socket .isClosed ()) {
471
475
throw new IOException ();
472
476
}
@@ -480,10 +484,17 @@ public void run() {
480
484
}
481
485
482
486
// 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
+ }
487
498
socket = factory .createSocket (socket , uri .getHost (), getPort (), true );
488
499
}
489
500
0 commit comments