Skip to content

Commit 0fb24ff

Browse files
committed
Added HTTPS support.
1 parent 5b3443b commit 0fb24ff

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

lib/util/request.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
var http = require('http');
3+
var https = require('https');
34
var parseUrl = require('url').parse;
45
var once = require('./once');
56
var joinPath = require('path').join;
@@ -16,12 +17,16 @@ function rawCopy(obj) {
1617
}
1718

1819
module.exports = function (baseUrl, agent, agentOptions) {
19-
if (!agent && http.Agent) agent = new http.Agent(agentOptions); // server only
20+
var baseUrlParts = rawCopy(parseUrl(baseUrl));
21+
var isSsl = baseUrlParts.protocol === 'https:';
22+
23+
if (!agent) {
24+
agent = new (isSsl ? https : http).Agent(agentOptions);
25+
}
2026

2127
var queue = new LinkedList();
2228
var maxTasks = typeof agent.maxSockets === 'number' ? agent.maxSockets * 2 : Infinity;
2329
var activeTasks = 0;
24-
var baseUrlParts = rawCopy(parseUrl(baseUrl));
2530

2631
function drainQueue() {
2732
if (!queue.length || activeTasks >= maxTasks) return;
@@ -52,7 +57,7 @@ module.exports = function (baseUrl, agent, agentOptions) {
5257
next();
5358
cb.apply(this, arguments);
5459
});
55-
var req = http.request(options, function (res) {
60+
var req = (isSsl ? https : http).request(options, function (res) {
5661
var data = [];
5762
res.on('data', function (b) {
5863
data.push(b);

src/util/request.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
var http = require('http');
3+
var https = require('https');
34
var parseUrl = require('url').parse;
45
var once = require('./once');
56
var joinPath = require('path').join;
@@ -16,12 +17,16 @@ function rawCopy(obj) {
1617
}
1718

1819
module.exports = function (baseUrl, agent, agentOptions) {
19-
if (!agent && http.Agent) agent = new http.Agent(agentOptions); // server only
20+
var baseUrlParts = rawCopy(parseUrl(baseUrl));
21+
var isSsl = baseUrlParts.protocol === 'https:';
22+
23+
if (!agent) {
24+
agent = new (isSsl ? https : http).Agent(agentOptions);
25+
}
2026

2127
var queue = new LinkedList();
2228
var maxTasks = typeof agent.maxSockets === 'number' ? agent.maxSockets * 2 : Infinity;
2329
var activeTasks = 0;
24-
var baseUrlParts = rawCopy(parseUrl(baseUrl));
2530

2631
function drainQueue() {
2732
if (!queue.length || activeTasks >= maxTasks) return;
@@ -51,7 +56,7 @@ module.exports = function (baseUrl, agent, agentOptions) {
5156
next();
5257
cb.apply(this, arguments);
5358
});
54-
var req = http.request(options, function (res) {
59+
var req = (isSsl ? https : http).request(options, function (res) {
5560
var data = [];
5661
res.on('data', function (b) {
5762
data.push(b);

0 commit comments

Comments
 (0)