summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorMartin Panter <vadmium+py@gmail.com>2016-08-27 01:39:26 (GMT)
committerMartin Panter <vadmium+py@gmail.com>2016-08-27 01:39:26 (GMT)
commitef91bb26604ddfae22aac56b3cfdaabf237db37a (patch)
tree491cbeadc0488a4e203de39ca42f8b132653195f /Doc
parent8f96a30630b5f6a984af3ee53c63bce3b16077e0 (diff)
downloadcpython-ef91bb26604ddfae22aac56b3cfdaabf237db37a.zip
cpython-ef91bb26604ddfae22aac56b3cfdaabf237db37a.tar.gz
cpython-ef91bb26604ddfae22aac56b3cfdaabf237db37a.tar.bz2
Issue #12319: Always send file request bodies using chunked encoding
The previous attempt to determine the file’s Content-Length gave a false positive for pipes on Windows. Also, drop the special case for sending zero-length iterable bodies.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/http.client.rst31
-rw-r--r--Doc/library/urllib.request.rst15
-rw-r--r--Doc/whatsnew/3.6.rst11
3 files changed, 31 insertions, 26 deletions
diff --git a/Doc/library/http.client.rst b/Doc/library/http.client.rst
index 9429fb6..d1b4450 100644
--- a/Doc/library/http.client.rst
+++ b/Doc/library/http.client.rst
@@ -240,17 +240,17 @@ HTTPConnection Objects
The *headers* argument should be a mapping of extra HTTP headers to send
with the request.
- If *headers* contains neither Content-Length nor Transfer-Encoding, a
- Content-Length header will be added automatically if possible. If
+ If *headers* contains neither Content-Length nor Transfer-Encoding,
+ but there is a request body, one of those
+ header fields will be added automatically. If
*body* is ``None``, the Content-Length header is set to ``0`` for
methods that expect a body (``PUT``, ``POST``, and ``PATCH``). If
- *body* is a string or bytes-like object, the Content-Length header is
- set to its length. If *body* is a binary :term:`file object`
- supporting :meth:`~io.IOBase.seek`, this will be used to determine
- its size. Otherwise, the Content-Length header is not added
- automatically. In cases where determining the Content-Length up
- front is not possible, the body will be chunk-encoded and the
- Transfer-Encoding header will automatically be set.
+ *body* is a string or a bytes-like object that is not also a
+ :term:`file <file object>`, the Content-Length header is
+ set to its length. Any other type of *body* (files
+ and iterables in general) will be chunk-encoded, and the
+ Transfer-Encoding header will automatically be set instead of
+ Content-Length.
The *encode_chunked* argument is only relevant if Transfer-Encoding is
specified in *headers*. If *encode_chunked* is ``False``, the
@@ -260,19 +260,18 @@ HTTPConnection Objects
.. note::
Chunked transfer encoding has been added to the HTTP protocol
version 1.1. Unless the HTTP server is known to handle HTTP 1.1,
- the caller must either specify the Content-Length or must use a
- body representation whose length can be determined automatically.
+ the caller must either specify the Content-Length, or must pass a
+ :class:`str` or bytes-like object that is not also a file as the
+ body representation.
.. versionadded:: 3.2
*body* can now be an iterable.
.. versionchanged:: 3.6
If neither Content-Length nor Transfer-Encoding are set in
- *headers* and Content-Length cannot be determined, *body* will now
- be automatically chunk-encoded. The *encode_chunked* argument
- was added.
- The Content-Length for binary file objects is determined with seek.
- No attempt is made to determine the Content-Length for text file
+ *headers*, file and iterable *body* objects are now chunk-encoded.
+ The *encode_chunked* argument was added.
+ No attempt is made to determine the Content-Length for file
objects.
.. method:: HTTPConnection.getresponse()
diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst
index e619cc1..d288165 100644
--- a/Doc/library/urllib.request.rst
+++ b/Doc/library/urllib.request.rst
@@ -187,12 +187,11 @@ The following classes are provided:
server, or ``None`` if no such data is needed. Currently HTTP
requests are the only ones that use *data*. The supported object
types include bytes, file-like objects, and iterables. If no
- ``Content-Length`` header has been provided, :class:`HTTPHandler` will
- try to determine the length of *data* and set this header accordingly.
- If this fails, ``Transfer-Encoding: chunked`` as specified in
- :rfc:`7230`, Section 3.3.1 will be used to send the data. See
- :meth:`http.client.HTTPConnection.request` for details on the
- supported object types and on how the content length is determined.
+ ``Content-Length`` nor ``Transfer-Encoding`` header field
+ has been provided, :class:`HTTPHandler` will set these headers according
+ to the type of *data*. ``Content-Length`` will be used to send
+ bytes objects, while ``Transfer-Encoding: chunked`` as specified in
+ :rfc:`7230`, Section 3.3.1 will be used to send files and other iterables.
For an HTTP POST request method, *data* should be a buffer in the
standard :mimetype:`application/x-www-form-urlencoded` format. The
@@ -256,8 +255,8 @@ The following classes are provided:
.. versionchanged:: 3.6
Do not raise an error if the ``Content-Length`` has not been
- provided and could not be determined. Fall back to use chunked
- transfer encoding instead.
+ provided and *data* is neither ``None`` nor a bytes object.
+ Fall back to use chunked transfer encoding instead.
.. class:: OpenerDirector()
diff --git a/Doc/whatsnew/3.6.rst b/Doc/whatsnew/3.6.rst
index ff3861b..6ef82d4 100644
--- a/Doc/whatsnew/3.6.rst
+++ b/Doc/whatsnew/3.6.rst
@@ -579,8 +579,8 @@ The :class:`~unittest.mock.Mock` class has the following improvements:
urllib.request
--------------
-If a HTTP request has a non-empty body but no Content-Length header
-and the content length cannot be determined up front, rather than
+If a HTTP request has a file or iterable body (other than a
+bytes object) but no Content-Length header, rather than
throwing an error, :class:`~urllib.request.AbstractHTTPHandler` now
falls back to use chunked transfer encoding.
(Contributed by Demian Brecht and Rolf Krahl in :issue:`12319`.)
@@ -935,6 +935,13 @@ Changes in the Python API
This behavior has also been backported to earlier Python versions
by Setuptools 26.0.0.
+* In the :mod:`urllib.request` module and the
+ :meth:`http.client.HTTPConnection.request` method, if no Content-Length
+ header field has been specified and the request body is a file object,
+ it is now sent with HTTP 1.1 chunked encoding. If a file object has to
+ be sent to a HTTP 1.0 server, the Content-Length value now has to be
+ specified by the caller. See :issue:`12319`.
+
Changes in the C API
--------------------