Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.9k
gh-42550: Add 'Expect: 100-Continue' support to httplib#133276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -34,7 +34,7 @@ The module provides the following classes: | ||
.. class:: HTTPConnection(host, port=None[, timeout], source_address=None, \ | ||
blocksize=8192, continue_timeout=2.5) | ||
An :class:`HTTPConnection` instance represents one transaction with an HTTP | ||
server. It should be instantiated by passing it a host and optional port | ||
@@ -46,7 +46,10 @@ The module provides the following classes: | ||
The optional *source_address* parameter may be a tuple of a (host, port) | ||
to use as the source address the HTTP connection is made from. | ||
The optional *blocksize* parameter sets the buffer size in bytes for | ||
sending a file-like message body. Finally, the optional *continue_timeout* | ||
parameter controls how long the connection will wait for a ``100 Continue`` | ||
response from the server before sending the body, if the request included | ||
an ``Expect: 100-Continue`` header. | ||
Comment on lines +49 to +52 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Does this need to be a constructor parameter? | ||
For example, the following calls all create instances that connect to the server | ||
at the same host and port:: | ||
@@ -66,6 +69,9 @@ The module provides the following classes: | ||
.. versionchanged:: 3.7 | ||
*blocksize* parameter was added. | ||
.. versionchanged:: next | ||
*continue_timeout* parameter was added. | ||
.. class:: HTTPSConnection(host, port=None, *[, timeout], \ | ||
source_address=None, context=None, \ | ||
@@ -321,11 +327,24 @@ HTTPConnection Objects | ||
No attempt is made to determine the Content-Length for file | ||
objects. | ||
.. versionchanged:: next | ||
If the headers include ``Expect: 100-Continue`` and *body* is set, | ||
the body will not be sent until either a ``100 Continue`` response is | ||
received from the server or the :class:`HTTPConnection`'s *continue_timeout* | ||
is reached; if a response code other than 100 is received, the body | ||
will not be sent at all. | ||
.. method:: HTTPConnection.getresponse(ignore_100_continue=True) | ||
Should be called after a request is sent to get the response from the server. | ||
Returns an :class:`HTTPResponse` instance. | ||
By default, a server response of ``100 Continue`` will be ignored and the | ||
call will not return until a response other than code 100 is received. | ||
Setting *ignore_100_continue* to ``False`` will allow a 100 response to be | ||
returned; you will then need to call :meth:`getresponse` a second time | ||
(after transmitting the body) to get the final response. | ||
.. note:: | ||
Note that you must have read the whole response before you can send a new | ||
@@ -336,6 +355,10 @@ HTTPConnection Objects | ||
:class:`HTTPConnection` object will be ready to reconnect when | ||
a new request is sent. | ||
.. versionchanged:: next | ||
Added the *ignore_100_continue* parameter. (In prior versions | ||
a ``Continue`` response was always ignored.) | ||
.. method:: HTTPConnection.set_debuglevel(level) | ||
@@ -439,11 +462,17 @@ also send your request step by step, by using the four functions below. | ||
an argument. | ||
.. method:: HTTPConnection.endheaders(message_body=None, *, encode_chunked=False, \ | ||
expect_continue=False) | ||
Send a blank line to the server, signalling the end of the headers. The | ||
optional *message_body* argument can be used to pass a message body | ||
associated with the request. If a body is provided, setting | ||
*expect_continue* to ``True`` will wait for a ``100 Continue`` response | ||
from the server before sending the body. (This should generally be | ||
used only when an ``Expect: 100-Continue`` header has been sent.) If no | ||
response is received within the :class:`HTTPConnection`'s *continue_timeout*, | ||
the body will be sent regardless. | ||
If *encode_chunked* is ``True``, the result of each iteration of | ||
*message_body* will be chunk-encoded as specified in :rfc:`7230`, | ||
@@ -464,6 +493,9 @@ also send your request step by step, by using the four functions below. | ||
.. versionchanged:: 3.6 | ||
Added chunked encoding support and the *encode_chunked* parameter. | ||
.. versionadded:: next | ||
The *expect_continue* parameter was added. | ||
.. method:: HTTPConnection.send(data) | ||
@@ -642,6 +674,48 @@ method attribute. Here is an example session that uses the ``PUT`` method:: | ||
>>> print(response.status, response.reason) | ||
200, OK | ||
Since version 3.15, conditional transmission of the body is supported when an | ||
``Expect: 100-Continue`` header is set. To use this in a simple case, just | ||
set the header, and optionally the time for which the client should wait for | ||
a ``100 Continue`` response before sending the body regardless:: | ||
>>> import http.client | ||
>>> BODY = "***filecontents***" | ||
>>> conn = http.client.HTTPConnection("localhost", 8080, continue_timeout=1.0) | ||
>>> conn.request("PUT", "/file", BODY, headers={'Expect': '100-Continue'}) | ||
>>> response = conn.getresponse() | ||
>>> # You will not see the '100' response, as it is handled internally | ||
>>> print(response.status, response.reason) | ||
200, OK | ||
Here is a more complex example in which we manually check the response and | ||
decide whether to send the body. This may be useful if the body must be | ||
generated by some resource-intensive process which should be skipped if the | ||
server will not accept it. :: | ||
>>> import http.client | ||
>>> conn = http.client.HTTPConnection("localhost", 8080) | ||
>>> conn.putrequest("PUT", "/file") | ||
>>> conn.putheader('Expect', '100-Continue') | ||
>>> # Assuming you know in advance what the length will be | ||
>>> # If not, you will need to encode it as chunked | ||
>>> conn.putheader('Content-Length', '42') | ||
>>> conn.endheaders() | ||
>>> response = conn.getresponse(ignore_100_continue=False) | ||
>>> print(response.status, response.reason) | ||
100, Continue | ||
>>> BODY = resource_intensive_calculation() | ||
>>> conn.send(BODY) | ||
>>> response = conn.getresponse() | ||
>>> print(response.status, response.reason) | ||
200, OK | ||
.. note:: | ||
The *continue_timeout* setting does not apply when directly using | ||
:meth:`HTTPConnection.getresponse`, so use the above example only if you are confident | ||
that the server respects the ``Expect: 100-Continue`` header. | ||
.. _httpmessage-objects: | ||
HTTPMessage Objects | ||
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.