Browse Source

[utils] Add support for cookies with spaces used instead of tabs

master
Sergey M․ 5 years ago
parent
commit
cff99c91d1
No known key found for this signature in database GPG Key ID: 2C393E0F18A9236D
3 changed files with 19 additions and 5 deletions
  1. +9
    -5
      test/test_YoutubeDLCookieJar.py
  2. +5
    -0
      test/testdata/cookies/cookie_file_with_spaces.txt
  3. +5
    -0
      youtube_dl/utils.py

+ 9
- 5
test/test_YoutubeDLCookieJar.py View File

@ -14,6 +14,9 @@ from youtube_dl.utils import YoutubeDLCookieJar
class TestYoutubeDLCookieJar(unittest.TestCase): class TestYoutubeDLCookieJar(unittest.TestCase):
def __assert_cookie_has_value(self, cookiejar, key):
self.assertEqual(cookiejar._cookies['www.foobar.foobar']['/'][key].value, key + '_VALUE')
def test_keep_session_cookies(self): def test_keep_session_cookies(self):
cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/session_cookies.txt') cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/session_cookies.txt')
cookiejar.load(ignore_discard=True, ignore_expires=True) cookiejar.load(ignore_discard=True, ignore_expires=True)
@ -32,12 +35,13 @@ class TestYoutubeDLCookieJar(unittest.TestCase):
def test_strip_httponly_prefix(self): def test_strip_httponly_prefix(self):
cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/httponly_cookies.txt') cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/httponly_cookies.txt')
cookiejar.load(ignore_discard=True, ignore_expires=True) cookiejar.load(ignore_discard=True, ignore_expires=True)
self.__assert_cookie_has_value(cookiejar, 'HTTPONLY_COOKIE')
self.__assert_cookie_has_value(cookiejar, 'JS_ACCESSIBLE_COOKIE')
def assert_cookie_has_value(key):
self.assertEqual(cookiejar._cookies['www.foobar.foobar']['/'][key].value, key + '_VALUE')
assert_cookie_has_value('HTTPONLY_COOKIE')
assert_cookie_has_value('JS_ACCESSIBLE_COOKIE')
def test_convert_spaces_to_tabs(self):
cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/cookie_file_with_spaces.txt')
cookiejar.load(ignore_discard=True, ignore_expires=True)
self.__assert_cookie_has_value(cookiejar, 'COOKIE')
if __name__ == '__main__': if __name__ == '__main__':


+ 5
- 0
test/testdata/cookies/cookie_file_with_spaces.txt View File

@ -0,0 +1,5 @@
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This is a generated file! Do not edit.
www.foobar.foobar FALSE / TRUE 2147483647 COOKIE COOKIE_VALUE

+ 5
- 0
youtube_dl/utils.py View File

@ -2752,6 +2752,11 @@ class YoutubeDLCookieJar(compat_cookiejar.MozillaCookieJar):
for line in f: for line in f:
if line.startswith(self._HTTPONLY_PREFIX): if line.startswith(self._HTTPONLY_PREFIX):
line = line[len(self._HTTPONLY_PREFIX):] line = line[len(self._HTTPONLY_PREFIX):]
# Cookie file may contain spaces instead of tabs.
# Replace all spaces with tabs to make such cookie files work
# with MozillaCookieJar.
if not line.startswith('#'):
line = re.sub(r' +', r'\t', line)
cf.write(compat_str(line)) cf.write(compat_str(line))
cf.seek(0) cf.seek(0)
self._really_load(cf, filename, ignore_discard, ignore_expires) self._really_load(cf, filename, ignore_discard, ignore_expires)


Loading…
Cancel
Save