Skip to content

Commit ccd5ca2

Browse files
committed
Add HTTP QUERY method (RFC 10008) to http library
Closes #153309
1 parent 079dd5e commit ccd5ca2

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
@@ -289,7 +289,7 @@ HTTPConnection Objects
289289
but there is a request body, one of those
290290
header fields will be added automatically. If
291291
*body* is ``None``, the Content-Length header is set to ``0`` for
292-
methods that expect a body (``PUT``, ``POST``, and ``PATCH``). If
292+
methods that expect a body (``PUT``, ``POST``, ``PATCH``, and ``QUERY``). If
293293
*body* is a string or a bytes-like object that is not also a
294294
:term:`file <file object>`, the Content-Length header is
295295
set to its length. Any other type of *body* (files
@@ -335,6 +335,9 @@ HTTPConnection Objects
335335
No attempt is made to determine the Content-Length for file
336336
objects.
337337

338+
.. versionchanged:: next
339+
``QUERY`` was added to the methods that expect a body.
340+
338341
.. method:: HTTPConnection.getresponse()
339342

340343
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
@@ -305,6 +305,13 @@ gzip
305305
which is passed on to the constructor of the :class:`~gzip.GzipFile` class.
306306
(Contributed by Marin Misur in :gh:`91372`.)
307307

308+
309+
http
310+
----
311+
312+
* Add the ``QUERY`` HTTP method (:rfc:`10008`) to :class:`~http.HTTPMethod`.
313+
(Contributed by Michiel W. Beijen in :gh:`153309`.)
314+
308315
io
309316
--
310317

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
@@ -167,7 +167,7 @@
167167

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

172172

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

Lib/test/test_httplib.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def append(self, item):
179179
# Here, we're testing that methods expecting a body get a
180180
# content-length set to zero if the body is empty (either None or '')
181181
bodies = (None, '')
182-
methods_with_body = ('PUT', 'POST', 'PATCH')
182+
methods_with_body = ('PUT', 'POST', 'PATCH', 'QUERY')
183183
for method, body in itertools.product(methods_with_body, bodies):
184184
conn = client.HTTPConnection('example.com')
185185
conn.sock = FakeSocket(None)
@@ -528,6 +528,34 @@ def test_invalid_method_names(self):
528528
conn.sock = FakeSocket(None)
529529
conn.request(method=method, url="/")
530530

531+
def test_query_request(self):
532+
# QUERY (RFC 10008) is sent with a body, like PUT/POST/PATCH.
533+
conn = client.HTTPConnection('example.com')
534+
conn.sock = FakeSocket(None)
535+
conn.request('QUERY', '/contacts', body='name=python',
536+
headers={'Content-Type': 'application/x-www-form-urlencoded'})
537+
self.assertEqual(conn.sock.data,
538+
b'QUERY /contacts HTTP/1.1\r\n'
539+
b'Host: example.com\r\n'
540+
b'Accept-Encoding: identity\r\n'
541+
b'Content-Length: 11\r\n'
542+
b'Content-Type: application/x-www-form-urlencoded\r\n'
543+
b'\r\n'
544+
b'name=python')
545+
546+
def test_query_request_no_body(self):
547+
# A QUERY without a body still gets Content-Length: 0, since some
548+
# servers respond with 411 otherwise.
549+
conn = client.HTTPConnection('example.com')
550+
conn.sock = FakeSocket(None)
551+
conn.request('QUERY', '/contacts')
552+
self.assertEqual(conn.sock.data,
553+
b'QUERY /contacts HTTP/1.1\r\n'
554+
b'Host: example.com\r\n'
555+
b'Accept-Encoding: identity\r\n'
556+
b'Content-Length: 0\r\n'
557+
b'\r\n')
558+
531559

532560
class TransferEncodingTest(TestCase):
533561
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)