Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.idea/
24 changes: 23 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,34 @@ var https = require('https');
var Promise = require('bluebird');
var url = require('url');

module.exports = questor;
function initProxy(uri, options) {
if (!options.proxyHost && !options.proxyPort) {
return;
}

// Handle proxy for HTTP requests
options.hostname = options.proxyHost;
options.port = options.proxyPort;
options.headers['Host'] = options.hostname;
options.host = options.hostname + ':' + options.port;
options.path = uri;

// Proxy basic auth
if (options.proxyUsername && options.proxyPassword) {
var auth = 'Basic ' + new Buffer(options.proxyUsername + ':' + options.proxyPassword).toString('base64');
options.headers['Proxy-Authorization'] = auth;
}
};

function questor(uri, options) {
if (!options) { options = {}; }

options = defaults({}, options, url.parse(uri), {
headers: {}
});

initProxy(uri, options);

var requestBody = options.body ? new Buffer(options.body) : void 0;
var requestBodyLength = options.body ? requestBody.length : 0;
options.headers['content-length'] = requestBodyLength;
Expand Down Expand Up @@ -50,3 +70,5 @@ function questor(uri, options) {
request.end(requestBody);
});
}

module.exports = questor;