2016-07-16 02:06:10 +00:00
|
|
|
import networkRequest from 'request';
|
2016-07-06 20:18:26 +00:00
|
|
|
|
2016-07-16 02:06:10 +00:00
|
|
|
import * as db from '../database';
|
|
|
|
import * as querystring from './querystring';
|
|
|
|
import {DEBOUNCE_MILLIS} from './constants';
|
2016-07-20 21:15:11 +00:00
|
|
|
import {STATUS_CODE_PEBKAC} from './constants';
|
2016-07-20 23:16:28 +00:00
|
|
|
import {getRenderedRequest} from './render';
|
2016-04-09 21:08:55 +00:00
|
|
|
|
2016-07-28 20:10:26 +00:00
|
|
|
const cookieJar = networkRequest.jar();
|
|
|
|
|
2016-07-07 22:06:18 +00:00
|
|
|
function buildRequestConfig (request, patch = {}) {
|
2016-04-09 21:08:55 +00:00
|
|
|
const config = {
|
2016-04-10 02:58:48 +00:00
|
|
|
method: request.method,
|
|
|
|
body: request.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-19 19:13:51 +00:00
|
|
|
timeout: -1,
|
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
|
|
|
|
rejectUnauthorized: true
|
2016-04-09 21:08:55 +00:00
|
|
|
};
|
|
|
|
|
2016-07-19 04:01:31 +00:00
|
|
|
// Set the URL, including the query parameters
|
|
|
|
const qs = querystring.buildFromParams(request.parameters);
|
2016-07-15 03:50:59 +00:00
|
|
|
config.url = querystring.joinURL(request.url, qs);
|
|
|
|
|
2016-04-17 04:28:57 +00:00
|
|
|
// Set basic auth if we need to
|
2016-04-09 21:08:55 +00:00
|
|
|
if (request.authentication.username) {
|
|
|
|
config.auth = {
|
|
|
|
user: request.authentication.username,
|
|
|
|
pass: request.authentication.password
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-10 02:58:48 +00:00
|
|
|
for (let i = 0; i < request.headers.length; i++) {
|
|
|
|
let header = request.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-07-20 21:15:11 +00:00
|
|
|
function actuallySend (request, settings) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
let config = buildRequestConfig(request, {
|
2016-07-28 20:10:26 +00:00
|
|
|
jar: cookieJar,
|
|
|
|
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-07-22 20:38:28 +00:00
|
|
|
networkRequest(config, function (err, networkResponse) {
|
2016-07-20 21:15:11 +00:00
|
|
|
if (err) {
|
|
|
|
db.responseCreate({
|
|
|
|
parentId: request._id,
|
2016-07-22 20:38:28 +00:00
|
|
|
elapsedTime: Date.now() - startTime,
|
2016-07-20 21:15:11 +00:00
|
|
|
error: err.toString()
|
|
|
|
});
|
|
|
|
console.warn(`Request to ${config.url} failed`, err);
|
|
|
|
return reject(err);
|
|
|
|
}
|
2016-07-22 20:38:28 +00:00
|
|
|
|
2016-07-20 21:15:11 +00:00
|
|
|
const responsePatch = {
|
2016-04-26 07:29:24 +00:00
|
|
|
parentId: request._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,
|
|
|
|
headers: Object.keys(networkResponse.headers).map(name => {
|
|
|
|
const value = networkResponse.headers[name];
|
2016-04-10 06:37:22 +00:00
|
|
|
return {name, value};
|
|
|
|
})
|
2016-07-20 21:15:11 +00:00
|
|
|
};
|
2016-04-28 07:30:26 +00:00
|
|
|
|
2016-07-20 21:15:11 +00:00
|
|
|
db.responseCreate(responsePatch).then(resolve, reject);
|
|
|
|
})
|
|
|
|
})
|
2016-04-09 21:08:55 +00:00
|
|
|
}
|
2016-04-17 01:52:10 +00:00
|
|
|
|
2016-07-20 21:15:11 +00:00
|
|
|
export function send (requestId) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
// First, lets wait for all debounces to finish
|
|
|
|
setTimeout(() => {
|
|
|
|
Promise.all([
|
|
|
|
db.requestGetById(requestId),
|
|
|
|
db.settingsGet()
|
|
|
|
]).then(([
|
|
|
|
request,
|
|
|
|
settings
|
|
|
|
]) => {
|
2016-07-20 23:16:28 +00:00
|
|
|
getRenderedRequest(request).then(renderedRequest => {
|
|
|
|
actuallySend(renderedRequest, settings).then(resolve, reject);
|
|
|
|
}, err => {
|
|
|
|
db.responseCreate({
|
|
|
|
parentId: request._id,
|
|
|
|
statusCode: STATUS_CODE_PEBKAC,
|
|
|
|
error: err.message
|
|
|
|
}).then(resolve, reject);
|
2016-07-20 21:15:11 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
}, DEBOUNCE_MILLIS);
|
|
|
|
});
|
2016-04-17 01:52:10 +00:00
|
|
|
}
|