Skip to content

Commit ad24492

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 f3bf304 commit ad24492

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
@@ -716,9 +716,10 @@ Cookies may have additional non-standard cookie-attributes. These may be
716716
accessed using the following methods:
717717

718718

719-
.. method:: Cookie.has_nonstandard_attr(name)
719+
.. method:: Cookie.has_nonstandard_attr(name, case_insensitive=False)
720720

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

723724

724725
.. 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
@@ -623,6 +623,7 @@ def test_ns_parser(self):
623623
# case is preserved
624624
self.assertTrue(cookie.has_nonstandard_attr("blArgh"))
625625
self.assertFalse(cookie.has_nonstandard_attr("blargh"))
626+
self.assertTrue(cookie.has_nonstandard_attr("blargh", case_insensitive=True))
626627

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

0 commit comments

Comments
 (0)