Skip to content
Closed
7 changes: 4 additions & 3 deletions Doc/library/http.cookiejar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,17 @@ The following classes are provided:
from / returned to the server.


.. class:: DefaultCookiePolicy( blocked_domains=None, allowed_domains=None, netscape=True, rfc2965=False, rfc2109_as_netscape=None, hide_cookie2=False, strict_domain=False, strict_rfc2965_unverifiable=True, strict_ns_unverifiable=False, strict_ns_domain=DefaultCookiePolicy.DomainLiberal, strict_ns_set_initial_dollar=False, strict_ns_set_path=False, secure_protocols=("https", "wss") )
.. class:: DefaultCookiePolicy( blocked_domains=None, allowed_domains=None, netscape=True, rfc2965=False, rfc2109_as_netscape=None, hide_cookie2=False, strict_domain=False, strict_rfc2965_unverifiable=True, strict_ns_unverifiable=False, strict_ns_domain=DefaultCookiePolicy.DomainLiberal, strict_ns_set_initial_dollar=False, strict_ns_set_path=False, secure_protocols=("https", "wss"), additional_country_code_slds={} )

Constructor arguments should be passed as keyword arguments only.
*blocked_domains* is a sequence of domain names that we never accept cookies
from, nor return cookies to. *allowed_domains* if not :const:`None`, this is a
sequence of the only domains for which we accept and return cookies.
*secure_protocols* is a sequence of protocols for which secure cookies can be
added to. By default *https* and *wss* (secure websocket) are considered
secure protocols. For all other arguments, see the documentation for
:class:`CookiePolicy` and :class:`DefaultCookiePolicy` objects.
secure protocols. *additional_country_code_slds* is a set of user-customized
additional country code second-level domains. For all other arguments, see the
documentation for :class:`CookiePolicy` and :class:`DefaultCookiePolicy` objects.

:class:`DefaultCookiePolicy` implements the standard accept / reject rules for
Netscape and :rfc:`2965` cookies. By default, :rfc:`2109` cookies (ie. cookies
Expand Down
18 changes: 13 additions & 5 deletions Lib/http/cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,8 @@ def __init__(self,
strict_ns_domain=DomainLiberal,
strict_ns_set_initial_dollar=False,
strict_ns_set_path=False,
secure_protocols=("https", "wss")
secure_protocols=("https", "wss"),
additional_country_code_slds=set(),
):
"""Constructor arguments should be passed as keyword arguments only."""
self.netscape = netscape
Expand All @@ -907,6 +908,16 @@ def __init__(self,
self.strict_ns_set_initial_dollar = strict_ns_set_initial_dollar
self.strict_ns_set_path = strict_ns_set_path
self.secure_protocols = secure_protocols
# source: https://en.wikipedia.org/wiki/Second-level_domain
well_known_slds = set(["co", "ac", "com", "edu", "org", "net",
"gov", "mil", "int", "aero", "biz", "cat", "coop",
"info", "jobs", "mobi", "museum", "name", "pro",
"travel", "eu", "tv", "or", "nom", "sch", "web"])
if isinstance(additional_country_code_slds, set):
self.slds = well_known_slds.union(additional_country_code_slds)
_debug(f"The set of country code slds is {self.slds}")
else:
raise TypeError("attribute 'additional_country_code_slds' should be a 'set' type")

if blocked_domains is not None:
self._blocked_domains = tuple(blocked_domains)
Expand Down Expand Up @@ -1032,10 +1043,7 @@ def set_ok_domain(self, cookie, request):
if j == 0: # domain like .foo.bar
tld = domain[i+1:]
sld = domain[j+1:i]
if sld.lower() in ("co", "ac", "com", "edu", "org", "net",
"gov", "mil", "int", "aero", "biz", "cat", "coop",
"info", "jobs", "mobi", "museum", "name", "pro",
"travel", "eu") and len(tld) == 2:
if sld.lower() in self.slds and len(tld) == 2:
# domain like .co.uk
_debug(" country-code second level domain %s", domain)
return False
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:mod:`http.cookiejar`: add new attribute additional_country_code_slds in DefaultPolicy to support customization in country code second-level domains and add "tv", "or", "nom", "sch", and "web" to the default list of supported country code second-level domains.
Loading