insomnia/app/backend/network.js

187 lines
5.6 KiB
JavaScript
Raw Normal View History

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-04-09 21:08:55 +00:00
2016-09-13 18:31:07 +00:00
let cancelRequestFunction = null;
export function cancelCurrentRequest () {
2016-09-13 18:31:07 +00:00
if (typeof cancelRequestFunction === 'function') {
cancelRequestFunction();
}
}
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
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
gzip: true,
// Time the request
time: true,
// SSL Checking
rejectUnauthorized: true,
// Proxy
proxy: null
2016-04-09 21:08:55 +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-28 07:30:26 +00:00
return Object.assign(config, patch);
}
2016-04-28 07:30:26 +00:00
export function _actuallySend (renderedRequest, settings) {
return new Promise(async (resolve, reject) => {
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;
const proxy = proxyHost ? setDefaultProtocol(proxyHost) : null;
const config = _buildRequestConfig(renderedRequest, {
jar: jar,
proxy: proxy,
followAllRedirects: settings.followRedirects,
timeout: settings.timeout > 0 ? settings.timeout : null,
rejectUnauthorized: settings.validateSSL
2016-07-20 21:15:11 +00:00
}, true);
// 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.
config.url = await swapHost(config.url);
// TODO: Handle redirects ourselves
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.'
}
await db.response.create({
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);
}
// 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`);
await db.response.create({
parentId: renderedRequest._id,
error: err.toString(),
statusMessage: 'UNSUPPORTED'
});
return reject(err);
}
// Update the cookie jar
const cookies = await cookiesFromJar(jar);
db.cookieJar.update(cookieJar, {cookies});
// 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 = {
parentId: renderedRequest._id,
statusCode: networkResponse.statusCode,
statusMessage: networkResponse.statusMessage,
contentType: networkResponse.headers['content-type'],
url: config.url, // TODO: Handle redirects somehow
elapsedTime: networkResponse.elapsedTime,
bytesRead: networkResponse.connection.bytesRead,
body: networkResponse.body,
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 = async () => {
2016-09-13 18:31:07 +00:00
req.abort();
await db.response.create({
2016-09-13 18:31:07 +00:00
parentId: renderedRequest._id,
elapsedTime: Date.now() - requestStartTime,
2016-09-13 18:31:07 +00:00
statusMessage: 'Cancelled',
error: 'The request was cancelled'
});
return reject(new Error('Cancelled'));
2016-09-13 18:31:07 +00:00
}
2016-07-20 21:15:11 +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);
}