From 990b1fac97413f97da6fbb314af66858eea7f3ef Mon Sep 17 00:00:00 2001 From: a5320 Date: Wed, 31 Jan 2024 22:23:16 +0800 Subject: [PATCH 1/4] add cookie.py Cookie to dict --- curl_cffi/requests/cookies.py | 55 ++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/curl_cffi/requests/cookies.py b/curl_cffi/requests/cookies.py index 5559a173..0aacd11c 100644 --- a/curl_cffi/requests/cookies.py +++ b/curl_cffi/requests/cookies.py @@ -8,7 +8,6 @@ import time import typing from http.cookiejar import Cookie, CookieJar -from http.cookies import _unquote from urllib.parse import urlparse from dataclasses import dataclass import warnings @@ -64,7 +63,7 @@ def from_curl_format(cls, set_cookie_line: bytes): secure=cls.parse_bool(secure), expires=int(expires), name=name, - value=_unquote(value), + value=value, http_only=http_only, ) @@ -190,7 +189,7 @@ def update_cookies_from_curl(self, morsels: typing.List[CurlMorsel]): self.jar.clear_expired_cookies() def set( - self, name: str, value: str, domain: str = "", path: str = "/", secure=False + self, name: str, value: str, domain: str = "", path: str = "/", secure=False ) -> None: """ Set a cookie value by name. May optionally include domain and path. @@ -230,11 +229,11 @@ def set( self.jar.set_cookie(cookie) def get( # type: ignore - self, - name: str, - default: typing.Optional[str] = None, - domain: typing.Optional[str] = None, - path: typing.Optional[str] = None, + self, + name: str, + default: typing.Optional[str] = None, + domain: typing.Optional[str] = None, + path: typing.Optional[str] = None, ) -> typing.Optional[str]: """ Get a cookie by name. May optionally include domain and path @@ -248,15 +247,14 @@ def get( # type: ignore if path is None or cookie.path == path: # if cookies on two different domains do not share a same value if ( - value is not None - and not matched_domain.endswith(cookie.domain) - and not str(cookie.domain).endswith(matched_domain) - and value != cookie.value + value is not None + and not matched_domain.endswith(cookie.domain) + and not str(cookie.domain).endswith(matched_domain) + and value != cookie.value ): message = ( f"Multiple cookies exist with name={name} on " - f"{matched_domain} and {cookie.domain}, add domain " - "parameter to suppress this error." + f"{matched_domain} and {cookie.domain}" ) raise CookieConflict(message) value = cookie.value @@ -267,10 +265,10 @@ def get( # type: ignore return value def delete( - self, - name: str, - domain: typing.Optional[str] = None, - path: typing.Optional[str] = None, + self, + name: str, + domain: typing.Optional[str] = None, + path: typing.Optional[str] = None, ) -> None: """ Delete a cookie by name. May optionally include domain and path @@ -283,15 +281,30 @@ def delete( cookie for cookie in self.jar if cookie.name == name - and (domain is None or cookie.domain == domain) - and (path is None or cookie.path == path) + and (domain is None or cookie.domain == domain) + and (path is None or cookie.path == path) ] for cookie in remove: self.jar.clear(cookie.domain, cookie.path, cookie.name) + def get_dict(self, domain=None, path=None): + """Takes as an argument an optional domain and path and returns a plain + old Python dict of name-value pairs of cookies that meet the + requirements. + + :rtype: dict + """ + dictionary = {} + for cookie in self.jar: + if (domain is None or cookie.domain == domain) and ( + path is None or cookie.path == path + ): + dictionary[cookie.name] = cookie.value + return dictionary + def clear( - self, domain: typing.Optional[str] = None, path: typing.Optional[str] = None + self, domain: typing.Optional[str] = None, path: typing.Optional[str] = None ) -> None: """ Delete all cookies. Optionally include a domain and path in From 9993716a50fb4fedfd5c33a76f7dbeab0cbe2882 Mon Sep 17 00:00:00 2001 From: a5320 Date: Wed, 31 Jan 2024 22:37:58 +0800 Subject: [PATCH 2/4] add cookie.py Cookie to dict --- curl_cffi/requests/cookies.py | 40 ++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/curl_cffi/requests/cookies.py b/curl_cffi/requests/cookies.py index 0aacd11c..8db7dfc5 100644 --- a/curl_cffi/requests/cookies.py +++ b/curl_cffi/requests/cookies.py @@ -8,6 +8,7 @@ import time import typing from http.cookiejar import Cookie, CookieJar +from http.cookies import _unquote from urllib.parse import urlparse from dataclasses import dataclass import warnings @@ -63,7 +64,7 @@ def from_curl_format(cls, set_cookie_line: bytes): secure=cls.parse_bool(secure), expires=int(expires), name=name, - value=value, + value=_unquote(value), http_only=http_only, ) @@ -189,7 +190,7 @@ def update_cookies_from_curl(self, morsels: typing.List[CurlMorsel]): self.jar.clear_expired_cookies() def set( - self, name: str, value: str, domain: str = "", path: str = "/", secure=False + self, name: str, value: str, domain: str = "", path: str = "/", secure=False ) -> None: """ Set a cookie value by name. May optionally include domain and path. @@ -229,11 +230,11 @@ def set( self.jar.set_cookie(cookie) def get( # type: ignore - self, - name: str, - default: typing.Optional[str] = None, - domain: typing.Optional[str] = None, - path: typing.Optional[str] = None, + self, + name: str, + default: typing.Optional[str] = None, + domain: typing.Optional[str] = None, + path: typing.Optional[str] = None, ) -> typing.Optional[str]: """ Get a cookie by name. May optionally include domain and path @@ -247,14 +248,15 @@ def get( # type: ignore if path is None or cookie.path == path: # if cookies on two different domains do not share a same value if ( - value is not None - and not matched_domain.endswith(cookie.domain) - and not str(cookie.domain).endswith(matched_domain) - and value != cookie.value + value is not None + and not matched_domain.endswith(cookie.domain) + and not str(cookie.domain).endswith(matched_domain) + and value != cookie.value ): message = ( f"Multiple cookies exist with name={name} on " - f"{matched_domain} and {cookie.domain}" + f"{matched_domain} and {cookie.domain}, add domain " + "parameter to suppress this error." ) raise CookieConflict(message) value = cookie.value @@ -265,10 +267,10 @@ def get( # type: ignore return value def delete( - self, - name: str, - domain: typing.Optional[str] = None, - path: typing.Optional[str] = None, + self, + name: str, + domain: typing.Optional[str] = None, + path: typing.Optional[str] = None, ) -> None: """ Delete a cookie by name. May optionally include domain and path @@ -281,8 +283,8 @@ def delete( cookie for cookie in self.jar if cookie.name == name - and (domain is None or cookie.domain == domain) - and (path is None or cookie.path == path) + and (domain is None or cookie.domain == domain) + and (path is None or cookie.path == path) ] for cookie in remove: @@ -304,7 +306,7 @@ def get_dict(self, domain=None, path=None): return dictionary def clear( - self, domain: typing.Optional[str] = None, path: typing.Optional[str] = None + self, domain: typing.Optional[str] = None, path: typing.Optional[str] = None ) -> None: """ Delete all cookies. Optionally include a domain and path in From 44e3091c3d00373c1f1519782c094d3d9cb81fb8 Mon Sep 17 00:00:00 2001 From: Ausli <70201547+Ausli@users.noreply.github.com> Date: Thu, 1 Feb 2024 22:51:49 +0800 Subject: [PATCH 3/4] Update curl_cffi/requests/cookies.py Co-authored-by: Yifei Kong --- curl_cffi/requests/cookies.py | 1 - 1 file changed, 1 deletion(-) diff --git a/curl_cffi/requests/cookies.py b/curl_cffi/requests/cookies.py index 8db7dfc5..6ae09caa 100644 --- a/curl_cffi/requests/cookies.py +++ b/curl_cffi/requests/cookies.py @@ -295,7 +295,6 @@ def get_dict(self, domain=None, path=None): old Python dict of name-value pairs of cookies that meet the requirements. - :rtype: dict """ dictionary = {} for cookie in self.jar: From 16fa0759f83ba042bb2088b73f9966a54f4d6b86 Mon Sep 17 00:00:00 2001 From: Ausli <70201547+Ausli@users.noreply.github.com> Date: Thu, 1 Feb 2024 22:52:07 +0800 Subject: [PATCH 4/4] Update curl_cffi/requests/cookies.py Co-authored-by: Yifei Kong --- curl_cffi/requests/cookies.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/curl_cffi/requests/cookies.py b/curl_cffi/requests/cookies.py index 6ae09caa..5c6842e9 100644 --- a/curl_cffi/requests/cookies.py +++ b/curl_cffi/requests/cookies.py @@ -290,7 +290,7 @@ def delete( for cookie in remove: self.jar.clear(cookie.domain, cookie.path, cookie.name) - def get_dict(self, domain=None, path=None): + def get_dict(self, domain: Optional[str] = None, path: Optional[str] = None) -> dict: """Takes as an argument an optional domain and path and returns a plain old Python dict of name-value pairs of cookies that meet the requirements.