Skip to content

Commit 876f7a4

Browse files
committed
gh-153648: Deprecate http.client.HTTPMessage.getallmatchingheaders()
getallmatchingheaders() has returned an empty list for every input since Python 3.0 -- it compares keys() entries, which are bare header names, against "name:". Its only user was the CGI handler, removed in 3.15, and run_cgi() had already stopped calling it in 3.10. Deprecate it, with removal in 3.18. Use email.message.Message.get_all() instead.
1 parent adebb68 commit 876f7a4

5 files changed

Lines changed: 33 additions & 2 deletions

File tree

Doc/deprecations/pending-removal-in-3.18.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ Pending removal in Python 3.18
1616
* ``import`` lines in :file:`{name}.pth` files are silently ignored.
1717

1818
(Contributed by Barry Warsaw in :gh:`148641`.)
19+
20+
* :mod:`http.client`:
21+
22+
* :meth:`!http.client.HTTPMessage.getallmatchingheaders` has been deprecated
23+
since Python 3.16. It has returned an empty list for every input since
24+
Python 3.0; use :meth:`email.message.Message.get_all` instead.
25+
(Contributed by Julian Soreavis in :gh:`153648`.)

Doc/whatsnew/3.16.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,15 @@ New deprecations
657657
3.9, now issues a deprecation warning on use. This property is slated for
658658
removal in 3.21. Use ``ast.Tuple.elts`` instead.
659659

660+
* :mod:`http.client`:
661+
662+
* :meth:`!http.client.HTTPMessage.getallmatchingheaders` is now deprecated
663+
and will be removed in Python 3.18. It has returned an empty list for
664+
every input since Python 3.0, and its only user, the CGI handler, was
665+
removed in Python 3.15. Use :meth:`email.message.Message.get_all`
666+
instead.
667+
(Contributed by Julian Soreavis in :gh:`153648`.)
668+
660669
* :mod:`struct`:
661670

662671
* Soft-deprecated since Python 3.15, using ``'F'`` and ``'D'`` type codes are now

Lib/http/client.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ def _strip_ipv6_iface(enc_name: bytes) -> bytes:
195195
class HTTPMessage(email.message.Message):
196196

197197
# The getallmatchingheaders() method was only used by the CGI handler
198-
# that was removed in Python 3.15. However, since the public API was not
199-
# properly defined, it will be kept for backwards compatibility reasons.
198+
# that was removed in Python 3.15. It has returned an empty list for every
199+
# input since Python 3.0.
200200

201201
def getallmatchingheaders(self, name):
202202
"""Find all header lines matching a given header name.
@@ -208,6 +208,11 @@ def getallmatchingheaders(self, name):
208208
occurrences are returned. Case is not important in the header name.
209209
210210
"""
211+
import warnings
212+
warnings._deprecated(
213+
"http.client.HTTPMessage.getallmatchingheaders",
214+
remove=(3, 18),
215+
)
211216
name = name.lower() + ':'
212217
n = len(name)
213218
lst = []

Lib/test/test_httplib.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,12 @@ def test_max_connection_trailers(self):
507507
response = conn.getresponse()
508508
self.assertEqual(response.read(), chunked_expected)
509509

510+
def test_getallmatchingheaders_deprecated(self):
511+
message = client.parse_headers(io.BytesIO(b"Set-Cookie: a=1\r\n\r\n"))
512+
with self.assertWarns(DeprecationWarning):
513+
message.getallmatchingheaders("Set-Cookie")
514+
515+
510516
class HttpMethodTests(TestCase):
511517
def test_invalid_method_names(self):
512518
methods = (
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Deprecate :meth:`!http.client.HTTPMessage.getallmatchingheaders`, to be
2+
removed in Python 3.18. It has returned an empty list for every input since
3+
Python 3.0, and its only user, the CGI handler, was removed in Python 3.15.
4+
Use :meth:`email.message.Message.get_all` instead. Patch by Julian Soreavis.

0 commit comments

Comments
 (0)