don't use http.createClient; deprecated in 0.8.x

This commit is contained in:
freewil 2012-07-03 15:21:08 -04:00
parent 1a2947d9aa
commit c4be38158c
2 changed files with 26 additions and 28 deletions

View File

@ -14,8 +14,6 @@ var Client = function(port, host, user, password) {
this.password = password;
this.call = function(method, params, callback, errback, path) {
var client = http.createClient(port, host);
var clientRequestErrorCalled = false;
// First we encode the request into JSON
var requestJSON = JSON.stringify({
@ -24,32 +22,27 @@ var Client = function(port, host, user, password) {
'params': params
});
var headers = {};
if (user && password) {
var buff = new Buffer(this.user + ":" + this.password)
.toString('base64');
var auth = 'Basic ' + buff;
headers['Authorization'] = auth;
}
// Then we build some basic headers.
headers['Host'] = host;
headers['Content-Length'] = requestJSON.length;
// Now we'll make a request to the server
var request = client.request('POST', path || '/', headers);
request.write(requestJSON);
function clientRequestErrorHandler(e) {
if (!clientRequestErrorCalled) {
clientRequestErrorCalled = true;
errback(e);
// prepare request options
var requestOptions = {
host: host,
port: port,
method: 'POST',
path: path || '/',
headers: {
'Host': host,
'Content-Length': requestJSON.length
}
};
// use HTTP auth if user and password set
if (user && password) {
requestOptions.auth = this.user + ':' + this.password;
}
client.on('error', clientRequestErrorHandler);
request.on('error', clientRequestErrorHandler);
// Now we'll make a request to the server
var request = http.request(requestOptions);
request.on('error', errback);
request.on('response', function(response) {
// We need to buffer the response chunks in a nonblocking way.
@ -102,6 +95,8 @@ var Client = function(port, host, user, password) {
});
});
request.write(requestJSON);
request.end();
};
}

View File

@ -8,6 +8,9 @@
"rpc"
],
"author": "Bill Casarin <bill@casarin.ca> (jb55.com)",
"contributors": [
"Sean Lavine <sean@eternalrise.com>"
],
"dependencies": {
"underscore": "1.3.x"
},
@ -19,10 +22,10 @@
"vows": "0.6.x"
},
"engines": {
"node": ">=0.2.6"
"node": ">= 0.6.0 < 0.9.0"
},
"scripts": {
"test": "make test"
},
"optionalDependencies": {}
}
}