Skip to content

Commit 646673c

Browse files
authored
gh-153309: Add HTTP QUERY method (RFC 10008) to http library (#155786)
Add HTTP QUERY method (RFC 10008) to http library Closes #153309
1 parent 69ac138 commit 646673c

8 files changed

Lines changed: 47 additions & 4 deletions

File tree

‎Doc/library/http.client.rst‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ HTTPConnection Objects
312312
but there is a request body, one of those
313313
header fields will be added automatically. If
314314
*body* is ``None``, the Content-Length header is set to ``0`` for
315-
methods that expect a body (``PUT``, ``POST``, and ``PATCH``). If
315+
methods that expect a body (``PUT``, ``POST``, ``PATCH``, and ``QUERY``). If
316316
*body* is a string or a bytes-like object that is not also a
317317
:term:`file <file object>`, the Content-Length header is
318318
set to its length. Any other type of *body* (files
@@ -369,6 +369,9 @@ HTTPConnection Objects
369369
*continue_timeout* value must be greater than zero, otherwise
370370
:exc:`ValueError` is raised.
371371

372+
.. versionchanged:: next
373+
``QUERY`` was added to the methods that expect a body.
374+
372375
.. method:: HTTPConnection.getresponse(support_continue=False)
373376

374377
Should be called after a request is sent to get the response from the server.

‎Doc/library/http.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ Property Indicates that Details
194194
<HTTPMethod.PATCH>,
195195
<HTTPMethod.POST>,
196196
<HTTPMethod.PUT>,
197+
<HTTPMethod.QUERY>,
197198
<HTTPMethod.TRACE>]
198199

199200
.. _http-methods:
@@ -217,4 +218,5 @@ Method Enum Name Details
217218
``OPTIONS`` ``OPTIONS`` HTTP Semantics :rfc:`9110`, Section 9.3.7
218219
``TRACE`` ``TRACE`` HTTP Semantics :rfc:`9110`, Section 9.3.8
219220
``PATCH`` ``PATCH`` HTTP/1.1 :rfc:`5789`
221+
``QUERY`` ``QUERY`` The HTTP QUERY Method :rfc:`10008`
220222
=========== =================================== ==================================================================

‎Doc/whatsnew/3.16.rst‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,13 @@ gzip
508508
(Contributed by Marin Misur in :gh:`91372`.)
509509

510510

511+
http
512+
----
513+
514+
* Add the ``QUERY`` HTTP method (:rfc:`10008`) to :class:`~http.HTTPMethod`.
515+
(Contributed by Michiel W. Beijen in :gh:`153309`.)
516+
517+
511518
http.client
512519
-----------
513520

‎Lib/http/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ class HTTPMethod:
192192
193193
* RFC 9110: HTTP Semantics, obsoletes 7231, which obsoleted 2616
194194
* RFC 5789: PATCH Method for HTTP
195+
* RFC 10008: The HTTP QUERY Method
195196
"""
196197
def __new__(cls, value, description):
197198
obj = str.__new__(cls, value)
@@ -210,4 +211,5 @@ def __repr__(self):
210211
PATCH = 'PATCH', 'Apply partial modifications to a target.'
211212
POST = 'POST', 'Perform target-specific processing with the request payload.'
212213
PUT = 'PUT', 'Replace the target with the request payload.'
214+
QUERY = 'QUERY', 'Request that the target process the request payload in a safe and idempotent manner.'
213215
TRACE = 'TRACE', 'Perform a message loop-back test along the path to the target.'

‎Lib/http/client.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@
183183

184184
# We always set the Content-Length header for these methods because some
185185
# servers will otherwise respond with a 411
186-
_METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'}
186+
_METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT', 'QUERY'}
187187

188188

189189
def _encode(data, name='data'):

‎Lib/test/test_httplib.py‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def append(self, item):
200200
# Here, we're testing that methods expecting a body get a
201201
# content-length set to zero if the body is empty (either None or '')
202202
bodies = (None, '')
203-
methods_with_body = ('PUT', 'POST', 'PATCH')
203+
methods_with_body = ('PUT', 'POST', 'PATCH', 'QUERY')
204204
for method, body in itertools.product(methods_with_body, bodies):
205205
conn = client.HTTPConnection('example.com')
206206
conn.sock = FakeSocket(None)
@@ -554,6 +554,34 @@ def test_invalid_method_names(self):
554554
conn.sock = FakeSocket(None)
555555
conn.request(method=method, url="/")
556556

557+
def test_query_request(self):
558+
# QUERY (RFC 10008) is sent with a body, like PUT/POST/PATCH.
559+
conn = client.HTTPConnection('example.com')
560+
conn.sock = FakeSocket(None)
561+
conn.request('QUERY', '/contacts', body='name=python',
562+
headers={'Content-Type': 'application/x-www-form-urlencoded'})
563+
self.assertEqual(conn.sock.data,
564+
b'QUERY /contacts HTTP/1.1\r\n'
565+
b'Host: example.com\r\n'
566+
b'Accept-Encoding: identity\r\n'
567+
b'Content-Length: 11\r\n'
568+
b'Content-Type: application/x-www-form-urlencoded\r\n'
569+
b'\r\n'
570+
b'name=python')
571+
572+
def test_query_request_no_body(self):
573+
# A QUERY without a body still gets Content-Length: 0, since some
574+
# servers respond with 411 otherwise.
575+
conn = client.HTTPConnection('example.com')
576+
conn.sock = FakeSocket(None)
577+
conn.request('QUERY', '/contacts')
578+
self.assertEqual(conn.sock.data,
579+
b'QUERY /contacts HTTP/1.1\r\n'
580+
b'Host: example.com\r\n'
581+
b'Accept-Encoding: identity\r\n'
582+
b'Content-Length: 0\r\n'
583+
b'\r\n')
584+
557585

558586
class TransferEncodingTest(TestCase):
559587
expected_body = b"It's just a flesh wound"

‎Lib/wsgiref/validate.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def check_environ(environ):
334334

335335
# @@: these need filling out:
336336
if environ['REQUEST_METHOD'] not in (
337-
'GET', 'HEAD', 'POST', 'OPTIONS', 'PATCH', 'PUT', 'DELETE', 'TRACE'):
337+
'GET', 'HEAD', 'POST', 'OPTIONS', 'PATCH', 'PUT', 'DELETE', 'QUERY', 'TRACE'):
338338
warnings.warn(
339339
"Unknown REQUEST_METHOD: %r" % environ['REQUEST_METHOD'],
340340
WSGIWarning)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:mod:`http`: Add the HTTP QUERY method (:rfc:`10008`) to :class:`~http.HTTPMethod`.

0 commit comments

Comments
 (0)