insomnia/app/common/har.js

468 lines
11 KiB
JavaScript
Raw Normal View History

// @flow
2016-11-22 22:26:52 +00:00
import fs from 'fs';
import {Cookie as toughCookie} from 'tough-cookie';
2016-11-10 01:15:27 +00:00
import * as models from '../models';
import {getRenderedRequest} from './render';
import type {RenderedRequest} from './render';
import {jarFromCookies} from './cookies';
import * as misc from './misc';
import type {Cookie} from '../models/cookie-jar';
2016-11-22 22:26:52 +00:00
import {newBodyRaw} from '../models/request';
import type {Request} from '../models/request';
import type {Response as ResponseModel} from '../models/response';
import {getAuthHeader} from '../network/authentication';
import {getAppVersion} from './constants';
import {getSetCookieHeaders} from './misc';
2017-11-01 12:40:24 +00:00
import {RenderError} from '../templating/index';
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
}
export type HarQueryString = {
name: string,
value: string,
comment?: string
}
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
};
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
}
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
}
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
}
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) {
const request: Request | null = await models.request.getById(exportRequest.requestId);
if (!request) {
continue;
2016-11-22 22:26:52 +00:00
}
const harRequest = await exportHarWithRequest(request, exportRequest.environmentId);
if (!harRequest) {
continue;
}
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
}
};
}
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
};
}
2017-11-06 20:44:55 +00:00
export async function exportHarRequest (
requestId: string,
environmentId: string,
addContentLength: boolean = false
): Promise<HarRequest | null> {
const request = await models.request.getById(requestId);
if (!request) {
return null;
}
return await exportHarWithRequest(request, environmentId, addContentLength);
}
2017-11-06 20:44:55 +00:00
export async function exportHarWithRequest (
request: Request,
environmentId: string,
addContentLength: boolean = false
): Promise<HarRequest | null> {
2017-11-01 12:40:24 +00:00
try {
const renderedRequest = await getRenderedRequest(request, environmentId);
return exportHarWithRenderedRequest(renderedRequest, addContentLength);
} catch (err) {
if (err instanceof RenderError) {
throw new Error(`Failed to render "${request.name}:${err.path}"\n ${err.message}`);
} else {
throw new Error(`Failed to export request "${request.name}"\n ${err.message}`);
}
}
}
2017-11-06 20:44:55 +00:00
export async function exportHarWithRenderedRequest (
renderedRequest: RenderedRequest,
addContentLength: boolean = false
): Promise<HarRequest> {
const url = misc.prepareUrlForSending(renderedRequest.url, renderedRequest.settingEncodeUrl);
2016-11-23 20:44:46 +00:00
if (addContentLength) {
const hasContentLengthHeader = misc.filterHeaders(
2017-11-06 20:44:55 +00:00
renderedRequest.headers,
'Content-Length'
).length > 0;
2016-11-23 20:44:46 +00:00
if (!hasContentLengthHeader) {
const name = 'Content-Length';
2016-11-23 20:44:46 +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
};
}
2016-08-15 22:31:30 +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);
}
function getReponseCookies (response: ResponseModel): Array<HarCookie> {
return getSetCookieHeaders(response.headers).map(h => {
let cookie;
try {
cookie = toughCookie.parse(h.value || '');
} catch (error) {
}
if (!cookie) {
return null;
}
return mapCookie(cookie);
2017-11-01 12:40:24 +00:00
}).filter(Boolean);
}
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;
}
function getResponseContent (response: ResponseModel): HarContent {
const body: Buffer = models.response.getBodyBuffer(response, new Buffer([]));
return {
size: body.byteLength,
mimeType: response.contentType,
text: body.toString('utf8')
};
}
function getResponseHeaders (response: ResponseModel): Array<HarHeader> {
return response.headers
.filter(header => (header.name))
.map(h => {
return {
name: h.name,
value: h.value
};
});
}
function getRequestHeaders (renderedRequest: RenderedRequest): Array<HarHeader> {
return renderedRequest.headers
.filter(header => (header.name))
.map(header => {
return {
name: header.name,
value: header.value
};
});
}
function getRequestQueryString (renderedRequest: RenderedRequest): Array<HarQueryString> {
return renderedRequest.parameters.map(parameter => {
return {
name: parameter.name,
value: parameter.value
};
});
}
function getRequestPostData (renderedRequest: RenderedRequest): HarPostData | void {
let body;
if (renderedRequest.body.fileName) {
try {
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
};
}