Return an HTTPResponse object for the request, using http_class. http_class must implement the HTTPConnection API from http.client.
(self, http_class, req, **http_conn_args)
| 1280 | return request |
| 1281 | |
| 1282 | def do_open(self, http_class, req, **http_conn_args): |
| 1283 | """Return an HTTPResponse object for the request, using http_class. |
| 1284 | |
| 1285 | http_class must implement the HTTPConnection API from http.client. |
| 1286 | """ |
| 1287 | host = req.host |
| 1288 | if not host: |
| 1289 | raise URLError('no host given') |
| 1290 | |
| 1291 | # will parse host:port |
| 1292 | h = http_class(host, timeout=req.timeout, **http_conn_args) |
| 1293 | h.set_debuglevel(self._debuglevel) |
| 1294 | |
| 1295 | headers = dict(req.unredirected_hdrs) |
| 1296 | headers.update({k: v for k, v in req.headers.items() |
| 1297 | if k not in headers}) |
| 1298 | |
| 1299 | # TODO(jhylton): Should this be redesigned to handle |
| 1300 | # persistent connections? |
| 1301 | |
| 1302 | # We want to make an HTTP/1.1 request, but the addinfourl |
| 1303 | # class isn't prepared to deal with a persistent connection. |
| 1304 | # It will try to read all remaining data from the socket, |
| 1305 | # which will block while the server waits for the next request. |
| 1306 | # So make sure the connection gets closed after the (only) |
| 1307 | # request. |
| 1308 | headers["Connection"] = "close" |
| 1309 | headers = {name.title(): val for name, val in headers.items()} |
| 1310 | |
| 1311 | if req._tunnel_host: |
| 1312 | tunnel_headers = {} |
| 1313 | proxy_auth_hdr = "Proxy-Authorization" |
| 1314 | if proxy_auth_hdr in headers: |
| 1315 | tunnel_headers[proxy_auth_hdr] = headers[proxy_auth_hdr] |
| 1316 | # Proxy-Authorization should not be sent to origin |
| 1317 | # server. |
| 1318 | del headers[proxy_auth_hdr] |
| 1319 | h.set_tunnel(req._tunnel_host, headers=tunnel_headers) |
| 1320 | |
| 1321 | try: |
| 1322 | try: |
| 1323 | h.request(req.get_method(), req.selector, req.data, headers, |
| 1324 | encode_chunked=req.has_header('Transfer-encoding')) |
| 1325 | except OSError as err: # timeout error |
| 1326 | raise URLError(err) |
| 1327 | r = h.getresponse() |
| 1328 | except: |
| 1329 | h.close() |
| 1330 | raise |
| 1331 | |
| 1332 | # If the server does not send us a 'Connection: close' header, |
| 1333 | # HTTPConnection assumes the socket should be left open. Manually |
| 1334 | # mark the socket to be closed when this response object goes away. |
| 1335 | if h.sock: |
| 1336 | h.sock.close() |
| 1337 | h.sock = None |
| 1338 | |
| 1339 | r.url = req.get_full_url() |