Skip to content

Commit e433c11

Browse files
committed
First stab at a HTTPS client
1 parent 147c22b commit e433c11

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

lib/client.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,19 @@ utils.inherits(Client, events.EventEmitter);
3434
module.exports = Client;
3535

3636
/**
37-
* HTTP(s) client constructor
37+
* HTTP client constructor
3838
* @type HttpClient
3939
* @static
4040
*/
4141
Client.http = require('./client/http');
4242

43+
/**
44+
* HTTPS client constructor
45+
* @type HttpsClient
46+
* @static
47+
*/
48+
Client.https = require('./client/https');
49+
4350
/**
4451
* Fork client constructor
4552
* @type ForkClient

lib/client/http.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ HttpClient.prototype._request = function(request, callback) {
4545
"Accept": "application/json"
4646
};
4747

48+
// let user override the headers
4849
options.headers = utils.merge(headers, options.headers || {});
4950

50-
var req = http.request(options);
51+
var req = self._getStream(options);
5152

5253
req.on('response', function(res) {
5354
self.emit('http response', res);
@@ -83,3 +84,13 @@ HttpClient.prototype._request = function(request, callback) {
8384
req.end(body);
8485
});
8586
};
87+
88+
/**
89+
* Gets a stream interface to a http server
90+
* @param {Object} options An options object
91+
* @return {require('http').ClientRequest}
92+
* @api private
93+
*/
94+
HttpClient.prototype._getStream = function(options) {
95+
return http.request(options || {});
96+
};

lib/client/https.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var https = require('https');
2+
var utils = require('../utils');
3+
var HttpClient = require('./http');
4+
5+
/**
6+
* Constructor for a Jayson HTTPS Client
7+
* @class Jayson JSON-RPC HTTPS Client
8+
* @constructor
9+
* @extends HttpClient
10+
* @param {Object|String} [options] Optional hash of settings or a URL
11+
* @return {HttpsClient}
12+
* @api public
13+
*/
14+
var HttpsClient = function(options) {
15+
if(!(this instanceof HttpsClient)) return new HttpsClient(options);
16+
// just proxy to constructor for HttpClient
17+
HttpClient.call(this, options);
18+
};
19+
utils.inherits(HttpsClient, HttpClient);
20+
21+
module.exports = HttpsClient;
22+
23+
/**
24+
* Gets a stream interface to a https server
25+
* @param {Object} options An options object
26+
* @return {require('https').ClientRequest}
27+
* @api private
28+
*/
29+
HttpsClient.prototype._getStream = function(options) {
30+
return https.request(options || {});
31+
};

0 commit comments

Comments
 (0)