insomnia/packages/insomnia-app/app/network/network.ts

1175 lines
37 KiB
TypeScript
Raw Normal View History

2021-07-22 23:04:56 +00:00
import aws4 from 'aws4';
import clone from 'clone';
import crypto from 'crypto';
import fs from 'fs';
import { cookiesFromJar, jarFromCookies } from 'insomnia-cookies';
import {
2021-07-22 23:04:56 +00:00
buildQueryStringFromParams,
joinUrlAndQueryString,
setDefaultProtocol,
smartEncodeUrl,
} from 'insomnia-url';
import mkdirp from 'mkdirp';
import {
Curl,
CurlAuth,
CurlCode,
CurlFeature,
CurlHttpVersion,
2021-07-22 23:04:56 +00:00
CurlInfoDebug,
CurlNetrc,
} from 'node-libcurl';
2018-06-25 17:42:50 +00:00
import { join as pathJoin } from 'path';
2021-07-22 23:04:56 +00:00
import { parse as urlParse, resolve as urlResolve } from 'url';
import * as uuid from 'uuid';
2021-07-22 23:04:56 +00:00
import {
AUTH_AWS_IAM,
AUTH_DIGEST,
AUTH_NETRC,
AUTH_NTLM,
CONTENT_TYPE_FORM_DATA,
CONTENT_TYPE_FORM_URLENCODED,
getAppVersion,
HttpVersions,
STATUS_CODE_PLUGIN_ERROR,
} from '../common/constants';
2021-07-22 23:04:56 +00:00
import { database as db } from '../common/database';
import { getDataDirectory, getTempDir } from '../common/electron-helpers';
import {
delay,
describeByteSize,
getContentTypeHeader,
getHostHeader,
getLocationHeader,
getSetCookieHeaders,
hasAcceptEncodingHeader,
hasAcceptHeader,
hasAuthHeader,
hasContentTypeHeader,
hasUserAgentHeader,
waitForStreamToFinish,
} from '../common/misc';
2021-07-22 23:04:56 +00:00
import type { ExtraRenderInfo, RenderedRequest } from '../common/render';
2018-06-25 17:42:50 +00:00
import {
2021-07-22 23:04:56 +00:00
getRenderedRequestAndContext,
RENDER_PURPOSE_NO_RENDER,
RENDER_PURPOSE_SEND,
} from '../common/render';
import * as models from '../models';
import type { Environment } from '../models/environment';
import type { Request, RequestHeader } from '../models/request';
import type { ResponseHeader, ResponseTimelineEntry } from '../models/response';
import type { Settings } from '../models/settings';
import { isWorkspace, Workspace } from '../models/workspace';
import * as pluginContexts from '../plugins/context/index';
2021-07-22 23:04:56 +00:00
import * as plugins from '../plugins/index';
2018-06-25 17:42:50 +00:00
import { getAuthHeader } from './authentication';
2021-07-22 23:04:56 +00:00
import caCerts from './ca-certs';
2018-06-25 17:42:50 +00:00
import { buildMultipart } from './multipart';
2021-07-22 23:04:56 +00:00
import { urlMatchesCertHost } from './url-matches-cert-host';
export interface ResponsePatch {
bodyCompression?: 'zip' | null;
bodyPath?: string;
bytesContent?: number;
bytesRead?: number;
contentType?: string;
elapsedTime: number;
environmentId?: string | null;
error?: string;
headers?: ResponseHeader[];
httpVersion?: string;
message?: string;
parentId?: string;
settingSendCookies?: boolean;
settingStoreCookies?: boolean;
statusCode?: number;
statusMessage?: string;
timelinePath?: string;
url?: string;
}
2017-07-18 20:38:19 +00:00
// Time since user's last keypress to wait before making the request
const MAX_DELAY_TIME = 1000;
// Special header value that will prevent the header being sent
const DISABLE_HEADER_VALUE = '__Di$aB13d__';
// 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',
'': '',
};
const cancelRequestFunctionMap = {};
let lastUserInteraction = Date.now();
export async function cancelRequestById(requestId) {
if (hasCancelFunctionForId(requestId)) {
const cancelRequestFunction = cancelRequestFunctionMap[requestId];
if (typeof cancelRequestFunction === 'function') {
return cancelRequestFunction();
}
}
console.log(`[network] Failed to cancel req=${requestId} because cancel function not found`);
}
function clearCancelFunctionForId(requestId) {
if (hasCancelFunctionForId(requestId)) {
delete cancelRequestFunctionMap[requestId];
}
}
export function hasCancelFunctionForId(requestId) {
return cancelRequestFunctionMap.hasOwnProperty(requestId);
}
2018-06-25 17:42:50 +00:00
export async function _actuallySend(
2017-07-18 22:10:57 +00:00
renderedRequest: RenderedRequest,
renderContext: Record<string, any>,
2017-07-18 22:10:57 +00:00
workspace: Workspace,
settings: Settings,
fix node-libcurl imports (#3384) * fix node-libcurl imports * removes unnecessary function wrapper and anyway, `number` is not the correct type (which is what motivated this change in the first place), `CurlFeature` is. * updates type for setOpt see https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgOIBs4GcvIN4BQyxyWwAXhAFzIDkWAtnOurcgD50MQAmwArgzadamKAHMItANwEAvgQKhIsRCgDCAe00BrYCkIlkYAJ4AHanSz9xcKMLoIAFpoSbMkdU+BmZ8xQD0AcjK0PBIyJoAbtDomnA8WErgYWrIAIIIYMCaIPhEJBBwYACMABSaZtm5NLQAssDoOrQANMjimDg0GNhYAJQ0UZrAPLJGRaUVVTkgtVq6+lityG4LlvN6EAPIQyOyCgRuIFhgyIjVsxlZM8gAvPnjxeWVF208xXB9+MhByGZQmgARugIAwaKYLCFcCBNKdQDBoFBeGcQDxkABJZBOOAxZAw5AAdzgJmMmmM5hQwFOsKc0AJwCwEAKRmIRyw7ggADo4uIyrRzjNORMSss8JFprk3h9kHI+syZf5DrkTshhQB1KBwMwWKA0TIXADatGFtAAundkFNXsh3mBPncAHzfX7-IEgsHkyEMvGwkIgBFQJFouCojFYnEofFEklgMkQynUsC0qD0xnytkc7maXnGp6i8XW21wGV9MaFJ4arU6q0zKV20uKgi-JHifhiZAwfgga65SIxKBxBJJTvdi6q4oAJhrNToDSayw6vW6nX6g2GowII57eQmU5eMzm2k2Szaq02NA2+m2uw3W7Hu+nIDr9rFLoBwNB4IpUJ9cP9iOREM0UxbFcSjYlSU9BNIiTOkGRQMpvQAAxDEwkOMWlIhAdASX3XIAH5iHSZAuxuBkQFoU43EDCAslVKlk05OUjAzEEsxzFs2zsDsu23fM8KfG1pVlRU2VOXdK21aAvwsTQYHHMAJwtR9ny+W4nVfYJXQ-D14x-GE-wDIMUWA8MwLJaNIL0qkYOTVMmRY5VMx5Pld2QSSdVoBtHkUlShPrfZAmCWDGUJTQoB0NpsFVAAPCwsl4KgmyCFLUrS9KMsyrKUoIAVciFJ4+TnZo2jFMhKFqRhmFYEtZHVTUpKgIrGhK75yssegmBYNhZTqydmvnUrSAoDqqu62qCAkhrq3qFr83ayqupq3rFDykACsmWhLwgE9vnjWpnFcdxiggLwfB6uV6qraA+W23axX2xwXDcDxTu8XwS0m-qtqPRZ80e2hDpek6zo+kSpuupqfrWe6oIO57js8d6LqCjCdpQAAqHgyQMjGwoiqLcAgOLaMgHgkuyymqepoJkuQAABMAsAAWmJ+KwFZwNwuQQEEg7YAoBVOxW24cBcu3DbymhzZ-opeGjte0GUd+RmWbZ0nOYBKAeb5mABaFiRBAgMWrsa27fqkIaAaBxG3vOz6VaZ1mSayTXud5tE9cF05haNk3vu22WLHl4GkftkTHbVl2OcRd3df1n3DdFsAvsUjybul-Qg46m3FeRz66dV532bd7WPdIWjcmDJPjZTtbJfNmHs5D23meccO5Uj4uNdjsu+cZI5q5F2vU5KdOobu5unoVk62-ziPgiL9XXd7nW0QHquzhr-3fMznap8BhHXrnjvC6d5eY65vv18r0NfeT1OJ3HxvjwP3PZ-bsG5SAA to see why `any` is required, for now (at least) * updates the getBodyBuffer parse calls now that the types are working * return to prior approach, this time with a warning
2021-05-17 13:59:43 +00:00
environment?: Environment | null,
) {
return new Promise<ResponsePatch>(async resolve => {
const timeline: ResponseTimelineEntry[] = [];
2017-07-11 00:23:40 +00:00
function addTimeline(name, value) {
timeline.push({
name,
value,
timestamp: Date.now(),
});
}
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,
noPlugins = false,
) {
const timelinePath = await storeTimeline(timeline);
// Tear Down the cancellation logic
clearCancelFunctionForId(renderedRequest._id);
const environmentId = environment ? environment._id : null;
2018-06-25 17:42:50 +00:00
const responsePatchBeforeHooks = Object.assign(
{
timelinePath,
environmentId,
2018-06-25 17:42:50 +00:00
parentId: renderedRequest._id,
bodyCompression: null,
// Will default to .zip otherwise
2018-06-25 17:42:50 +00:00
bodyPath: bodyPath || '',
settingSendCookies: renderedRequest.settingSendCookies,
settingStoreCookies: renderedRequest.settingStoreCookies,
} as ResponsePatch,
patch,
2018-06-25 17:42:50 +00:00
);
2017-07-11 00:23:40 +00:00
if (noPlugins) {
resolve(responsePatchBeforeHooks);
return;
}
let responsePatch: ResponsePatch | null = null;
try {
2018-06-25 17:42:50 +00:00
responsePatch = await _applyResponsePluginHooks(
responsePatchBeforeHooks,
renderedRequest,
renderContext,
2018-06-25 17:42:50 +00:00
);
} catch (err) {
await handleError(
new Error(`[plugin] Response hook failed plugin=${err.plugin.name} err=${err.message}`),
2018-06-25 17:42:50 +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 */
async function handleError(err: Error) {
await respond(
2018-06-25 17:42:50 +00:00
{
url: renderedRequest.url,
parentId: renderedRequest._id,
error: err.message,
elapsedTime: 0, // 0 because this path is hit during plugin calls
2018-06-25 17:42:50 +00:00
statusMessage: 'Error',
settingSendCookies: renderedRequest.settingSendCookies,
settingStoreCookies: renderedRequest.settingStoreCookies,
2018-06-25 17:42:50 +00:00
},
null,
true,
2018-06-25 17:42:50 +00:00
);
}
2017-07-19 04:48:28 +00:00
/** Helper function to set Curl options */
fix node-libcurl imports (#3384) * fix node-libcurl imports * removes unnecessary function wrapper and anyway, `number` is not the correct type (which is what motivated this change in the first place), `CurlFeature` is. * updates type for setOpt see https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgOIBs4GcvIN4BQyxyWwAXhAFzIDkWAtnOurcgD50MQAmwArgzadamKAHMItANwEAvgQKhIsRCgDCAe00BrYCkIlkYAJ4AHanSz9xcKMLoIAFpoSbMkdU+BmZ8xQD0Acj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bymhzZ-opeGjte0GUd+RmWbZ0nOYBKAeb5mABaFiRBAgMWrsa27fqkIaAaBxG3vOz6VaZ1mSayTXud5tE9cF05haNk3vu22WLHl4GkftkTHbVl2OcRd3df1n3DdFsAvsUjybul-Qg46m3FeRz66dV532bd7WPdIWjcmDJPjZTtbJfNmHs5D23meccO5Uj4uNdjsu+cZI5q5F2vU5KdOobu5unoVk62-ziPgiL9XXd7nW0QHquzhr-3fMznap8BhHXrnjvC6d5eY65vv18r0NfeT1OJ3HxvjwP3PZ-bsG5SAA to see why `any` is required, for now (at least) * updates the getBodyBuffer parse calls now that the types are working * return to prior approach, this time with a warning
2021-05-17 13:59:43 +00:00
const setOpt: typeof curl.setOpt = (opt: any, val: any) => {
2017-07-19 04:48:28 +00:00
try {
fix node-libcurl imports (#3384) * fix node-libcurl imports * removes unnecessary function wrapper and anyway, `number` is not the correct type (which is what motivated this change in the first place), `CurlFeature` is. * updates type for setOpt see https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgOIBs4GcvIN4BQyxyWwAXhAFzIDkWAtnOurcgD50MQAmwArgzadamKAHMItANwEAvgQKhIsRCgDCAe00BrYCkIlkYAJ4AHanSz9xcKMLoIAFpoSbMkdU+BmZ8xQD0AcjK0PBIyJoAbtDomnA8WErgYWrIAIIIYMCaIPhEJBBwYACMABSaZtm5NLQAssDoOrQANMjimDg0GNhYAJQ0UZrAPLJGRaUVVTkgtVq6+lityG4LlvN6EAPIQyOyCgRuIFhgyIjVsxlZM8gAvPnjxeWVF208xXB9+MhByGZQmgARugIAwaKYLCFcCBNKdQDBoFBeGcQDxkABJZBOOAxZAw5AAdzgJmMmmM5hQwFOsKc0AJwCwEAKRmIRyw7ggADo4uIyrRzjNORMSss8JFprk3h9kHI+syZf5DrkTshhQB1KBwMwWKA0TIXADatGFtAAundkFNXsh3mBPncAHzfX7-IEgsHkyEMvGwkIgBFQJFouCojFYnEofFEklgMkQynUsC0qD0xnytkc7maXnGp6i8XW21wGV9MaFJ4arU6q0zKV20uKgi-JHifhiZAwfgga65SIxKBxBJJTvdi6q4oAJhrNToDSayw6vW6nX6g2GowII57eQmU5eMzm2k2Szaq02NA2+m2uw3W7Hu+nIDr9rFLoBwNB4IpUJ9cP9iOREM0UxbFcSjYlSU9BNIiTOkGRQMpvQAAxDEwkOMWlIhAdASX3XIAH5iHSZAuxuBkQFoU43EDCAslVKlk05OUjAzEEsxzFs2zsDsu23fM8KfG1pVlRU2VOXdK21aAvwsTQYHHMAJwtR9ny+W4nVfYJXQ-D14x-GE-wDIMUWA8MwLJaNIL0qkYOTVMmRY5VMx5Pld2QSSdVoBtHkUlShPrfZAmCWDGUJTQoB0NpsFVAAPCwsl4KgmyCFLUrS9KMsyrKUoIAVciFJ4+TnZo2jFMhKFqRhmFYEtZHVTUpKgIrGhK75yssegmBYNhZTqydmvnUrSAoDqqu62qCAkhrq3qFr83ayqupq3rFDykACsmWhLwgE9vnjWpnFcdxiggLwfB6uV6qraA+W23axX2xwXDcDxTu8XwS0m-qtqPRZ80e2hDpek6zo+kSpuupqfrWe6oIO57js8d6LqCjCdpQAAqHgyQMjGwoiqLcAgOLaMgHgkuyymqepoJkuQAABMAsAAWmJ+KwFZwNwuQQEEg7YAoBVOxW24cBcu3DbymhzZ-opeGjte0GUd+RmWbZ0nOYBKAeb5mABaFiRBAgMWrsa27fqkIaAaBxG3vOz6VaZ1mSayTXud5tE9cF05haNk3vu22WLHl4GkftkTHbVl2OcRd3df1n3DdFsAvsUjybul-Qg46m3FeRz66dV532bd7WPdIWjcmDJPjZTtbJfNmHs5D23meccO5Uj4uNdjsu+cZI5q5F2vU5KdOobu5unoVk62-ziPgiL9XXd7nW0QHquzhr-3fMznap8BhHXrnjvC6d5eY65vv18r0NfeT1OJ3HxvjwP3PZ-bsG5SAA to see why `any` is required, for now (at least) * updates the getBodyBuffer parse calls now that the types are working * return to prior approach, this time with a warning
2021-05-17 13:59:43 +00:00
return curl.setOpt(opt, val);
2017-07-19 04:48:28 +00:00
} catch (err) {
const name = Object.keys(Curl.option).find(name => Curl.option[name] === opt);
fix node-libcurl imports (#3384) * fix node-libcurl imports * removes unnecessary function wrapper and anyway, `number` is not the correct type (which is what motivated this change in the first place), `CurlFeature` is. * updates type for setOpt see https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgOIBs4GcvIN4BQyxyWwAXhAFzIDkWAtnOurcgD50MQAmwArgzadamKAHMItANwEAvgQKhIsRCgDCAe00BrYCkIlkYAJ4AHanSz9xcKMLoIAFpoSbMkdU+BmZ8xQD0AcjK0PBIyJoAbtDomnA8WErgYWrIAIIIYMCaIPhEJBBwYACMABSaZtm5NLQAssDoOrQANMjimDg0GNhYAJQ0UZrAPLJGRaUVVTkgtVq6+lityG4LlvN6EAPIQyOyCgRuIFhgyIjVsxlZM8gAvPnjxeWVF208xXB9+MhByGZQmgARugIAwaKYLCFcCBNKdQDBoFBeGcQDxkABJZBOOAxZAw5AAdzgJmMmmM5hQwFOsKc0AJwCwEAKRmIRyw7ggADo4uIyrRzjNORMSss8JFprk3h9kHI+syZf5DrkTshhQB1KBwMwWKA0TIXADatGFtAAundkFNXsh3mBPncAHzfX7-IEgsHkyEMvGwkIgBFQJFouCojFYnEofFEklgMkQynUsC0qD0xnytkc7maXnGp6i8XW21wGV9MaFJ4arU6q0zKV20uKgi-JHifhiZAwfgga65SIxKBxBJJTvdi6q4oAJhrNToDSayw6vW6nX6g2GowII57eQmU5eMzm2k2Szaq02NA2+m2uw3W7Hu+nIDr9rFLoBwNB4IpUJ9cP9iOREM0UxbFcSjYlSU9BNIiTOkGRQMpvQAAxDEwkOMWlIhAdASX3XIAH5iHSZAuxuBkQFoU43EDCAslVKlk05OUjAzEEsxzFs2zsDsu23fM8KfG1pVlRU2VOXdK21aAvwsTQYHHMAJwtR9ny+W4nVfYJXQ-D14x-GE-wDIMUWA8MwLJaNIL0qkYOTVMmRY5VMx5Pld2QSSdVoBtHkUlShPrfZAmCWDGUJTQoB0NpsFVAAPCwsl4KgmyCFLUrS9KMsyrKUoIAVciFJ4+TnZo2jFMhKFqRhmFYEtZHVTUpKgIrGhK75yssegmBYNhZTqydmvnUrSAoDqqu62qCAkhrq3qFr83ayqupq3rFDykACsmWhLwgE9vnjWpnFcdxiggLwfB6uV6qraA+W23axX2xwXDcDxTu8XwS0m-qtqPRZ80e2hDpek6zo+kSpuupqfrWe6oIO57js8d6LqCjCdpQAAqHgyQMjGwoiqLcAgOLaMgHgkuyymqepoJkuQAABMAsAAWmJ+KwFZwNwuQQEEg7YAoBVOxW24cBcu3DbymhzZ-opeGjte0GUd+RmWbZ0nOYBKAeb5mABaFiRBAgMWrsa27fqkIaAaBxG3vOz6VaZ1mSayTXud5tE9cF05haNk3vu22WLHl4GkftkTHbVl2OcRd3df1n3DdFsAvsUjybul-Qg46m3FeRz66dV532bd7WPdIWjcmDJPjZTtbJfNmHs5D23meccO5Uj4uNdjsu+cZI5q5F2vU5KdOobu5unoVk62-ziPgiL9XXd7nW0QHquzhr-3fMznap8BhHXrnjvC6d5eY65vv18r0NfeT1OJ3HxvjwP3PZ-bsG5SAA to see why `any` is required, for now (at least) * updates the getBodyBuffer parse calls now that the types are working * return to prior approach, this time with a warning
2021-05-17 13:59:43 +00:00
throw new Error(`${err.message} (${opt} ${name || 'n/a'})`);
2017-07-19 04:48:28 +00:00
}
fix node-libcurl imports (#3384) * fix node-libcurl imports * removes unnecessary function wrapper and anyway, `number` is not the correct type (which is what motivated this change in the first place), `CurlFeature` is. * updates type for setOpt see https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgOIBs4GcvIN4BQyxyWwAXhAFzIDkWAtnOurcgD50MQAmwArgzadamKAHMItANwEAvgQKhIsRCgDCAe00BrYCkIlkYAJ4AHanSz9xcKMLoIAFpoSbMkdU+BmZ8xQD0AcjK0PBIyJoAbtDomnA8WErgYWrIAIIIYMCaIPhEJBBwYACMABSaZtm5NLQAssDoOrQANMjimDg0GNhYAJQ0UZrAPLJGRaUVVTkgtVq6+lityG4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Vl2OcRd3df1n3DdFsAvsUjybul-Qg46m3FeRz66dV532bd7WPdIWjcmDJPjZTtbJfNmHs5D23meccO5Uj4uNdjsu+cZI5q5F2vU5KdOobu5unoVk62-ziPgiL9XXd7nW0QHquzhr-3fMznap8BhHXrnjvC6d5eY65vv18r0NfeT1OJ3HxvjwP3PZ-bsG5SAA to see why `any` is required, for now (at least) * updates the getBodyBuffer parse calls now that the types are working * return to prior approach, this time with a warning
2021-05-17 13:59:43 +00:00
};
2017-11-13 23:10:53 +00:00
2017-07-19 04:48:28 +00:00
try {
// Setup the cancellation logic
cancelRequestFunctionMap[renderedRequest._id] = async () => {
await respond(
2018-06-25 17:42:50 +00:00
{
elapsedTime: (curl.getInfo(Curl.info.TOTAL_TIME) as number || 0) * 1000,
// @ts-expect-error -- needs generic
2018-06-25 17:42:50 +00:00
bytesRead: curl.getInfo(Curl.info.SIZE_DOWNLOAD),
// @ts-expect-error -- needs generic
2018-06-25 17:42:50 +00:00
url: curl.getInfo(Curl.info.EFFECTIVE_URL),
statusMessage: 'Cancelled',
error: 'Request was cancelled',
2018-06-25 17:42:50 +00:00
},
null,
true,
2018-06-25 17:42:50 +00:00
);
// Kill it!
curl.close();
};
// Set all the basic options
setOpt(Curl.option.VERBOSE, true);
// True so debug function works\
setOpt(Curl.option.NOPROGRESS, true);
// True so curl doesn't print progress
setOpt(Curl.option.ACCEPT_ENCODING, '');
// Auto decode everything
fix node-libcurl imports (#3384) * fix node-libcurl imports * removes unnecessary function wrapper and anyway, `number` is not the correct type (which is what motivated this change in the first place), `CurlFeature` is. * updates type for setOpt see https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgOIBs4GcvIN4BQyxyWwAXhAFzIDkWAtnOurcgD50MQAmwArgzadamKAHMItANwEAvgQKhIsRCgDCAe00BrYCkIlkYAJ4AHanSz9xcKMLoIAFpoSbMkdU+BmZ8xQD0AcjK0PBIyJoAbtDomnA8WErgYWrIAIIIYMCaIPhEJBBwYACMABSaZtm5NL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IaAaBxG3vOz6VaZ1mSayTXud5tE9cF05haNk3vu22WLHl4GkftkTHbVl2OcRd3df1n3DdFsAvsUjybul-Qg46m3FeRz66dV532bd7WPdIWjcmDJPjZTtbJfNmHs5D23meccO5Uj4uNdjsu+cZI5q5F2vU5KdOobu5unoVk62-ziPgiL9XXd7nW0QHquzhr-3fMznap8BhHXrnjvC6d5eY65vv18r0NfeT1OJ3HxvjwP3PZ-bsG5SAA to see why `any` is required, for now (at least) * updates the getBodyBuffer parse calls now that the types are working * return to prior approach, this time with a warning
2021-05-17 13:59:43 +00:00
curl.enable(CurlFeature.Raw);
// 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;
}
// 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);
}
// Don't rebuild dot sequences in path
if (!renderedRequest.settingRebuildPath) {
setOpt(Curl.option.PATH_AS_IS, true);
}
// 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;
}
// Setup debug handler
fix node-libcurl imports (#3384) * fix node-libcurl imports * removes unnecessary function wrapper and anyway, `number` is not the correct type (which is what motivated this change in the first place), `CurlFeature` is. * updates type for setOpt see https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgY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m-qtqPRZ80e2hDpek6zo+kSpuupqfrWe6oIO57js8d6LqCjCdpQAAqHgyQMjGwoiqLcAgOLaMgHgkuyymqepoJkuQAABMAsAAWmJ+KwFZwNwuQQEEg7YAoBVOxW24cBcu3DbymhzZ-opeGjte0GUd+RmWbZ0nOYBKAeb5mABaFiRBAgMWrsa27fqkIaAaBxG3vOz6VaZ1mSayTXud5tE9cF05haNk3vu22WLHl4GkftkTHbVl2OcRd3df1n3DdFsAvsUjybul-Qg46m3FeRz66dV532bd7WPdIWjcmDJPjZTtbJfNmHs5D23meccO5Uj4uNdjsu+cZI5q5F2vU5KdOobu5unoVk62-ziPgiL9XXd7nW0QHquzhr-3fMznap8BhHXrnjvC6d5eY65vv18r0NfeT1OJ3HxvjwP3PZ-bsG5SAA to see why `any` is required, for now (at least) * updates the getBodyBuffer parse calls now that the types are working * return to prior approach, this time with a warning
2021-05-17 13:59:43 +00:00
setOpt(Curl.option.DEBUGFUNCTION, (infoType, contentBuffer) => {
const content = contentBuffer.toString('utf8');
const rawName = Object.keys(CurlInfoDebug).find(k => CurlInfoDebug[k] === infoType) || '';
const name = LIBCURL_DEBUG_MIGRATION_MAP[rawName] || rawName;
if (infoType === CurlInfoDebug.SslDataIn || infoType === CurlInfoDebug.SslDataOut) {
return 0;
}
// Ignore the possibly large data messages
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.
} else if (contentBuffer.length / 1024 < settings.maxTimelineDataSizeKB) {
addTimeline(name, content);
2017-03-29 02:21:49 +00:00
} else {
addTimeline(name, `(${describeByteSize(contentBuffer.length)} hidden)`);
2017-03-29 02:21:49 +00:00
}
2017-03-29 02:21:49 +00:00
return 0;
}
if (infoType === CurlInfoDebug.DataIn) {
addTimelineText(`Received ${describeByteSize(contentBuffer.length)} chunk`);
return 0;
}
// Don't show cookie setting because this will display every domain in the jar
if (infoType === CurlInfoDebug.Text && content.indexOf('Added cookie') === 0) {
return 0;
}
addTimeline(name, content);
return 0; // Must be here
});
// Set the headers (to be modified as we go)
const headers = clone(renderedRequest.headers);
// Set the URL, including the query parameters
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:\//);
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]) || '';
setOpt(Curl.option.URL, `${protocol}//${socketUrl}`);
2017-06-22 18:43:00 +00:00
setOpt(Curl.option.UNIX_SOCKET_PATH, socketPath);
} else {
setOpt(Curl.option.URL, finalUrl);
2017-06-22 18:43:00 +00:00
}
addTimelineText('Preparing request to ' + finalUrl);
addTimelineText('Current time is ' + new Date().toISOString());
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;
}
// Set timeout
if (settings.timeout > 0) {
addTimelineText(`Enable timeout of ${settings.timeout}ms`);
setOpt(Curl.option.TIMEOUT_MS, settings.timeout);
} else {
addTimelineText('Disable timeout');
2018-12-14 18:34:48 +00:00
setOpt(Curl.option.TIMEOUT_MS, 0);
}
// log some things
if (renderedRequest.settingEncodeUrl) {
addTimelineText('Enable automatic URL encoding');
} else {
addTimelineText('Disable automatic URL encoding');
}
// SSL Validation
if (settings.validateSSL) {
addTimelineText('Enable SSL validation');
} else {
setOpt(Curl.option.SSL_VERIFYHOST, 0);
setOpt(Curl.option.SSL_VERIFYPEER, 0);
addTimelineText('Disable SSL validation');
}
// Setup CA Root Certificates
const baseCAPath = getTempDir();
const fullCAPath = pathJoin(baseCAPath, 'ca-certs.pem');
try {
fs.statSync(fullCAPath);
} catch (err) {
// Doesn't exist yet, so write it
mkdirp.sync(baseCAPath);
// TODO: Should mock cacerts module for testing. This is literally
// coercing a function to string in tests due to lack of val-loader.
fs.writeFileSync(fullCAPath, String(caCerts));
console.log('[net] Set CA to', fullCAPath);
}
setOpt(Curl.option.CAINFO, fullCAPath);
// Set cookies from jar
if (renderedRequest.settingSendCookies) {
// 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.
fix node-libcurl imports (#3384) * fix node-libcurl imports * removes unnecessary function wrapper and anyway, `number` is not the correct type (which is what motivated this change in the first place), `CurlFeature` is. * updates type for setOpt see https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgOIBs4GcvIN4BQyxyWwAXhAFzIDkWAtnOurcgD50MQAmwArgzadamKAHMItANwEAvgQKhIsRCgDCAe00BrYCkIlkYAJ4AHanSz9xcKMLoIAFpoSbMkdU+BmZ8xQD0AcjK0PBIyJoAbtDomnA8WErgYWrIAIIIYMCaIPhEJBBwYACMABSaZtm5NLQAssDoOrQANMjimDg0GNhYAJQ0UZrAPLJGRaUVVTkgtVq6+lityG4LlvN6EAPIQyOyCgRuIFhgyIjVsxlZM8gAvPnjxeWVF208xXB9+MhByGZQmgARugIAwaKYLC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meccO5Uj4uNdjsu+cZI5q5F2vU5KdOobu5unoVk62-ziPgiL9XXd7nW0QHquzhr-3fMznap8BhHXrnjvC6d5eY65vv18r0NfeT1OJ3HxvjwP3PZ-bsG5SAA to see why `any` is required, for now (at least) * updates the getBodyBuffer parse calls now that the types are working * return to prior approach, this time with a warning
2021-05-17 13:59:43 +00:00
setOpt(Curl.option.COOKIEFILE, '');
const cookies = renderedRequest.cookieJar.cookies || [];
for (const cookie of cookies) {
let expiresTimestamp = 0;
if (cookie.expires) {
const expiresDate = new Date(cookie.expires);
expiresTimestamp = Math.round(expiresDate.getTime() / 1000);
}
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,
cookie.value,
].join('\t'),
2018-06-25 17:42:50 +00:00
);
}
2018-06-25 17:42:50 +00:00
for (const { name, value } of renderedRequest.cookies) {
setOpt(Curl.option.COOKIE, `${name}=${value}`);
}
addTimelineText(
`Enable cookie sending with jar of ${cookies.length} cookie${
cookies.length !== 1 ? 's' : ''
}`,
);
} else {
addTimelineText('Disable cookie sending due to user setting');
}
// 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;
const proxyHost = protocol === 'https:' ? httpsProxy : httpProxy;
const proxy = proxyHost ? setDefaultProtocol(proxyHost) : null;
addTimelineText(`Enable network proxy for ${protocol || ''}`);
if (proxy) {
setOpt(Curl.option.PROXY, proxy);
setOpt(Curl.option.PROXYAUTH, CurlAuth.Any);
}
2017-06-06 20:21:59 +00:00
if (noProxy) {
setOpt(Curl.option.NOPROXY, noProxy);
}
} else {
setOpt(Curl.option.PROXY, '');
}
// Set client certs if needed
const clientCertificates = await models.clientCertificate.findByParentId(workspace._id);
for (const certificate of (clientCertificates || [])) {
if (certificate.disabled) {
continue;
}
const cHostWithProtocol = setDefaultProtocol(certificate.host, 'https:');
if (urlMatchesCertHost(cHostWithProtocol, renderedRequest.url)) {
const ensureFile = blobOrFilename => {
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
// the temp directory first.
const fullBase = getTempDir();
const name = `${renderedRequest._id}_${renderedRequest.modified}`;
const fullPath = pathJoin(fullBase, name);
fs.writeFileSync(fullPath, Buffer.from(blobOrFilename, 'base64'));
// Set filename to the one we just saved
blobOrFilename = fullPath;
}
return blobOrFilename;
};
2018-06-25 17:42:50 +00:00
const { passphrase, cert, key, pfx } = certificate;
if (cert) {
setOpt(Curl.option.SSLCERT, ensureFile(cert));
setOpt(Curl.option.SSLCERTTYPE, 'PEM');
addTimelineText('Adding SSL PEM certificate');
}
if (pfx) {
setOpt(Curl.option.SSLCERT, ensureFile(pfx));
setOpt(Curl.option.SSLCERTTYPE, 'P12');
addTimelineText('Adding SSL P12 certificate');
}
if (key) {
setOpt(Curl.option.SSLKEY, ensureFile(key));
addTimelineText('Adding SSL KEY certificate');
}
if (passphrase) {
setOpt(Curl.option.KEYPASSWD, passphrase);
}
}
}
// Build the body
2017-04-09 21:53:46 +00:00
let noBody = false;
let requestBody: string | null = null;
const expectsBody = ['POST', 'PUT', 'PATCH'].includes(renderedRequest.method.toUpperCase());
if (renderedRequest.body.mimeType === CONTENT_TYPE_FORM_URLENCODED) {
requestBody = buildQueryStringFromParams(renderedRequest.body.params || [], false);
} else if (renderedRequest.body.mimeType === CONTENT_TYPE_FORM_DATA) {
2017-07-18 20:38:19 +00:00
const params = renderedRequest.body.params || [];
const { filePath: multipartBodyPath, boundary, contentLength } = await buildMultipart(
params,
);
2017-10-12 17:32:26 +00:00
// Extend the Content-Type header
const contentTypeHeader = getContentTypeHeader(headers);
2017-10-12 17:32:26 +00:00
if (contentTypeHeader) {
contentTypeHeader.value = `multipart/form-data; boundary=${boundary}`;
} else {
headers.push({
name: 'Content-Type',
value: `multipart/form-data; boundary=${boundary}`,
2017-10-12 17:32:26 +00:00
});
}
const fd = fs.openSync(multipartBodyPath, 'r');
setOpt(Curl.option.INFILESIZE_LARGE, contentLength);
2017-10-12 17:32:26 +00:00
setOpt(Curl.option.UPLOAD, 1);
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);
const fn = () => {
fs.closeSync(fd);
fs.unlink(multipartBodyPath, () => {
// Pass
});
};
curl.on('end', fn);
curl.on('error', fn);
} 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 || '';
const fd = fs.openSync(fileName, 'r');
setOpt(Curl.option.INFILESIZE_LARGE, size);
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);
const fn = () => fs.closeSync(fd);
curl.on('end', fn);
curl.on('error', fn);
} else if (typeof renderedRequest.body.mimeType === 'string' || expectsBody) {
requestBody = renderedRequest.body.text || '';
} else {
// No body
2017-04-09 21:53:46 +00:00
noBody = true;
}
if (!noBody) {
// Don't chunk uploads
headers.push({
name: 'Expect',
value: DISABLE_HEADER_VALUE,
});
2018-06-25 17:42:50 +00:00
headers.push({
name: 'Transfer-Encoding',
value: DISABLE_HEADER_VALUE,
2018-06-25 17:42:50 +00:00
});
}
// If we calculated the body within Insomnia (ie. not computed by Curl)
if (requestBody !== null) {
setOpt(Curl.option.POSTFIELDS, requestBody);
}
// Handle Authorization header
if (!hasAuthHeader(headers) && !renderedRequest.authentication.disabled) {
if (renderedRequest.authentication.type === AUTH_DIGEST) {
2018-06-25 17:42:50 +00:00
const { username, password } = renderedRequest.authentication;
setOpt(Curl.option.HTTPAUTH, CurlAuth.Digest);
setOpt(Curl.option.USERNAME, username || '');
setOpt(Curl.option.PASSWORD, password || '');
} else if (renderedRequest.authentication.type === AUTH_NTLM) {
2018-06-25 17:42:50 +00:00
const { username, password } = renderedRequest.authentication;
setOpt(Curl.option.HTTPAUTH, CurlAuth.Ntlm);
setOpt(Curl.option.USERNAME, username || '');
setOpt(Curl.option.PASSWORD, password || '');
} else if (renderedRequest.authentication.type === AUTH_AWS_IAM) {
2017-08-01 22:13:24 +00:00
if (!noBody && !requestBody) {
return handleError(
new Error('AWS authentication not supported for provided body type'),
2018-06-25 17:42:50 +00:00
);
}
2018-06-25 17:42:50 +00:00
const { authentication } = renderedRequest;
const credentials = {
2018-04-30 12:48:00 +00:00
accessKeyId: authentication.accessKeyId || '',
secretAccessKey: authentication.secretAccessKey || '',
sessionToken: authentication.sessionToken || '',
};
const extraHeaders = _getAwsAuthHeaders(
credentials,
headers,
2017-08-01 22:13:24 +00:00
requestBody || '',
finalUrl,
2018-04-30 12:48:00 +00:00
renderedRequest.method,
authentication.region || '',
authentication.service || '',
);
for (const header of extraHeaders) {
headers.push(header);
}
} else if (renderedRequest.authentication.type === AUTH_NETRC) {
setOpt(Curl.option.NETRC, CurlNetrc.Required);
} else {
const authHeader = await getAuthHeader(renderedRequest, finalUrl);
if (authHeader) {
2017-11-06 20:44:55 +00:00
headers.push({
name: authHeader.name,
value: authHeader.value,
2017-11-06 20:44:55 +00:00
});
}
}
}
2017-11-12 18:35:01 +00:00
// Send a default Accept headers of anything
if (!hasAcceptHeader(headers)) {
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)) {
headers.push({
name: 'Accept-Encoding',
value: DISABLE_HEADER_VALUE,
});
2017-11-12 18:35:01 +00:00
}
2021-05-25 16:16:43 +00:00
// Set User-Agent if it's not already in headers
if (!hasUserAgentHeader(headers)) {
setOpt(Curl.option.USERAGENT, `insomnia/${getAppVersion()}`);
}
// Prevent curl from adding default content-type header
if (!hasContentTypeHeader(headers)) {
headers.push({
name: 'content-type',
value: DISABLE_HEADER_VALUE,
});
}
// NOTE: This is last because headers might be modified multiple times
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}`;
}
});
setOpt(Curl.option.HTTPHEADER, headerStrings);
let responseBodyBytes = 0;
const responsesDir = pathJoin(getDataDirectory(), 'responses');
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());
fix node-libcurl imports (#3384) * fix node-libcurl imports * removes unnecessary function wrapper and anyway, `number` is not the correct type (which is what motivated this change in the first place), `CurlFeature` is. * updates type for setOpt see https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgOIBs4GcvIN4BQyxyWwAXhAFzIDkWAtnOurcgD50MQAmwArgzadamKAHMItANwEAvgQKhIsRCgDCAe00BrYCkIlkYAJ4AHanSz9xcKMLoIAFpoSbMkdU+BmZ8xQD0AcjK0PBIyJoAbtDomnA8WErgYWrIAIIIYMCaIPhEJBBwYACMABSaZtm5NLQAssDoOrQANMjimDg0GNhYAJQ0UZrAPLJGRaUVVTkgtVq6+lityG4LlvN6EAPIQyOyCgRuIFhgyIjVsxlZM8gAvPnjxeWVF208xXB9+MhByGZQmgARugIAwaKYLCFcCBNKdQDBoFBeGcQDxkABJZBOOAxZAw5AAdzgJmMmmM5hQwFOsKc0AJwCwEAKRmIRyw7ggADo4u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6d5eY65vv18r0NfeT1OJ3HxvjwP3PZ-bsG5SAA to see why `any` is required, for now (at least) * updates the getBodyBuffer parse calls now that the types are working * return to prior approach, this time with a warning
2021-05-17 13:59:43 +00:00
setOpt(Curl.option.WRITEFUNCTION, buff => {
responseBodyBytes += buff.length;
responseBodyWriteStream.write(buff);
return buff.length;
});
// Handle the response ending
curl.on('end', async (_1, _2, rawHeaders: Buffer) => {
2017-11-13 23:10:53 +00:00
const allCurlHeadersObjects = _parseHeaders(rawHeaders);
// Headers are an array (one for each redirect)
const lastCurlHeadersObject = allCurlHeadersObjects[allCurlHeadersObjects.length - 1];
// Collect various things
const httpVersion = lastCurlHeadersObject.version || '';
2017-11-13 23:10:53 +00:00
const statusCode = lastCurlHeadersObject.code || -1;
const statusMessage = lastCurlHeadersObject.reason || '';
// Collect the headers
2017-11-13 23:10:53 +00:00
const headers = lastCurlHeadersObject.headers;
// Calculate the content type
const contentTypeHeader = getContentTypeHeader(headers);
const contentType = contentTypeHeader ? contentTypeHeader.value : '';
// Update Cookie Jar
let currentUrl = finalUrl;
let setCookieStrings: string[] = [];
const jar = jarFromCookies(renderedRequest.cookieJar.cookies);
2018-06-25 17:42:50 +00:00
for (const { headers } of allCurlHeadersObjects) {
// Collect Set-Cookie headers
2017-11-13 23:10:53 +00:00
const setCookieHeaders = getSetCookieHeaders(headers);
setCookieStrings = [...setCookieStrings, ...setCookieHeaders.map(h => h.value)];
// Pull out new URL if there is a redirect
2017-11-13 23:10:53 +00:00
const newLocation = getLocationHeader(headers);
if (newLocation !== null) {
2017-11-13 23:10:53 +00:00
currentUrl = urlResolve(currentUrl, newLocation.value);
}
}
// Update jar with Set-Cookie headers
for (const setCookieStr of setCookieStrings) {
try {
jar.setCookieSync(setCookieStr, currentUrl);
} catch (err) {
addTimelineText(`Rejected cookie: ${err.message}`);
}
}
// Update cookie jar if we need to and if we found any cookies
if (renderedRequest.settingStoreCookies && setCookieStrings.length) {
const cookies = await cookiesFromJar(jar);
await models.cookieJar.update(renderedRequest.cookieJar, {
cookies,
});
}
// Print informational message
if (setCookieStrings.length > 0) {
const n = setCookieStrings.length;
if (renderedRequest.settingStoreCookies) {
addTimelineText(`Saved ${n} cookie${n === 1 ? '' : 's'}`);
} else {
addTimelineText(`Ignored ${n} cookie${n === 1 ? '' : 's'}`);
}
}
// Return the response data
const responsePatch: ResponsePatch = {
contentType,
headers,
2017-11-13 23:10:53 +00:00
httpVersion,
statusCode,
statusMessage,
bytesContent: responseBodyBytes,
// @ts-expect-error -- TSCONVERSION appears to be a genuine error
bytesRead: curl.getInfo(Curl.info.SIZE_DOWNLOAD),
elapsedTime: curl.getInfo(Curl.info.TOTAL_TIME) as number * 1000,
// @ts-expect-error -- TSCONVERSION appears to be a genuine error
url: curl.getInfo(Curl.info.EFFECTIVE_URL),
};
// Close the request
2017-11-13 23:10:53 +00:00
curl.close();
// 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);
});
curl.on('error', async function(err, code) {
let error = err + '';
let statusMessage = 'Error';
if (code === CurlCode.CURLE_ABORTED_BY_CALLBACK) {
error = 'Request aborted';
statusMessage = 'Abort';
}
await respond(
{
statusMessage,
error,
elapsedTime: curl.getInfo(Curl.info.TOTAL_TIME) as number * 1000,
},
null,
true,
);
});
curl.perform();
} catch (err) {
console.log('[network] Error', err);
await handleError(err);
}
});
}
2018-06-25 17:42:50 +00:00
export async function sendWithSettings(
requestId: string,
requestPatch: Record<string, any>,
) {
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,
models.requestGroup.type,
models.workspace.type,
]);
2021-06-16 21:05:31 +00:00
const workspaceDoc = ancestors.find(isWorkspace);
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}`);
}
const workspaceMeta = await models.workspaceMeta.getOrCreateByParentId(workspace._id);
const environmentId: string = workspaceMeta.activeEnvironmentId || 'n/a';
const newRequest: Request = await models.initModel(models.request.type, requestPatch, {
_id: request._id + '.other',
parentId: request._id,
});
const environment: Environment | null = await models.environment.getById(environmentId || 'n/a');
let renderResult: {
request: RenderedRequest;
context: Record<string, any>;
};
try {
renderResult = await getRenderedRequestAndContext({ request: newRequest, environmentId });
} catch (err) {
throw new Error(`Failed to render request: ${requestId}`);
}
return _actuallySend(
renderResult.request,
renderResult.context,
workspace,
settings,
environment,
);
}
export async function send(
requestId: string,
environmentId?: string,
extraInfo?: ExtraRenderInfo,
) {
console.log(`[network] Sending req=${requestId} env=${environmentId || 'null'}`);
// 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) {
await delay(delayMillis);
}
// Fetch some things
const request = await models.request.getById(requestId);
const settings = await models.settings.getOrCreate();
const ancestors = await db.withAncestors(request, [
models.request.type,
models.requestGroup.type,
models.workspace.type,
]);
if (!request) {
throw new Error(`Failed to find request to send for ${requestId}`);
}
const environment: Environment | null = await models.environment.getById(environmentId || 'n/a');
const renderResult = await getRenderedRequestAndContext(
{
request,
environmentId,
purpose: RENDER_PURPOSE_SEND,
extraInfo,
},
);
const renderedRequestBeforePlugins = renderResult.request;
const renderedContextBeforePlugins = renderResult.context;
2021-06-16 21:05:31 +00:00
const workspaceDoc = ancestors.find(isWorkspace);
const workspace = await models.workspace.getById(workspaceDoc ? workspaceDoc._id : 'n/a');
if (!workspace) {
throw new Error(`Failed to find workspace for request: ${requestId}`);
}
let renderedRequest: RenderedRequest;
try {
renderedRequest = await _applyRequestPluginHooks(
renderedRequestBeforePlugins,
renderedContextBeforePlugins,
);
} catch (err) {
return {
environmentId: environmentId,
error: err.message,
parentId: renderedRequestBeforePlugins._id,
settingSendCookies: renderedRequestBeforePlugins.settingSendCookies,
settingStoreCookies: renderedRequestBeforePlugins.settingStoreCookies,
statusCode: STATUS_CODE_PLUGIN_ERROR,
statusMessage: err.plugin ? `Plugin ${err.plugin.name}` : 'Plugin',
url: renderedRequestBeforePlugins.url,
} as ResponsePatch;
}
const response = await _actuallySend(
renderedRequest,
renderedContextBeforePlugins,
workspace,
settings,
environment,
);
console.log(
response.error
? `[network] Response failed req=${requestId} err=${response.error || 'n/a'}`
: `[network] Response succeeded req=${requestId} status=${response.statusCode || '?'}`,
);
return response;
}
2018-06-25 17:42:50 +00:00
async function _applyRequestPluginHooks(
renderedRequest: RenderedRequest,
renderedContext: Record<string, any>,
) {
const newRenderedRequest = clone(renderedRequest);
2018-06-25 17:42:50 +00:00
for (const { plugin, hook } of await plugins.getRequestHooks()) {
const context = {
...(pluginContexts.app.init(RENDER_PURPOSE_NO_RENDER) as Record<string, any>),
...pluginContexts.data.init(renderedContext.getProjectId()),
...(pluginContexts.store.init(plugin) as Record<string, any>),
...(pluginContexts.request.init(newRenderedRequest, renderedContext) as Record<string, any>),
...(pluginContexts.network.init(renderedContext.getEnvironmentId()) as Record<string, any>),
};
try {
await hook(context);
} catch (err) {
err.plugin = plugin;
throw err;
}
}
return newRenderedRequest;
}
2018-06-25 17:42:50 +00:00
async function _applyResponsePluginHooks(
response: ResponsePatch,
renderedRequest: RenderedRequest,
renderedContext: Record<string, any>,
) {
const newResponse = clone(response);
const newRequest = clone(renderedRequest);
2018-06-25 17:42:50 +00:00
for (const { plugin, hook } of await plugins.getResponseHooks()) {
const context = {
...(pluginContexts.app.init(RENDER_PURPOSE_NO_RENDER) as Record<string, any>),
...pluginContexts.data.init(renderedContext.getProjectId()),
...(pluginContexts.store.init(plugin) as Record<string, any>),
...(pluginContexts.response.init(newResponse) as Record<string, any>),
...(pluginContexts.request.init(newRequest, renderedContext, true) as Record<string, any>),
...(pluginContexts.network.init(renderedContext.getEnvironmentId()) as Record<string, any>),
};
try {
await hook(context);
} catch (err) {
err.plugin = plugin;
throw err;
}
}
return newResponse;
}
interface HeaderResult {
headers: ResponseHeader[];
version: string;
code: number;
reason: string;
}
2018-06-25 17:42:50 +00:00
export function _parseHeaders(
buffer: Buffer,
) {
const results: HeaderResult[] = [];
2017-11-13 23:10:53 +00:00
const lines = buffer.toString('utf8').split(/\r?\n|\r/g);
for (let i = 0, currentResult: HeaderResult | null = null; i < lines.length; i++) {
2017-11-13 23:10:53 +00:00
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(' '),
headers: [],
2017-11-13 23:10:53 +00:00
};
} else {
const [name, value] = line.split(/:\s(.+)/);
const header: ResponseHeader = {
name,
value: value || '',
};
2017-11-13 23:10:53 +00:00
currentResult.headers.push(header);
}
}
2017-11-13 23:10:53 +00:00
return results;
}
// 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;
sessionToken: string;
2018-04-30 12:48:00 +00:00
},
headers: RequestHeader[],
2017-07-18 22:10:57 +00:00
body: string,
url: string,
2018-04-30 12:48:00 +00:00
method: string,
region?: string,
service?: string,
): {
name: string;
value: string;
description?: string;
disabled?: boolean;
}[] {
const parsedUrl = urlParse(url);
const contentTypeHeader = getContentTypeHeader(headers);
// 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;
const awsSignOptions = {
2018-04-30 12:48:00 +00:00
service,
region,
host,
body,
method,
path: parsedUrl.path,
headers: contentTypeHeader
? {
'content-type': contentTypeHeader.value,
}
: {},
};
const signature = aws4.sign(awsSignOptions, credentials);
return Object.keys(signature.headers)
.filter(name => name !== 'content-type') // Don't add this because we already have it
.map(name => ({
name,
value: signature.headers[name],
}));
}
function storeTimeline(timeline: ResponseTimelineEntry[]) {
return new Promise<string>((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);
}
});
});
}
if (global.document) {
document.addEventListener('keydown', (e: KeyboardEvent) => {
if (e.ctrlKey || e.metaKey || e.altKey) {
return;
}
lastUserInteraction = Date.now();
});
document.addEventListener('paste', () => {
lastUserInteraction = Date.now();
});
}