Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions src/jsonrpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ Endpoint.prototype.expose = function(name, func, scope)
*/
Endpoint.prototype.handleCall = function handleCall(decoded, conn, callback)
{
var paramLog = Array.isArray(decoded.params) ? decoded.params.join(', ') : JSON.stringify(decoded.params);

Endpoint.trace('<--', 'Request (id ' + decoded.id + '): ' +
decoded.method + '(' + decoded.params.join(', ') + ')');
decoded.method + '(' + paramLog + ')');

if (!this.functions.hasOwnProperty(decoded.method)) {
callback(new Error("Unknown RPC call '"+decoded.method+"'"));
Expand Down Expand Up @@ -121,7 +123,6 @@ Client.prototype.connectHttp = function connectHttp(method, params, opts, callba
}
opts = opts || {};

var client = http.createClient(this.port, this.host);

var id = 1;

Expand All @@ -133,12 +134,6 @@ Client.prototype.connectHttp = function connectHttp(method, params, opts, callba
'jsonrpc': '2.0'
});

// Report errors from the http client. This also prevents crashes since
// an exception is thrown if we don't handle this event.
client.on('error', function(err) {
callback(err);
});

var headers = {};

if (this.user && this.password) {
Expand All @@ -150,11 +145,20 @@ Client.prototype.connectHttp = function connectHttp(method, params, opts, callba
// Then we build some basic headers.
headers['Host'] = this.host;
headers['Content-Length'] = Buffer.byteLength(requestJSON, 'utf8');

var client = http.request({
'port': this.port,
'host': this.host,

// Now we'll make a request to the server
var request = client.request('POST', opts.path || '/', headers);
request.write(requestJSON);
request.on('response', callback.bind(this, id, request));
'method': 'POST',
'path': opts.path || '/',
'headers' : headers
}, callback.bind(this, id, client));
client.write(requestJSON);

client.on('error', function(err) {
callback(err);
});
};

/**
Expand Down Expand Up @@ -379,7 +383,11 @@ Server.prototype.handleHttp = function(req, res)
}

var handle = function (buf) {
var decoded = JSON.parse(buf);
try{
var decoded = JSON.parse(buf);
}catch(e){
var decoded = {};
}

// Check for the required fields, and if they aren't there, then
// dispatch to the handleHttpError function.
Expand Down