insomnia/packages/insomnia-app/app/common/har.js

532 lines
12 KiB
JavaScript
Raw Normal View History

// @flow
2016-11-22 22:26:52 +00:00
import fs from 'fs';
import clone from 'clone';
2018-06-25 17:42:50 +00:00
import { Cookie as toughCookie } from 'tough-cookie';
2016-11-10 01:15:27 +00:00
import * as models from '../models';
2018-06-25 17:42:50 +00:00
import type { RenderedRequest } from './render';
import { getRenderedRequestAndContext } from './render';
import { jarFromCookies } from 'insomnia-cookies';
import * as pluginContexts from '../plugins/context/index';
import * as misc from './misc';
2018-06-25 17:42:50 +00:00
import type { Cookie } from '../models/cookie-jar';
import type { Request } from '../models/request';
import { newBodyRaw } from '../models/request';
import type { Response as ResponseModel } from '../models/response';
import { getAuthHeader } from '../network/authentication';
import { getAppVersion } from './constants';
import { RenderError } from '../templating/index';
import { smartEncodeUrl } from 'insomnia-url';
import * as plugins from '../plugins';
export type HarCookie = {
name: string,
value: string,
path?: string,
domain?: string,
expires?: string,
httpOnly?: boolean,
secure?: boolean,
comment?: string
};
export type HarHeader = {
name: string,
value: string,
comment?: string
2018-06-25 17:42:50 +00:00
};
export type HarQueryString = {
name: string,
value: string,
comment?: string
2018-06-25 17:42:50 +00:00
};
export type HarPostParam = {
name: string,
value?: string,
fileName?: string,
contentType?: string,
comment?: string
};
export type HarPostData = {
mimeType: string,
params: Array<HarPostParam>,
text: string,
comment?: string
};
export type HarRequest = {
method: string,
url: string,
httpVersion: string,
cookies: Array<HarCookie>,
headers: Array<HarHeader>,
queryString: Array<HarQueryString>,
postData?: HarPostData,
headersSize: number,
bodySize: number,
comment?: string,
settingEncodeUrl: boolean
};
export type HarContent = {
size: number,
compression?: number,
mimeType: string,
text?: string,
encoding?: string,
comment?: string
};
export type HarResponse = {
status: number,
statusText: string,
httpVersion: string,
cookies: Array<HarCookie>,
headers: Array<HarHeader>,
content: HarContent,
redirectURL: string,
headersSize: number,
bodySize: number,
comment?: string
};
export type HarRequestCache = {
expires?: string,
lastAccess: string,
eTag: string,
hitCount: number,
comment?: string
2018-06-25 17:42:50 +00:00
};
export type HarCache = {
beforeRequest?: HarRequestCache,
afterRequest?: HarRequestCache,
comment?: string
};
export type HarEntryTimings = {
blocked?: number,
dns?: number,
connect?: number,
send: number,
wait: number,
receive: number,
ssl?: number,
comment?: string
};
export type HarEntry = {
pageref?: string,
startedDateTime: string,
time: number,
request: HarRequest,
response: HarResponse,
cache: HarCache,
timings: HarEntryTimings,
serverIPAddress?: string,
connection?: string,
comment?: string
};
export type HarPageTimings = {
onContentLoad?: number,
onLoad?: number,
comment?: string
};
export type HarPage = {
startedDateTime: string,
id: string,
title: string,
pageTimings: HarPageTimings,
comment?: string
2018-06-25 17:42:50 +00:00
};
export type HarCreator = {
name: string,
version: string,
comment?: string
};
export type HarBrowser = {
name: string,
version: string,
comment?: string
};
export type HarLog = {
version: string,
creator: HarCreator,
browser?: HarBrowser,
pages?: Array<HarPage>,
entries: Array<HarEntry>,
comment?: string
};
export type Har = {
log: HarLog
};
export type ExportRequest = {
requestId: string,
environmentId: string
2018-06-25 17:42:50 +00:00
};
2018-06-25 17:42:50 +00:00
export async function exportHar(
exportRequests: Array<ExportRequest>
): Promise<Har> {
// Export HAR entries with the same start time in order to keep their workspace sort order.
const startedDateTime = new Date().toISOString();
const entries: Array<HarEntry> = [];
for (let exportRequest of exportRequests) {
2018-06-25 17:42:50 +00:00
const request: Request | null = await models.request.getById(
exportRequest.requestId
);
if (!request) {
continue;
2016-11-22 22:26:52 +00:00
}
2018-06-25 17:42:50 +00:00
const harRequest = await exportHarWithRequest(
request,
exportRequest.environmentId
);
if (!harRequest) {
continue;
}
2018-06-25 17:42:50 +00:00
const response: ResponseModel | null = await models.response.getLatestForRequest(
exportRequest.requestId
);
const harResponse = await exportHarResponse(response);
if (!harResponse) {
continue;
}
const entry = {
startedDateTime: startedDateTime,
time: response ? response.elapsedTime : 0,
request: harRequest,
response: harResponse,
cache: {},
timings: {
blocked: -1,
dns: -1,
connect: -1,
send: 0,
wait: response ? response.elapsedTime : 0,
receive: 0,
ssl: -1
},
comment: request.name
};
entries.push(entry);
}
return {
log: {
version: '1.2',
creator: {
name: 'Insomnia REST Client',
version: `insomnia.desktop.app:v${getAppVersion()}`
},
entries: entries
}
};
}
2018-06-25 17:42:50 +00:00
export async function exportHarResponse(
response: ResponseModel | null
): Promise<HarResponse> {
if (!response) {
return {
status: 0,
statusText: '',
httpVersion: 'HTTP/1.1',
cookies: [],
headers: [],
content: {
size: 0,
mimeType: ''
},
redirectURL: '',
headersSize: -1,
bodySize: -1
};
2016-11-22 22:26:52 +00:00
}
return {
status: response.statusCode,
statusText: response.statusMessage,
httpVersion: 'HTTP/1.1',
cookies: getReponseCookies(response),
headers: getResponseHeaders(response),
content: getResponseContent(response),
redirectURL: '',
headersSize: -1,
bodySize: -1
};
}
2018-06-25 17:42:50 +00:00
export async function exportHarRequest(
2017-11-06 20:44:55 +00:00
requestId: string,
environmentId: string,
addContentLength: boolean = false
): Promise<HarRequest | null> {
const request = await models.request.getById(requestId);
if (!request) {
return null;
}
return exportHarWithRequest(request, environmentId, addContentLength);
}
2018-06-25 17:42:50 +00:00
export async function exportHarWithRequest(
2017-11-06 20:44:55 +00:00
request: Request,
environmentId: string,
addContentLength: boolean = false
): Promise<HarRequest | null> {
2017-11-01 12:40:24 +00:00
try {
2018-06-25 17:42:50 +00:00
const renderResult = await getRenderedRequestAndContext(
request,
environmentId
);
const renderedRequest = await _applyRequestPluginHooks(
renderResult.request,
renderResult.context
);
2017-11-01 12:40:24 +00:00
return exportHarWithRenderedRequest(renderedRequest, addContentLength);
} catch (err) {
if (err instanceof RenderError) {
2018-06-25 17:42:50 +00:00
throw new Error(
`Failed to render "${request.name}:${err.path}"\n ${err.message}`
);
2017-11-01 12:40:24 +00:00
} else {
2018-06-25 17:42:50 +00:00
throw new Error(
`Failed to export request "${request.name}"\n ${err.message}`
);
2017-11-01 12:40:24 +00:00
}
}
}
2018-06-25 17:42:50 +00:00
async function _applyRequestPluginHooks(
renderedRequest: RenderedRequest,
renderedContext: Object
): Promise<RenderedRequest> {
let newRenderedRequest = renderedRequest;
2018-06-25 17:42:50 +00:00
for (const { plugin, hook } of await plugins.getRequestHooks()) {
newRenderedRequest = clone(newRenderedRequest);
const context = {
...pluginContexts.app.init(),
2018-06-27 05:29:37 +00:00
...pluginContexts.request.init(newRenderedRequest, renderedContext),
...pluginContexts.store.init(plugin)
};
try {
await hook(context);
} catch (err) {
err.plugin = plugin;
throw err;
}
}
return newRenderedRequest;
}
2018-06-25 17:42:50 +00:00
export async function exportHarWithRenderedRequest(
2017-11-06 20:44:55 +00:00
renderedRequest: RenderedRequest,
addContentLength: boolean = false
): Promise<HarRequest> {
2018-06-25 17:42:50 +00:00
const url = smartEncodeUrl(
renderedRequest.url,
renderedRequest.settingEncodeUrl
);
2016-11-23 20:44:46 +00:00
if (addContentLength) {
2018-06-25 17:42:50 +00:00
const hasContentLengthHeader =
misc.filterHeaders(renderedRequest.headers, 'Content-Length').length > 0;
2016-11-23 20:44:46 +00:00
if (!hasContentLengthHeader) {
const name = 'Content-Length';
2018-06-25 17:42:50 +00:00
const value = Buffer.byteLength(
(renderedRequest.body || {}).text || ''
).toString();
renderedRequest.headers.push({ name, value });
2016-11-23 20:44:46 +00:00
}
}
// Set auth header if we have it
if (!misc.hasAuthHeader(renderedRequest.headers)) {
const header = await getAuthHeader(
renderedRequest._id,
url,
renderedRequest.method,
renderedRequest.authentication
);
if (header) {
2017-11-06 20:44:55 +00:00
renderedRequest.headers.push({
name: header.name,
value: header.value
});
}
}
2016-09-24 03:06:38 +00:00
return {
method: renderedRequest.method,
url,
2016-09-24 03:06:38 +00:00
httpVersion: 'HTTP/1.1',
cookies: getRequestCookies(renderedRequest),
headers: getRequestHeaders(renderedRequest),
queryString: getRequestQueryString(renderedRequest),
postData: getRequestPostData(renderedRequest),
2016-09-24 03:06:38 +00:00
headersSize: -1,
bodySize: -1,
settingEncodeUrl: renderedRequest.settingEncodeUrl
2016-09-24 03:06:38 +00:00
};
}
2016-08-15 22:31:30 +00:00
2018-06-25 17:42:50 +00:00
function getRequestCookies(renderedRequest: RenderedRequest): Array<HarCookie> {
const jar = jarFromCookies(renderedRequest.cookieJar.cookies);
2016-10-05 15:07:49 +00:00
const domainCookies = jar.getCookiesSync(renderedRequest.url);
return domainCookies.map(mapCookie);
}
2018-06-25 17:42:50 +00:00
function getReponseCookies(response: ResponseModel): Array<HarCookie> {
return misc
.getSetCookieHeaders(response.headers)
.map(h => {
let cookie;
try {
cookie = toughCookie.parse(h.value || '');
} catch (error) {}
if (!cookie) {
return null;
}
return mapCookie(cookie);
})
.filter(Boolean);
}
2018-06-25 17:42:50 +00:00
function mapCookie(cookie: Cookie): HarCookie {
const harCookie: HarCookie = {
name: cookie.key,
value: cookie.value
};
if (cookie.path) {
harCookie.path = cookie.path;
}
if (cookie.domain) {
harCookie.domain = cookie.domain;
}
if (cookie.expires) {
let expires = null;
if (cookie.expires instanceof Date) {
expires = cookie.expires;
} else if (typeof cookie.expires === 'string') {
expires = new Date(cookie.expires);
} else if (typeof cookie.expires === 'number') {
expires = new Date();
expires.setTime(cookie.expires);
}
if (expires && !isNaN(expires.getTime())) {
harCookie.expires = expires.toISOString();
}
}
if (cookie.httpOnly) {
harCookie.httpOnly = true;
}
if (cookie.secure) {
harCookie.secure = true;
}
return harCookie;
}
2018-06-25 17:42:50 +00:00
function getResponseContent(response: ResponseModel): HarContent {
let body: Buffer | null = models.response.getBodyBuffer(response);
if (body === null) {
body = Buffer.alloc(0);
}
return {
size: body.byteLength,
mimeType: response.contentType,
text: body.toString('utf8')
};
}
2018-06-25 17:42:50 +00:00
function getResponseHeaders(response: ResponseModel): Array<HarHeader> {
return response.headers.filter(header => header.name).map(h => {
return {
name: h.name,
value: h.value
};
});
}
2018-06-25 17:42:50 +00:00
function getRequestHeaders(renderedRequest: RenderedRequest): Array<HarHeader> {
return renderedRequest.headers.filter(header => header.name).map(header => {
return {
name: header.name,
value: header.value
};
});
}
2018-06-25 17:42:50 +00:00
function getRequestQueryString(
renderedRequest: RenderedRequest
): Array<HarQueryString> {
return renderedRequest.parameters.map(parameter => {
return {
name: parameter.name,
value: parameter.value
};
});
}
2018-06-25 17:42:50 +00:00
function getRequestPostData(
renderedRequest: RenderedRequest
): HarPostData | void {
let body;
if (renderedRequest.body.fileName) {
try {
2018-06-25 17:42:50 +00:00
body = newBodyRaw(
fs.readFileSync(renderedRequest.body.fileName, 'base64')
);
} catch (e) {
console.warn('[code gen] Failed to read file', e);
return undefined;
}
} else {
// For every other type, Insomnia uses the same body format as HAR
body = renderedRequest.body;
}
let params = [];
if (body.params) {
params = body.params.map(param => {
return {
name: param.name,
value: param.value
};
});
}
return {
mimeType: body.mimeType || '',
text: body.text || '',
params: params
};
}