[New-bugs-announce] [issue5542] Socket is closed prematurely in httplib, if server sends response before request body has been sent

Jason Davies report at bugs.python.org
Mon Mar 23 10:29:10 CET 2009


New submission from Jason Davies <jason at jasondavies.com>:

I came across this bug when trying to use CouchDB-Python to PUT an
attachment using HTTP authentication (using the httplib2 library). 
Essentially what httplib2 does is this:

1. Attempt PUT with no auth credentials
2. If a 401 response is received, try again with credentials

However, CouchDB sends a 401 response as soon as it has consumed the
request headers, and before the request body has been fully sent by the
client.

This triggers a bug in both httplib and httplib2, whereby they raise
exceptions due to a broken pipe being encountered when trying to finish
sending the request body.

It seems that Python's httplib checks for broken pipe errors and closes
the connection entirely when they occur when making a request.  This is
unhelpful if a legitimate response was sent and the socket was closed
early by the server.

The offending try/except handler is in httplib.HttpConnection.send and
has a comment saying:

# send the data to the server. if we get a broken pipe, then close
# the socket. we want to reconnect when somebody tries to send again.

This is wrong, as someone might want to read the response before closing
the socket.

For reference, the CouchDB-Python issue is:
http://code.google.com/p/couchdb-python/issues/detail?id=68

This bug may also be related to: http://bugs.python.org/issue3566

----------
components: Library (Lib)
messages: 84000
nosy: jasondavies
severity: normal
status: open
title: Socket is closed prematurely in httplib, if server sends response before request body has been sent
versions: Python 2.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5542>
_______________________________________


More information about the New-bugs-announce mailing list