@@ -558,21 +558,27 @@ def handle_redirect(self, uri: str) -> None:
558
558
raise SecurityError ("redirect from WSS to WS" )
559
559
560
560
same_origin = (
561
- old_wsuri .host == new_wsuri .host and old_wsuri .port == new_wsuri .port
561
+ old_wsuri .secure == new_wsuri .secure
562
+ and old_wsuri .host == new_wsuri .host
563
+ and old_wsuri .port == new_wsuri .port
562
564
)
563
565
564
- # Rewrite the host and port arguments for cross-origin redirects.
566
+ # Rewrite secure, host, and port for cross-origin redirects.
565
567
# This preserves connection overrides with the host and port
566
568
# arguments if the redirect points to the same host and port.
567
569
if not same_origin :
568
- # Replace the host and port argument passed to the protocol factory.
569
570
factory = self ._create_connection .args [0 ]
571
+ # Support TLS upgrade.
572
+ if not old_wsuri .secure and new_wsuri .secure :
573
+ factory .keywords ["secure" ] = True
574
+ self ._create_connection .keywords .setdefault ("ssl" , True )
575
+ # Replace secure, host, and port arguments of the protocol factory.
570
576
factory = functools .partial (
571
577
factory .func ,
572
578
* factory .args ,
573
579
** dict (factory .keywords , host = new_wsuri .host , port = new_wsuri .port ),
574
580
)
575
- # Replace the host and port argument passed to create_connection.
581
+ # Replace secure, host, and port arguments of create_connection.
576
582
self ._create_connection = functools .partial (
577
583
self ._create_connection .func ,
578
584
* (factory , new_wsuri .host , new_wsuri .port ),
0 commit comments