diff --git a/python3/httplib2/__init__.py b/python3/httplib2/__init__.py index 3ce019e..398f9af 100644 --- a/python3/httplib2/__init__.py +++ b/python3/httplib2/__init__.py @@ -996,16 +996,18 @@ def _conn_request(self, conn, request_uri, method, body, headers): errno_ = (e.args[0].errno if isinstance(e.args[0], socket.error) else e.errno) if errno_ in (errno.ENETUNREACH, errno.EADDRNOTAVAIL) and i < RETRIES: continue # retry on potentially transient errors + if errno_ in (errno.ECONNRESET, errno.EPIPE) and i == 1: + conn.close() + conn.connect() + continue # retry on closed connection raise except http.client.HTTPException: if conn.sock is None: + conn.close() if i < RETRIES-1: - conn.close() conn.connect() continue - else: - conn.close() - raise + raise if i < RETRIES-1: conn.close() conn.connect() @@ -1015,39 +1017,34 @@ def _conn_request(self, conn, request_uri, method, body, headers): pass try: response = conn.getresponse() + content = b"" + if method == "HEAD": + conn.close() + else: + content = response.read() + response = Response(response) + if method != "HEAD": + content = _decompressContent(response, content) except (http.client.BadStatusLine, http.client.ResponseNotReady): # If we get a BadStatusLine on the first try then that means # the connection just went stale, so retry regardless of the # number of RETRIES set. + conn.close() if not seen_bad_status_line and i == 1: i = 0 seen_bad_status_line = True - conn.close() conn.connect() continue - else: - conn.close() - raise + raise except socket.timeout: raise - except (socket.error, http.client.HTTPException): + except (socket.error, http.client.HTTPException) as e: conn.close() - if i == 0: - conn.close() + errno_ = (e.args[0].errno if isinstance(e.args[0], socket.error) else e.errno) + if errno_ in (errno.ECONNRESET, errno.EPIPE) and i == 1: conn.connect() continue - else: - raise - else: - content = b"" - if method == "HEAD": - conn.close() - else: - content = response.read() - response = Response(response) - if method != "HEAD": - content = _decompressContent(response, content) - + raise break return (response, content)