meebo/python-lounge — pull request #4 by markdoliner (2012-04-10 02:36:33+00:00)
Title: Send empty body when creating new database

Override the _put function in the Database class so that we don't send
any data in the body of the HTTP request when using PUT to create a
new database. The couchdb API documentation says this data is
ignored [1], but it's a little more complicated than that.

For this request couchdb will respond as soon as it's finished parsing
the headers (since it's not expecting a body). If the headers and body
arrive at the couchdb server in separate TCP chunks then couchdb will
not have read the body. couchdb fails to respect the content-length
header [2] and so it will not read and discard the body. Instead, it
reads the body and treats it as a new HTTP request. Since the body is
most likely not a valid HTTP request, couchdb will send a TCP RST.

There are a few race conditions here... but if we receive the TCP RST
before we've finished reading the first response from the server then
our call to handle.request() in Resource._request() will raise
socket.error and the database create will raise a SocketError.

[1] http://wiki.apache.org/couchdb/HTTP_database_API#PUT_.28Create_New_Database.29
[2] https://issues.apache.org/jira/browse/COUCHDB-1146