summary refs log tree commit diff
diff options
context:
space:
mode:
authorSergey M․ <dstftw@gmail.com>2015-09-06 06:20:48 +0600
committerSergey M․ <dstftw@gmail.com>2015-09-06 06:20:48 +0600
commita6420bf50cc60aa9c8e37bfb2acb10eca361a4f0 (patch)
tree51de9b490b4f2abcd9caed6d548e77895b9f947d
parenteb387896e90d3c0b9043169769783796c564f4a0 (diff)
downloadyoutube-dl-a6420bf50cc60aa9c8e37bfb2acb10eca361a4f0.tar.gz
youtube-dl-a6420bf50cc60aa9c8e37bfb2acb10eca361a4f0.tar.xz
youtube-dl-a6420bf50cc60aa9c8e37bfb2acb10eca361a4f0.zip
[utils] Add cookie processor for cookie correction (Closes #6769)
-rw-r--r--youtube_dl/utils.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py
index 79381b380..9bc292a39 100644
--- a/youtube_dl/utils.py
+++ b/youtube_dl/utils.py
@@ -782,6 +782,32 @@ class YoutubeDLHTTPSHandler(compat_urllib_request.HTTPSHandler):
             req, **kwargs)
 
 
+class YoutubeDLCookieProcessor(compat_urllib_request.HTTPCookieProcessor):
+    def __init__(self, cookiejar=None):
+        compat_urllib_request.HTTPCookieProcessor.__init__(self, cookiejar)
+
+    def http_response(self, request, response):
+        # Python 2 will choke on next HTTP request in row if there are non-ASCII
+        # characters in Set-Cookie HTTP header of last response (see
+        # https://github.com/rg3/youtube-dl/issues/6769).
+        # In order to at least prevent crashing we will percent encode Set-Cookie
+        # header before HTTPCookieProcessor starts processing it.
+        if sys.version_info < (3, 0) and response.headers:
+            for set_cookie_header in ('Set-Cookie', 'Set-Cookie2'):
+                set_cookie = response.headers.get(set_cookie_header)
+                if set_cookie:
+                    set_cookie_escaped = '; '.join([
+                        escape_rfc3986(cookie_attr.strip())
+                        for cookie_attr in set_cookie.decode('iso-8859-1').split(';')]).encode('iso-8859-1')
+                    if set_cookie != set_cookie_escaped:
+                        del response.headers[set_cookie_header]
+                        response.headers[set_cookie_header] = set_cookie_escaped
+        return compat_urllib_request.HTTPCookieProcessor.http_response(self, request, response)
+
+    https_request = compat_urllib_request.HTTPCookieProcessor.http_request
+    https_response = http_response
+
+
 def parse_iso8601(date_str, delimiter='T', timezone=None):
     """ Return a UNIX timestamp from the given date """