Skip to content

Commit

Permalink
NEW: Add support for HTTPS, close #111.
Browse files Browse the repository at this point in the history
Usage:
var client = solr.createClient({ secure : true});
  • Loading branch information
lbdremy committed Aug 16, 2014
1 parent bae5f28 commit 70886cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
29 changes: 23 additions & 6 deletions lib/solr.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

var http = require('http'),
https = require('https'),
Query = require('./query'),
querystring = require('querystring'),
format = require('./utils/format'),
Expand All @@ -33,18 +34,21 @@ exports.createClient = createClient;
* @param {Number|String} [port='8983'] - port of the Solr server
* @param {String} [core=''] - name of the Solr core requested
* @param {String} [path='/solr'] - root path of all requests
* @param {http.Agent} [agent] - HTTP Agent which is used for pooling sockets used in HTTP(s) client requests
* @param {Boolean} [secure=false] - if true HTTPS will be used instead of HTTP
*
* @return {Client}
* @api public
*/

function createClient(host, port, core, path, agent){
function createClient(host, port, core, path, agent, secure){
var options = (typeof host === 'object')? host : {
host : host,
port : port,
core : core,
path : path,
agent : agent
agent : agent,
secure : secure
};
return new Client(options);
}
Expand All @@ -69,7 +73,8 @@ function Client(options){
port : options.port || '8983',
core : options.core || '',
path : options.path || '/solr',
agent : options.agent
agent : options.agent,
secure : options.secure || false
};

// Default paths of all request handlers
Expand Down Expand Up @@ -212,8 +217,9 @@ Client.prototype.createAddStream = function(options){
if(this.options.authorization){
headers['authorization'] = this.options.authorization;
}
var protocol = this.options.secure ? 'https' : 'http';
var optionsRequest = {
url : 'http://' + this.options.host +':' + this.options.port + path ,
url : protocol + '://' + this.options.host +':' + this.options.port + path ,
method : 'POST',
headers : headers
};
Expand Down Expand Up @@ -583,6 +589,17 @@ Client.prototype.ping = function(callback){
return this.get(this.ADMIN_PING_HANDLER, callback);
}

/**
* Pick appropriate protocol based on the given `secure` flag
*
* @param {Boolean} secure -
* @return {Object} http or https module
*/

function pickProtocol(secure){
return secure ? https : http;
};

/**
* HTTP POST request. Send update commands to the Solr server (commit, add, delete, optimize)
*
Expand Down Expand Up @@ -622,7 +639,7 @@ function postJSON(params,callback){
options.agent = params.agent;
}

var request = http.request(options);
var request = pickProtocol(params.secure).request(options);

request.on('response', handleJSONResponse(request,callback));

Expand Down Expand Up @@ -675,7 +692,7 @@ function getJSON(params,callback){
options.headers = headers;
}

var request = http.get(options);
var request = pickProtocol(params.secure).get(options);

request.on('response', handleJSONResponse(request,callback));

Expand Down
11 changes: 10 additions & 1 deletion test/createClient-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,14 @@ describe('solr',function(){
assert.equal(client.options.core,'core1');
assert.equal(client.options.path,'/solr4');
});
})
});
describe('.createClient({ secure : true })', function(){
it('should create a `Client` instance with secure set to true', function(){
var options = {
secure : true
};
var client = solr.createClient(options);
assert.isTrue(client.options.secure);
});
});
});

0 comments on commit 70886cd

Please sign in to comment.