mostlyserious/riak-js — pull request #155 by englercj (2013-02-05 17:30:34+00:00)
Title: Fix content length override

If you try to pass an http response as the `data` so that it will be piped into riak it doesn't work properly.

That is, doing the following does not work:

```js
var http = require('http');

http.get('http://myhost.com/something.mp3', function(res) {
riakDB.save(
'mp3s',
'mykey',
res,
{
contentType: res.headers['content-type'],
contentLength: res.headers['content-length'],
stream: true
}
);
});
```

It took me a while to see why but I finally found this in [`http-meta`][0]:

```js
if (this.data) {
if (this.data.length) length = String(this.data.length);
}

this._headers['Content-Length'] = length;
```

Since the response object didn't have a `.length` property the `'Content-Length'` would always be 0. Even when you passed `contentLength` explicitly in options, this would overwrite it.

This small change will only set the `content-length` header value if you do not pass in an explicit value.

I also noticed you were setting 'Content-Length' but the option mapped to 'content-length' so I changed that as well to prevent having 2 content-length's in the headers object.

[0]: https://github.com/mostlyserious/riak-js/blob/master/lib/http-meta.js#L102