2016-09-21 00:03:26 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const networkRequest = require('request');
|
|
|
|
const {parse: urlParse, format: urlFormat} = require('url');
|
|
|
|
const db = require('./database');
|
|
|
|
const querystring = require('./querystring');
|
2016-09-22 19:44:28 +00:00
|
|
|
var util = require('./util.js');
|
2016-09-21 00:03:26 +00:00
|
|
|
const {DEBOUNCE_MILLIS, STATUS_CODE_PEBKAC} = require('./constants');
|
|
|
|
const {jarFromCookies, cookiesFromJar} = require('./cookies');
|
|
|
|
const {setDefaultProtocol} = require('./util');
|
|
|
|
const {getRenderedRequest} = require('./render');
|
2016-04-09 21:08:55 +00:00
|
|
|
|
2016-09-13 18:31:07 +00:00
|
|
|
let cancelRequestFunction = null;
|
|
|
|
|
2016-09-21 00:03:26 +00:00
|
|
|
module.exports.cancelCurrentRequest = () => {
|
2016-09-13 18:31:07 +00:00
|
|
|
if (typeof cancelRequestFunction === 'function') {
|
|
|
|
cancelRequestFunction();
|
|
|
|
}
|
2016-09-21 00:03:26 +00:00
|
|
|
};
|
2016-07-28 20:10:26 +00:00
|
|
|
|
2016-09-21 00:03:26 +00:00
|
|
|
module.exports._buildRequestConfig = (renderedRequest, patch = {}) => {
|
2016-04-09 21:08:55 +00:00
|
|
|
const config = {
|
2016-08-31 20:50:27 +00:00
|
|
|
method: renderedRequest.method,
|
|
|
|
body: renderedRequest.body,
|
2016-04-28 07:30:26 +00:00
|
|
|
headers: {},
|
|
|
|
|
|
|
|
// Setup redirect rules
|
2016-07-28 20:10:26 +00:00
|
|
|
followAllRedirects: true,
|
|
|
|
maxRedirects: 20,
|
2016-07-29 00:24:05 +00:00
|
|
|
timeout: 0,
|
2016-04-28 07:30:26 +00:00
|
|
|
|
|
|
|
// Unzip gzipped responses
|
2016-07-22 21:35:49 +00:00
|
|
|
gzip: true,
|
|
|
|
|
|
|
|
// Time the request
|
2016-07-28 20:10:26 +00:00
|
|
|
time: true,
|
|
|
|
|
|
|
|
// SSL Checking
|
2016-08-15 17:04:36 +00:00
|
|
|
rejectUnauthorized: true,
|
|
|
|
|
|
|
|
// Proxy
|
|
|
|
proxy: null
|
2016-04-09 21:08:55 +00:00
|
|
|
};
|
|
|
|
|
2016-07-19 04:01:31 +00:00
|
|
|
// Set the URL, including the query parameters
|
2016-08-31 20:50:27 +00:00
|
|
|
const qs = querystring.buildFromParams(renderedRequest.parameters);
|
2016-08-31 20:52:48 +00:00
|
|
|
const url = querystring.joinURL(renderedRequest.url, qs);
|
2016-09-22 19:44:28 +00:00
|
|
|
config.url = util.prepareUrlForSending(url);
|
|
|
|
config.headers.host = urlParse(config.url).host;
|
2016-08-31 20:50:27 +00:00
|
|
|
|
|
|
|
for (let i = 0; i < renderedRequest.headers.length; i++) {
|
|
|
|
let header = renderedRequest.headers[i];
|
2016-04-12 00:39:49 +00:00
|
|
|
if (header.name) {
|
|
|
|
config.headers[header.name] = header.value;
|
|
|
|
}
|
2016-04-09 21:08:55 +00:00
|
|
|
}
|
2016-04-17 01:52:10 +00:00
|
|
|
|
2016-04-28 07:30:26 +00:00
|
|
|
return Object.assign(config, patch);
|
2016-09-21 00:03:26 +00:00
|
|
|
};
|
2016-04-28 07:30:26 +00:00
|
|
|
|
2016-09-21 00:03:26 +00:00
|
|
|
module.exports._actuallySend = (renderedRequest, settings) => {
|
2016-07-20 21:15:11 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2016-08-15 17:04:36 +00:00
|
|
|
const cookieJar = renderedRequest.cookieJar;
|
|
|
|
const jar = jarFromCookies(cookieJar.cookies);
|
|
|
|
|
|
|
|
// Detect and set the proxy based on the request protocol
|
|
|
|
// NOTE: request does not have a separate settings for http/https proxies
|
|
|
|
const {protocol} = urlParse(renderedRequest.url);
|
2016-09-22 19:44:28 +00:00
|
|
|
const {httpProxy, httpsProxy} = settings;
|
|
|
|
const proxyHost = protocol === 'https:' ? httpsProxy : httpProxy;
|
2016-09-12 20:04:15 +00:00
|
|
|
const proxy = proxyHost ? setDefaultProtocol(proxyHost) : null;
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2016-09-21 00:03:26 +00:00
|
|
|
let config = module.exports._buildRequestConfig(renderedRequest, {
|
2016-08-15 17:04:36 +00:00
|
|
|
jar: jar,
|
|
|
|
proxy: proxy,
|
2016-07-28 20:10:26 +00:00
|
|
|
followAllRedirects: settings.followRedirects,
|
|
|
|
timeout: settings.timeout > 0 ? settings.timeout : null,
|
|
|
|
rejectUnauthorized: settings.validateSSL
|
2016-07-20 21:15:11 +00:00
|
|
|
}, true);
|
|
|
|
|
|
|
|
const startTime = Date.now();
|
2016-08-15 17:04:36 +00:00
|
|
|
// TODO: Handle redirects ourselves
|
2016-09-13 18:31:07 +00:00
|
|
|
const req = networkRequest(config, function (err, networkResponse) {
|
2016-07-20 21:15:11 +00:00
|
|
|
if (err) {
|
2016-09-21 20:49:54 +00:00
|
|
|
db.response.create({
|
2016-08-15 17:04:36 +00:00
|
|
|
parentId: renderedRequest._id,
|
2016-07-22 20:38:28 +00:00
|
|
|
elapsedTime: Date.now() - startTime,
|
2016-07-20 21:15:11 +00:00
|
|
|
error: err.toString()
|
|
|
|
});
|
|
|
|
return reject(err);
|
|
|
|
}
|
2016-07-22 20:38:28 +00:00
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
// TODO: Add image support to Insomnia
|
|
|
|
const contentType = networkResponse.headers['content-type'];
|
|
|
|
if (contentType && contentType.toLowerCase().indexOf('image/') === 0) {
|
|
|
|
const err = new Error(`Content-Type ${contentType} not supported`);
|
|
|
|
|
2016-09-21 20:49:54 +00:00
|
|
|
db.response.create({
|
2016-08-15 17:04:36 +00:00
|
|
|
parentId: renderedRequest._id,
|
|
|
|
elapsedTime: Date.now() - startTime,
|
|
|
|
error: err.toString(),
|
|
|
|
statusMessage: 'UNSUPPORTED'
|
|
|
|
});
|
|
|
|
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the cookie jar
|
|
|
|
cookiesFromJar(jar).then(cookies => {
|
2016-09-21 20:49:54 +00:00
|
|
|
db.cookieJar.update(cookieJar, {cookies});
|
2016-08-15 17:04:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Format the headers into Insomnia format
|
|
|
|
// TODO: Move this to a better place
|
|
|
|
const headers = [];
|
|
|
|
for (const name of Object.keys(networkResponse.headers)) {
|
|
|
|
const tmp = networkResponse.headers[name];
|
|
|
|
const values = Array.isArray(tmp) ? tmp : [tmp];
|
|
|
|
for (const value of values) {
|
|
|
|
headers.push({name, value});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-20 21:15:11 +00:00
|
|
|
const responsePatch = {
|
2016-08-15 17:04:36 +00:00
|
|
|
parentId: renderedRequest._id,
|
2016-07-22 20:38:28 +00:00
|
|
|
statusCode: networkResponse.statusCode,
|
|
|
|
statusMessage: networkResponse.statusMessage,
|
|
|
|
contentType: networkResponse.headers['content-type'],
|
|
|
|
url: config.url, // TODO: Handle redirects somehow
|
|
|
|
elapsedTime: networkResponse.elapsedTime,
|
2016-07-22 21:35:49 +00:00
|
|
|
bytesRead: networkResponse.connection.bytesRead,
|
2016-07-22 20:38:28 +00:00
|
|
|
body: networkResponse.body,
|
2016-08-15 17:04:36 +00:00
|
|
|
headers: headers
|
2016-07-20 21:15:11 +00:00
|
|
|
};
|
2016-04-28 07:30:26 +00:00
|
|
|
|
2016-09-21 20:49:54 +00:00
|
|
|
db.response.create(responsePatch).then(resolve, reject);
|
2016-09-13 18:31:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Kind of hacky, but this is how we cancel a request.
|
|
|
|
cancelRequestFunction = () => {
|
|
|
|
req.abort();
|
|
|
|
|
2016-09-21 20:49:54 +00:00
|
|
|
db.response.create({
|
2016-09-13 18:31:07 +00:00
|
|
|
parentId: renderedRequest._id,
|
|
|
|
elapsedTime: Date.now() - startTime,
|
|
|
|
statusMessage: 'Cancelled',
|
|
|
|
error: 'The request was cancelled'
|
|
|
|
});
|
|
|
|
|
|
|
|
return reject('Cancelled');
|
|
|
|
}
|
2016-07-20 21:15:11 +00:00
|
|
|
})
|
2016-09-21 00:03:26 +00:00
|
|
|
};
|
2016-04-17 01:52:10 +00:00
|
|
|
|
2016-09-21 00:03:26 +00:00
|
|
|
module.exports.send = requestId => {
|
2016-07-20 21:15:11 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
2016-09-08 06:54:35 +00:00
|
|
|
// First, lets wait for all debounces to finish
|
|
|
|
setTimeout(() => {
|
|
|
|
Promise.all([
|
2016-09-21 20:49:54 +00:00
|
|
|
db.request.getById(requestId),
|
|
|
|
db.settings.getOrCreate()
|
2016-09-08 06:54:35 +00:00
|
|
|
]).then(([request, settings]) => {
|
|
|
|
getRenderedRequest(request).then(renderedRequest => {
|
2016-09-21 00:03:26 +00:00
|
|
|
module.exports._actuallySend(renderedRequest, settings).then(resolve, reject);
|
2016-09-08 06:54:35 +00:00
|
|
|
}, err => {
|
2016-09-21 20:49:54 +00:00
|
|
|
db.response.create({
|
2016-09-08 06:54:35 +00:00
|
|
|
parentId: request._id,
|
|
|
|
statusCode: STATUS_CODE_PEBKAC,
|
|
|
|
error: err.message
|
|
|
|
}).then(resolve, reject);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}, DEBOUNCE_MILLIS);
|
|
|
|
})
|
2016-09-21 00:03:26 +00:00
|
|
|
};
|