Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

When using nginx as a proxy, saveDoc fails due to missing content length #44

Open
wants to merge 3 commits into from

2 participants

@twistdigital

We're using nginx to load-balance to our couch installs.

When using saveDoc, we get HTTP 411 back from the proxy (and worse, the saveDoc callback just gets called with no args). This patch adds content length to the request if it's missing.

Before:

db.saveDoc(someDoc, console.log); // Outputs { '0': {} } and someDoc is not saved.

After:

db.saveDoc(someDoc, console.log);

/* Output:

{ '0': {},
'1':
{ ok: true,
id: '4eb6896f8e5fe31ab5eeba0ebc8e3cf7',
rev: '1-e2c7016a5c44d76c9c91b8ca30d7b6c5' } }

*/

@twistdigital

In the end listener, we added this debug code:

request.addListener("response", function(res) {
      var buffer = '';
      res.setEncoding(options.responseEncoding || 'utf8');
      res
        .addListener('data', function(chunk) {
          buffer += (chunk || '');
        })
        .addListener('end', function() {
console.log(buffer);

and it output this:

<html>
<head><title>411 Length Required</title></head>
<body bgcolor="white">
<center><h1>411 Length Required</h1></center>
<hr><center>nginx/1.2.3</center>
</body>
</html>

And the saveDoc callback was then called with no args. I think it should have been called either with that text or with a JSON parse error. This patch doesn't address this. Maybe non-2xx replies should throw? Different issue though.

@SlexAxton

Was having this same issue and this cleared it up. Just as a heads up or whatever . Thanks!

@SlexAxton

This needs to be Buffer.byteLength(options.data, 'utf8') or utf8 characters break the request length.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 6 additions and 4 deletions.
  1. +6 −4 lib/couchdb.js
View
10 lib/couchdb.js
@@ -143,6 +143,12 @@ exports.createClient = function(port, host, user, pass, maxListeners) {
// CouchDB 1.0 requires application/json for _bulk_docs and other POST APIs.
options.headers['Content-Type'] = options.headers['Content-Type'] || 'application/json';
+ // Add content length, eg for nginx proxy servers
+ if (options.data && typeof options.data != 'string') {
+ options.data = exports.toJSON(options.data);
+ }
+ options.headers['Content-Length'] = options.headers['Content-Length'] || (options.data) ? Buffer.byteLength(options.data, 'utf8') : 0;
+
var
request = httpClient.request(
options.method.toUpperCase(),
@@ -160,10 +166,6 @@ exports.createClient = function(port, host, user, pass, maxListeners) {
request.addListener('error', onError);
request.addListener('close', onClose);
- if (options.data && typeof options.data != 'string') {
- options.data = exports.toJSON(options.data);
- }
-
if (options.data) {
request.write(options.data, options.requestEncoding || 'utf8');
}
Something went wrong with that request. Please try again.