Skip to content

Commit 94b3c46

Browse files
committed
Add case_insensitive option to Cookie.has_nonstandard_attr method
Cookie.has_nonstandard_attr method is case-sensitive, but HTTP is case-insensitive for cookie attribute names. This option is useful for MozillaCookieJar to check the HttpOnly flag when saving.
1 parent f19b93f commit 94b3c46

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

Doc/library/http.cookiejar.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,9 +711,10 @@ Cookies may have additional non-standard cookie-attributes. These may be
711711
accessed using the following methods:
712712

713713

714-
.. method:: Cookie.has_nonstandard_attr(name)
714+
.. method:: Cookie.has_nonstandard_attr(name, case_insensitive=False)
715715

716-
Return ``True`` if cookie has the named cookie-attribute.
716+
Return ``True`` if cookie has the named cookie-attribute. If *case_insensitive*
717+
is true, the name is compared without regard to case.
717718

718719

719720
.. method:: Cookie.get_nonstandard_attr(name, default=None)

Lib/http/cookiejar.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,10 @@ def __init__(self, version, name, value,
800800

801801
self._rest = copy.copy(rest)
802802

803-
def has_nonstandard_attr(self, name):
803+
def has_nonstandard_attr(self, name, case_insensitive=False):
804+
if case_insensitive:
805+
name = name.lower()
806+
return any(k.lower() == name for k in self._rest)
804807
return name in self._rest
805808
def get_nonstandard_attr(self, name, default=None):
806809
return self._rest.get(name, default)

Lib/test/test_http_cookiejar.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ def test_ns_parser(self):
612612
# case is preserved
613613
self.assertTrue(cookie.has_nonstandard_attr("blArgh"))
614614
self.assertFalse(cookie.has_nonstandard_attr("blargh"))
615+
self.assertTrue(cookie.has_nonstandard_attr("blargh", case_insensitive=True))
615616

616617
cookie = c._cookies["www.acme.com"]["/"]["ni"]
617618
self.assertEqual(cookie.domain, "www.acme.com")

0 commit comments

Comments
 (0)