From 3044f5c16aa4742bd71127bff5211273d638156a Mon Sep 17 00:00:00 2001 From: Mort Yao Date: Sun, 11 Jul 2021 18:34:33 +0200 Subject: [PATCH] [common] manually add cookies in get_content and post_content (due to python's lack of support for #HttpOnly_) --- src/you_get/common.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/you_get/common.py b/src/you_get/common.py index 67ef581b..597ed45a 100755 --- a/src/you_get/common.py +++ b/src/you_get/common.py @@ -433,8 +433,17 @@ def get_content(url, headers={}, decoded=True): req = request.Request(url, headers=headers) if cookies: - cookies.add_cookie_header(req) - req.headers.update(req.unredirected_hdrs) + # NOTE: Do not use cookies.add_cookie_header(req) + # #HttpOnly_ cookies were not supported by CookieJar and MozillaCookieJar properly until python 3.10 + # See also: + # - https://github.com/python/cpython/pull/17471 + # - https://bugs.python.org/issue2190 + # Here we add cookies to the request headers manually + cookie_strings = [] + for cookie in list(cookies): + cookie_strings.append(cookie.name + '=' + cookie.value) + cookie_headers = {'Cookie': '; '.join(cookie_strings)} + req.headers.update(cookie_headers) response = urlopen_with_retry(req) data = response.read() @@ -477,8 +486,17 @@ def post_content(url, headers={}, post_data={}, decoded=True, **kwargs): req = request.Request(url, headers=headers) if cookies: - cookies.add_cookie_header(req) - req.headers.update(req.unredirected_hdrs) + # NOTE: Do not use cookies.add_cookie_header(req) + # #HttpOnly_ cookies were not supported by CookieJar and MozillaCookieJar properly until python 3.10 + # See also: + # - https://github.com/python/cpython/pull/17471 + # - https://bugs.python.org/issue2190 + # Here we add cookies to the request headers manually + cookie_strings = [] + for cookie in list(cookies): + cookie_strings.append(cookie.name + '=' + cookie.value) + cookie_headers = {'Cookie': '; '.join(cookie_strings)} + req.headers.update(cookie_headers) if kwargs.get('post_data_raw'): post_data_enc = bytes(kwargs['post_data_raw'], 'utf-8') else: