@@ -164,8 +164,10 @@ def remove(self, connection):
164164
165165 def set_ready (self , connection , ready ):
166166 self ._lock .acquire ()
167- if connection in self ._readymap : self ._readymap [connection ] = ready
168- self ._lock .release ()
167+ try :
168+ if connection in self ._readymap : self ._readymap [connection ] = ready
169+ finally :
170+ self ._lock .release ()
169171
170172 def get_ready_conn (self , host ):
171173 conn = None
@@ -258,6 +260,16 @@ def do_open(self, req):
258260
259261 if DEBUG : DEBUG .info ("STATUS: %s, %s" , r .status , r .reason )
260262
263+ if not r .will_close :
264+ try :
265+ headers = getattr (r , 'msg' , None )
266+ if headers :
267+ c_head = headers .get ("connection" )
268+ if c_head and "close" in c_head .lower ():
269+ r .will_close = True
270+ except Exception :
271+ pass
272+
261273 # if not a persistent connection, don't try to reuse it
262274 if r .will_close :
263275 if DEBUG : DEBUG .info ('server will close connection, discarding' )
@@ -322,16 +334,16 @@ def _reuse_connection(self, h, req, host):
322334
323335 def _start_transaction (self , h , req ):
324336 try :
325- if req .data is not None :
337+ if req .data :
326338 data = req .data
327339 if hasattr (req , 'selector' ):
328340 h .putrequest (req .get_method () or 'POST' , req .selector , skip_host = req .has_header ("Host" ), skip_accept_encoding = req .has_header ("Accept-encoding" ))
329341 else :
330342 h .putrequest (req .get_method () or 'POST' , req .get_selector (), skip_host = req .has_header ("Host" ), skip_accept_encoding = req .has_header ("Accept-encoding" ))
331- if not req . has_header ( 'Content-type' ) :
343+ if 'Content-type' not in req . headers :
332344 h .putheader ('Content-type' ,
333345 'application/x-www-form-urlencoded' )
334- if not req . has_header ( 'Content-length' ) :
346+ if 'Content-length' not in req . headers :
335347 h .putheader ('Content-length' , '%d' % len (data ))
336348 else :
337349 if hasattr (req , 'selector' ):
@@ -341,17 +353,17 @@ def _start_transaction(self, h, req):
341353 except (socket .error , _http_client .HTTPException ) as err :
342354 raise _urllib .error .URLError (err )
343355
344- if not req .has_header ( 'Connection' ) :
356+ if 'Connection' not in req .headers :
345357 h .putheader ('Connection' , 'keep-alive' )
346358
347359 for args in self .parent .addheaders :
348- if not req . has_header ( args [0 ]) :
360+ if args [0 ] not in req . headers :
349361 h .putheader (* args )
350362 for k , v in req .headers .items ():
351363 h .putheader (k , v )
352364 h .endheaders ()
353- if req .data is not None :
354- h .send (data )
365+ if req .data :
366+ h .send (req . data )
355367
356368 def _get_connection (self , host ):
357369 raise NotImplementedError ()
0 commit comments