Skip to content

Commit

Permalink
fixed bug with host header refering to uri.host results in host not f…
Browse files Browse the repository at this point in the history
…ound issues. Adding authorization header for basic auth when communicating over a secure channel. Also using the new node (v0.3.6) http & https client APIs
  • Loading branch information
steelThread committed Jan 26, 2011
1 parent 81fef48 commit 90a0411
Showing 1 changed file with 35 additions and 46 deletions.
81 changes: 35 additions & 46 deletions lib/couch-client.js
@@ -1,52 +1,33 @@
/*global Buffer */

var Http = require('http'),
Url = require('url'),
var http = require('http'),
https = require('https'),
Url = require('url'),
EventEmitter = require('events').EventEmitter,
querystring = require('querystring');


var POOL_SIZE = 200; // Maximum number of concurrent connections allowed.
var MAX_DOCS = 1000; // The maximum number of docs to send in a single batch

// Handles changes made in node v0.3.0
var NOT_FOUND_ERR_NO = process.ENOENT ? process.ENOENT : require('constants').ENOENT;
var MAX_DOCS = 1000; // The maximum number of docs to send in a single batch

function noOp(err) { if (err) { throw err; } }

var pool = new Array(POOL_SIZE);
var poolIndex = 0;
function getServerFromPool(uri, callback) {
try {
poolIndex = (poolIndex + 1) % POOL_SIZE;
if (!pool[poolIndex]) {
pool[poolIndex] = Http.createClient(uri.port, uri.hostname);
}
process.nextTick(function () {
callback(null, pool[poolIndex]);
});
} catch (err) {
process.nextTick(function () {
callback(err);
});
}
}

var CONNECTION_DEFAULTS = {
host: '127.0.0.1:5984',
port: 5984,
hostname: '127.0.0.1',
pathname: "/"
};



function CouchClient(url) {
var uri = Url.parse(url);
uri.secure = uri.protocol == 'https:';
uri.protocolHandler = uri.secure ? https : http;
uri.__proto__ = CONNECTION_DEFAULTS;
var revCache = {};

// A simple wrapper around node's http client.
// A simple wrapper around node's http(s) request.
function request(method, path, body, callback) {
var stream;
// Body is optional
Expand All @@ -68,35 +49,43 @@ function CouchClient(url) {
}

var headers = {
"Host": uri.host
"Host": uri.hostname
};
// add the authorization header if provided and using https
if (uri.auth && uri.secure) {
headers["Authorization"] = "Basic " + new Buffer(uri.auth, "ascii").toString("base64");
}

if (body) {
body = JSON.stringify(body);
headers["Content-Length"] = Buffer.byteLength(body);
headers["Content-Type"] = "application/json";
}
getServerFromPool(uri, function (err, server) {
var request = server.request(method, path, headers);
if (body) {
request.write(body, 'utf8');
}
request.end();

request.on('response', function (response) {
response.setEncoding('utf8');
var body = "";
response.on('data', function (chunk) {
if (callback) { body += chunk; }
if (stream) { stream.emit('data', chunk); }
});
response.on('end', function () {
if (callback) { callback(null, JSON.parse(body)); }
if (stream) { stream.emit('end'); }
});
response.on('error', errorHandler);
var options = {
host: uri.hostname,
method: method,
path: path,
port: uri.port,
headers: headers
};
var request = uri.protocolHandler.request(options, function (response) {
response.setEncoding('utf8');
var body = "";
response.on('data', function (chunk) {
if (callback) { body += chunk; }
if (stream) { stream.emit('data', chunk); }
});
request.on('error', errorHandler);
response.on('end', function () {
if (callback) { callback(null, JSON.parse(body)); }
if (stream) { stream.emit('end'); }
});
response.on('error', errorHandler);
});
request.on('error', errorHandler);

if (body) { request.write(body, 'utf8'); }
request.end();

return stream;
}
Expand Down

0 comments on commit 90a0411

Please sign in to comment.