(self, method, url, body, headers, encode_chunked)
| 1362 | self._send_request(method, url, body, headers, encode_chunked) |
| 1363 | |
| 1364 | def _send_request(self, method, url, body, headers, encode_chunked): |
| 1365 | # Honor explicitly requested Host: and Accept-Encoding: headers. |
| 1366 | header_names = frozenset(k.lower() for k in headers) |
| 1367 | skips = {} |
| 1368 | if 'host' in header_names: |
| 1369 | skips['skip_host'] = 1 |
| 1370 | if 'accept-encoding' in header_names: |
| 1371 | skips['skip_accept_encoding'] = 1 |
| 1372 | |
| 1373 | self.putrequest(method, url, **skips) |
| 1374 | |
| 1375 | # chunked encoding will happen if HTTP/1.1 is used and either |
| 1376 | # the caller passes encode_chunked=True or the following |
| 1377 | # conditions hold: |
| 1378 | # 1. content-length has not been explicitly set |
| 1379 | # 2. the body is a file or iterable, but not a str or bytes-like |
| 1380 | # 3. Transfer-Encoding has NOT been explicitly set by the caller |
| 1381 | |
| 1382 | if 'content-length' not in header_names: |
| 1383 | # only chunk body if not explicitly set for backwards |
| 1384 | # compatibility, assuming the client code is already handling the |
| 1385 | # chunking |
| 1386 | if 'transfer-encoding' not in header_names: |
| 1387 | # if content-length cannot be automatically determined, fall |
| 1388 | # back to chunked encoding |
| 1389 | encode_chunked = False |
| 1390 | content_length = self._get_content_length(body, method) |
| 1391 | if content_length is None: |
| 1392 | if body is not None: |
| 1393 | if self.debuglevel > 0: |
| 1394 | print('Unable to determine size of %r' % body) |
| 1395 | encode_chunked = True |
| 1396 | self.putheader('Transfer-Encoding', 'chunked') |
| 1397 | else: |
| 1398 | self.putheader('Content-Length', str(content_length)) |
| 1399 | else: |
| 1400 | encode_chunked = False |
| 1401 | |
| 1402 | for hdr, value in headers.items(): |
| 1403 | self.putheader(hdr, value) |
| 1404 | if isinstance(body, str): |
| 1405 | # RFC 2616 Section 3.7.1 says that text default has a |
| 1406 | # default charset of iso-8859-1. |
| 1407 | body = _encode(body, 'body') |
| 1408 | self.endheaders(body, encode_chunked=encode_chunked) |
| 1409 | |
| 1410 | def getresponse(self): |
| 1411 | """Get the response from the server. |
no test coverage detected