2017-01-31 07:50:28 +01:00
|
|
|
const Launcher = require('./launcher');
|
|
|
|
const Logger = require('../util/logger');
|
2015-12-12 09:53:50 +01:00
|
|
|
|
2017-01-31 07:50:28 +01:00
|
|
|
const logger = new Logger('transport');
|
2015-11-13 20:56:22 +01:00
|
|
|
|
2017-01-31 07:50:28 +01:00
|
|
|
const Transport = {
|
2015-11-13 20:56:22 +01:00
|
|
|
httpGet: function(config) {
|
2017-01-31 07:50:28 +01:00
|
|
|
let tmpFile;
|
|
|
|
const fs = Launcher.req('fs');
|
2015-11-13 20:56:22 +01:00
|
|
|
if (config.file) {
|
|
|
|
tmpFile = Launcher.getTempPath(config.file);
|
|
|
|
if (fs.existsSync(tmpFile)) {
|
|
|
|
try {
|
|
|
|
if (config.cache && fs.statSync(tmpFile).size > 0) {
|
2015-12-12 09:53:50 +01:00
|
|
|
logger.info('File already downloaded ' + config.url);
|
2015-11-13 20:56:22 +01:00
|
|
|
return config.success(tmpFile);
|
|
|
|
} else {
|
|
|
|
fs.unlinkSync(tmpFile);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2019-01-06 19:20:56 +01:00
|
|
|
fs.unlink(tmpFile, _.noop);
|
2015-11-13 20:56:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-31 07:50:28 +01:00
|
|
|
const proto = config.url.split(':')[0];
|
2015-12-12 09:53:50 +01:00
|
|
|
logger.info('GET ' + config.url);
|
2017-01-31 07:50:28 +01:00
|
|
|
const opts = Launcher.req('url').parse(config.url);
|
2015-11-14 17:58:34 +01:00
|
|
|
opts.headers = { 'User-Agent': navigator.userAgent };
|
2016-07-17 13:30:38 +02:00
|
|
|
Launcher.resolveProxy(config.url, proxy => {
|
2016-02-07 12:14:43 +01:00
|
|
|
logger.info('Request to ' + config.url + ' ' + (proxy ? 'using proxy ' + proxy.host + ':' + proxy.port : 'without proxy'));
|
|
|
|
if (proxy) {
|
|
|
|
opts.headers.Host = opts.host;
|
|
|
|
opts.host = proxy.host;
|
|
|
|
opts.port = proxy.port;
|
|
|
|
opts.path = config.url;
|
|
|
|
}
|
2016-07-17 13:30:38 +02:00
|
|
|
Launcher.req(proto).get(opts, res => {
|
2016-02-07 12:14:43 +01:00
|
|
|
logger.info('Response from ' + config.url + ': ' + res.statusCode);
|
|
|
|
if (res.statusCode === 200) {
|
|
|
|
if (config.file) {
|
2017-01-31 07:50:28 +01:00
|
|
|
const file = fs.createWriteStream(tmpFile);
|
2016-02-07 12:14:43 +01:00
|
|
|
res.pipe(file);
|
2016-07-17 13:30:38 +02:00
|
|
|
file.on('finish', () => {
|
|
|
|
file.close(() => {
|
2016-02-07 12:14:43 +01:00
|
|
|
config.success(tmpFile);
|
|
|
|
});
|
|
|
|
});
|
2016-07-17 13:30:38 +02:00
|
|
|
file.on('error', err => {
|
2016-02-07 12:14:43 +01:00
|
|
|
config.error(err);
|
|
|
|
});
|
|
|
|
} else {
|
2017-01-31 07:50:28 +01:00
|
|
|
let data = [];
|
2016-07-17 13:30:38 +02:00
|
|
|
res.on('data', chunk => {
|
2016-02-07 12:14:43 +01:00
|
|
|
data.push(chunk);
|
|
|
|
});
|
2016-07-17 13:30:38 +02:00
|
|
|
res.on('end', () => {
|
2016-02-07 12:14:43 +01:00
|
|
|
data = window.Buffer.concat(data);
|
|
|
|
if (config.utf8) {
|
|
|
|
data = data.toString('utf8');
|
|
|
|
}
|
|
|
|
config.success(data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if (res.headers.location && [301, 302].indexOf(res.statusCode) >= 0) {
|
|
|
|
if (config.noRedirect) {
|
|
|
|
return config.error('Too many redirects');
|
|
|
|
}
|
|
|
|
config.url = res.headers.location;
|
|
|
|
config.noRedirect = true;
|
|
|
|
Transport.httpGet(config);
|
2015-11-13 20:56:22 +01:00
|
|
|
} else {
|
2016-02-07 12:14:43 +01:00
|
|
|
config.error('HTTP status ' + res.statusCode);
|
2015-11-13 20:56:22 +01:00
|
|
|
}
|
2016-07-17 13:30:38 +02:00
|
|
|
}).on('error', e => {
|
2016-02-07 12:14:43 +01:00
|
|
|
logger.error('Cannot GET ' + config.url, e);
|
|
|
|
if (tmpFile) {
|
2019-01-06 19:20:56 +01:00
|
|
|
fs.unlink(tmpFile, _.noop);
|
2015-11-22 13:03:54 +01:00
|
|
|
}
|
2016-02-07 12:14:43 +01:00
|
|
|
config.error(e);
|
|
|
|
});
|
2015-11-13 20:56:22 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Transport;
|