Skip to content

Commit

Permalink
Fix reconnecting, through a proxy, to a closed connection
Browse files Browse the repository at this point in the history
treat closed connections as dropped
Since appengine (and possibly other) environments may not have
the conn.auto_open attribute, don't rely on its presence (use getattr to read
it).
  • Loading branch information
cg2v committed May 19, 2014
1 parent 691cf5e commit 1c30a1f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions urllib3/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def _prepare_conn(self, conn):
if getattr(self, '_tunnel_host', None):
# TODO: Fix tunnel so it doesn't depend on self.sock state.
self._tunnel()
# mark connection as not reusable
self.auto_open = 0

def connect(self):
conn = self._new_conn()
Expand Down Expand Up @@ -177,6 +179,7 @@ def connect(self):
# Calls self._set_hostport(), so self.host is
# self._tunnel_host below.
self._tunnel()
self.auto_open = 0

# Override the host with the one we're requesting data from.
hostname = self._tunnel_host
Expand Down
5 changes: 5 additions & 0 deletions urllib3/connectionpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ def _get_conn(self, timeout=None):
if conn and is_connection_dropped(conn):
log.info("Resetting dropped connection: %s" % self.host)
conn.close()
if getattr(conn, 'auto_open', 1) == 0:
# this is a proxied connection that has been mutated by
# httplib._tunnel() and cannot be reused (since it would
# attempt to bypass the proxy)
conn = None

return conn or self._new_conn()

Expand Down
2 changes: 1 addition & 1 deletion urllib3/util/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def is_connection_dropped(conn): # Platform-specific
if sock is False: # Platform-specific: AppEngine
return False
if sock is None: # Connection already closed (such as by httplib).
return False
return True

if not poll:
if not select: # Platform-specific: AppEngine
Expand Down

0 comments on commit 1c30a1f

Please sign in to comment.