2017-07-18 20:38:19 +00:00
|
|
|
// @flow
|
2018-06-25 17:42:50 +00:00
|
|
|
import type { ResponseHeader, ResponseTimelineEntry } from '../models/response';
|
|
|
|
import type { Request, RequestHeader } from '../models/request';
|
|
|
|
import type { Workspace } from '../models/workspace';
|
|
|
|
import type { Settings } from '../models/settings';
|
2019-05-15 20:01:36 +00:00
|
|
|
import type { ExtraRenderInfo, RenderedRequest } from '../common/render';
|
2019-05-03 14:53:18 +00:00
|
|
|
import {
|
|
|
|
getRenderedRequestAndContext,
|
|
|
|
RENDER_PURPOSE_NO_RENDER,
|
|
|
|
RENDER_PURPOSE_SEND,
|
|
|
|
} from '../common/render';
|
2017-03-16 17:51:56 +00:00
|
|
|
import mkdirp from 'mkdirp';
|
2019-04-27 08:46:10 +00:00
|
|
|
import crypto from 'crypto';
|
2017-07-12 21:20:31 +00:00
|
|
|
import clone from 'clone';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { parse as urlParse, resolve as urlResolve } from 'url';
|
2020-06-10 18:15:56 +00:00
|
|
|
import {
|
|
|
|
Curl,
|
|
|
|
CurlAuth,
|
|
|
|
CurlCode,
|
|
|
|
CurlInfoDebug,
|
|
|
|
CurlFeature,
|
|
|
|
CurlNetrc,
|
|
|
|
CurlHttpVersion,
|
|
|
|
} from 'node-libcurl';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { join as pathJoin } from 'path';
|
2020-06-30 19:36:15 +00:00
|
|
|
import * as uuid from 'uuid';
|
2017-03-16 17:51:56 +00:00
|
|
|
import * as models from '../models';
|
2018-04-25 18:24:52 +00:00
|
|
|
import {
|
|
|
|
AUTH_AWS_IAM,
|
|
|
|
AUTH_DIGEST,
|
|
|
|
AUTH_NETRC,
|
|
|
|
AUTH_NTLM,
|
|
|
|
CONTENT_TYPE_FORM_DATA,
|
|
|
|
CONTENT_TYPE_FORM_URLENCODED,
|
|
|
|
getAppVersion,
|
|
|
|
getTempDir,
|
2020-06-10 18:15:56 +00:00
|
|
|
HttpVersions,
|
2018-12-12 17:36:11 +00:00
|
|
|
STATUS_CODE_PLUGIN_ERROR,
|
2018-04-25 18:24:52 +00:00
|
|
|
} from '../common/constants';
|
|
|
|
import {
|
|
|
|
delay,
|
|
|
|
describeByteSize,
|
|
|
|
getContentTypeHeader,
|
2019-05-15 20:01:36 +00:00
|
|
|
getDataDirectory,
|
2018-04-25 18:24:52 +00:00
|
|
|
getHostHeader,
|
|
|
|
getLocationHeader,
|
|
|
|
getSetCookieHeaders,
|
|
|
|
hasAcceptEncodingHeader,
|
|
|
|
hasAcceptHeader,
|
|
|
|
hasAuthHeader,
|
|
|
|
hasContentTypeHeader,
|
|
|
|
hasUserAgentHeader,
|
2018-11-06 02:39:50 +00:00
|
|
|
waitForStreamToFinish,
|
2018-04-25 18:24:52 +00:00
|
|
|
} from '../common/misc';
|
2018-06-25 17:42:50 +00:00
|
|
|
import {
|
|
|
|
buildQueryStringFromParams,
|
|
|
|
joinUrlAndQueryString,
|
|
|
|
setDefaultProtocol,
|
2018-12-12 17:36:11 +00:00
|
|
|
smartEncodeUrl,
|
2018-06-25 17:42:50 +00:00
|
|
|
} from 'insomnia-url';
|
2017-03-16 17:51:56 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import * as db from '../common/database';
|
2017-03-28 22:45:23 +00:00
|
|
|
import * as CACerts from './cacert';
|
2017-07-20 01:55:40 +00:00
|
|
|
import * as plugins from '../plugins/index';
|
|
|
|
import * as pluginContexts from '../plugins/context/index';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { getAuthHeader } from './authentication';
|
|
|
|
import { cookiesFromJar, jarFromCookies } from 'insomnia-cookies';
|
|
|
|
import { urlMatchesCertHost } from './url-matches-cert-host';
|
2017-07-12 21:01:14 +00:00
|
|
|
import aws4 from 'aws4';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { buildMultipart } from './multipart';
|
2020-01-22 19:23:19 +00:00
|
|
|
import type { Environment } from '../models/environment';
|
2017-03-16 17:51:56 +00:00
|
|
|
|
2019-04-27 08:46:10 +00:00
|
|
|
export type ResponsePatch = {|
|
2017-11-23 21:57:08 +00:00
|
|
|
bodyCompression?: 'zip' | null,
|
2020-01-22 19:23:19 +00:00
|
|
|
bodyPath?: string,
|
|
|
|
bytesContent?: number,
|
2017-07-20 01:55:40 +00:00
|
|
|
bytesRead?: number,
|
2020-01-22 19:23:19 +00:00
|
|
|
contentType?: string,
|
|
|
|
elapsedTime?: number,
|
|
|
|
environmentId?: string | null,
|
|
|
|
error?: string,
|
|
|
|
headers?: Array<ResponseHeader>,
|
|
|
|
httpVersion?: string,
|
|
|
|
message?: string,
|
2017-07-20 01:55:40 +00:00
|
|
|
parentId?: string,
|
|
|
|
settingSendCookies?: boolean,
|
2020-01-22 19:23:19 +00:00
|
|
|
settingStoreCookies?: boolean,
|
|
|
|
statusCode?: number,
|
|
|
|
statusMessage?: string,
|
2019-04-27 08:46:10 +00:00
|
|
|
timelinePath?: string,
|
2020-01-22 19:23:19 +00:00
|
|
|
url?: string,
|
2019-04-27 08:46:10 +00:00
|
|
|
|};
|
2017-07-18 20:38:19 +00:00
|
|
|
|
2017-06-09 03:02:16 +00:00
|
|
|
// Time since user's last keypress to wait before making the request
|
|
|
|
const MAX_DELAY_TIME = 1000;
|
|
|
|
|
2018-05-04 14:37:37 +00:00
|
|
|
// Special header value that will prevent the header being sent
|
|
|
|
const DISABLE_HEADER_VALUE = '__Di$aB13d__';
|
|
|
|
|
2020-05-28 17:09:51 +00:00
|
|
|
// Because node-libcurl changed some names that we used in the timeline
|
|
|
|
const LIBCURL_DEBUG_MIGRATION_MAP = {
|
|
|
|
HeaderIn: 'HEADER_IN',
|
|
|
|
DataIn: 'DATA_IN',
|
|
|
|
SslDataIn: 'SSL_DATA_IN',
|
|
|
|
HeaderOut: 'HEADER_OUT',
|
|
|
|
DataOut: 'DATA_OUT',
|
|
|
|
SslDataOut: 'SSL_DATA_OUT',
|
|
|
|
Text: 'TEXT',
|
|
|
|
'': '',
|
|
|
|
};
|
|
|
|
|
2017-03-16 17:51:56 +00:00
|
|
|
let cancelRequestFunction = null;
|
2017-06-09 03:02:16 +00:00
|
|
|
let lastUserInteraction = Date.now();
|
2017-03-16 17:51:56 +00:00
|
|
|
|
2020-05-28 17:09:51 +00:00
|
|
|
export async function cancelCurrentRequest() {
|
2017-03-16 17:51:56 +00:00
|
|
|
if (typeof cancelRequestFunction === 'function') {
|
2020-05-28 17:09:51 +00:00
|
|
|
return cancelRequestFunction();
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export async function _actuallySend(
|
2017-07-18 22:10:57 +00:00
|
|
|
renderedRequest: RenderedRequest,
|
2018-04-25 18:24:52 +00:00
|
|
|
renderContext: Object,
|
2017-07-18 22:10:57 +00:00
|
|
|
workspace: Workspace,
|
2018-12-12 17:36:11 +00:00
|
|
|
settings: Settings,
|
2020-01-22 19:23:19 +00:00
|
|
|
environment: Environment | null,
|
2017-11-21 17:49:17 +00:00
|
|
|
): Promise<ResponsePatch> {
|
2017-03-16 17:51:56 +00:00
|
|
|
return new Promise(async resolve => {
|
2020-04-09 17:32:19 +00:00
|
|
|
const timeline: Array<ResponseTimelineEntry> = [];
|
2017-07-11 00:23:40 +00:00
|
|
|
|
2018-10-11 17:12:31 +00:00
|
|
|
function addTimeline(name, value) {
|
2020-05-28 17:09:51 +00:00
|
|
|
timeline.push({
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
});
|
2018-10-11 17:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function addTimelineText(value) {
|
|
|
|
addTimeline('TEXT', value);
|
|
|
|
}
|
|
|
|
|
2017-07-19 04:48:28 +00:00
|
|
|
// Initialize the curl handle
|
|
|
|
const curl = new Curl();
|
|
|
|
|
|
|
|
/** Helper function to respond with a success */
|
2018-06-25 17:42:50 +00:00
|
|
|
async function respond(
|
|
|
|
patch: ResponsePatch,
|
|
|
|
bodyPath: string | null,
|
2018-12-12 17:36:11 +00:00
|
|
|
noPlugins: boolean = false,
|
2018-06-25 17:42:50 +00:00
|
|
|
): Promise<void> {
|
2019-04-27 08:46:10 +00:00
|
|
|
const timelinePath = await storeTimeline(timeline);
|
|
|
|
|
2020-01-22 19:23:19 +00:00
|
|
|
const environmentId = environment ? environment._id : null;
|
2018-06-25 17:42:50 +00:00
|
|
|
const responsePatchBeforeHooks = Object.assign(
|
|
|
|
({
|
2019-04-27 08:46:10 +00:00
|
|
|
timelinePath,
|
2020-01-22 19:23:19 +00:00
|
|
|
environmentId,
|
2018-06-25 17:42:50 +00:00
|
|
|
parentId: renderedRequest._id,
|
|
|
|
bodyCompression: null, // Will default to .zip otherwise
|
|
|
|
bodyPath: bodyPath || '',
|
|
|
|
settingSendCookies: renderedRequest.settingSendCookies,
|
2018-12-12 17:36:11 +00:00
|
|
|
settingStoreCookies: renderedRequest.settingStoreCookies,
|
2018-06-25 17:42:50 +00:00
|
|
|
}: ResponsePatch),
|
2018-12-12 17:36:11 +00:00
|
|
|
patch,
|
2018-06-25 17:42:50 +00:00
|
|
|
);
|
2017-07-11 00:23:40 +00:00
|
|
|
|
2018-06-06 20:48:14 +00:00
|
|
|
if (noPlugins) {
|
|
|
|
resolve(responsePatchBeforeHooks);
|
|
|
|
return;
|
|
|
|
}
|
2017-07-20 01:55:40 +00:00
|
|
|
|
2018-06-06 20:48:14 +00:00
|
|
|
let responsePatch: ?ResponsePatch;
|
|
|
|
try {
|
2018-06-25 17:42:50 +00:00
|
|
|
responsePatch = await _applyResponsePluginHooks(
|
|
|
|
responsePatchBeforeHooks,
|
|
|
|
renderedRequest,
|
2018-12-12 17:36:11 +00:00
|
|
|
renderContext,
|
2018-06-25 17:42:50 +00:00
|
|
|
);
|
2018-06-06 20:48:14 +00:00
|
|
|
} catch (err) {
|
2020-05-28 17:09:51 +00:00
|
|
|
await handleError(
|
2018-12-12 17:36:11 +00:00
|
|
|
new Error(`[plugin] Response hook failed plugin=${err.plugin.name} err=${err.message}`),
|
2018-06-25 17:42:50 +00:00
|
|
|
);
|
2018-06-06 20:48:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(responsePatch);
|
2017-07-18 20:38:19 +00:00
|
|
|
}
|
2017-07-11 00:23:40 +00:00
|
|
|
|
2017-07-19 04:48:28 +00:00
|
|
|
/** Helper function to respond with an error */
|
2020-05-28 17:09:51 +00:00
|
|
|
async function handleError(err: Error): Promise<void> {
|
|
|
|
await respond(
|
2018-06-25 17:42:50 +00:00
|
|
|
{
|
|
|
|
url: renderedRequest.url,
|
|
|
|
parentId: renderedRequest._id,
|
|
|
|
error: err.message,
|
|
|
|
elapsedTime: 0,
|
|
|
|
statusMessage: 'Error',
|
|
|
|
settingSendCookies: renderedRequest.settingSendCookies,
|
2018-12-12 17:36:11 +00:00
|
|
|
settingStoreCookies: renderedRequest.settingStoreCookies,
|
2018-06-25 17:42:50 +00:00
|
|
|
},
|
|
|
|
null,
|
2018-12-12 17:36:11 +00:00
|
|
|
true,
|
2018-06-25 17:42:50 +00:00
|
|
|
);
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 04:48:28 +00:00
|
|
|
/** Helper function to set Curl options */
|
2018-06-25 17:42:50 +00:00
|
|
|
function setOpt(opt: number, val: any, optional: boolean = false) {
|
2018-08-01 18:28:11 +00:00
|
|
|
const name = Object.keys(Curl.option).find(name => Curl.option[name] === opt);
|
2017-07-19 04:48:28 +00:00
|
|
|
try {
|
|
|
|
curl.setOpt(opt, val);
|
|
|
|
} catch (err) {
|
|
|
|
if (!optional) {
|
|
|
|
throw new Error(`${err.message} (${opt} ${name || 'n/a'})`);
|
|
|
|
} else {
|
2018-08-01 18:28:11 +00:00
|
|
|
console.warn(`Failed to set optional Curl opt (${opt} ${name || 'n/a'})`);
|
2017-04-07 18:10:15 +00:00
|
|
|
}
|
2017-07-19 04:48:28 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-07 18:10:15 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
function enable(feature: number) {
|
2017-11-13 23:10:53 +00:00
|
|
|
curl.enable(feature);
|
|
|
|
}
|
|
|
|
|
2017-07-19 04:48:28 +00:00
|
|
|
try {
|
2017-03-16 17:51:56 +00:00
|
|
|
// Setup the cancellation logic
|
2020-05-28 17:09:51 +00:00
|
|
|
cancelRequestFunction = async () => {
|
|
|
|
await respond(
|
2018-06-25 17:42:50 +00:00
|
|
|
{
|
|
|
|
elapsedTime: curl.getInfo(Curl.info.TOTAL_TIME) * 1000,
|
|
|
|
bytesRead: curl.getInfo(Curl.info.SIZE_DOWNLOAD),
|
|
|
|
url: curl.getInfo(Curl.info.EFFECTIVE_URL),
|
|
|
|
statusMessage: 'Cancelled',
|
2018-12-12 17:36:11 +00:00
|
|
|
error: 'Request was cancelled',
|
2018-06-25 17:42:50 +00:00
|
|
|
},
|
|
|
|
null,
|
2018-12-12 17:36:11 +00:00
|
|
|
true,
|
2018-06-25 17:42:50 +00:00
|
|
|
);
|
2017-03-16 17:51:56 +00:00
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
// Kill it!
|
|
|
|
curl.close();
|
|
|
|
};
|
2017-03-16 17:51:56 +00:00
|
|
|
|
|
|
|
// Set all the basic options
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.VERBOSE, true); // True so debug function works
|
2019-09-18 00:02:42 +00:00
|
|
|
setOpt(Curl.option.NOPROGRESS, true); // True so curl doesn't print progress
|
2017-11-12 18:35:01 +00:00
|
|
|
setOpt(Curl.option.ACCEPT_ENCODING, ''); // Auto decode everything
|
2020-05-28 17:09:51 +00:00
|
|
|
enable(CurlFeature.Raw);
|
2017-03-28 22:45:23 +00:00
|
|
|
|
2019-10-31 16:02:03 +00:00
|
|
|
// Set follow redirects setting
|
|
|
|
switch (renderedRequest.settingFollowRedirects) {
|
|
|
|
case 'off':
|
|
|
|
setOpt(Curl.option.FOLLOWLOCATION, false);
|
|
|
|
break;
|
|
|
|
case 'on':
|
|
|
|
setOpt(Curl.option.FOLLOWLOCATION, true);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Set to global setting
|
|
|
|
setOpt(Curl.option.FOLLOWLOCATION, settings.followRedirects);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-10-31 18:05:35 +00:00
|
|
|
// Set maximum amount of redirects allowed
|
|
|
|
// NOTE: Setting this to -1 breaks some versions of libcurl
|
|
|
|
if (settings.maxRedirects > 0) {
|
|
|
|
setOpt(Curl.option.MAXREDIRS, settings.maxRedirects);
|
|
|
|
}
|
|
|
|
|
2018-03-26 17:43:42 +00:00
|
|
|
// Don't rebuild dot sequences in path
|
|
|
|
if (!renderedRequest.settingRebuildPath) {
|
|
|
|
setOpt(Curl.option.PATH_AS_IS, true);
|
|
|
|
}
|
|
|
|
|
2017-07-26 00:49:22 +00:00
|
|
|
// Only set CURLOPT_CUSTOMREQUEST if not HEAD or GET. This is because Curl
|
|
|
|
// See https://curl.haxx.se/libcurl/c/CURLOPT_CUSTOMREQUEST.html
|
|
|
|
switch (renderedRequest.method.toUpperCase()) {
|
|
|
|
case 'HEAD':
|
|
|
|
// This is how you tell Curl to send a HEAD request
|
|
|
|
setOpt(Curl.option.NOBODY, 1);
|
|
|
|
break;
|
|
|
|
case 'POST':
|
|
|
|
// This is how you tell Curl to send a POST request
|
|
|
|
setOpt(Curl.option.POST, 1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// IMPORTANT: Only use CUSTOMREQUEST for all but HEAD and POST
|
|
|
|
setOpt(Curl.option.CUSTOMREQUEST, renderedRequest.method);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
// Setup debug handler
|
2020-05-28 17:09:51 +00:00
|
|
|
setOpt(Curl.option.DEBUGFUNCTION, (infoType: string, contentBuffer: Buffer) => {
|
|
|
|
const content = contentBuffer.toString('utf8');
|
|
|
|
const rawName = Object.keys(CurlInfoDebug).find(k => CurlInfoDebug[k] === infoType) || '';
|
|
|
|
const name = LIBCURL_DEBUG_MIGRATION_MAP[rawName] || rawName;
|
2018-08-01 18:28:11 +00:00
|
|
|
|
2020-05-28 17:09:51 +00:00
|
|
|
if (infoType === CurlInfoDebug.SslDataIn || infoType === CurlInfoDebug.SslDataOut) {
|
2017-04-04 23:06:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
// Ignore the possibly large data messages
|
2020-05-28 17:09:51 +00:00
|
|
|
if (infoType === CurlInfoDebug.DataOut) {
|
|
|
|
if (contentBuffer.length === 0) {
|
2017-06-21 20:58:41 +00:00
|
|
|
// Sometimes this happens, but I'm not sure why. Just ignore it.
|
2020-05-28 17:09:51 +00:00
|
|
|
} else if (contentBuffer.length / 1024 < settings.maxTimelineDataSizeKB) {
|
2018-10-11 17:12:31 +00:00
|
|
|
addTimeline(name, content);
|
2017-03-29 02:21:49 +00:00
|
|
|
} else {
|
2020-05-28 17:09:51 +00:00
|
|
|
addTimeline(name, `(${describeByteSize(contentBuffer.length)} hidden)`);
|
2017-03-29 02:21:49 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-28 17:09:51 +00:00
|
|
|
if (infoType === CurlInfoDebug.DataIn) {
|
|
|
|
addTimelineText(`Received ${describeByteSize(contentBuffer.length)} chunk`);
|
2017-03-28 22:45:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't show cookie setting because this will display every domain in the jar
|
2020-05-28 17:09:51 +00:00
|
|
|
if (infoType === CurlInfoDebug.Text && content.indexOf('Added cookie') === 0) {
|
2017-03-28 22:45:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-11 17:12:31 +00:00
|
|
|
addTimeline(name, content);
|
2017-03-28 22:45:23 +00:00
|
|
|
|
|
|
|
return 0; // Must be here
|
|
|
|
});
|
2017-03-16 17:51:56 +00:00
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
// Set the headers (to be modified as we go)
|
2017-07-12 21:20:31 +00:00
|
|
|
const headers = clone(renderedRequest.headers);
|
2017-03-23 22:10:42 +00:00
|
|
|
|
2017-03-16 17:51:56 +00:00
|
|
|
// Set the URL, including the query parameters
|
2017-11-26 20:45:40 +00:00
|
|
|
const qs = buildQueryStringFromParams(renderedRequest.parameters);
|
|
|
|
const url = joinUrlAndQueryString(renderedRequest.url, qs);
|
2017-06-30 16:00:00 +00:00
|
|
|
const isUnixSocket = url.match(/https?:\/\/unix:\//);
|
2017-11-26 20:45:40 +00:00
|
|
|
const finalUrl = smartEncodeUrl(url, renderedRequest.settingEncodeUrl);
|
2017-06-22 18:43:00 +00:00
|
|
|
if (isUnixSocket) {
|
|
|
|
// URL prep will convert "unix:/path" hostname to "unix/path"
|
2017-06-22 19:43:11 +00:00
|
|
|
const match = finalUrl.match(/(https?:)\/\/unix:?(\/[^:]+):\/(.+)/);
|
|
|
|
const protocol = (match && match[1]) || '';
|
|
|
|
const socketPath = (match && match[2]) || '';
|
|
|
|
const socketUrl = (match && match[3]) || '';
|
2020-05-28 17:09:51 +00:00
|
|
|
setOpt(Curl.option.URL, `${protocol}//${socketUrl}`);
|
2017-06-22 18:43:00 +00:00
|
|
|
setOpt(Curl.option.UNIX_SOCKET_PATH, socketPath);
|
|
|
|
} else {
|
2020-05-28 17:09:51 +00:00
|
|
|
setOpt(Curl.option.URL, finalUrl);
|
2017-06-22 18:43:00 +00:00
|
|
|
}
|
2018-10-11 17:12:31 +00:00
|
|
|
addTimelineText('Preparing request to ' + finalUrl);
|
2019-05-09 17:38:36 +00:00
|
|
|
addTimelineText('Current time is ' + new Date().toISOString());
|
2020-06-10 18:15:56 +00:00
|
|
|
addTimelineText(`Using ${Curl.getVersion()}`);
|
|
|
|
|
|
|
|
// Set HTTP version
|
|
|
|
switch (settings.preferredHttpVersion) {
|
|
|
|
case HttpVersions.V1_0:
|
|
|
|
addTimelineText('Using HTTP 1.0');
|
|
|
|
setOpt(Curl.option.HTTP_VERSION, CurlHttpVersion.V1_0);
|
|
|
|
break;
|
|
|
|
case HttpVersions.V1_1:
|
|
|
|
addTimelineText('Using HTTP 1.1');
|
|
|
|
setOpt(Curl.option.HTTP_VERSION, CurlHttpVersion.V1_1);
|
|
|
|
break;
|
|
|
|
case HttpVersions.V2_0:
|
|
|
|
addTimelineText('Using HTTP/2');
|
|
|
|
setOpt(Curl.option.HTTP_VERSION, CurlHttpVersion.V2_0);
|
|
|
|
break;
|
|
|
|
case HttpVersions.v3:
|
|
|
|
addTimelineText('Using HTTP/3');
|
|
|
|
setOpt(Curl.option.HTTP_VERSION, CurlHttpVersion.v3);
|
|
|
|
break;
|
|
|
|
case HttpVersions.default:
|
|
|
|
addTimelineText('Using default HTTP version');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
addTimelineText(`Unknown HTTP version specified ${settings.preferredHttpVersion}`);
|
|
|
|
break;
|
|
|
|
}
|
2017-03-29 23:09:28 +00:00
|
|
|
|
2018-12-14 18:26:10 +00:00
|
|
|
// Set timeout
|
|
|
|
if (settings.timeout > 0) {
|
|
|
|
addTimelineText(`Enable timeout of ${settings.timeout}ms`);
|
|
|
|
setOpt(Curl.option.TIMEOUT_MS, settings.timeout);
|
|
|
|
} else {
|
2020-04-09 17:32:19 +00:00
|
|
|
addTimelineText('Disable timeout');
|
2018-12-14 18:34:48 +00:00
|
|
|
setOpt(Curl.option.TIMEOUT_MS, 0);
|
2018-12-14 18:26:10 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 23:09:28 +00:00
|
|
|
// log some things
|
|
|
|
if (renderedRequest.settingEncodeUrl) {
|
2018-10-11 17:12:31 +00:00
|
|
|
addTimelineText('Enable automatic URL encoding');
|
2017-03-29 23:09:28 +00:00
|
|
|
} else {
|
2018-10-11 17:12:31 +00:00
|
|
|
addTimelineText('Disable automatic URL encoding');
|
2017-03-29 23:09:28 +00:00
|
|
|
}
|
2017-03-16 17:51:56 +00:00
|
|
|
|
2017-04-07 18:10:15 +00:00
|
|
|
// SSL Validation
|
|
|
|
if (settings.validateSSL) {
|
2018-10-11 17:12:31 +00:00
|
|
|
addTimelineText('Enable SSL validation');
|
2017-04-07 18:10:15 +00:00
|
|
|
} else {
|
|
|
|
setOpt(Curl.option.SSL_VERIFYHOST, 0);
|
|
|
|
setOpt(Curl.option.SSL_VERIFYPEER, 0);
|
2018-10-11 17:12:31 +00:00
|
|
|
addTimelineText('Disable SSL validation');
|
2017-04-07 18:10:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 17:51:56 +00:00
|
|
|
// Setup CA Root Certificates if not on Mac. Thanks to libcurl, Mac will use
|
|
|
|
// certificates form the OS.
|
|
|
|
if (process.platform !== 'darwin') {
|
2017-11-21 17:52:47 +00:00
|
|
|
const baseCAPath = getTempDir();
|
|
|
|
const fullCAPath = pathJoin(baseCAPath, CACerts.filename);
|
2017-03-28 22:45:23 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
fs.statSync(fullCAPath);
|
|
|
|
} catch (err) {
|
|
|
|
// Doesn't exist yet, so write it
|
2017-11-21 17:52:47 +00:00
|
|
|
mkdirp.sync(baseCAPath);
|
2017-03-28 22:45:23 +00:00
|
|
|
fs.writeFileSync(fullCAPath, CACerts.blob);
|
2017-11-22 00:07:28 +00:00
|
|
|
console.log('[net] Set CA to', fullCAPath);
|
2017-03-28 22:45:23 +00:00
|
|
|
}
|
2017-03-16 17:51:56 +00:00
|
|
|
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.CAINFO, fullCAPath);
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 21:17:40 +00:00
|
|
|
// Set cookies from jar
|
2017-03-28 22:45:23 +00:00
|
|
|
if (renderedRequest.settingSendCookies) {
|
2017-06-15 17:52:51 +00:00
|
|
|
// Tell Curl to store cookies that it receives. This is only important if we receive
|
|
|
|
// a cookie on a redirect that needs to be sent on the next request in the chain.
|
|
|
|
curl.setOpt(Curl.option.COOKIEFILE, '');
|
|
|
|
|
2017-03-29 23:09:28 +00:00
|
|
|
const cookies = renderedRequest.cookieJar.cookies || [];
|
|
|
|
for (const cookie of cookies) {
|
2017-03-28 22:45:23 +00:00
|
|
|
let expiresTimestamp = 0;
|
|
|
|
if (cookie.expires) {
|
|
|
|
const expiresDate = new Date(cookie.expires);
|
|
|
|
expiresTimestamp = Math.round(expiresDate.getTime() / 1000);
|
|
|
|
}
|
2017-05-16 17:45:09 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
setOpt(
|
|
|
|
Curl.option.COOKIELIST,
|
|
|
|
[
|
|
|
|
cookie.httpOnly ? `#HttpOnly_${cookie.domain}` : cookie.domain,
|
|
|
|
cookie.hostOnly ? 'FALSE' : 'TRUE',
|
|
|
|
cookie.path,
|
|
|
|
cookie.secure ? 'TRUE' : 'FALSE',
|
|
|
|
expiresTimestamp,
|
|
|
|
cookie.key,
|
2018-12-12 17:36:11 +00:00
|
|
|
cookie.value,
|
|
|
|
].join('\t'),
|
2018-06-25 17:42:50 +00:00
|
|
|
);
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
2017-03-29 23:09:28 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
for (const { name, value } of renderedRequest.cookies) {
|
2017-07-20 01:55:40 +00:00
|
|
|
setOpt(Curl.option.COOKIE, `${name}=${value}`);
|
|
|
|
}
|
|
|
|
|
2018-10-11 17:12:31 +00:00
|
|
|
addTimelineText(
|
2020-05-28 17:09:51 +00:00
|
|
|
`Enable cookie sending with jar of ${cookies.length} cookie${
|
|
|
|
cookies.length !== 1 ? 's' : ''
|
|
|
|
}`,
|
2018-10-11 17:12:31 +00:00
|
|
|
);
|
2017-03-29 23:09:28 +00:00
|
|
|
} else {
|
2018-10-11 17:12:31 +00:00
|
|
|
addTimelineText('Disable cookie sending due to user setting');
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
// Set proxy settings if we have them
|
|
|
|
if (settings.proxyEnabled) {
|
2018-06-25 17:42:50 +00:00
|
|
|
const { protocol } = urlParse(renderedRequest.url);
|
|
|
|
const { httpProxy, httpsProxy, noProxy } = settings;
|
2017-03-28 22:45:23 +00:00
|
|
|
const proxyHost = protocol === 'https:' ? httpsProxy : httpProxy;
|
|
|
|
const proxy = proxyHost ? setDefaultProtocol(proxyHost) : null;
|
2018-10-11 17:12:31 +00:00
|
|
|
addTimelineText(`Enable network proxy for ${protocol || ''}`);
|
2017-03-28 22:45:23 +00:00
|
|
|
if (proxy) {
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.PROXY, proxy);
|
2020-05-28 17:09:51 +00:00
|
|
|
setOpt(Curl.option.PROXYAUTH, CurlAuth.Any);
|
2017-03-28 22:45:23 +00:00
|
|
|
}
|
2017-06-06 20:21:59 +00:00
|
|
|
if (noProxy) {
|
|
|
|
setOpt(Curl.option.NOPROXY, noProxy);
|
|
|
|
}
|
2017-04-07 18:10:15 +00:00
|
|
|
} else {
|
|
|
|
setOpt(Curl.option.PROXY, '');
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set client certs if needed
|
2018-08-01 18:28:11 +00:00
|
|
|
const clientCertificates = await models.clientCertificate.findByParentId(workspace._id);
|
2017-10-31 18:05:35 +00:00
|
|
|
for (const certificate of clientCertificates) {
|
2017-06-21 21:20:42 +00:00
|
|
|
if (certificate.disabled) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-01 18:28:11 +00:00
|
|
|
const cHostWithProtocol = setDefaultProtocol(certificate.host, 'https:');
|
2017-03-16 17:51:56 +00:00
|
|
|
|
2017-06-21 20:56:19 +00:00
|
|
|
if (urlMatchesCertHost(cHostWithProtocol, renderedRequest.url)) {
|
2017-03-16 17:51:56 +00:00
|
|
|
const ensureFile = blobOrFilename => {
|
2017-10-05 13:11:22 +00:00
|
|
|
try {
|
|
|
|
fs.statSync(blobOrFilename);
|
|
|
|
} catch (err) {
|
2017-11-21 17:52:47 +00:00
|
|
|
// Certificate file not found!
|
|
|
|
// LEGACY: Certs used to be stored in blobs (not as paths), so let's write it to
|
2017-03-16 17:51:56 +00:00
|
|
|
// the temp directory first.
|
2017-11-04 20:10:06 +00:00
|
|
|
const fullBase = getTempDir();
|
2017-03-16 17:51:56 +00:00
|
|
|
const name = `${renderedRequest._id}_${renderedRequest.modified}`;
|
|
|
|
const fullPath = pathJoin(fullBase, name);
|
2017-11-20 16:07:36 +00:00
|
|
|
fs.writeFileSync(fullPath, Buffer.from(blobOrFilename, 'base64'));
|
2017-03-16 17:51:56 +00:00
|
|
|
|
2017-10-05 13:11:22 +00:00
|
|
|
// Set filename to the one we just saved
|
|
|
|
blobOrFilename = fullPath;
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
2017-10-05 13:11:22 +00:00
|
|
|
|
|
|
|
return blobOrFilename;
|
2017-03-16 17:51:56 +00:00
|
|
|
};
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
const { passphrase, cert, key, pfx } = certificate;
|
2017-03-16 17:51:56 +00:00
|
|
|
|
|
|
|
if (cert) {
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.SSLCERT, ensureFile(cert));
|
|
|
|
setOpt(Curl.option.SSLCERTTYPE, 'PEM');
|
2018-10-11 17:12:31 +00:00
|
|
|
addTimelineText('Adding SSL PEM certificate');
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pfx) {
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.SSLCERT, ensureFile(pfx));
|
|
|
|
setOpt(Curl.option.SSLCERTTYPE, 'P12');
|
2018-10-11 17:12:31 +00:00
|
|
|
addTimelineText('Adding SSL P12 certificate');
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (key) {
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.SSLKEY, ensureFile(key));
|
2018-10-11 17:12:31 +00:00
|
|
|
addTimelineText('Adding SSL KEY certificate');
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (passphrase) {
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.KEYPASSWD, passphrase);
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the body
|
2017-04-09 21:53:46 +00:00
|
|
|
let noBody = false;
|
2017-07-12 21:20:31 +00:00
|
|
|
let requestBody = null;
|
2018-08-01 18:28:11 +00:00
|
|
|
const expectsBody = ['POST', 'PUT', 'PATCH'].includes(renderedRequest.method.toUpperCase());
|
2017-03-16 17:51:56 +00:00
|
|
|
if (renderedRequest.body.mimeType === CONTENT_TYPE_FORM_URLENCODED) {
|
2018-08-01 18:28:11 +00:00
|
|
|
requestBody = buildQueryStringFromParams(renderedRequest.body.params || [], false);
|
2017-03-16 17:51:56 +00:00
|
|
|
} else if (renderedRequest.body.mimeType === CONTENT_TYPE_FORM_DATA) {
|
2017-07-18 20:38:19 +00:00
|
|
|
const params = renderedRequest.body.params || [];
|
2018-08-01 18:28:11 +00:00
|
|
|
const { filePath: multipartBodyPath, boundary, contentLength } = await buildMultipart(
|
2018-12-12 17:36:11 +00:00
|
|
|
params,
|
2018-08-01 18:28:11 +00:00
|
|
|
);
|
2017-10-12 17:32:26 +00:00
|
|
|
|
|
|
|
// Extend the Content-Type header
|
|
|
|
const contentTypeHeader = getContentTypeHeader(headers);
|
|
|
|
if (contentTypeHeader) {
|
|
|
|
contentTypeHeader.value = `multipart/form-data; boundary=${boundary}`;
|
|
|
|
} else {
|
|
|
|
headers.push({
|
|
|
|
name: 'Content-Type',
|
2018-12-12 17:36:11 +00:00
|
|
|
value: `multipart/form-data; boundary=${boundary}`,
|
2017-10-12 17:32:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-06 05:30:40 +00:00
|
|
|
const fd = fs.openSync(multipartBodyPath, 'r');
|
2017-10-28 22:16:34 +00:00
|
|
|
|
|
|
|
setOpt(Curl.option.INFILESIZE_LARGE, contentLength);
|
2017-10-12 17:32:26 +00:00
|
|
|
setOpt(Curl.option.UPLOAD, 1);
|
2017-10-28 22:16:34 +00:00
|
|
|
setOpt(Curl.option.READDATA, fd);
|
2017-10-12 17:32:26 +00:00
|
|
|
|
|
|
|
// We need this, otherwise curl will send it as a PUT
|
|
|
|
setOpt(Curl.option.CUSTOMREQUEST, renderedRequest.method);
|
|
|
|
|
2017-10-28 22:16:34 +00:00
|
|
|
const fn = () => {
|
|
|
|
fs.closeSync(fd);
|
2020-05-28 17:09:51 +00:00
|
|
|
fs.unlink(multipartBodyPath, () => {
|
|
|
|
// Pass
|
|
|
|
});
|
2017-10-28 22:16:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
curl.on('end', fn);
|
|
|
|
curl.on('error', fn);
|
2017-03-16 17:51:56 +00:00
|
|
|
} else if (renderedRequest.body.fileName) {
|
2018-06-25 17:42:50 +00:00
|
|
|
const { size } = fs.statSync(renderedRequest.body.fileName);
|
2017-07-18 20:38:19 +00:00
|
|
|
const fileName = renderedRequest.body.fileName || '';
|
2018-03-06 05:30:40 +00:00
|
|
|
const fd = fs.openSync(fileName, 'r');
|
2017-07-26 02:40:32 +00:00
|
|
|
|
2017-09-09 12:48:27 +00:00
|
|
|
setOpt(Curl.option.INFILESIZE_LARGE, size);
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.UPLOAD, 1);
|
|
|
|
setOpt(Curl.option.READDATA, fd);
|
2017-07-26 02:40:32 +00:00
|
|
|
|
|
|
|
// We need this, otherwise curl will send it as a POST
|
|
|
|
setOpt(Curl.option.CUSTOMREQUEST, renderedRequest.method);
|
|
|
|
|
2017-03-16 17:51:56 +00:00
|
|
|
const fn = () => fs.closeSync(fd);
|
|
|
|
curl.on('end', fn);
|
|
|
|
curl.on('error', fn);
|
2018-08-01 18:28:11 +00:00
|
|
|
} else if (typeof renderedRequest.body.mimeType === 'string' || expectsBody) {
|
2017-07-12 21:20:31 +00:00
|
|
|
requestBody = renderedRequest.body.text || '';
|
2017-03-16 17:51:56 +00:00
|
|
|
} else {
|
|
|
|
// No body
|
2017-04-09 21:53:46 +00:00
|
|
|
noBody = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!noBody) {
|
|
|
|
// Don't chunk uploads
|
2020-05-28 17:09:51 +00:00
|
|
|
headers.push({
|
|
|
|
name: 'Expect',
|
|
|
|
value: DISABLE_HEADER_VALUE,
|
|
|
|
});
|
2018-06-25 17:42:50 +00:00
|
|
|
headers.push({
|
|
|
|
name: 'Transfer-Encoding',
|
2018-12-12 17:36:11 +00:00
|
|
|
value: DISABLE_HEADER_VALUE,
|
2018-06-25 17:42:50 +00:00
|
|
|
});
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 21:20:31 +00:00
|
|
|
// If we calculated the body within Insomnia (ie. not computed by Curl)
|
|
|
|
if (requestBody !== null) {
|
|
|
|
setOpt(Curl.option.POSTFIELDS, requestBody);
|
|
|
|
}
|
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
// Handle Authorization header
|
2017-04-11 21:20:01 +00:00
|
|
|
if (!hasAuthHeader(headers) && !renderedRequest.authentication.disabled) {
|
2020-09-24 20:58:28 +00:00
|
|
|
if (renderedRequest.authentication.type === AUTH_DIGEST) {
|
2018-06-25 17:42:50 +00:00
|
|
|
const { username, password } = renderedRequest.authentication;
|
2020-05-28 17:09:51 +00:00
|
|
|
setOpt(Curl.option.HTTPAUTH, CurlAuth.Digest);
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.USERNAME, username || '');
|
|
|
|
setOpt(Curl.option.PASSWORD, password || '');
|
2017-04-05 05:48:04 +00:00
|
|
|
} else if (renderedRequest.authentication.type === AUTH_NTLM) {
|
2018-06-25 17:42:50 +00:00
|
|
|
const { username, password } = renderedRequest.authentication;
|
2020-05-28 17:09:51 +00:00
|
|
|
setOpt(Curl.option.HTTPAUTH, CurlAuth.Ntlm);
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.USERNAME, username || '');
|
|
|
|
setOpt(Curl.option.PASSWORD, password || '');
|
2017-07-12 21:01:14 +00:00
|
|
|
} else if (renderedRequest.authentication.type === AUTH_AWS_IAM) {
|
2017-08-01 22:13:24 +00:00
|
|
|
if (!noBody && !requestBody) {
|
2017-07-20 01:55:40 +00:00
|
|
|
return handleError(
|
2018-12-12 17:36:11 +00:00
|
|
|
new Error('AWS authentication not supported for provided body type'),
|
2018-06-25 17:42:50 +00:00
|
|
|
);
|
2017-07-12 21:01:14 +00:00
|
|
|
}
|
2018-06-25 17:42:50 +00:00
|
|
|
const { authentication } = renderedRequest;
|
2018-01-24 17:51:13 +00:00
|
|
|
const credentials = {
|
2018-04-30 12:48:00 +00:00
|
|
|
accessKeyId: authentication.accessKeyId || '',
|
|
|
|
secretAccessKey: authentication.secretAccessKey || '',
|
2018-12-12 17:36:11 +00:00
|
|
|
sessionToken: authentication.sessionToken || '',
|
2018-01-24 17:51:13 +00:00
|
|
|
};
|
2018-06-30 19:55:36 +00:00
|
|
|
|
2017-07-12 21:20:31 +00:00
|
|
|
const extraHeaders = _getAwsAuthHeaders(
|
2018-01-24 17:51:13 +00:00
|
|
|
credentials,
|
2017-07-12 21:20:31 +00:00
|
|
|
headers,
|
2017-08-01 22:13:24 +00:00
|
|
|
requestBody || '',
|
2017-10-10 08:58:13 +00:00
|
|
|
finalUrl,
|
2018-04-30 12:48:00 +00:00
|
|
|
renderedRequest.method,
|
|
|
|
authentication.region || '',
|
2018-12-12 17:36:11 +00:00
|
|
|
authentication.service || '',
|
2017-07-12 21:20:31 +00:00
|
|
|
);
|
2018-06-30 19:55:36 +00:00
|
|
|
|
2017-07-12 21:20:31 +00:00
|
|
|
for (const header of extraHeaders) {
|
2017-07-12 21:01:14 +00:00
|
|
|
headers.push(header);
|
2017-07-12 21:20:31 +00:00
|
|
|
}
|
2017-07-31 17:29:36 +00:00
|
|
|
} else if (renderedRequest.authentication.type === AUTH_NETRC) {
|
2020-05-28 17:09:51 +00:00
|
|
|
setOpt(Curl.option.NETRC, CurlNetrc.Required);
|
2017-03-28 22:45:23 +00:00
|
|
|
} else {
|
2019-03-11 21:52:48 +00:00
|
|
|
const authHeader = await getAuthHeader(renderedRequest, finalUrl);
|
2017-03-28 22:45:23 +00:00
|
|
|
|
|
|
|
if (authHeader) {
|
2017-11-06 20:44:55 +00:00
|
|
|
headers.push({
|
|
|
|
name: authHeader.name,
|
2018-12-12 17:36:11 +00:00
|
|
|
value: authHeader.value,
|
2017-11-06 20:44:55 +00:00
|
|
|
});
|
2017-03-28 22:45:23 +00:00
|
|
|
}
|
2017-03-23 22:10:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 18:35:01 +00:00
|
|
|
// Send a default Accept headers of anything
|
|
|
|
if (!hasAcceptHeader(headers)) {
|
2020-05-28 17:09:51 +00:00
|
|
|
headers.push({
|
|
|
|
name: 'Accept',
|
|
|
|
value: '*/*',
|
|
|
|
}); // Default to anything
|
2017-11-12 18:35:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't auto-send Accept-Encoding header
|
|
|
|
if (!hasAcceptEncodingHeader(headers)) {
|
2020-05-28 17:09:51 +00:00
|
|
|
headers.push({
|
|
|
|
name: 'Accept-Encoding',
|
|
|
|
value: DISABLE_HEADER_VALUE,
|
|
|
|
});
|
2017-11-12 18:35:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
// Set User-Agent if it't not already in headers
|
2017-04-11 21:20:01 +00:00
|
|
|
if (!hasUserAgentHeader(headers)) {
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.USERAGENT, `insomnia/${getAppVersion()}`);
|
2017-03-23 22:10:42 +00:00
|
|
|
}
|
2017-03-16 17:51:56 +00:00
|
|
|
|
2017-04-11 21:20:01 +00:00
|
|
|
// Prevent curl from adding default content-type header
|
|
|
|
if (!hasContentTypeHeader(headers)) {
|
2020-05-28 17:09:51 +00:00
|
|
|
headers.push({
|
|
|
|
name: 'content-type',
|
|
|
|
value: DISABLE_HEADER_VALUE,
|
|
|
|
});
|
2017-04-11 21:20:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: This is last because headers might be modified multiple times
|
2019-04-18 17:23:15 +00:00
|
|
|
const headerStrings = headers
|
|
|
|
.filter(h => h.name)
|
|
|
|
.map(h => {
|
|
|
|
const value = h.value || '';
|
|
|
|
if (value === '') {
|
|
|
|
// Curl needs a semicolon suffix to send empty header values
|
|
|
|
return `${h.name};`;
|
|
|
|
} else if (value === DISABLE_HEADER_VALUE) {
|
|
|
|
// Tell Curl NOT to send the header if value is null
|
|
|
|
return `${h.name}:`;
|
|
|
|
} else {
|
|
|
|
// Send normal header value
|
|
|
|
return `${h.name}: ${value}`;
|
|
|
|
}
|
|
|
|
});
|
2017-04-11 21:20:01 +00:00
|
|
|
setOpt(Curl.option.HTTPHEADER, headerStrings);
|
|
|
|
|
2017-11-30 11:06:04 +00:00
|
|
|
let responseBodyBytes = 0;
|
2018-11-06 02:39:50 +00:00
|
|
|
const responsesDir = pathJoin(getDataDirectory(), 'responses');
|
2017-11-30 11:06:04 +00:00
|
|
|
mkdirp.sync(responsesDir);
|
|
|
|
const responseBodyPath = pathJoin(responsesDir, uuid.v4() + '.response');
|
|
|
|
const responseBodyWriteStream = fs.createWriteStream(responseBodyPath);
|
|
|
|
curl.on('end', () => responseBodyWriteStream.end());
|
|
|
|
curl.on('error', () => responseBodyWriteStream.end());
|
|
|
|
setOpt(Curl.option.WRITEFUNCTION, (buff: Buffer) => {
|
|
|
|
responseBodyBytes += buff.length;
|
|
|
|
responseBodyWriteStream.write(buff);
|
|
|
|
return buff.length;
|
|
|
|
});
|
|
|
|
|
2017-03-16 17:51:56 +00:00
|
|
|
// Handle the response ending
|
2017-11-13 23:10:53 +00:00
|
|
|
curl.on('end', async (_1, _2, rawHeaders) => {
|
|
|
|
const allCurlHeadersObjects = _parseHeaders(rawHeaders);
|
2017-03-16 17:51:56 +00:00
|
|
|
// Headers are an array (one for each redirect)
|
2018-08-01 18:28:11 +00:00
|
|
|
const lastCurlHeadersObject = allCurlHeadersObjects[allCurlHeadersObjects.length - 1];
|
2017-03-16 17:51:56 +00:00
|
|
|
|
|
|
|
// Collect various things
|
2017-12-18 18:10:34 +00:00
|
|
|
const httpVersion = lastCurlHeadersObject.version || '';
|
2017-11-13 23:10:53 +00:00
|
|
|
const statusCode = lastCurlHeadersObject.code || -1;
|
2017-12-18 18:10:34 +00:00
|
|
|
const statusMessage = lastCurlHeadersObject.reason || '';
|
2017-03-16 17:51:56 +00:00
|
|
|
|
|
|
|
// Collect the headers
|
2017-11-13 23:10:53 +00:00
|
|
|
const headers = lastCurlHeadersObject.headers;
|
2017-03-16 17:51:56 +00:00
|
|
|
|
|
|
|
// Calculate the content type
|
2017-11-20 16:07:36 +00:00
|
|
|
const contentTypeHeader = getContentTypeHeader(headers);
|
2017-03-16 17:51:56 +00:00
|
|
|
const contentType = contentTypeHeader ? contentTypeHeader.value : '';
|
|
|
|
|
|
|
|
// Update Cookie Jar
|
2017-06-15 17:52:51 +00:00
|
|
|
let currentUrl = finalUrl;
|
2017-11-13 23:10:53 +00:00
|
|
|
let setCookieStrings: Array<string> = [];
|
2017-06-15 17:52:51 +00:00
|
|
|
const jar = jarFromCookies(renderedRequest.cookieJar.cookies);
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
for (const { headers } of allCurlHeadersObjects) {
|
2017-06-15 17:52:51 +00:00
|
|
|
// Collect Set-Cookie headers
|
2017-11-13 23:10:53 +00:00
|
|
|
const setCookieHeaders = getSetCookieHeaders(headers);
|
2018-08-01 18:28:11 +00:00
|
|
|
setCookieStrings = [...setCookieStrings, ...setCookieHeaders.map(h => h.value)];
|
2017-06-15 17:52:51 +00:00
|
|
|
|
|
|
|
// Pull out new URL if there is a redirect
|
2017-11-13 23:10:53 +00:00
|
|
|
const newLocation = getLocationHeader(headers);
|
2017-06-15 17:52:51 +00:00
|
|
|
if (newLocation !== null) {
|
2017-11-13 23:10:53 +00:00
|
|
|
currentUrl = urlResolve(currentUrl, newLocation.value);
|
2017-06-15 17:52:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update jar with Set-Cookie headers
|
|
|
|
for (const setCookieStr of setCookieStrings) {
|
|
|
|
try {
|
|
|
|
jar.setCookieSync(setCookieStr, currentUrl);
|
|
|
|
} catch (err) {
|
2018-10-11 17:12:31 +00:00
|
|
|
addTimelineText(`Rejected cookie: ${err.message}`);
|
2017-05-16 17:45:09 +00:00
|
|
|
}
|
2017-06-15 17:52:51 +00:00
|
|
|
}
|
2017-03-28 22:45:23 +00:00
|
|
|
|
2017-06-15 17:52:51 +00:00
|
|
|
// Update cookie jar if we need to and if we found any cookies
|
|
|
|
if (renderedRequest.settingStoreCookies && setCookieStrings.length) {
|
2017-05-16 17:45:09 +00:00
|
|
|
const cookies = await cookiesFromJar(jar);
|
2019-09-18 00:02:42 +00:00
|
|
|
await models.cookieJar.update(renderedRequest.cookieJar, { cookies });
|
2017-06-15 17:52:51 +00:00
|
|
|
}
|
2017-05-16 17:45:09 +00:00
|
|
|
|
2017-06-15 17:52:51 +00:00
|
|
|
// Print informational message
|
|
|
|
if (setCookieStrings.length > 0) {
|
|
|
|
const n = setCookieStrings.length;
|
|
|
|
if (renderedRequest.settingStoreCookies) {
|
2018-10-11 17:12:31 +00:00
|
|
|
addTimelineText(`Saved ${n} cookie${n === 1 ? '' : 's'}`);
|
2017-06-15 17:52:51 +00:00
|
|
|
} else {
|
2018-10-11 17:12:31 +00:00
|
|
|
addTimelineText(`Ignored ${n} cookie${n === 1 ? '' : 's'}`);
|
2017-06-15 17:52:51 +00:00
|
|
|
}
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the response data
|
2017-07-20 01:55:40 +00:00
|
|
|
const responsePatch = {
|
2017-03-16 17:51:56 +00:00
|
|
|
contentType,
|
2020-01-22 19:23:19 +00:00
|
|
|
headers,
|
2017-11-13 23:10:53 +00:00
|
|
|
httpVersion,
|
2020-01-22 19:23:19 +00:00
|
|
|
statusCode,
|
2017-04-06 02:30:28 +00:00
|
|
|
statusMessage,
|
2017-11-21 17:49:17 +00:00
|
|
|
bytesContent: responseBodyBytes,
|
2020-01-22 19:23:19 +00:00
|
|
|
bytesRead: curl.getInfo(Curl.info.SIZE_DOWNLOAD),
|
|
|
|
elapsedTime: curl.getInfo(Curl.info.TOTAL_TIME) * 1000,
|
2018-12-12 17:36:11 +00:00
|
|
|
url: curl.getInfo(Curl.info.EFFECTIVE_URL),
|
2017-07-20 01:55:40 +00:00
|
|
|
};
|
2017-03-16 17:51:56 +00:00
|
|
|
|
|
|
|
// Close the request
|
2017-11-13 23:10:53 +00:00
|
|
|
curl.close();
|
2017-07-20 01:55:40 +00:00
|
|
|
|
2017-11-21 17:49:17 +00:00
|
|
|
// Make sure the response body has been fully written first
|
|
|
|
await waitForStreamToFinish(responseBodyWriteStream);
|
|
|
|
|
2019-09-18 00:02:42 +00:00
|
|
|
// Send response
|
|
|
|
await respond(responsePatch, responseBodyPath);
|
2017-03-16 17:51:56 +00:00
|
|
|
});
|
|
|
|
|
2020-05-28 17:09:51 +00:00
|
|
|
curl.on('error', async function(err, code) {
|
2017-03-16 17:51:56 +00:00
|
|
|
let error = err + '';
|
|
|
|
let statusMessage = 'Error';
|
|
|
|
|
2020-05-28 17:09:51 +00:00
|
|
|
if (code === CurlCode.CURLE_ABORTED_BY_CALLBACK) {
|
2017-03-16 17:51:56 +00:00
|
|
|
error = 'Request aborted';
|
|
|
|
statusMessage = 'Abort';
|
|
|
|
}
|
|
|
|
|
2020-05-28 17:09:51 +00:00
|
|
|
await respond(
|
2020-01-22 19:23:19 +00:00
|
|
|
{
|
|
|
|
statusMessage,
|
|
|
|
error,
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
true,
|
|
|
|
);
|
2017-03-16 17:51:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
curl.perform();
|
|
|
|
} catch (err) {
|
2020-05-28 17:09:51 +00:00
|
|
|
console.log('[network] Error', err);
|
|
|
|
await handleError(err);
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export async function sendWithSettings(
|
2017-11-10 10:50:39 +00:00
|
|
|
requestId: string,
|
2018-12-12 17:36:11 +00:00
|
|
|
requestPatch: Object,
|
2017-11-21 17:49:17 +00:00
|
|
|
): Promise<ResponsePatch> {
|
2017-11-10 10:50:39 +00:00
|
|
|
const request = await models.request.getById(requestId);
|
|
|
|
if (!request) {
|
|
|
|
throw new Error(`Failed to find request: ${requestId}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const settings = await models.settings.getOrCreate();
|
|
|
|
const ancestors = await db.withAncestors(request, [
|
2018-01-17 04:20:45 +00:00
|
|
|
models.request.type,
|
2017-11-10 10:50:39 +00:00
|
|
|
models.requestGroup.type,
|
2018-12-12 17:36:11 +00:00
|
|
|
models.workspace.type,
|
2017-11-10 10:50:39 +00:00
|
|
|
]);
|
|
|
|
|
2018-08-01 18:28:11 +00:00
|
|
|
const workspaceDoc = ancestors.find(doc => doc.type === models.workspace.type);
|
2017-11-10 10:50:39 +00:00
|
|
|
const workspaceId = workspaceDoc ? workspaceDoc._id : 'n/a';
|
|
|
|
const workspace = await models.workspace.getById(workspaceId);
|
|
|
|
if (!workspace) {
|
|
|
|
throw new Error(`Failed to find workspace for: ${requestId}`);
|
|
|
|
}
|
|
|
|
|
2018-08-01 18:28:11 +00:00
|
|
|
const workspaceMeta = await models.workspaceMeta.getOrCreateByParentId(workspace._id);
|
2017-11-10 10:50:39 +00:00
|
|
|
const environmentId: string = workspaceMeta.activeEnvironmentId || 'n/a';
|
|
|
|
|
2018-08-01 18:28:11 +00:00
|
|
|
const newRequest: Request = await models.initModel(models.request.type, requestPatch, {
|
|
|
|
_id: request._id + '.other',
|
2018-12-12 17:36:11 +00:00
|
|
|
parentId: request._id,
|
2018-08-01 18:28:11 +00:00
|
|
|
});
|
2017-11-10 10:50:39 +00:00
|
|
|
|
2020-01-22 19:23:19 +00:00
|
|
|
const environment: Environment | null = await models.environment.getById(environmentId || 'n/a');
|
|
|
|
|
2018-04-30 12:48:00 +00:00
|
|
|
let renderResult: { request: RenderedRequest, context: Object };
|
2017-11-10 10:50:39 +00:00
|
|
|
try {
|
2018-08-01 18:28:11 +00:00
|
|
|
renderResult = await getRenderedRequestAndContext(newRequest, environmentId);
|
2017-11-10 10:50:39 +00:00
|
|
|
} catch (err) {
|
|
|
|
throw new Error(`Failed to render request: ${requestId}`);
|
|
|
|
}
|
|
|
|
|
2020-01-22 19:23:19 +00:00
|
|
|
return _actuallySend(
|
|
|
|
renderResult.request,
|
|
|
|
renderResult.context,
|
|
|
|
workspace,
|
|
|
|
settings,
|
|
|
|
environment,
|
|
|
|
);
|
2017-11-10 10:50:39 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 20:34:52 +00:00
|
|
|
export async function send(
|
|
|
|
requestId: string,
|
2020-01-22 19:23:19 +00:00
|
|
|
environmentId?: string,
|
2019-05-15 20:01:36 +00:00
|
|
|
extraInfo?: ExtraRenderInfo,
|
2019-05-04 20:34:52 +00:00
|
|
|
): Promise<ResponsePatch> {
|
2020-06-30 19:36:15 +00:00
|
|
|
console.log(`[network] Sending req=${requestId} env=${environmentId || 'null'}`);
|
2019-05-15 20:01:36 +00:00
|
|
|
|
2017-06-09 03:02:16 +00:00
|
|
|
// HACK: wait for all debounces to finish
|
|
|
|
/*
|
|
|
|
* TODO: Do this in a more robust way
|
|
|
|
* The following block adds a "long" delay to let potential debounces and
|
|
|
|
* database updates finish before making the request. This is done by tracking
|
|
|
|
* the time of the user's last keypress and making sure the request is sent a
|
|
|
|
* significant time after the last press.
|
|
|
|
*/
|
|
|
|
const timeSinceLastInteraction = Date.now() - lastUserInteraction;
|
|
|
|
const delayMillis = Math.max(0, MAX_DELAY_TIME - timeSinceLastInteraction);
|
|
|
|
if (delayMillis > 0) {
|
2017-11-20 16:07:36 +00:00
|
|
|
await delay(delayMillis);
|
2017-06-09 03:02:16 +00:00
|
|
|
}
|
2017-03-16 17:51:56 +00:00
|
|
|
|
2017-06-09 03:02:16 +00:00
|
|
|
// Fetch some things
|
2017-03-16 17:51:56 +00:00
|
|
|
const request = await models.request.getById(requestId);
|
|
|
|
const settings = await models.settings.getOrCreate();
|
2017-04-21 04:30:52 +00:00
|
|
|
const ancestors = await db.withAncestors(request, [
|
2018-03-06 02:26:59 +00:00
|
|
|
models.request.type,
|
2017-04-21 04:30:52 +00:00
|
|
|
models.requestGroup.type,
|
2018-12-12 17:36:11 +00:00
|
|
|
models.workspace.type,
|
2017-04-21 04:30:52 +00:00
|
|
|
]);
|
2017-06-15 17:52:51 +00:00
|
|
|
|
2017-07-20 01:55:40 +00:00
|
|
|
if (!request) {
|
|
|
|
throw new Error(`Failed to find request to send for ${requestId}`);
|
|
|
|
}
|
|
|
|
|
2020-01-22 19:23:19 +00:00
|
|
|
const environment: Environment | null = await models.environment.getById(environmentId || 'n/a');
|
|
|
|
|
2017-12-21 14:01:51 +00:00
|
|
|
const renderResult = await getRenderedRequestAndContext(
|
|
|
|
request,
|
2020-01-22 19:23:19 +00:00
|
|
|
environmentId || null,
|
2018-12-12 17:36:11 +00:00
|
|
|
RENDER_PURPOSE_SEND,
|
2019-05-15 20:01:36 +00:00
|
|
|
extraInfo,
|
2017-12-21 14:01:51 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const renderedRequestBeforePlugins = renderResult.request;
|
|
|
|
const renderedContextBeforePlugins = renderResult.context;
|
2017-07-20 01:55:40 +00:00
|
|
|
|
2018-08-01 18:28:11 +00:00
|
|
|
const workspaceDoc = ancestors.find(doc => doc.type === models.workspace.type);
|
|
|
|
const workspace = await models.workspace.getById(workspaceDoc ? workspaceDoc._id : 'n/a');
|
2017-11-26 13:42:43 +00:00
|
|
|
if (!workspace) {
|
|
|
|
throw new Error(`Failed to find workspace for request: ${requestId}`);
|
|
|
|
}
|
|
|
|
|
2017-07-20 01:55:40 +00:00
|
|
|
let renderedRequest: RenderedRequest;
|
|
|
|
try {
|
2017-11-26 13:42:43 +00:00
|
|
|
renderedRequest = await _applyRequestPluginHooks(
|
|
|
|
renderedRequestBeforePlugins,
|
2018-12-12 17:36:11 +00:00
|
|
|
renderedContextBeforePlugins,
|
2017-11-26 13:42:43 +00:00
|
|
|
);
|
2017-07-20 01:55:40 +00:00
|
|
|
} catch (err) {
|
|
|
|
return {
|
2020-01-22 19:23:19 +00:00
|
|
|
environmentId: environmentId,
|
2018-04-05 12:35:56 +00:00
|
|
|
error: err.message,
|
2020-01-22 19:23:19 +00:00
|
|
|
parentId: renderedRequestBeforePlugins._id,
|
2018-04-05 12:35:56 +00:00
|
|
|
settingSendCookies: renderedRequestBeforePlugins.settingSendCookies,
|
2018-12-12 17:36:11 +00:00
|
|
|
settingStoreCookies: renderedRequestBeforePlugins.settingStoreCookies,
|
2020-01-22 19:23:19 +00:00
|
|
|
statusCode: STATUS_CODE_PLUGIN_ERROR,
|
|
|
|
statusMessage: err.plugin ? `Plugin ${err.plugin.name}` : 'Plugin',
|
|
|
|
url: renderedRequestBeforePlugins.url,
|
2017-07-20 01:55:40 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-15 20:01:36 +00:00
|
|
|
const response = await _actuallySend(
|
|
|
|
renderedRequest,
|
|
|
|
renderedContextBeforePlugins,
|
|
|
|
workspace,
|
|
|
|
settings,
|
2020-01-22 19:23:19 +00:00
|
|
|
environment,
|
2019-05-15 20:01:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
response.error
|
|
|
|
? `[network] Response failed req=${requestId} err=${response.error || 'n/a'}`
|
|
|
|
: `[network] Response succeeded req=${requestId} status=${response.statusCode || '?'}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
return response;
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
2017-06-09 03:02:16 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
async function _applyRequestPluginHooks(
|
2017-10-10 08:58:13 +00:00
|
|
|
renderedRequest: RenderedRequest,
|
2018-12-12 17:36:11 +00:00
|
|
|
renderedContext: Object,
|
2017-10-10 08:58:13 +00:00
|
|
|
): Promise<RenderedRequest> {
|
2018-06-06 20:48:14 +00:00
|
|
|
const newRenderedRequest = clone(renderedRequest);
|
2018-06-25 17:42:50 +00:00
|
|
|
for (const { plugin, hook } of await plugins.getRequestHooks()) {
|
2017-07-20 01:55:40 +00:00
|
|
|
const context = {
|
2020-04-09 17:32:19 +00:00
|
|
|
...(pluginContexts.app.init(RENDER_PURPOSE_NO_RENDER): Object),
|
|
|
|
...(pluginContexts.data.init(): Object),
|
|
|
|
...(pluginContexts.store.init(plugin): Object),
|
|
|
|
...(pluginContexts.request.init(newRenderedRequest, renderedContext): Object),
|
2020-09-29 02:10:51 +00:00
|
|
|
...(pluginContexts.network.init(renderedContext.getEnvironmentId()): Object),
|
2017-07-20 01:55:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
await hook(context);
|
|
|
|
} catch (err) {
|
|
|
|
err.plugin = plugin;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newRenderedRequest;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
async function _applyResponsePluginHooks(
|
2018-04-25 18:24:52 +00:00
|
|
|
response: ResponsePatch,
|
2020-09-29 02:10:51 +00:00
|
|
|
renderedRequest: RenderedRequest,
|
|
|
|
renderedContext: Object,
|
2018-06-06 20:48:14 +00:00
|
|
|
): Promise<ResponsePatch> {
|
|
|
|
const newResponse = clone(response);
|
2020-09-29 02:10:51 +00:00
|
|
|
const newRequest = clone(renderedRequest);
|
2018-06-06 20:48:14 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
for (const { plugin, hook } of await plugins.getResponseHooks()) {
|
2017-07-20 01:55:40 +00:00
|
|
|
const context = {
|
2020-04-09 17:32:19 +00:00
|
|
|
...(pluginContexts.app.init(RENDER_PURPOSE_NO_RENDER): Object),
|
|
|
|
...(pluginContexts.data.init(): Object),
|
|
|
|
...(pluginContexts.store.init(plugin): Object),
|
|
|
|
...(pluginContexts.response.init(newResponse): Object),
|
2020-09-29 02:10:51 +00:00
|
|
|
...(pluginContexts.request.init(newRequest, renderedContext, true): Object),
|
|
|
|
...(pluginContexts.network.init(renderedContext.getEnvironmentId()): Object),
|
2017-07-20 01:55:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
await hook(context);
|
|
|
|
} catch (err) {
|
|
|
|
err.plugin = plugin;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2018-06-06 20:48:14 +00:00
|
|
|
|
|
|
|
return newResponse;
|
2017-07-20 01:55:40 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function _parseHeaders(
|
2018-12-12 17:36:11 +00:00
|
|
|
buffer: Buffer,
|
2018-06-25 17:42:50 +00:00
|
|
|
): Array<{
|
|
|
|
headers: Array<ResponseHeader>,
|
|
|
|
version: string,
|
|
|
|
code: number,
|
2018-12-12 17:36:11 +00:00
|
|
|
reason: string,
|
2018-06-25 17:42:50 +00:00
|
|
|
}> {
|
2017-11-13 23:10:53 +00:00
|
|
|
const results = [];
|
|
|
|
|
|
|
|
const lines = buffer.toString('utf8').split(/\r?\n|\r/g);
|
|
|
|
|
|
|
|
for (let i = 0, currentResult = null; i < lines.length; i++) {
|
|
|
|
const line = lines[i];
|
|
|
|
const isEmptyLine = line.trim() === '';
|
|
|
|
|
|
|
|
// If we hit an empty line, start parsing the next response
|
|
|
|
if (isEmptyLine && currentResult) {
|
|
|
|
results.push(currentResult);
|
|
|
|
currentResult = null;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!currentResult) {
|
|
|
|
const [version, code, ...other] = line.split(/ +/g);
|
|
|
|
currentResult = {
|
|
|
|
version,
|
|
|
|
code: parseInt(code, 10),
|
|
|
|
reason: other.join(' '),
|
2018-12-12 17:36:11 +00:00
|
|
|
headers: [],
|
2017-11-13 23:10:53 +00:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
const [name, value] = line.split(/:\s(.+)/);
|
2020-05-28 17:09:51 +00:00
|
|
|
const header: ResponseHeader = {
|
|
|
|
name,
|
|
|
|
value: value || '',
|
|
|
|
};
|
2017-11-13 23:10:53 +00:00
|
|
|
currentResult.headers.push(header);
|
|
|
|
}
|
2017-06-15 17:52:51 +00:00
|
|
|
}
|
2017-11-13 23:10:53 +00:00
|
|
|
|
|
|
|
return results;
|
2017-06-15 17:52:51 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 21:01:14 +00:00
|
|
|
// exported for unit tests only
|
2018-06-25 17:42:50 +00:00
|
|
|
export function _getAwsAuthHeaders(
|
2018-04-30 12:48:00 +00:00
|
|
|
credentials: {
|
|
|
|
accessKeyId: string,
|
|
|
|
secretAccessKey: string,
|
2018-12-12 17:36:11 +00:00
|
|
|
sessionToken: string,
|
2018-04-30 12:48:00 +00:00
|
|
|
},
|
2017-07-19 02:54:03 +00:00
|
|
|
headers: Array<RequestHeader>,
|
2017-07-18 22:10:57 +00:00
|
|
|
body: string,
|
2017-10-10 08:58:13 +00:00
|
|
|
url: string,
|
2018-04-30 12:48:00 +00:00
|
|
|
method: string,
|
|
|
|
region?: string,
|
2018-12-12 17:36:11 +00:00
|
|
|
service?: string,
|
2019-11-25 22:48:30 +00:00
|
|
|
): Array<{ name: string, value: string, description?: string, disabled?: boolean }> {
|
2017-07-12 21:01:14 +00:00
|
|
|
const parsedUrl = urlParse(url);
|
2017-11-20 16:07:36 +00:00
|
|
|
const contentTypeHeader = getContentTypeHeader(headers);
|
2017-07-12 21:20:31 +00:00
|
|
|
|
2018-02-08 09:09:13 +00:00
|
|
|
// AWS uses host header for signing so prioritize that if the user set it manually
|
|
|
|
const hostHeader = getHostHeader(headers);
|
|
|
|
const host = hostHeader ? hostHeader.value : parsedUrl.host;
|
|
|
|
|
2017-07-12 21:20:31 +00:00
|
|
|
const awsSignOptions = {
|
2018-04-30 12:48:00 +00:00
|
|
|
service,
|
|
|
|
region,
|
2018-10-11 17:12:31 +00:00
|
|
|
host,
|
2017-07-12 21:20:31 +00:00
|
|
|
body,
|
2017-10-10 08:58:13 +00:00
|
|
|
method,
|
2017-07-12 21:01:14 +00:00
|
|
|
path: parsedUrl.path,
|
2018-12-12 17:36:11 +00:00
|
|
|
headers: contentTypeHeader ? { 'content-type': contentTypeHeader.value } : {},
|
2017-07-12 21:01:14 +00:00
|
|
|
};
|
2017-07-12 21:20:31 +00:00
|
|
|
|
|
|
|
const signature = aws4.sign(awsSignOptions, credentials);
|
|
|
|
|
2017-07-12 21:01:14 +00:00
|
|
|
return Object.keys(signature.headers)
|
2017-07-12 21:20:31 +00:00
|
|
|
.filter(name => name !== 'content-type') // Don't add this because we already have it
|
2020-05-28 17:09:51 +00:00
|
|
|
.map(name => ({
|
|
|
|
name,
|
|
|
|
value: signature.headers[name],
|
|
|
|
}));
|
2017-07-12 21:01:14 +00:00
|
|
|
}
|
|
|
|
|
2019-04-27 08:46:10 +00:00
|
|
|
function storeTimeline(timeline: Array<ResponseTimelineEntry>): Promise<string> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const timelineStr = JSON.stringify(timeline, null, '\t');
|
|
|
|
const timelineHash = crypto
|
|
|
|
.createHash('sha1')
|
|
|
|
.update(timelineStr)
|
|
|
|
.digest('hex');
|
|
|
|
const responsesDir = pathJoin(getDataDirectory(), 'responses');
|
|
|
|
mkdirp.sync(responsesDir);
|
|
|
|
const timelinePath = pathJoin(responsesDir, timelineHash + '.timeline');
|
|
|
|
fs.writeFile(timelinePath, timelineStr, err => {
|
|
|
|
if (err != null) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(timelinePath);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-30 19:36:15 +00:00
|
|
|
if (global.document) {
|
|
|
|
document.addEventListener('keydown', (e: KeyboardEvent) => {
|
|
|
|
if (e.ctrlKey || e.metaKey || e.altKey) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-09 03:02:16 +00:00
|
|
|
|
2020-06-30 19:36:15 +00:00
|
|
|
lastUserInteraction = Date.now();
|
|
|
|
});
|
2017-07-10 22:34:22 +00:00
|
|
|
|
2020-06-30 19:36:15 +00:00
|
|
|
document.addEventListener('paste', (e: Event) => {
|
|
|
|
lastUserInteraction = Date.now();
|
|
|
|
});
|
|
|
|
}
|