2017-07-18 20:38:19 +00:00
|
|
|
// @flow
|
2017-07-20 01:55:40 +00:00
|
|
|
import type {ResponseHeader, ResponseTimelineEntry} from '../models/response';
|
|
|
|
import type {RequestHeader} from '../models/request';
|
2017-07-19 01:55:47 +00:00
|
|
|
import type {Workspace} from '../models/workspace';
|
2017-07-19 04:48:28 +00:00
|
|
|
import type {Settings} from '../models/settings';
|
2017-07-20 01:55:40 +00:00
|
|
|
import type {RenderedRequest} from '../common/render';
|
2017-07-18 22:10:57 +00:00
|
|
|
|
2017-03-16 17:51:56 +00:00
|
|
|
import electron from 'electron';
|
|
|
|
import mkdirp from 'mkdirp';
|
2017-04-09 21:53:46 +00:00
|
|
|
import mimes from 'mime-types';
|
2017-07-12 21:20:31 +00:00
|
|
|
import clone from 'clone';
|
2017-06-15 17:52:51 +00:00
|
|
|
import {parse as urlParse, resolve as urlResolve} from 'url';
|
2017-03-16 17:51:56 +00:00
|
|
|
import {Curl} from 'node-libcurl';
|
2017-04-07 18:10:15 +00:00
|
|
|
import {join as pathJoin} from 'path';
|
2017-03-16 17:51:56 +00:00
|
|
|
import * as models from '../models';
|
|
|
|
import * as querystring from '../common/querystring';
|
|
|
|
import * as util from '../common/misc.js';
|
2017-07-31 17:29:36 +00:00
|
|
|
import {AUTH_AWS_IAM, AUTH_BASIC, AUTH_DIGEST, AUTH_NETRC, AUTH_NTLM, CONTENT_TYPE_FORM_DATA, CONTENT_TYPE_FORM_URLENCODED, getAppVersion, STATUS_CODE_PLUGIN_ERROR} from '../common/constants';
|
2017-06-15 17:52:51 +00:00
|
|
|
import {describeByteSize, hasAuthHeader, hasContentTypeHeader, hasUserAgentHeader, setDefaultProtocol} from '../common/misc';
|
2017-03-16 17:51:56 +00:00
|
|
|
import {getRenderedRequest} from '../common/render';
|
|
|
|
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';
|
2017-03-23 22:10:42 +00:00
|
|
|
import {getAuthHeader} from './authentication';
|
2017-05-16 17:45:09 +00:00
|
|
|
import {cookiesFromJar, jarFromCookies} from '../common/cookies';
|
2017-06-21 20:56:19 +00:00
|
|
|
import urlMatchesCertHost from './url-matches-cert-host';
|
2017-07-12 21:01:14 +00:00
|
|
|
import aws4 from 'aws4';
|
2017-03-16 17:51:56 +00:00
|
|
|
|
2017-07-20 01:55:40 +00:00
|
|
|
export type ResponsePatch = {
|
|
|
|
statusMessage?: string,
|
|
|
|
error?: string,
|
|
|
|
url?: string,
|
|
|
|
statusCode?: number,
|
|
|
|
headers?: Array<ResponseHeader>,
|
|
|
|
elapsedTime?: number,
|
|
|
|
contentType?: string,
|
|
|
|
bytesRead?: number,
|
|
|
|
parentId?: string,
|
|
|
|
settingStoreCookies?: boolean,
|
|
|
|
settingSendCookies?: boolean,
|
|
|
|
timeline?: Array<ResponseTimelineEntry>
|
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;
|
|
|
|
|
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
|
|
|
|
|
|
|
export function cancelCurrentRequest () {
|
|
|
|
if (typeof cancelRequestFunction === 'function') {
|
|
|
|
cancelRequestFunction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 22:10:57 +00:00
|
|
|
export function _actuallySend (
|
|
|
|
renderedRequest: RenderedRequest,
|
|
|
|
workspace: Workspace,
|
|
|
|
settings: Settings
|
|
|
|
): Promise<{bodyBuffer: ?Buffer, response: ResponsePatch}> {
|
2017-08-15 23:30:59 +00:00
|
|
|
console.log('[curl] CREATING PROMISE');
|
2017-03-16 17:51:56 +00:00
|
|
|
return new Promise(async resolve => {
|
2017-07-18 22:10:57 +00:00
|
|
|
let timeline: Array<ResponseTimelineEntry> = [];
|
2017-07-11 00:23:40 +00:00
|
|
|
|
2017-07-19 04:48:28 +00:00
|
|
|
// Initialize the curl handle
|
2017-08-15 23:30:59 +00:00
|
|
|
console.log('[curl] INIT CURL HANDLE');
|
2017-07-19 04:48:28 +00:00
|
|
|
const curl = new Curl();
|
|
|
|
|
|
|
|
/** Helper function to respond with a success */
|
2017-07-18 20:38:19 +00:00
|
|
|
function respond (patch: ResponsePatch, bodyBuffer: ?Buffer = null): void {
|
2017-07-20 01:55:40 +00:00
|
|
|
const response = Object.assign(({
|
2017-07-11 00:23:40 +00:00
|
|
|
parentId: renderedRequest._id,
|
|
|
|
timeline: timeline,
|
|
|
|
settingSendCookies: renderedRequest.settingSendCookies,
|
|
|
|
settingStoreCookies: renderedRequest.settingStoreCookies
|
2017-07-20 01:55:40 +00:00
|
|
|
}: ResponsePatch), patch);
|
2017-07-11 00:23:40 +00:00
|
|
|
|
|
|
|
resolve({bodyBuffer, response});
|
2017-07-20 01:55:40 +00:00
|
|
|
|
|
|
|
// Apply plugin hooks and don't wait for them and don't throw from them
|
|
|
|
process.nextTick(async () => {
|
|
|
|
try {
|
|
|
|
await _applyResponsePluginHooks(response, bodyBuffer);
|
|
|
|
} catch (err) {
|
|
|
|
// TODO: Better error handling here
|
|
|
|
console.warn('Response plugin failed', err);
|
|
|
|
}
|
|
|
|
});
|
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 */
|
|
|
|
function handleError (err: Error): void {
|
2017-07-11 00:23:40 +00:00
|
|
|
respond({
|
2017-03-16 17:51:56 +00:00
|
|
|
url: renderedRequest.url,
|
|
|
|
parentId: renderedRequest._id,
|
2017-07-19 04:48:28 +00:00
|
|
|
error: err.message,
|
2017-03-16 17:51:56 +00:00
|
|
|
elapsedTime: 0,
|
2017-04-07 18:10:15 +00:00
|
|
|
statusMessage: 'Error',
|
|
|
|
settingSendCookies: renderedRequest.settingSendCookies,
|
|
|
|
settingStoreCookies: renderedRequest.settingStoreCookies
|
2017-03-16 17:51:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-19 04:48:28 +00:00
|
|
|
/** Helper function to set Curl options */
|
2017-07-26 00:49:22 +00:00
|
|
|
function setOpt (opt: number, val: any, optional: boolean = false) {
|
2017-07-19 04:48:28 +00:00
|
|
|
const name = Object.keys(Curl.option).find(name => Curl.option[name] === opt);
|
|
|
|
try {
|
|
|
|
curl.setOpt(opt, val);
|
|
|
|
} catch (err) {
|
|
|
|
if (!optional) {
|
|
|
|
throw new Error(`${err.message} (${opt} ${name || 'n/a'})`);
|
|
|
|
} else {
|
|
|
|
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
|
|
|
|
2017-07-19 04:48:28 +00:00
|
|
|
try {
|
2017-03-16 17:51:56 +00:00
|
|
|
// Setup the cancellation logic
|
|
|
|
cancelRequestFunction = () => {
|
2017-03-29 23:09:28 +00:00
|
|
|
respond({
|
2017-04-06 02:30:28 +00:00
|
|
|
elapsedTime: curl.getInfo(Curl.info.TOTAL_TIME) * 1000,
|
|
|
|
bytesRead: curl.getInfo(Curl.info.SIZE_DOWNLOAD),
|
|
|
|
url: curl.getInfo(Curl.info.EFFECTIVE_URL),
|
2017-03-23 22:10:42 +00:00
|
|
|
statusMessage: 'Cancelled',
|
|
|
|
error: 'Request was cancelled'
|
|
|
|
});
|
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
|
|
|
|
2017-08-15 23:30:59 +00:00
|
|
|
console.log('[curl] SETTING BASIC OPTS');
|
2017-03-16 17:51:56 +00:00
|
|
|
// Set all the basic options
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.FOLLOWLOCATION, settings.followRedirects);
|
2017-08-10 15:44:24 +00:00
|
|
|
setOpt(Curl.option.MAXREDIRS, 20);
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.TIMEOUT_MS, settings.timeout); // 0 for no timeout
|
|
|
|
setOpt(Curl.option.VERBOSE, true); // True so debug function works
|
|
|
|
setOpt(Curl.option.NOPROGRESS, false); // False so progress function works
|
2017-04-20 01:37:40 +00:00
|
|
|
setOpt(Curl.option.ACCEPT_ENCODING, ''); // Auto decode everything
|
2017-03-28 22:45:23 +00:00
|
|
|
|
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-08-15 23:30:59 +00:00
|
|
|
console.log('[curl] SETTING DEBUG FUNC');
|
2017-03-28 22:45:23 +00:00
|
|
|
// Setup debug handler
|
2017-07-18 22:10:57 +00:00
|
|
|
setOpt(Curl.option.DEBUGFUNCTION, (infoType: string, content: string) => {
|
|
|
|
const name = Object.keys(Curl.info.debug).find(k => Curl.info.debug[k] === infoType) || '';
|
2017-03-29 02:21:49 +00:00
|
|
|
|
2017-04-04 23:06:43 +00:00
|
|
|
if (
|
|
|
|
infoType === Curl.info.debug.SSL_DATA_IN ||
|
|
|
|
infoType === Curl.info.debug.SSL_DATA_OUT
|
|
|
|
) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-15 23:30:59 +00:00
|
|
|
console.log('[curl] TIMELINE PUSH', {name, content});
|
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
// Ignore the possibly large data messages
|
2017-03-29 02:21:49 +00:00
|
|
|
if (infoType === Curl.info.debug.DATA_OUT) {
|
2017-06-21 20:58:41 +00:00
|
|
|
if (content.length === 0) {
|
|
|
|
// Sometimes this happens, but I'm not sure why. Just ignore it.
|
|
|
|
} else if (content.length < 1000) {
|
2017-03-29 02:21:49 +00:00
|
|
|
timeline.push({name, value: content});
|
|
|
|
} else {
|
|
|
|
timeline.push({name, value: `(${describeByteSize(content.length)} hidden)`});
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (infoType === Curl.info.debug.DATA_IN) {
|
2017-03-29 23:09:28 +00:00
|
|
|
timeline.push({
|
|
|
|
name: 'TEXT',
|
|
|
|
value: `Received ${describeByteSize(content.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
|
|
|
|
if (infoType === Curl.info.debug.TEXT && content.indexOf('Added cookie') === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
timeline.push({name, value: content});
|
|
|
|
|
|
|
|
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
|
|
|
let lastPercent = 0;
|
2017-04-07 18:10:15 +00:00
|
|
|
// NOTE: This option was added in 7.32.0 so make it optional
|
2017-07-19 04:48:28 +00:00
|
|
|
setOpt(Curl.option.XFERINFOFUNCTION, (dltotal, dlnow, ultotal, ulnow) => {
|
2017-03-16 17:51:56 +00:00
|
|
|
if (dltotal === 0) {
|
2017-03-23 22:10:42 +00:00
|
|
|
return 0;
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const percent = Math.round(dlnow / dltotal * 100);
|
|
|
|
if (percent !== lastPercent) {
|
2017-03-23 22:10:42 +00:00
|
|
|
// console.log('PROGRESS 2', `${percent}%`, ultotal, ulnow);
|
2017-03-16 17:51:56 +00:00
|
|
|
lastPercent = percent;
|
|
|
|
}
|
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
return 0;
|
2017-07-19 04:48:28 +00:00
|
|
|
}, true);
|
2017-03-16 17:51:56 +00:00
|
|
|
|
2017-08-15 23:30:59 +00:00
|
|
|
console.log('[curl] BUILD URL');
|
2017-03-16 17:51:56 +00:00
|
|
|
// Set the URL, including the query parameters
|
|
|
|
const qs = querystring.buildFromParams(renderedRequest.parameters);
|
|
|
|
const url = querystring.joinUrl(renderedRequest.url, qs);
|
2017-06-30 16:00:00 +00:00
|
|
|
const isUnixSocket = url.match(/https?:\/\/unix:\//);
|
2017-03-29 23:09:28 +00:00
|
|
|
const finalUrl = util.prepareUrlForSending(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]) || '';
|
2017-06-22 18:43:00 +00:00
|
|
|
setOpt(Curl.option.URL, `${protocol}//${socketUrl}`);
|
|
|
|
setOpt(Curl.option.UNIX_SOCKET_PATH, socketPath);
|
|
|
|
} else {
|
|
|
|
setOpt(Curl.option.URL, finalUrl);
|
|
|
|
}
|
2017-03-29 23:09:28 +00:00
|
|
|
timeline.push({name: 'TEXT', value: 'Preparing request to ' + finalUrl});
|
|
|
|
|
|
|
|
// log some things
|
|
|
|
if (renderedRequest.settingEncodeUrl) {
|
|
|
|
timeline.push({name: 'TEXT', value: 'Enable automatic URL encoding'});
|
|
|
|
} else {
|
|
|
|
timeline.push({name: 'TEXT', value: 'Disable automatic URL encoding'});
|
|
|
|
}
|
2017-03-16 17:51:56 +00:00
|
|
|
|
2017-04-07 18:10:15 +00:00
|
|
|
// SSL Validation
|
|
|
|
if (settings.validateSSL) {
|
|
|
|
timeline.push({name: 'TEXT', value: 'Enable SSL validation'});
|
|
|
|
} else {
|
|
|
|
setOpt(Curl.option.SSL_VERIFYHOST, 0);
|
|
|
|
setOpt(Curl.option.SSL_VERIFYPEER, 0);
|
|
|
|
timeline.push({name: 'TEXT', value: 'Disable SSL validation'});
|
|
|
|
}
|
|
|
|
|
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-03-28 22:45:23 +00:00
|
|
|
const basCAPath = pathJoin(electron.remote.app.getPath('temp'), 'insomnia');
|
|
|
|
const fullCAPath = pathJoin(basCAPath, CACerts.filename);
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.statSync(fullCAPath);
|
|
|
|
} catch (err) {
|
|
|
|
// Doesn't exist yet, so write it
|
|
|
|
mkdirp.sync(basCAPath);
|
|
|
|
fs.writeFileSync(fullCAPath, CACerts.blob);
|
|
|
|
console.log('[net] Set CA to', fullCAPath);
|
|
|
|
}
|
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-08-15 23:30:59 +00:00
|
|
|
console.log('[curl] SETUP COOKIES');
|
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
|
|
|
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.COOKIELIST, [
|
2017-03-28 22:45:23 +00:00
|
|
|
cookie.httpOnly ? `#HttpOnly_${cookie.domain}` : cookie.domain,
|
2017-05-16 17:45:09 +00:00
|
|
|
cookie.hostOnly ? 'FALSE' : 'TRUE',
|
2017-03-28 22:45:23 +00:00
|
|
|
cookie.path,
|
|
|
|
cookie.secure ? 'TRUE' : 'FALSE',
|
|
|
|
expiresTimestamp,
|
|
|
|
cookie.key,
|
|
|
|
cookie.value
|
|
|
|
].join('\t'));
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
2017-03-29 23:09:28 +00:00
|
|
|
|
2017-07-20 01:55:40 +00:00
|
|
|
for (const {name, value} of renderedRequest.cookies) {
|
|
|
|
setOpt(Curl.option.COOKIE, `${name}=${value}`);
|
|
|
|
}
|
|
|
|
|
2017-03-29 23:09:28 +00:00
|
|
|
timeline.push({
|
|
|
|
name: 'TEXT',
|
|
|
|
value: 'Enable cookie sending with jar of ' +
|
|
|
|
`${cookies.length} cookie${cookies.length !== 1 ? 's' : ''}`
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
timeline.push({
|
|
|
|
name: 'TEXT',
|
|
|
|
value: '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) {
|
2017-08-15 23:30:59 +00:00
|
|
|
console.log('[curl] SETUP PROXY');
|
2017-03-28 22:45:23 +00:00
|
|
|
const {protocol} = urlParse(renderedRequest.url);
|
2017-06-06 20:21:59 +00:00
|
|
|
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;
|
2017-07-18 20:38:19 +00:00
|
|
|
timeline.push({name: 'TEXT', value: `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);
|
|
|
|
setOpt(Curl.option.PROXYAUTH, Curl.auth.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
|
|
|
|
for (const certificate of workspace.certificates) {
|
2017-08-15 23:30:59 +00:00
|
|
|
console.log('[curl] SETUP CERTS');
|
2017-06-21 21:20:42 +00:00
|
|
|
if (certificate.disabled) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-16 17:51:56 +00:00
|
|
|
const cHostWithProtocol = setDefaultProtocol(certificate.host, 'https:');
|
|
|
|
|
2017-06-21 20:56:19 +00:00
|
|
|
if (urlMatchesCertHost(cHostWithProtocol, renderedRequest.url)) {
|
2017-03-16 17:51:56 +00:00
|
|
|
const ensureFile = blobOrFilename => {
|
|
|
|
if (blobOrFilename.indexOf('/') === 0) {
|
|
|
|
return blobOrFilename;
|
|
|
|
} else {
|
|
|
|
// Legacy support. Certs used to be stored in blobs, so lets write it to
|
|
|
|
// the temp directory first.
|
|
|
|
// TODO: Delete this fallback eventually
|
|
|
|
const fullBase = pathJoin(electron.remote.app.getPath('temp'), 'insomnia');
|
|
|
|
mkdirp.sync(fullBase);
|
|
|
|
|
|
|
|
const name = `${renderedRequest._id}_${renderedRequest.modified}`;
|
|
|
|
const fullPath = pathJoin(fullBase, name);
|
|
|
|
fs.writeFileSync(fullPath, new Buffer(blobOrFilename, 'base64'));
|
|
|
|
|
|
|
|
return fullPath;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const {passphrase, cert, key, pfx} = certificate;
|
|
|
|
|
|
|
|
if (cert) {
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.SSLCERT, ensureFile(cert));
|
|
|
|
setOpt(Curl.option.SSLCERTTYPE, 'PEM');
|
2017-03-29 23:09:28 +00:00
|
|
|
timeline.push({name: 'TEXT', value: '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');
|
2017-03-29 23:09:28 +00:00
|
|
|
timeline.push({name: 'TEXT', value: '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));
|
2017-03-29 23:09:28 +00:00
|
|
|
timeline.push({name: 'TEXT', value: '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;
|
2017-04-20 17:18:00 +00:00
|
|
|
const expectsBody = ['POST', 'PUT', 'PATCH'].includes(renderedRequest.method.toUpperCase());
|
2017-08-15 23:30:59 +00:00
|
|
|
console.log('[curl] SETUP BODY');
|
2017-03-16 17:51:56 +00:00
|
|
|
if (renderedRequest.body.mimeType === CONTENT_TYPE_FORM_URLENCODED) {
|
2017-07-12 21:20:31 +00:00
|
|
|
requestBody = querystring.buildFromParams(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 || [];
|
|
|
|
const data = params.map(param => {
|
2017-03-16 17:51:56 +00:00
|
|
|
if (param.type === 'file' && param.fileName) {
|
2017-04-12 21:17:40 +00:00
|
|
|
const type = mimes.lookup(param.fileName) || 'application/octet-stream';
|
|
|
|
return {name: param.name, file: param.fileName, type};
|
2017-03-16 17:51:56 +00:00
|
|
|
} else {
|
|
|
|
return {name: param.name, contents: param.value};
|
|
|
|
}
|
|
|
|
});
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.HTTPPOST, data);
|
2017-03-16 17:51:56 +00:00
|
|
|
} else if (renderedRequest.body.fileName) {
|
2017-04-09 21:53:46 +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+');
|
2017-07-26 02:40:32 +00:00
|
|
|
|
2017-06-21 21:21:45 +00:00
|
|
|
setOpt(Curl.option.INFILESIZE, 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);
|
2017-04-20 17:18:00 +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
|
|
|
|
headers.push({name: 'Expect', value: ''});
|
|
|
|
headers.push({name: 'Transfer-Encoding', value: ''});
|
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-16 17:51:56 +00:00
|
|
|
// Build the body
|
|
|
|
const dataBuffers = [];
|
|
|
|
let dataBuffersLength = 0;
|
|
|
|
curl.on('data', chunk => {
|
2017-08-15 23:30:59 +00:00
|
|
|
console.log('[curl] GOT DATA', chunk + '');
|
2017-03-16 17:51:56 +00:00
|
|
|
dataBuffers.push(chunk);
|
|
|
|
dataBuffersLength += chunk.length;
|
|
|
|
});
|
|
|
|
|
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) {
|
2017-08-15 23:30:59 +00:00
|
|
|
console.log('[curl] AUTH HEADERS');
|
2017-03-28 22:45:23 +00:00
|
|
|
if (renderedRequest.authentication.type === AUTH_BASIC) {
|
|
|
|
const {username, password} = renderedRequest.authentication;
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.HTTPAUTH, Curl.auth.BASIC);
|
|
|
|
setOpt(Curl.option.USERNAME, username || '');
|
|
|
|
setOpt(Curl.option.PASSWORD, password || '');
|
2017-03-28 22:45:23 +00:00
|
|
|
} else if (renderedRequest.authentication.type === AUTH_DIGEST) {
|
|
|
|
const {username, password} = renderedRequest.authentication;
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.HTTPAUTH, Curl.auth.DIGEST);
|
|
|
|
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) {
|
|
|
|
const {username, password} = renderedRequest.authentication;
|
2017-04-07 18:10:15 +00:00
|
|
|
setOpt(Curl.option.HTTPAUTH, Curl.auth.NTLM);
|
|
|
|
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(
|
|
|
|
new Error('AWS authentication not supported for provided body type'));
|
2017-07-12 21:01:14 +00:00
|
|
|
}
|
2017-07-12 21:20:31 +00:00
|
|
|
const extraHeaders = _getAwsAuthHeaders(
|
|
|
|
renderedRequest.authentication.accessKeyId || '',
|
|
|
|
renderedRequest.authentication.secretAccessKey || '',
|
|
|
|
headers,
|
2017-08-01 22:13:24 +00:00
|
|
|
requestBody || '',
|
2017-07-12 21:20:31 +00:00
|
|
|
finalUrl
|
|
|
|
);
|
|
|
|
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) {
|
|
|
|
setOpt(Curl.option.NETRC, Curl.netrc.REQUIRED);
|
2017-03-28 22:45:23 +00:00
|
|
|
} else {
|
|
|
|
const authHeader = await getAuthHeader(
|
|
|
|
renderedRequest._id,
|
|
|
|
renderedRequest.authentication
|
|
|
|
);
|
|
|
|
|
|
|
|
if (authHeader) {
|
|
|
|
headers.push(authHeader);
|
|
|
|
}
|
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)) {
|
|
|
|
headers.push({name: 'content-type', value: ''});
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: This is last because headers might be modified multiple times
|
|
|
|
const headerStrings = headers
|
|
|
|
.filter(h => h.name)
|
|
|
|
.map(h => `${(h.name || '').trim()}: ${h.value}`);
|
|
|
|
setOpt(Curl.option.HTTPHEADER, headerStrings);
|
|
|
|
|
2017-03-16 17:51:56 +00:00
|
|
|
// Handle the response ending
|
2017-06-15 03:59:03 +00:00
|
|
|
curl.on('end', async function (_1, _2, allCurlHeadersObjects) {
|
2017-08-15 23:30:59 +00:00
|
|
|
console.log('[curl] REQUEST END', allCurlHeadersObjects);
|
2017-03-16 17:51:56 +00:00
|
|
|
// Headers are an array (one for each redirect)
|
2017-06-15 03:59:03 +00:00
|
|
|
const lastCurlHeadersObject = allCurlHeadersObjects[allCurlHeadersObjects.length - 1];
|
2017-03-16 17:51:56 +00:00
|
|
|
|
|
|
|
// Collect various things
|
2017-06-15 03:59:03 +00:00
|
|
|
const result = lastCurlHeadersObject && lastCurlHeadersObject.result;
|
2017-08-01 18:20:47 +00:00
|
|
|
const statusCode = result ? result.code : -1;
|
2017-04-06 02:30:28 +00:00
|
|
|
const statusMessage = result ? result.reason : 'Unknown';
|
2017-03-16 17:51:56 +00:00
|
|
|
|
|
|
|
// Collect the headers
|
|
|
|
const headers = [];
|
2017-06-15 03:59:03 +00:00
|
|
|
for (const name of lastCurlHeadersObject ? Object.keys(lastCurlHeadersObject) : []) {
|
|
|
|
if (typeof lastCurlHeadersObject[name] === 'string') {
|
|
|
|
headers.push({name, value: lastCurlHeadersObject[name]});
|
|
|
|
} else if (Array.isArray(lastCurlHeadersObject[name])) {
|
|
|
|
for (const value of lastCurlHeadersObject[name]) {
|
2017-03-16 17:51:56 +00:00
|
|
|
headers.push({name, value});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the content type
|
|
|
|
const contentTypeHeader = util.getContentTypeHeader(headers);
|
|
|
|
const contentType = contentTypeHeader ? contentTypeHeader.value : '';
|
|
|
|
|
|
|
|
// Update Cookie Jar
|
2017-06-15 17:52:51 +00:00
|
|
|
let currentUrl = finalUrl;
|
|
|
|
let setCookieStrings = [];
|
|
|
|
const jar = jarFromCookies(renderedRequest.cookieJar.cookies);
|
|
|
|
|
|
|
|
for (const curlHeaderObject of allCurlHeadersObjects) {
|
|
|
|
// Collect Set-Cookie headers
|
|
|
|
const setCookieHeaders = _getCurlHeader(curlHeaderObject, 'set-cookie', []);
|
|
|
|
setCookieStrings = [...setCookieStrings, ...setCookieHeaders];
|
|
|
|
|
|
|
|
// Pull out new URL if there is a redirect
|
|
|
|
const newLocation = _getCurlHeader(curlHeaderObject, 'location', null);
|
|
|
|
if (newLocation !== null) {
|
|
|
|
currentUrl = urlResolve(currentUrl, newLocation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update jar with Set-Cookie headers
|
|
|
|
for (const setCookieStr of setCookieStrings) {
|
|
|
|
try {
|
|
|
|
jar.setCookieSync(setCookieStr, currentUrl);
|
|
|
|
} catch (err) {
|
|
|
|
timeline.push({name: 'TEXT', value: `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);
|
|
|
|
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) {
|
|
|
|
timeline.push({name: 'TEXT', value: `Saved ${n} cookie${n === 1 ? '' : 's'}`});
|
|
|
|
} else {
|
|
|
|
timeline.push({name: 'TEXT', value: `Ignored ${n} cookie${n === 1 ? '' : 's'}`});
|
|
|
|
}
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the body
|
|
|
|
const bodyBuffer = Buffer.concat(dataBuffers, dataBuffersLength);
|
|
|
|
|
|
|
|
// Return the response data
|
2017-07-20 01:55:40 +00:00
|
|
|
const responsePatch = {
|
2017-03-16 17:51:56 +00:00
|
|
|
headers,
|
|
|
|
contentType,
|
|
|
|
statusCode,
|
2017-04-06 02:30:28 +00:00
|
|
|
statusMessage,
|
|
|
|
elapsedTime: curl.getInfo(Curl.info.TOTAL_TIME) * 1000,
|
|
|
|
bytesRead: curl.getInfo(Curl.info.SIZE_DOWNLOAD),
|
2017-08-11 21:44:34 +00:00
|
|
|
bytesContent: bodyBuffer.length,
|
2017-04-06 02:30:28 +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
|
|
|
|
this.close();
|
2017-07-20 01:55:40 +00:00
|
|
|
|
|
|
|
respond(responsePatch, bodyBuffer);
|
2017-03-16 17:51:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
curl.on('error', function (err, code) {
|
|
|
|
let error = err + '';
|
|
|
|
let statusMessage = 'Error';
|
|
|
|
|
|
|
|
if (code === Curl.code.CURLE_ABORTED_BY_CALLBACK) {
|
|
|
|
error = 'Request aborted';
|
|
|
|
statusMessage = 'Abort';
|
|
|
|
}
|
|
|
|
|
2017-03-29 23:09:28 +00:00
|
|
|
respond({statusMessage, error});
|
2017-03-16 17:51:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
curl.perform();
|
|
|
|
} catch (err) {
|
|
|
|
handleError(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:38:19 +00:00
|
|
|
export async function send (requestId: string, environmentId: string) {
|
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) {
|
|
|
|
await util.delay(delayMillis);
|
|
|
|
}
|
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, [
|
|
|
|
models.requestGroup.type,
|
|
|
|
models.workspace.type
|
|
|
|
]);
|
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}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderedRequestBeforePlugins = await getRenderedRequest(request, environmentId);
|
|
|
|
|
|
|
|
let renderedRequest: RenderedRequest;
|
|
|
|
try {
|
|
|
|
renderedRequest = await _applyRequestPluginHooks(renderedRequestBeforePlugins);
|
|
|
|
} catch (err) {
|
|
|
|
return {
|
|
|
|
response: {
|
|
|
|
url: renderedRequestBeforePlugins.url,
|
|
|
|
parentId: renderedRequestBeforePlugins._id,
|
|
|
|
error: err.message,
|
|
|
|
statusCode: STATUS_CODE_PLUGIN_ERROR,
|
|
|
|
statusMessage: err.plugin ? `Plugin ${err.plugin}` : 'Plugin',
|
|
|
|
settingSendCookies: renderedRequestBeforePlugins.settingSendCookies,
|
|
|
|
settingStoreCookies: renderedRequestBeforePlugins.settingStoreCookies
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-19 01:55:47 +00:00
|
|
|
const workspaceDoc = ancestors.find(doc => doc.type === models.workspace.type);
|
|
|
|
const workspace = await models.workspace.getById(workspaceDoc ? workspaceDoc._id : 'n/a');
|
|
|
|
if (!workspace) {
|
|
|
|
throw new Error(`Failed to find workspace for request: ${requestId}`);
|
|
|
|
}
|
2017-03-16 17:51:56 +00:00
|
|
|
|
2017-04-07 18:10:15 +00:00
|
|
|
return _actuallySend(renderedRequest, workspace, settings);
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
2017-06-09 03:02:16 +00:00
|
|
|
|
2017-07-20 01:55:40 +00:00
|
|
|
async function _applyRequestPluginHooks (renderedRequest: RenderedRequest): Promise<RenderedRequest> {
|
|
|
|
let newRenderedRequest = renderedRequest;
|
|
|
|
for (const {plugin, hook} of await plugins.getRequestHooks()) {
|
|
|
|
newRenderedRequest = clone(newRenderedRequest);
|
|
|
|
|
|
|
|
const context = {
|
|
|
|
...pluginContexts.app.init(plugin),
|
|
|
|
...pluginContexts.request.init(plugin, newRenderedRequest)
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
await hook(context);
|
|
|
|
} catch (err) {
|
|
|
|
err.plugin = plugin;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newRenderedRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function _applyResponsePluginHooks (
|
|
|
|
response: ResponsePatch,
|
|
|
|
bodyBuffer: ?Buffer = null
|
|
|
|
): Promise<void> {
|
|
|
|
for (const {plugin, hook} of await plugins.getResponseHooks()) {
|
|
|
|
const context = {
|
|
|
|
...pluginContexts.app.init(plugin),
|
|
|
|
...pluginContexts.response.init(plugin, response, bodyBuffer)
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
await hook(context);
|
|
|
|
} catch (err) {
|
|
|
|
err.plugin = plugin;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getCurlHeader (
|
|
|
|
curlHeadersObj: {[string]: string},
|
|
|
|
name: string,
|
|
|
|
fallback: any
|
|
|
|
): string {
|
2017-06-15 17:52:51 +00:00
|
|
|
const headerName = Object.keys(curlHeadersObj).find(
|
|
|
|
n => n.toLowerCase() === name.toLowerCase()
|
|
|
|
);
|
|
|
|
|
|
|
|
if (headerName) {
|
|
|
|
return curlHeadersObj[headerName];
|
|
|
|
} else {
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-12 21:01:14 +00:00
|
|
|
// exported for unit tests only
|
2017-07-18 22:10:57 +00:00
|
|
|
export function _getAwsAuthHeaders (
|
|
|
|
accessKeyId: string,
|
|
|
|
secretAccessKey: string,
|
2017-07-19 02:54:03 +00:00
|
|
|
headers: Array<RequestHeader>,
|
2017-07-18 22:10:57 +00:00
|
|
|
body: string,
|
|
|
|
url: string
|
|
|
|
) {
|
2017-07-12 21:20:31 +00:00
|
|
|
const credentials = {accessKeyId, secretAccessKey};
|
|
|
|
|
2017-07-12 21:01:14 +00:00
|
|
|
const parsedUrl = urlParse(url);
|
2017-07-12 21:20:31 +00:00
|
|
|
const contentTypeHeader = util.getContentTypeHeader(headers);
|
|
|
|
|
|
|
|
const awsSignOptions = {
|
|
|
|
body,
|
2017-07-12 21:01:14 +00:00
|
|
|
path: parsedUrl.path,
|
2017-07-12 21:20:31 +00:00
|
|
|
host: parsedUrl.hostname, // Purposefully not ".host" because we don't want the port
|
2017-07-12 21:01:14 +00:00
|
|
|
headers: {
|
|
|
|
'content-type': contentTypeHeader ? contentTypeHeader.value : ''
|
|
|
|
}
|
|
|
|
};
|
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
|
|
|
|
.map(name => ({name, value: signature.headers[name]}));
|
2017-07-12 21:01:14 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 20:38:19 +00:00
|
|
|
document.addEventListener('keydown', (e: KeyboardEvent) => {
|
2017-06-09 03:02:16 +00:00
|
|
|
if (e.ctrlKey || e.metaKey || e.altKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastUserInteraction = Date.now();
|
|
|
|
});
|
2017-07-10 22:34:22 +00:00
|
|
|
|
|
|
|
document.addEventListener('paste', e => {
|
|
|
|
lastUserInteraction = Date.now();
|
|
|
|
});
|