insomnia/packages/insomnia-app/app/ui/components/response-pane.js

395 lines
13 KiB
JavaScript
Raw Normal View History

2017-07-17 23:32:47 +00:00
// @flow
2018-06-25 17:42:50 +00:00
import type { Request } from '../../models/request';
import type { Response } from '../../models/response';
2017-07-18 22:10:57 +00:00
import * as React from 'react';
import autobind from 'autobind-decorator';
2016-10-28 17:51:08 +00:00
import fs from 'fs';
import mime from 'mime-types';
2018-06-25 17:42:50 +00:00
import { remote } from 'electron';
import { Tab, TabList, TabPanel, Tabs } from 'react-tabs';
import SizeTag from './tags/size-tag';
import StatusTag from './tags/status-tag';
import TimeTag from './tags/time-tag';
import Button from './base/button';
import PreviewModeDropdown from './dropdowns/preview-mode-dropdown';
import ResponseViewer from './viewers/response-viewer';
import ResponseHistoryDropdown from './dropdowns/response-history-dropdown';
import ResponseTimer from './response-timer';
import ResponseTimelineViewer from './viewers/response-timeline-viewer';
import ResponseHeadersViewer from './viewers/response-headers-viewer';
import ResponseCookiesViewer from './viewers/response-cookies-viewer';
2016-11-10 05:56:23 +00:00
import * as models from '../../models';
2018-06-25 17:42:50 +00:00
import { PREVIEW_MODE_SOURCE } from '../../common/constants';
2019-06-07 15:33:10 +00:00
import { getSetCookieHeaders } from '../../common/misc';
2018-06-25 17:42:50 +00:00
import { cancelCurrentRequest } from '../../network/network';
import Hotkey from './hotkey';
import ErrorBoundary from './error-boundary';
import type { HotKeyRegistry } from '../../common/hotkeys';
import { hotKeyRefs } from '../../common/hotkeys';
import type { RequestVersion } from '../../models/request-version';
import { showError } from '../components/modals/index';
import { json as jsonPrettify } from 'insomnia-prettify';
2016-09-17 05:46:44 +00:00
type Props = {
// Functions
handleSetFilter: Function,
showCookiesModal: Function,
handleSetPreviewMode: Function,
handleSetActiveResponse: Function,
handleDeleteResponses: Function,
handleDeleteResponse: Function,
handleShowRequestSettings: Function,
2017-07-17 23:32:47 +00:00
// Required
previewMode: string,
filter: string,
filterHistory: Array<string>,
editorFontSize: number,
editorIndentSize: number,
editorKeyMap: string,
editorLineWrapping: boolean,
loadStartTime: number,
responses: Array<Response>,
hotKeyRegistry: HotKeyRegistry,
2017-07-17 23:32:47 +00:00
// Other
requestVersions: Array<RequestVersion>,
request: ?Request,
response: ?Response,
};
2017-07-17 23:32:47 +00:00
@autobind
class ResponsePane extends React.PureComponent<Props> {
_responseViewer: any;
_setResponseViewerRef(n: any) {
this._responseViewer = n;
}
2018-06-25 17:42:50 +00:00
_handleGetResponseBody(): Buffer | null {
2017-07-18 22:10:57 +00:00
if (!this.props.response) {
return null;
}
2017-07-17 18:20:38 +00:00
return models.response.getBodyBuffer(this.props.response);
}
async _handleDownloadResponseBody(prettify: boolean) {
2018-06-25 17:42:50 +00:00
const { response, request } = this.props;
if (!response || !request) {
2016-10-28 17:51:08 +00:00
// Should never happen
console.warn('No response to download');
return;
}
2018-06-25 17:42:50 +00:00
const { contentType } = response;
const extension = mime.extension(contentType) || 'unknown';
2016-10-28 17:51:08 +00:00
const options = {
title: 'Save Response Body',
2016-10-28 17:51:08 +00:00
buttonLabel: 'Save',
defaultPath: `${request.name.replace(/ +/g, '_')}-${Date.now()}.${extension}`,
2016-10-28 17:51:08 +00:00
};
remote.dialog.showSaveDialog(options, outputPath => {
if (!outputPath) {
2016-10-28 17:51:08 +00:00
return;
}
const readStream = models.response.getBodyStream(response);
let dataBuffers = [];
if (readStream) {
readStream.on('data', data => {
dataBuffers.push(data);
});
readStream.on('end', () => {
const to = fs.createWriteStream(outputPath);
const finalBuffer = Buffer.concat(dataBuffers);
to.on('error', err => {
showError({
title: 'Save Failed',
message: 'Failed to save response body',
error: err,
});
});
if (prettify && contentType.includes('json')) {
to.write(jsonPrettify(finalBuffer.toString('utf8')));
} else {
to.write(finalBuffer);
}
2017-07-18 22:10:57 +00:00
});
}
2016-10-28 17:51:08 +00:00
});
}
2016-10-28 17:51:08 +00:00
async _handleDownloadFullResponseBody() {
2018-06-25 17:42:50 +00:00
const { response, request } = this.props;
if (!response || !request) {
// Should never happen
console.warn('No response to download');
return;
}
const timeline = await models.response.getTimeline(response);
const headers = timeline
.filter(v => v.name === 'HEADER_IN')
.map(v => v.value)
.join('');
const options = {
title: 'Save Full Response',
buttonLabel: 'Save',
defaultPath: `${request.name.replace(/ +/g, '_')}-${Date.now()}.txt`,
};
remote.dialog.showSaveDialog(options, filename => {
if (!filename) {
return;
}
const readStream = models.response.getBodyStream(response);
if (readStream) {
const to = fs.createWriteStream(filename);
to.write(headers);
readStream.pipe(to);
to.on('error', err => {
console.warn('Failed to save full response', err);
});
}
});
}
_handleTabSelect(index: number, lastIndex: number) {
if (this._responseViewer != null && index === 0 && index !== lastIndex) {
// Fix for CodeMirror editor not updating its content.
// Refresh must be called when the editor is visible,
// so use nextTick to give time for it to be visible.
process.nextTick(() => {
this._responseViewer.refresh();
});
}
}
2018-06-25 17:42:50 +00:00
render() {
2016-09-17 05:46:44 +00:00
const {
request,
2017-07-17 18:20:38 +00:00
responses,
requestVersions,
2017-07-17 18:20:38 +00:00
response,
2016-09-17 05:46:44 +00:00
previewMode,
handleShowRequestSettings,
handleSetPreviewMode,
handleSetActiveResponse,
handleDeleteResponses,
2017-06-12 21:48:17 +00:00
handleDeleteResponse,
handleSetFilter,
loadStartTime,
2016-09-17 05:46:44 +00:00
editorLineWrapping,
editorFontSize,
editorIndentSize,
2017-01-24 22:18:11 +00:00
editorKeyMap,
filter,
filterHistory,
showCookiesModal,
hotKeyRegistry,
2016-09-17 05:46:44 +00:00
} = this.props;
const paneClasses = 'response-pane theme--pane pane';
const paneHeaderClasses = 'pane__header theme--pane__header';
const paneBodyClasses = 'pane__body theme--pane__body';
2016-09-17 05:46:44 +00:00
if (!request) {
return (
<section className={paneClasses}>
<header className={paneHeaderClasses} />
<div className={paneBodyClasses + ' pane__body--placeholder'} />
2016-09-20 21:17:01 +00:00
</section>
);
2016-09-17 05:46:44 +00:00
}
if (!response) {
return (
<section className={paneClasses}>
<header className={paneHeaderClasses} />
<div className={paneBodyClasses + ' pane__body--placeholder'}>
2016-09-20 21:17:01 +00:00
<div>
<table className="table--fancy">
2016-09-20 21:17:01 +00:00
<tbody>
2018-06-25 17:42:50 +00:00
<tr>
<td>Send Request</td>
<td className="text-right">
<code>
<Hotkey
keyBindings={hotKeyRegistry[hotKeyRefs.REQUEST_SEND.id]}
useFallbackMessage
/>
2018-06-25 17:42:50 +00:00
</code>
</td>
</tr>
<tr>
<td>Focus Url Bar</td>
<td className="text-right">
<code>
<Hotkey
keyBindings={hotKeyRegistry[hotKeyRefs.REQUEST_FOCUS_URL.id]}
useFallbackMessage
/>
2018-06-25 17:42:50 +00:00
</code>
</td>
</tr>
<tr>
<td>Manage Cookies</td>
<td className="text-right">
<code>
<Hotkey
keyBindings={hotKeyRegistry[hotKeyRefs.SHOW_COOKIES_EDITOR.id]}
useFallbackMessage
/>
2018-06-25 17:42:50 +00:00
</code>
</td>
</tr>
<tr>
<td>Edit Environments</td>
<td className="text-right">
<code>
<Hotkey
keyBindings={hotKeyRegistry[hotKeyRefs.ENVIRONMENT_SHOW_EDITOR.id]}
useFallbackMessage
/>
2018-06-25 17:42:50 +00:00
</code>
</td>
</tr>
2016-09-20 21:17:01 +00:00
</tbody>
</table>
2016-09-17 05:46:44 +00:00
</div>
2016-09-20 21:17:01 +00:00
</div>
2018-10-17 16:42:33 +00:00
<ResponseTimer handleCancel={cancelCurrentRequest} loadStartTime={loadStartTime} />
2016-09-20 21:17:01 +00:00
</section>
);
2016-09-17 05:46:44 +00:00
}
const cookieHeaders = getSetCookieHeaders(response.headers);
return (
<section className={paneClasses}>
2016-09-20 21:17:01 +00:00
{!response ? null : (
<header className={paneHeaderClasses + ' row-spaced'}>
<div className="no-wrap scrollable scrollable--no-bars pad-left">
2018-10-17 16:42:33 +00:00
<StatusTag statusCode={response.statusCode} statusMessage={response.statusMessage} />
2018-06-25 17:42:50 +00:00
<TimeTag milliseconds={response.elapsedTime} />
2018-10-17 16:42:33 +00:00
<SizeTag bytesRead={response.bytesRead} bytesContent={response.bytesContent} />
</div>
<ResponseHistoryDropdown
2017-07-17 18:20:38 +00:00
activeResponse={response}
responses={responses}
requestVersions={requestVersions}
requestId={request._id}
handleSetActiveResponse={handleSetActiveResponse}
handleDeleteResponses={handleDeleteResponses}
2017-06-12 21:48:17 +00:00
handleDeleteResponse={handleDeleteResponse}
className="tall pane__header__right"
right
/>
</header>
)}
<Tabs
className={paneBodyClasses + ' react-tabs'}
onSelect={this._handleTabSelect}
forceRenderTabPanel>
2016-09-20 21:17:01 +00:00
<TabList>
<Tab tabIndex="-1">
2016-09-20 21:17:01 +00:00
<PreviewModeDropdown
download={this._handleDownloadResponseBody}
fullDownload={this._handleDownloadFullResponseBody}
2016-09-20 21:17:01 +00:00
previewMode={previewMode}
updatePreviewMode={handleSetPreviewMode}
showPrettifyOption={response.contentType.includes('json')}
2016-09-20 21:17:01 +00:00
/>
</Tab>
<Tab tabIndex="-1">
2017-07-25 22:28:53 +00:00
<Button>
2018-06-25 17:42:50 +00:00
Header{' '}
{response.headers.length > 0 && (
<span className="bubble">{response.headers.length}</span>
)}
</Button>
2016-09-20 21:17:01 +00:00
</Tab>
<Tab tabIndex="-1">
2017-07-25 22:28:53 +00:00
<Button>
2018-06-25 17:42:50 +00:00
Cookie{' '}
{cookieHeaders.length ? (
<span className="bubble">{cookieHeaders.length}</span>
) : null}
</Button>
2016-09-20 21:17:01 +00:00
</Tab>
<Tab tabIndex="-1">
2017-07-25 22:28:53 +00:00
<Button>Timeline</Button>
</Tab>
2016-09-20 21:17:01 +00:00
</TabList>
2017-08-10 01:56:27 +00:00
<TabPanel className="react-tabs__tab-panel">
2018-02-08 11:12:29 +00:00
<ResponseViewer
ref={this._setResponseViewerRef}
2018-02-08 11:12:29 +00:00
// Send larger one because legacy responses have bytesContent === -1
responseId={response._id}
bytes={Math.max(response.bytesContent, response.bytesRead)}
contentType={response.contentType || ''}
previewMode={response.error ? PREVIEW_MODE_SOURCE : previewMode}
filter={filter}
filterHistory={filterHistory}
updateFilter={response.error ? null : handleSetFilter}
download={this._handleDownloadResponseBody}
getBody={this._handleGetResponseBody}
error={response.error}
editorLineWrapping={editorLineWrapping}
editorFontSize={editorFontSize}
editorIndentSize={editorIndentSize}
editorKeyMap={editorKeyMap}
url={response.url}
/>
2016-09-20 21:17:01 +00:00
</TabPanel>
2017-08-10 01:56:27 +00:00
<TabPanel className="react-tabs__tab-panel scrollable-container">
2016-09-21 00:03:26 +00:00
<div className="scrollable pad">
2018-10-17 16:42:33 +00:00
<ErrorBoundary key={response._id} errorClassName="font-error pad text-center">
2018-06-25 17:42:50 +00:00
<ResponseHeadersViewer headers={response.headers} />
</ErrorBoundary>
2016-09-21 00:03:26 +00:00
</div>
2016-09-20 21:17:01 +00:00
</TabPanel>
2017-08-10 01:56:27 +00:00
<TabPanel className="react-tabs__tab-panel scrollable-container">
2016-09-21 00:03:26 +00:00
<div className="scrollable pad">
2018-10-17 16:42:33 +00:00
<ErrorBoundary key={response._id} errorClassName="font-error pad text-center">
<ResponseCookiesViewer
handleShowRequestSettings={handleShowRequestSettings}
cookiesSent={response.settingSendCookies}
cookiesStored={response.settingStoreCookies}
showCookiesModal={showCookiesModal}
headers={cookieHeaders}
/>
</ErrorBoundary>
2016-09-21 00:03:26 +00:00
</div>
2016-09-20 21:17:01 +00:00
</TabPanel>
2017-08-10 01:56:27 +00:00
<TabPanel className="react-tabs__tab-panel">
2018-10-17 16:42:33 +00:00
<ErrorBoundary key={response._id} errorClassName="font-error pad text-center">
<ResponseTimelineViewer
response={response}
editorLineWrapping={editorLineWrapping}
editorFontSize={editorFontSize}
editorIndentSize={editorIndentSize}
/>
</ErrorBoundary>
</TabPanel>
2016-09-20 21:17:01 +00:00
</Tabs>
<ErrorBoundary errorClassName="font-error pad text-center">
2018-10-17 16:42:33 +00:00
<ResponseTimer handleCancel={cancelCurrentRequest} loadStartTime={loadStartTime} />
</ErrorBoundary>
2016-09-20 21:17:01 +00:00
</section>
);
2016-09-17 05:46:44 +00:00
}
}
export default ResponsePane;