2016-10-02 20:57:00 +00:00
|
|
|
import networkRequest from 'request';
|
|
|
|
import {parse as urlParse} from 'url';
|
|
|
|
import * as db from './database';
|
|
|
|
import * as querystring from './querystring';
|
|
|
|
import * as util from './util.js';
|
|
|
|
import {DEBOUNCE_MILLIS, STATUS_CODE_PEBKAC} from './constants';
|
|
|
|
import {jarFromCookies, cookiesFromJar} from './cookies';
|
|
|
|
import {setDefaultProtocol} from './util';
|
|
|
|
import {getRenderedRequest} from './render';
|
|
|
|
import {swapHost} from './dns';
|
2016-10-05 04:43:48 +00:00
|
|
|
import {cookieHeaderValueForUri} from './cookies';
|
2016-04-09 21:08:55 +00:00
|
|
|
|
2016-09-13 18:31:07 +00:00
|
|
|
let cancelRequestFunction = null;
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function cancelCurrentRequest () {
|
2016-09-13 18:31:07 +00:00
|
|
|
if (typeof cancelRequestFunction === 'function') {
|
|
|
|
cancelRequestFunction();
|
|
|
|
}
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-07-28 20:10:26 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function _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-10-02 20:57:00 +00:00
|
|
|
}
|
2016-04-28 07:30:26 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function _actuallySend (renderedRequest, settings) {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
2016-08-15 17:04:36 +00:00
|
|
|
// 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-10-02 20:57:00 +00:00
|
|
|
const config = _buildRequestConfig(renderedRequest, {
|
2016-10-05 04:43:48 +00:00
|
|
|
jar: null, // We're doing our own cookies
|
2016-08-15 17:04:36 +00:00
|
|
|
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);
|
|
|
|
|
2016-10-05 04:43:48 +00:00
|
|
|
// Add the cookie header to the request
|
|
|
|
const cookieJar = renderedRequest.cookieJar;
|
|
|
|
const jar = jarFromCookies(cookieJar.cookies);
|
|
|
|
const existingCookieHeaderName = Object.keys(config.headers).find(k => k.toLowerCase() === 'cookie');
|
|
|
|
const cookieString = await cookieHeaderValueForUri(jar, config.url);
|
2016-10-05 05:10:48 +00:00
|
|
|
|
2016-10-05 04:43:48 +00:00
|
|
|
if (cookieString && existingCookieHeaderName) {
|
|
|
|
config.headers[existingCookieHeaderName] += `; ${cookieString}`;
|
|
|
|
} else if (cookieString) {
|
2016-10-05 05:10:48 +00:00
|
|
|
config.headers['Cookie'] = cookieString;
|
2016-10-05 04:43:48 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
// Do DNS lookup ourselves
|
|
|
|
// We don't want to let NodeJS do DNS, because it doesn't use
|
|
|
|
// getaddrinfo by default. Instead, it first tries to reach out
|
|
|
|
// to the network.
|
2016-10-05 04:43:48 +00:00
|
|
|
const originalUrl = config.url;
|
2016-10-02 20:57:00 +00:00
|
|
|
config.url = await swapHost(config.url);
|
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
// TODO: Handle redirects ourselves
|
2016-10-02 22:10:43 +00:00
|
|
|
const requestStartTime = Date.now();
|
2016-10-02 20:57:00 +00:00
|
|
|
const req = networkRequest(config, async (err, networkResponse) => {
|
2016-07-20 21:15:11 +00:00
|
|
|
if (err) {
|
2016-09-28 21:17:57 +00:00
|
|
|
const isShittyParseError = err.toString() === 'Error: Parse Error';
|
|
|
|
|
|
|
|
let message = err.toString();
|
|
|
|
if (isShittyParseError) {
|
|
|
|
message = 'Could not parse malformed response.'
|
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
await db.response.create({
|
2016-08-15 17:04:36 +00:00
|
|
|
parentId: renderedRequest._id,
|
2016-09-28 21:17:57 +00:00
|
|
|
error: message
|
2016-07-20 21:15:11 +00:00
|
|
|
});
|
2016-09-28 21:17:57 +00:00
|
|
|
|
2016-07-20 21:15:11 +00:00
|
|
|
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-10-02 20:57:00 +00:00
|
|
|
await db.response.create({
|
2016-08-15 17:04:36 +00:00
|
|
|
parentId: renderedRequest._id,
|
|
|
|
error: err.toString(),
|
|
|
|
statusMessage: 'UNSUPPORTED'
|
|
|
|
});
|
|
|
|
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-10-05 04:43:48 +00:00
|
|
|
// Update the cookie jar
|
|
|
|
// NOTE: Since we're doing own DNS, we can't rely on Request to do this
|
|
|
|
for (const h of util.getSetCookieHeaders(headers)) {
|
2016-10-05 16:34:13 +00:00
|
|
|
try {
|
|
|
|
jar.setCookieSync(h.value, originalUrl);
|
|
|
|
} catch (e) {
|
|
|
|
console.warn('Failed to parse set-cookie', h.value);
|
|
|
|
}
|
2016-10-05 04:43:48 +00:00
|
|
|
}
|
|
|
|
const cookies = await cookiesFromJar(jar);
|
|
|
|
await db.cookieJar.update(cookieJar, {cookies});
|
|
|
|
|
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'],
|
2016-10-05 04:43:48 +00:00
|
|
|
url: originalUrl, // TODO: Handle redirects somehow
|
2016-07-22 20:38:28 +00:00
|
|
|
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.
|
2016-10-02 20:57:00 +00:00
|
|
|
cancelRequestFunction = async () => {
|
2016-09-13 18:31:07 +00:00
|
|
|
req.abort();
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
await db.response.create({
|
2016-09-13 18:31:07 +00:00
|
|
|
parentId: renderedRequest._id,
|
2016-10-02 20:57:00 +00:00
|
|
|
elapsedTime: Date.now() - requestStartTime,
|
2016-09-13 18:31:07 +00:00
|
|
|
statusMessage: 'Cancelled',
|
|
|
|
error: 'The request was cancelled'
|
|
|
|
});
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
return reject(new Error('Cancelled'));
|
2016-09-13 18:31:07 +00:00
|
|
|
}
|
2016-07-20 21:15:11 +00:00
|
|
|
})
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function send (requestId) {
|
|
|
|
// First, lets wait for all debounces to finish
|
|
|
|
await util.delay(DEBOUNCE_MILLIS);
|
|
|
|
|
|
|
|
const request = await db.request.getById(requestId);
|
|
|
|
const settings = await db.settings.getOrCreate();
|
|
|
|
|
|
|
|
let renderedRequest;
|
|
|
|
|
|
|
|
try {
|
|
|
|
renderedRequest = await getRenderedRequest(request);
|
|
|
|
} catch (e) {
|
|
|
|
// Failed to render. Must be the user's fault
|
|
|
|
return await db.response.create({
|
|
|
|
parentId: request._id,
|
|
|
|
statusCode: STATUS_CODE_PEBKAC,
|
|
|
|
error: e.message
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render succeeded so we're good to go!
|
|
|
|
return await _actuallySend(renderedRequest, settings);
|
|
|
|
}
|