Skip to content

Commit

Permalink
[compat] Fix old Pythons broken loading of valueless cookie attributes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dirkf committed Jul 18, 2023
1 parent 1d8d5a9 commit 47214e4
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions youtube_dl/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,24 @@ def __init__(self, version, name, value, *args, **kwargs):
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
Expand Down

0 comments on commit 47214e4

Please sign in to comment.