From: Senthil Kumaran Date: Thu, 8 Sep 2016 21:29:23 +0000 (-0700) Subject: [merge from 3.5] - Issue28010 - Make http.client.HTTPConnection.putrequest X-Git-Tag: v3.6.0b1~247 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=10427f44852b6e872034061421a8890902b8f854;p=thirdparty%2FPython%2Fcpython.git [merge from 3.5] - Issue28010 - Make http.client.HTTPConnection.putrequest documentation consistent with the code. --- 10427f44852b6e872034061421a8890902b8f854 diff --cc Lib/http/client.py index a1c4ab948255,352c1017adce..ad8f4104f4fb --- a/Lib/http/client.py +++ b/Lib/http/client.py @@@ -1023,52 -930,13 +1023,53 @@@ class HTTPConnection self._buffer.extend((b"", b"")) msg = b"\r\n".join(self._buffer) del self._buffer[:] - self.send(msg) + if message_body is not None: - self.send(message_body) + + # create a consistent interface to message_body + if hasattr(message_body, 'read'): + # Let file-like take precedence over byte-like. This + # is needed to allow the current position of mmap'ed + # files to be taken into account. + chunks = self._read_readable(message_body) + else: + try: + # this is solely to check to see if message_body + # implements the buffer API. it /would/ be easier + # to capture if PyObject_CheckBuffer was exposed + # to Python. + memoryview(message_body) + except TypeError: + try: + chunks = iter(message_body) + except TypeError: + raise TypeError("message_body should be a bytes-like " + "object or an iterable, got %r" + % type(message_body)) + else: + # the object implements the buffer interface and + # can be passed directly into socket methods + chunks = (message_body,) + + for chunk in chunks: + if not chunk: + if self.debuglevel > 0: + print('Zero length chunk ignored') + continue + + if encode_chunked and self._http_vsn == 11: + # chunked encoding + chunk = f'{len(chunk):X}''\r\n'.encode('ascii') + chunk \ + + b'\r\n' + self.send(chunk) + + if encode_chunked and self._http_vsn == 11: + # end chunked transfer + self.send(b'0\r\n\r\n') - def putrequest(self, method, url, skip_host=0, skip_accept_encoding=0): + def putrequest(self, method, url, skip_host=False, + skip_accept_encoding=False): """Send a request to the server. `method' specifies an HTTP request method, e.g. 'GET'.