aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordirkf <fieldhouse@gmx.net>2023-07-17 20:39:11 +0100
committerdirkf <fieldhouse@gmx.net>2023-07-18 10:50:46 +0100
commit47214e46d852e9d7ddf81d69a8e70806e2396c6c (patch)
tree35f5ea2d9104002cd27d91d814dc0f4f87db6867
parent1d8d5a93f7187438587c3a754b53fdf30322cef0 (diff)
[compat] Fix old Pythons broken loading of valueless cookie attributes
Cookie string parsing in Py 2.6.9, probably earlier, requires `=`. Also 3.2, though the CPython code appears to be OK: 3.1 was also wrong.
-rw-r--r--youtube_dl/compat.py20
1 files changed, 16 insertions, 4 deletions
diff --git a/youtube_dl/compat.py b/youtube_dl/compat.py
index cd11ba5aa..1d784d90f 100644
--- a/youtube_dl/compat.py
+++ b/youtube_dl/compat.py
@@ -126,12 +126,24 @@ except ImportError: # Python 2
import Cookie as compat_cookies
compat_http_cookies = compat_cookies
-if sys.version_info[0] == 2:
+if sys.version_info[0] == 2 or sys.version_info < (3, 3):
class compat_cookies_SimpleCookie(compat_cookies.SimpleCookie):
def load(self, rawdata):
- if isinstance(rawdata, compat_str):
- rawdata = str(rawdata)
- return super(compat_cookies_SimpleCookie, self).load(rawdata)
+ must_have_value = 0
+ if not isinstance(rawdata, dict):
+ if sys.version_info[:2] != (2, 7):
+ # attribute must have value for parsing
+ rawdata, must_have_value = re.subn(
+ r'(?i)(;\s*)(secure|httponly)(\s*(?:;|$))', r'\1\2=\2\3', rawdata)
+ if sys.version_info[0] == 2:
+ if isinstance(rawdata, compat_str):
+ rawdata = str(rawdata)
+ super(compat_cookies_SimpleCookie, self).load(rawdata)
+ if must_have_value > 0:
+ for morsel in self.values():
+ for attr in ('secure', 'httponly'):
+ if morsel.get(attr):
+ morsel[attr] = True
else:
compat_cookies_SimpleCookie = compat_cookies.SimpleCookie
compat_http_cookies_SimpleCookie = compat_cookies_SimpleCookie