about summary refs log tree commit diff
diff options
context:
space:
mode:
authordirkf <fieldhouse@gmx.net>2022-05-28 13:52:51 +0100
committerdirkf <fieldhouse@gmx.net>2022-05-28 13:52:51 +0100
commit52c3751df722ab6f31f0229a415c7389a95c2307 (patch)
tree5e5e9a9e89fc48d447714101d7214e5dd3a2145a
parent187a48aee29847664e0c4cd80fe90c32e1fb334b (diff)
downloadyoutube-dl-52c3751df722ab6f31f0229a415c7389a95c2307.tar.gz
youtube-dl-52c3751df722ab6f31f0229a415c7389a95c2307.tar.xz
youtube-dl-52c3751df722ab6f31f0229a415c7389a95c2307.zip
[utils] Enable ALPN in HTTPS to satisfy broken servers
See https://github.com/yt-dlp/yt-dlp/issues/3878
-rw-r--r--youtube_dl/utils.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py
index e722eed58..4ff27db3d 100644
--- a/youtube_dl/utils.py
+++ b/youtube_dl/utils.py
@@ -2292,12 +2292,30 @@ def formatSeconds(secs):
 
 
 def make_HTTPS_handler(params, **kwargs):
+
+    # https://www.rfc-editor.org/info/rfc7301
+    ALPN_PROTOCOLS = ['http/1.1']
+
+    def set_alpn_protocols(ctx):
+        # From https://github.com/yt-dlp/yt-dlp/commit/2c6dcb65fb612fc5bc5c61937bf438d3c473d8d0
+        # Thanks @coletdjnz
+        # Some servers may (wrongly) reject requests if ALPN extension is not sent. See:
+        # https://github.com/python/cpython/issues/85140
+        # https://github.com/yt-dlp/yt-dlp/issues/3878
+        try:
+            ctx.set_alpn_protocols(ALPN_PROTOCOLS)
+        except (AttributeError, NotImplementedError):
+            # Python < 2.7.10, not ssl.HAS_ALPN
+            pass
+
     opts_no_check_certificate = params.get('nocheckcertificate', False)
     if hasattr(ssl, 'create_default_context'):  # Python >= 3.4 or 2.7.9
         context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
+        set_alpn_protocols(context)
         if opts_no_check_certificate:
             context.check_hostname = False
             context.verify_mode = ssl.CERT_NONE
+
         try:
             return YoutubeDLHTTPSHandler(params, context=context, **kwargs)
         except TypeError:
@@ -2313,6 +2331,7 @@ def make_HTTPS_handler(params, **kwargs):
                                if opts_no_check_certificate
                                else ssl.CERT_REQUIRED)
         context.set_default_verify_paths()
+        set_alpn_protocols(context)
         return YoutubeDLHTTPSHandler(params, context=context, **kwargs)