insomnia/app/common/network.js

332 lines
11 KiB
JavaScript
Raw Normal View History

import networkRequest from 'request';
import {parse as urlParse} from 'url';
import mime from 'mime-types';
import {basename as pathBasename} from 'path';
2016-11-10 05:56:23 +00:00
import * as models from '../models';
import * as querystring from './querystring';
import {buildFromParams} from './querystring';
2016-11-10 05:56:23 +00:00
import * as util from './misc.js';
import {DEBOUNCE_MILLIS, STATUS_CODE_RENDER_FAILED, CONTENT_TYPE_FORM_DATA, CONTENT_TYPE_FORM_URLENCODED, getAppVersion} from './constants';
import {jarFromCookies, cookiesFromJar} from './cookies';
import {setDefaultProtocol, hasAcceptHeader, hasUserAgentHeader} from './misc';
import {getRenderedRequest} from './render';
2016-11-22 22:26:52 +00:00
import * as fs from 'fs';
import * as db from './database';
2016-04-09 21:08:55 +00:00
2016-12-30 23:06:27 +00:00
// Defined fallback strategies for DNS lookup. By default, request uses Node's
// default dns.resolve which uses c-ares to do lookups. This doesn't work for
// some people, so we also fallback to IPv6 then IPv4 to force it to use
// getaddrinfo (OS lookup) instead of c-ares (external lookup).
2016-12-23 19:31:38 +00:00
const FAMILY_FALLBACKS = [
2016-12-30 23:06:27 +00:00
null, // Use the request library default lookup
2016-12-23 19:31:38 +00:00
6, // IPv6
4, // IPv4
];
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-04-28 07:30:26 +00:00
// Setup redirect rules
followAllRedirects: true,
2016-10-11 17:40:17 +00:00
followRedirect: true,
maxRedirects: 50, // Arbitrary (large) number
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
2016-10-10 18:21:26 +00:00
proxy: null,
// Use keep-alive by default
forever: true,
2016-10-26 23:22:15 +00:00
// Force request to return response body as a Buffer instead of string
encoding: null,
2016-04-09 21:08:55 +00:00
};
// Set the body
if (renderedRequest.body.mimeType === CONTENT_TYPE_FORM_URLENCODED) {
config.body = buildFromParams(renderedRequest.body.params || [], true);
} else if (renderedRequest.body.mimeType === CONTENT_TYPE_FORM_DATA) {
const formData = {};
for (const param of renderedRequest.body.params) {
if (param.type === 'file' && param.fileName) {
formData[param.name] = {
value: fs.readFileSync(param.fileName),
options: {
2016-12-30 23:06:27 +00:00
filename: pathBasename(param.fileName),
contentType: mime.lookup(param.fileName) // Guess the mime-type
}
}
} else {
formData[param.name] = param.value || '';
}
}
config.formData = formData;
2016-11-23 23:57:34 +00:00
} else if (renderedRequest.body.fileName) {
2016-11-22 22:26:52 +00:00
config.body = fs.readFileSync(renderedRequest.body.fileName);
} else {
config.body = renderedRequest.body.text || '';
}
2016-08-31 20:50:27 +00:00
// Set the method
config.method = renderedRequest.method;
// Set the headers
const headers = {};
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) {
headers[header.name] = header.value;
2016-04-12 00:39:49 +00:00
}
2016-04-09 21:08:55 +00:00
}
config.headers = headers;
// Set the Accept header if it doesn't exist
if (!hasAcceptHeader(renderedRequest.headers)) {
config.headers['Accept'] = '*/*';
}
// Set UserAgent if it doesn't exist
if (!hasUserAgentHeader(renderedRequest.headers)) {
config.headers['User-Agent'] = `insomnia/${getAppVersion()}`
}
// Set the URL, including the query parameters
const qs = querystring.buildFromParams(renderedRequest.parameters);
const url = querystring.joinUrl(renderedRequest.url, qs);
config.url = util.prepareUrlForSending(url);
2016-04-28 07:30:26 +00:00
return Object.assign(config, patch);
}
2016-04-28 07:30:26 +00:00
2016-12-23 19:31:38 +00:00
export function _actuallySend (renderedRequest, workspace, settings, familyIndex = 0) {
return new Promise(async (resolve, reject) => {
2016-12-08 23:29:45 +00:00
async function handleError (err, prefix = '') {
await models.response.create({
url: renderedRequest.url,
parentId: renderedRequest._id,
elapsedTime: 0,
statusMessage: 'Error',
error: prefix ? `${prefix}: ${err.message}` : err.message
});
reject(err);
}
// 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;
2016-11-22 22:26:52 +00:00
let config;
try {
config = _buildRequestConfig(renderedRequest, {
jar: null, // We're doing our own cookies
proxy: proxy,
followAllRedirects: settings.followRedirects,
followRedirect: settings.followRedirects,
timeout: settings.timeout > 0 ? settings.timeout : null,
rejectUnauthorized: settings.validateSSL
}, true);
2016-12-08 23:29:45 +00:00
} catch (err) {
return handleError(err, 'Failed to setup request');
2016-11-22 22:26:52 +00:00
}
2016-07-20 21:15:11 +00:00
2016-12-08 23:29:45 +00:00
try {
// Add certs if needed
// https://vanjakom.wordpress.com/2011/08/11/client-and-server-side-ssl-with-nodejs/
const {hostname, port} = urlParse(config.url);
const certificate = workspace.certificates.find(certificate => {
const cHostWithProtocol = setDefaultProtocol(certificate.host, 'https:');
const {hostname: cHostname, port: cPort} = urlParse(cHostWithProtocol);
const assumedPort = parseInt(port) || 443;
const assumedCPort = parseInt(cPort) || 443;
// Exact host match (includes port)
return cHostname === hostname && assumedCPort === assumedPort;
});
if (certificate && !certificate.disabled) {
const {passphrase, cert, key, pfx} = certificate;
config.cert = cert ? Buffer.from(cert, 'base64') : null;
config.key = key ? Buffer.from(key, 'base64') : null;
config.pfx = pfx ? Buffer.from(pfx, 'base64') : null;
config.passphrase = passphrase || null;
}
} catch (err) {
return handleError(err, 'Failed to set certificate');
}
2016-12-08 23:29:45 +00:00
try {
// Add the cookie header to the request
config.jar = networkRequest.jar();
config.jar._jar = jarFromCookies(renderedRequest.cookieJar.cookies);
} catch (err) {
return handleError(err, 'Failed to set cookie jar');
}
// Set the IP family. This fallback behaviour is copied from Curl
2016-12-08 23:29:45 +00:00
try {
2016-12-23 19:31:38 +00:00
const family = FAMILY_FALLBACKS[familyIndex];
if (family) {
config.family = family;
}
2016-12-08 23:29:45 +00:00
} catch (err) {
return handleError(err, 'Failed to set IP family');
}
config.callback = 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';
2016-10-26 22:13:16 +00:00
// Failed to connect while prioritizing IPv6 address, fallback to IPv4
const isNetworkRelatedError = (
2016-12-30 23:06:27 +00:00
err.code === 'EAI_AGAIN' || // No entry
2016-12-23 19:31:38 +00:00
err.code === 'ENOENT' || // No entry
err.code === 'ENODATA' || // DNS resolve failed
err.code === 'ENOTFOUND' || // Could not resolve DNS
err.code === 'ECONNREFUSED' || // Could not talk to server
2016-11-19 08:38:26 +00:00
err.code === 'EHOSTUNREACH' || // Could not reach host
err.code === 'ENETUNREACH' // Could not access the network
);
2016-12-23 19:31:38 +00:00
const nextFamilyIndex = familyIndex + 1;
if (isNetworkRelatedError && nextFamilyIndex < FAMILY_FALLBACKS.length) {
const family = FAMILY_FALLBACKS[nextFamilyIndex];
console.log(`-- Falling back to family ${family} --`);
2016-12-30 23:06:27 +00:00
_actuallySend(
renderedRequest, workspace, settings, nextFamilyIndex
).then(resolve, reject);
2016-10-26 22:13:16 +00:00
return;
}
2016-09-28 21:17:57 +00:00
let message = err.toString();
if (isShittyParseError) {
2016-10-06 16:52:26 +00:00
message = `Error parsing response after ${err.bytesParsed} bytes.\n\n`;
message += `Code: ${err.code}`;
2016-09-28 21:17:57 +00:00
}
2016-11-10 01:15:27 +00:00
await models.response.create({
url: config.url,
parentId: renderedRequest._id,
statusMessage: 'Error',
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-12-08 23:29:45 +00:00
// handle response headers
const headers = [];
2016-12-08 23:29:45 +00:00
try {
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-12-08 23:29:45 +00:00
} catch (err) {
return handleError(err, 'Failed to parse response headers');
}
2016-12-08 23:29:45 +00:00
try {
const cookies = await cookiesFromJar(config.jar._jar);
await models.cookieJar.update(renderedRequest.cookieJar, {cookies});
} catch (err) {
return handleError(err, 'Failed to update cookie jar');
}
2016-10-05 04:43:48 +00:00
2016-12-08 23:47:24 +00:00
let contentType = '';
if (networkResponse.headers) {
contentType = networkResponse.headers['content-type'] || ''
}
let bytesRead = 0;
if (networkResponse.body) {
bytesRead = networkResponse.body.length;
}
const bodyEncoding = 'base64';
2016-07-20 21:15:11 +00:00
const responsePatch = {
parentId: renderedRequest._id,
statusCode: networkResponse.statusCode,
statusMessage: networkResponse.statusMessage,
url: config.url,
2016-12-08 23:47:24 +00:00
contentType: contentType,
elapsedTime: networkResponse.elapsedTime,
2016-12-08 23:47:24 +00:00
bytesRead: bytesRead,
body: networkResponse.body.toString(bodyEncoding),
encoding: bodyEncoding,
headers: headers
2016-07-20 21:15:11 +00:00
};
2016-04-28 07:30:26 +00:00
2016-11-10 01:15:27 +00:00
models.response.create(responsePatch).then(resolve, reject);
};
const requestStartTime = Date.now();
const req = new networkRequest.Request(config);
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();
2016-11-10 01:15:27 +00:00
await models.response.create({
url: config.url,
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, environmentId) {
// First, lets wait for all debounces to finish
await util.delay(DEBOUNCE_MILLIS * 2);
2016-11-10 01:15:27 +00:00
const request = await models.request.getById(requestId);
const settings = await models.settings.getOrCreate();
let renderedRequest;
try {
renderedRequest = await getRenderedRequest(request, environmentId);
} catch (e) {
// Failed to render. Must be the user's fault
2016-11-10 01:15:27 +00:00
return await models.response.create({
parentId: request._id,
statusCode: STATUS_CODE_RENDER_FAILED,
error: e.message
});
}
// Get the workspace for the request
const ancestors = await db.withAncestors(request);
const workspace = ancestors.find(doc => doc.type === models.workspace.type);
// Render succeeded so we're good to go!
return await _actuallySend(renderedRequest, workspace, settings);
}