Index: Lib/http/client.py =================================================================== --- Lib/http/client.py (revision 86911) +++ Lib/http/client.py (working copy) @@ -71,6 +71,7 @@ import io import os import socket +import collections from urllib.parse import urlsplit import warnings @@ -731,7 +732,11 @@ self.__state = _CS_IDLE def send(self, data): - """Send `data' to the server.""" + """Send `data' to the server. + ``data`` can be a string object, a bytes object, an array object, a + file-like object that supports a .read() method, or an iterable object. + """ + if self.sock is None: if self.auto_open: self.connect() @@ -763,9 +768,19 @@ if encode: datablock = datablock.encode("iso-8859-1") self.sock.sendall(datablock) - else: - self.sock.sendall(data) + try: + self.sock.sendall(data) + except TypeError: + if isinstance(data, collections.Iterable): + it = iter(data) + for d in it: + self.sock.sendall(d) + else: + raise TypeError("data should be bytes-like object\ + or an iterable, got %r " % type(it)) + + def _output(self, s): """Add a line of output to the current request buffer. Index: Lib/urllib/request.py =================================================================== --- Lib/urllib/request.py (revision 86911) +++ Lib/urllib/request.py (working copy) @@ -94,6 +94,7 @@ import socket import sys import time +import collections from urllib.error import URLError, HTTPError, ContentTooShortError from urllib.parse import ( @@ -1053,8 +1054,21 @@ 'Content-type', 'application/x-www-form-urlencoded') if not request.has_header('Content-length'): + if isinstance(data,str): + content_length = len(data) + else: + try: + mv = memoryview(data) + except TypeError: + if isinstance(data,collections.Iterable): + raise ValueError("Content-Length should be \ + specified for the iterable data of \ + type %r" % type(data)) + else: + content_length = len(mv) * mv.itemsize + request.add_unredirected_header( - 'Content-length', '%d' % len(data)) + 'Content-length', '%d' % content_length) sel_host = host if request.has_proxy(): Index: Lib/test/test_httplib.py =================================================================== --- Lib/test/test_httplib.py (revision 86911) +++ Lib/test/test_httplib.py (working copy) @@ -229,7 +229,23 @@ sock.data = b'' conn.send(io.BytesIO(expected)) self.assertEqual(expected, sock.data) + + def test_send_iter(self): + expected = b'GET /foo HTTP/1.1\r\nHost: example.com\r\n' \ + b'Accept-Encoding: identity\r\nContent-Length: 11\r\n' \ + b'\r\nonetwothree' + def body(): + yield b"one" + yield b"two" + yield b"three" + + conn = client.HTTPConnection('example.com') + sock = FakeSocket("") + conn.sock = sock + conn.request('GET', '/foo', body(), {'Content-Length': '11'}) + self.assertEquals(sock.data, expected) + def test_chunked(self): chunked_start = ( 'HTTP/1.1 200 OK\r\n' Index: Lib/test/test_urllib2.py =================================================================== --- Lib/test/test_urllib2.py (revision 86911) +++ Lib/test/test_urllib2.py (working copy) @@ -4,6 +4,7 @@ import os import io import socket +import array import urllib.request from urllib.request import Request, OpenerDirector @@ -821,6 +822,48 @@ self.assertEqual(req.unredirected_hdrs["Host"], "baz") self.assertEqual(req.unredirected_hdrs["Spam"], "foo") + # Check iterable body support + def iterable_body(): + yield "one" + yield "two" + yield "three" + + for headers in {}, {"Content-Length": 11}: + req = Request("http://example.com/", iterable_body(), headers) + if not headers: + # Having an iterable body without a Content-Length should + # raise an exception + self.assertRaises(ValueError, h.do_request_, req) + else: + newreq = h.do_request_(req) + + # A file object + + file_obj = io.StringIO() + file_obj.write("Something\nSomething\nSomething\n") + + for headers in {}, {"Content-Length": 30}: + req = Request("http://example.com/", file_obj, headers) + if not headers: + # Having an iterable body without a Content-Length should + # raise an exception + self.assertRaises(ValueError, h.do_request_, req) + else: + newreq = h.do_request_(req) + self.assertEqual(int(newreq.get_header('Content-length')),30) + + file_obj.close() + + # array.array Iterable - Content Length is calculated + + iterable_array = array.array("I",[1,2,3,4]) + + for headers in {}, {"Content-Length": 16}: + req = Request("http://example.com/", iterable_array, headers) + newreq = h.do_request_(req) + self.assertEqual(int(newreq.get_header('Content-length')),16) + + def test_http_doubleslash(self): # Checks the presence of any unnecessary double slash in url does not # break anything. Previously, a double slash directly after the host