2018-12-18 04:34:02 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const utils = require('./utils');
|
2018-12-19 03:34:24 +08:00
|
|
|
const log = require('./log');
|
2018-12-18 04:34:02 +08:00
|
|
|
const url = require('url');
|
|
|
|
|
|
|
|
// this service provides abstraction over node's HTTP/HTTPS and electron net.client APIs
|
|
|
|
// this allows to support system proxy
|
|
|
|
|
|
|
|
function exec(opts) {
|
|
|
|
const client = getClient(opts);
|
2018-12-18 05:54:54 +08:00
|
|
|
const parsedTargetUrl = url.parse(opts.url);
|
2018-12-18 04:34:02 +08:00
|
|
|
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
2018-12-18 05:12:26 +08:00
|
|
|
const headers = {
|
|
|
|
Cookie: (opts.cookieJar && opts.cookieJar.header) || "",
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
};
|
|
|
|
|
|
|
|
if (opts.auth) {
|
|
|
|
const token = new Buffer(opts.auth.user + ":" + opts.auth.pass).toString('base64');
|
|
|
|
|
|
|
|
headers['Authorization'] = `Basic ${token}`;
|
|
|
|
}
|
|
|
|
|
2018-12-18 05:54:54 +08:00
|
|
|
let host = parsedTargetUrl.hostname;
|
|
|
|
let protocol = parsedTargetUrl.protocol;
|
|
|
|
let port = parsedTargetUrl.port;
|
|
|
|
let path = parsedTargetUrl.path;
|
|
|
|
|
|
|
|
if (opts.proxy) {
|
|
|
|
// see https://stackoverflow.com/questions/3862813/how-can-i-use-an-http-proxy-with-node-js-http-client
|
|
|
|
const parsedProxyUrl = url.parse(opts.proxy);
|
|
|
|
|
|
|
|
protocol = parsedProxyUrl.protocol;
|
|
|
|
host = parsedProxyUrl.hostname;
|
|
|
|
port = parsedProxyUrl.port;
|
|
|
|
path = opts.url;
|
|
|
|
|
|
|
|
headers['Host'] = parsedTargetUrl.host; // host also includes port
|
|
|
|
}
|
|
|
|
|
2018-12-18 04:34:02 +08:00
|
|
|
const request = client.request({
|
|
|
|
method: opts.method,
|
|
|
|
// url is used by electron net module
|
|
|
|
url: opts.url,
|
|
|
|
// 4 fields below are used by http and https node modules
|
2018-12-18 05:54:54 +08:00
|
|
|
protocol,
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
path,
|
2018-12-18 04:34:02 +08:00
|
|
|
timeout: opts.timeout,
|
2018-12-18 05:12:26 +08:00
|
|
|
headers
|
2018-12-18 04:34:02 +08:00
|
|
|
});
|
|
|
|
|
2018-12-20 04:29:35 +08:00
|
|
|
request.on('error', err => reject(generateError(opts, err)));
|
|
|
|
|
2018-12-18 04:34:02 +08:00
|
|
|
request.on('response', response => {
|
2018-12-19 03:39:56 +08:00
|
|
|
if (![200, 201, 204].includes(response.statusCode)) {
|
|
|
|
reject(generateError(opts, response.statusCode + ' ' + response.statusMessage));
|
|
|
|
}
|
|
|
|
|
2018-12-18 04:34:02 +08:00
|
|
|
if (opts.cookieJar && response.headers['set-cookie']) {
|
|
|
|
opts.cookieJar.header = response.headers['set-cookie'];
|
|
|
|
}
|
|
|
|
|
|
|
|
let responseStr = '';
|
|
|
|
|
|
|
|
response.on('data', chunk => responseStr += chunk);
|
|
|
|
|
|
|
|
response.on('end', () => {
|
|
|
|
try {
|
|
|
|
const jsonObj = responseStr.trim() ? JSON.parse(responseStr) : null;
|
|
|
|
|
|
|
|
resolve(jsonObj);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
log.error("Failed to deserialize sync response: " + responseStr);
|
|
|
|
|
2018-12-19 03:39:56 +08:00
|
|
|
reject(generateError(opts, e.message));
|
2018-12-18 04:34:02 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request.end(opts.body ? JSON.stringify(opts.body) : undefined);
|
|
|
|
}
|
|
|
|
catch (e) {
|
2018-12-19 03:39:56 +08:00
|
|
|
reject(generateError(opts, e.message));
|
2018-12-18 04:34:02 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function getClient(opts) {
|
2018-12-18 05:54:54 +08:00
|
|
|
// it's not clear how to explicitly configure proxy (as opposed to system proxy)
|
|
|
|
// so in that case we always use node's modules
|
|
|
|
if (utils.isElectron() && !opts.proxy) {
|
2018-12-18 04:34:02 +08:00
|
|
|
return require('electron').net;
|
|
|
|
}
|
|
|
|
else {
|
2018-12-18 05:54:54 +08:00
|
|
|
// in case there's explicit proxy then we need to use protocol of the proxy since we're actually
|
|
|
|
// connecting to the proxy server and not to the end-target server
|
|
|
|
const {protocol} = url.parse(opts.proxy || opts.url);
|
2018-12-18 04:34:02 +08:00
|
|
|
|
|
|
|
if (protocol === 'http:' || protocol === 'https:') {
|
|
|
|
return require(protocol.substr(0, protocol.length - 1));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error(`Unrecognized protocol "${protocol}"`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 03:39:56 +08:00
|
|
|
function generateError(opts, message) {
|
|
|
|
return new Error(`Request to ${opts.method} ${opts.url} failed, error: ${message}`);
|
2018-12-18 04:34:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
exec
|
|
|
|
};
|