2017-02-28 21:32:23 +00:00
|
|
|
import React, {PropTypes, PureComponent} from 'react';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2016-10-28 17:51:08 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import mime from 'mime-types';
|
|
|
|
import {remote} from 'electron';
|
2017-04-04 23:06:43 +00:00
|
|
|
import {Tab, TabList, TabPanel, Tabs} from 'react-tabs';
|
2017-03-08 05:52:17 +00:00
|
|
|
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';
|
2017-03-28 22:45:23 +00:00
|
|
|
import ResponseTimelineViewer from './viewers/response-timeline-viewer';
|
2017-03-08 05:52:17 +00:00
|
|
|
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';
|
2017-06-16 22:21:41 +00:00
|
|
|
import {PREVIEW_MODE_SOURCE} from '../../common/constants';
|
2017-02-28 21:32:23 +00:00
|
|
|
import {getSetCookieHeaders, nullFn} from '../../common/misc';
|
2017-03-16 17:51:56 +00:00
|
|
|
import {cancelCurrentRequest} from '../../network/network';
|
2016-11-10 05:47:20 +00:00
|
|
|
import {trackEvent} from '../../analytics';
|
2017-04-04 23:06:43 +00:00
|
|
|
import Hotkey from './hotkey';
|
2016-09-17 05:46:44 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2017-02-28 21:32:23 +00:00
|
|
|
class ResponsePane extends PureComponent {
|
2017-03-03 01:44:07 +00:00
|
|
|
_trackTab (name) {
|
|
|
|
trackEvent('Response Pane', 'View', name);
|
|
|
|
}
|
2017-02-28 21:32:23 +00:00
|
|
|
|
2017-06-30 03:30:22 +00:00
|
|
|
_handleGetResponseBody () {
|
2017-07-17 18:20:38 +00:00
|
|
|
return models.response.getBodyBuffer(this.props.response);
|
2017-06-30 03:30:22 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 21:42:38 +00:00
|
|
|
async _getResponse (requestId, responseId) {
|
2017-04-21 04:30:52 +00:00
|
|
|
let response = responseId ? await models.response.getById(responseId) : null;
|
2016-11-27 21:42:38 +00:00
|
|
|
|
|
|
|
if (!response) {
|
2017-06-30 03:30:22 +00:00
|
|
|
response = await models.response.getLatestForRequest(requestId);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-11-27 21:42:38 +00:00
|
|
|
|
|
|
|
this.setState({response});
|
2016-09-17 05:46:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
async _handleDownloadResponseBody () {
|
2017-07-17 18:20:38 +00:00
|
|
|
if (!this.props.response) {
|
2016-10-28 17:51:08 +00:00
|
|
|
// Should never happen
|
|
|
|
console.warn('No response to download');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-17 22:58:41 +00:00
|
|
|
const {response} = this.props;
|
2017-06-30 03:30:22 +00:00
|
|
|
const {contentType} = response;
|
2016-10-28 17:51:08 +00:00
|
|
|
const extension = mime.extension(contentType) || '';
|
|
|
|
|
|
|
|
const options = {
|
2017-05-15 19:44:12 +00:00
|
|
|
title: 'Save Response Body',
|
2016-10-28 17:51:08 +00:00
|
|
|
buttonLabel: 'Save',
|
|
|
|
filters: [{
|
2017-03-03 20:09:08 +00:00
|
|
|
name: 'Download', extensions: [extension]
|
2016-10-28 17:51:08 +00:00
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
2017-06-30 03:30:22 +00:00
|
|
|
remote.dialog.showSaveDialog(options, outputPath => {
|
|
|
|
if (!outputPath) {
|
2016-11-23 19:33:24 +00:00
|
|
|
trackEvent('Response', 'Save Cancel');
|
2016-10-28 17:51:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-30 03:30:22 +00:00
|
|
|
const bodyBuffer = models.response.getBodyBuffer(response);
|
|
|
|
|
|
|
|
fs.writeFile(outputPath, bodyBuffer, err => {
|
2016-10-28 17:51:08 +00:00
|
|
|
if (err) {
|
|
|
|
console.warn('Failed to save response body', err);
|
2016-11-23 19:33:24 +00:00
|
|
|
trackEvent('Response', 'Save Failure');
|
2016-10-28 17:51:08 +00:00
|
|
|
} else {
|
2016-11-23 19:33:24 +00:00
|
|
|
trackEvent('Response', 'Save Success');
|
2016-10-28 17:51:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-10-28 17:51:08 +00:00
|
|
|
|
2017-06-30 03:30:22 +00:00
|
|
|
_handleDownloadFullResponseBody () {
|
2017-07-17 22:58:41 +00:00
|
|
|
const {response} = this.props;
|
2017-06-30 03:30:22 +00:00
|
|
|
|
|
|
|
if (!response) {
|
2017-05-15 19:44:12 +00:00
|
|
|
// Should never happen
|
|
|
|
console.warn('No response to download');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-30 03:30:22 +00:00
|
|
|
const headers = response.timeline
|
2017-05-25 18:31:36 +00:00
|
|
|
.filter(v => v.name === 'HEADER_IN')
|
|
|
|
.map(v => v.value)
|
|
|
|
.join('');
|
2017-06-30 03:30:22 +00:00
|
|
|
|
|
|
|
const bodyBuffer = models.response.getBodyBuffer(response);
|
|
|
|
|
2017-05-15 19:44:12 +00:00
|
|
|
const fullResponse = `${headers}${bodyBuffer}`;
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
title: 'Save Full Response',
|
|
|
|
buttonLabel: 'Save',
|
|
|
|
filters: [{
|
|
|
|
name: 'Download'
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
remote.dialog.showSaveDialog(options, filename => {
|
|
|
|
if (!filename) {
|
|
|
|
trackEvent('Response', 'Save Full Cancel');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFile(filename, fullResponse, {}, err => {
|
|
|
|
if (err) {
|
|
|
|
console.warn('Failed to save full response', err);
|
|
|
|
trackEvent('Response', 'Save Full Failure');
|
|
|
|
} else {
|
|
|
|
trackEvent('Response', 'Save Full Success');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-17 05:46:44 +00:00
|
|
|
render () {
|
|
|
|
const {
|
|
|
|
request,
|
2017-07-17 18:20:38 +00:00
|
|
|
responses,
|
|
|
|
response,
|
2016-09-17 05:46:44 +00:00
|
|
|
previewMode,
|
2017-03-29 23:09:28 +00:00
|
|
|
handleShowRequestSettings,
|
2016-11-16 17:18:39 +00:00
|
|
|
handleSetPreviewMode,
|
2016-11-27 21:42:38 +00:00
|
|
|
handleSetActiveResponse,
|
|
|
|
handleDeleteResponses,
|
2017-06-12 21:48:17 +00:00
|
|
|
handleDeleteResponse,
|
2016-11-16 17:18:39 +00:00
|
|
|
handleSetFilter,
|
|
|
|
loadStartTime,
|
2016-09-17 05:46:44 +00:00
|
|
|
editorLineWrapping,
|
|
|
|
editorFontSize,
|
2017-04-14 21:47:15 +00:00
|
|
|
editorIndentSize,
|
2017-01-24 22:18:11 +00:00
|
|
|
editorKeyMap,
|
2016-11-16 17:18:39 +00:00
|
|
|
filter,
|
2017-06-08 02:42:16 +00:00
|
|
|
filterHistory,
|
2016-09-17 05:46:44 +00:00
|
|
|
showCookiesModal
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
if (!request) {
|
|
|
|
return (
|
2016-09-20 21:17:01 +00:00
|
|
|
<section className="response-pane pane">
|
|
|
|
<header className="pane__header"></header>
|
|
|
|
<div className="pane__body pane__body--placeholder"></div>
|
|
|
|
</section>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-09-17 05:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!response) {
|
|
|
|
return (
|
2016-09-20 21:17:01 +00:00
|
|
|
<section className="response-pane pane">
|
|
|
|
<header className="pane__header"></header>
|
|
|
|
<div className="pane__body pane__body--placeholder">
|
|
|
|
<div>
|
2017-03-23 22:10:42 +00:00
|
|
|
<table className="table--fancy">
|
2016-09-20 21:17:01 +00:00
|
|
|
<tbody>
|
|
|
|
<tr>
|
|
|
|
<td>Send Request</td>
|
|
|
|
<td className="text-right">
|
2017-04-04 23:06:43 +00:00
|
|
|
<code><Hotkey char="Enter"/></code>
|
2016-09-20 21:17:01 +00:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>Focus Url Bar</td>
|
|
|
|
<td className="text-right">
|
2017-04-04 23:06:43 +00:00
|
|
|
<code><Hotkey char="L"/></code>
|
2016-09-20 21:17:01 +00:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>Manage Cookies</td>
|
|
|
|
<td className="text-right">
|
2017-04-04 23:06:43 +00:00
|
|
|
<code><Hotkey char="K"/></code>
|
2016-09-20 21:17:01 +00:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>Edit Environments</td>
|
|
|
|
<td className="text-right">
|
2017-04-04 23:06:43 +00:00
|
|
|
<code><Hotkey char="E"/></code>
|
2016-09-20 21:17:01 +00:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2016-09-17 05:46:44 +00:00
|
|
|
</div>
|
2016-09-20 21:17:01 +00:00
|
|
|
</div>
|
2017-03-06 17:51:58 +00:00
|
|
|
|
|
|
|
<ResponseTimer
|
|
|
|
handleCancel={cancelCurrentRequest}
|
|
|
|
loadStartTime={loadStartTime}
|
|
|
|
/>
|
2016-09-20 21:17:01 +00:00
|
|
|
</section>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-09-17 05:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const cookieHeaders = getSetCookieHeaders(response.headers);
|
|
|
|
|
|
|
|
return (
|
2016-09-20 21:17:01 +00:00
|
|
|
<section className="response-pane pane">
|
|
|
|
{!response ? null : (
|
2017-03-28 22:45:23 +00:00
|
|
|
<header className="pane__header row-spaced">
|
|
|
|
<div className="no-wrap scrollable scrollable--no-bars pad-left">
|
|
|
|
<StatusTag
|
|
|
|
statusCode={response.statusCode}
|
|
|
|
statusMessage={response.statusMessage || null}
|
2016-11-27 21:42:38 +00:00
|
|
|
/>
|
2017-03-28 22:45:23 +00:00
|
|
|
<TimeTag milliseconds={response.elapsedTime}/>
|
|
|
|
<SizeTag bytes={response.bytesRead}/>
|
|
|
|
</div>
|
|
|
|
<ResponseHistoryDropdown
|
2017-07-17 18:20:38 +00:00
|
|
|
activeResponse={response}
|
|
|
|
responses={responses}
|
2017-03-28 22:45:23 +00:00
|
|
|
requestId={request._id}
|
|
|
|
handleSetActiveResponse={handleSetActiveResponse}
|
|
|
|
handleDeleteResponses={handleDeleteResponses}
|
2017-06-12 21:48:17 +00:00
|
|
|
handleDeleteResponse={handleDeleteResponse}
|
2017-03-28 22:45:23 +00:00
|
|
|
onChange={nullFn}
|
|
|
|
className="tall pane__header__right"
|
|
|
|
right
|
|
|
|
/>
|
|
|
|
</header>
|
|
|
|
)}
|
2017-03-01 21:15:56 +00:00
|
|
|
<Tabs className="pane__body" forceRenderTabPanel>
|
2016-09-20 21:17:01 +00:00
|
|
|
<TabList>
|
2017-02-28 21:32:23 +00:00
|
|
|
<Tab>
|
2016-09-20 21:17:01 +00:00
|
|
|
<PreviewModeDropdown
|
2017-02-28 21:32:23 +00:00
|
|
|
download={this._handleDownloadResponseBody}
|
2017-05-15 19:44:12 +00:00
|
|
|
fullDownload={this._handleDownloadFullResponseBody}
|
2016-09-20 21:17:01 +00:00
|
|
|
previewMode={previewMode}
|
2016-11-16 17:18:39 +00:00
|
|
|
updatePreviewMode={handleSetPreviewMode}
|
2016-09-20 21:17:01 +00:00
|
|
|
/>
|
|
|
|
</Tab>
|
2017-02-28 21:32:23 +00:00
|
|
|
<Tab>
|
2017-03-28 22:45:23 +00:00
|
|
|
<Button onClick={this._trackTab} value="Headers">
|
2017-05-25 18:31:36 +00:00
|
|
|
Header
|
|
|
|
{' '}
|
|
|
|
{response.headers.length > 0 && (
|
|
|
|
<span className="bubble">{response.headers.length}</span>
|
|
|
|
)}
|
2017-02-28 21:32:23 +00:00
|
|
|
</Button>
|
2016-09-20 21:17:01 +00:00
|
|
|
</Tab>
|
2017-02-28 21:32:23 +00:00
|
|
|
<Tab>
|
2017-03-28 22:45:23 +00:00
|
|
|
<Button onClick={this._trackTab} value="Cookies">
|
|
|
|
Cookie {cookieHeaders.length ? (
|
|
|
|
<span className="bubble">{cookieHeaders.length}</span>) : null}
|
2017-02-28 21:32:23 +00:00
|
|
|
</Button>
|
2016-09-20 21:17:01 +00:00
|
|
|
</Tab>
|
2017-07-17 22:17:31 +00:00
|
|
|
<Tab>
|
|
|
|
<Button onClick={this._trackTab} value="Timeline">Timeline</Button>
|
|
|
|
</Tab>
|
2016-09-20 21:17:01 +00:00
|
|
|
</TabList>
|
|
|
|
<TabPanel>
|
|
|
|
<ResponseViewer
|
|
|
|
key={response._id}
|
2016-10-02 22:10:43 +00:00
|
|
|
bytes={response.bytesRead}
|
2016-10-27 16:45:44 +00:00
|
|
|
contentType={response.contentType || ''}
|
2016-09-20 21:17:01 +00:00
|
|
|
previewMode={response.error ? PREVIEW_MODE_SOURCE : previewMode}
|
2016-11-16 17:18:39 +00:00
|
|
|
filter={filter}
|
2017-06-08 02:42:16 +00:00
|
|
|
filterHistory={filterHistory}
|
2016-11-16 17:18:39 +00:00
|
|
|
updateFilter={response.error ? null : handleSetFilter}
|
2017-06-30 03:30:22 +00:00
|
|
|
bodyPath={response.bodyPath}
|
|
|
|
getBody={this._handleGetResponseBody}
|
2016-10-26 23:22:15 +00:00
|
|
|
encoding={response.encoding}
|
2017-06-30 03:30:22 +00:00
|
|
|
error={response.error}
|
2016-09-20 21:17:01 +00:00
|
|
|
editorLineWrapping={editorLineWrapping}
|
|
|
|
editorFontSize={editorFontSize}
|
2017-04-14 21:47:15 +00:00
|
|
|
editorIndentSize={editorIndentSize}
|
2017-01-24 22:18:11 +00:00
|
|
|
editorKeyMap={editorKeyMap}
|
2016-09-20 21:17:01 +00:00
|
|
|
url={response.url}
|
|
|
|
/>
|
|
|
|
</TabPanel>
|
2016-09-21 00:03:26 +00:00
|
|
|
<TabPanel className="scrollable-container">
|
|
|
|
<div className="scrollable pad">
|
2017-03-28 22:45:23 +00:00
|
|
|
<ResponseHeadersViewer
|
2016-09-21 00:03:26 +00:00
|
|
|
key={response._id}
|
2017-03-28 22:45:23 +00:00
|
|
|
headers={response.headers}
|
2016-09-21 00:03:26 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2016-09-20 21:17:01 +00:00
|
|
|
</TabPanel>
|
2016-09-21 00:03:26 +00:00
|
|
|
<TabPanel className="scrollable-container">
|
|
|
|
<div className="scrollable pad">
|
2017-03-28 22:45:23 +00:00
|
|
|
<ResponseCookiesViewer
|
2017-03-29 23:09:28 +00:00
|
|
|
handleShowRequestSettings={handleShowRequestSettings}
|
|
|
|
cookiesSent={response.settingSendCookies}
|
|
|
|
cookiesStored={response.settingStoreCookies}
|
2017-03-28 22:45:23 +00:00
|
|
|
showCookiesModal={showCookiesModal}
|
2016-09-21 00:03:26 +00:00
|
|
|
key={response._id}
|
2017-03-28 22:45:23 +00:00
|
|
|
headers={cookieHeaders}
|
2016-09-21 00:03:26 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2016-09-20 21:17:01 +00:00
|
|
|
</TabPanel>
|
2017-07-17 22:17:31 +00:00
|
|
|
<TabPanel>
|
|
|
|
<ResponseTimelineViewer
|
|
|
|
key={response._id}
|
|
|
|
timeline={response.timeline || []}
|
|
|
|
editorLineWrapping={editorLineWrapping}
|
|
|
|
editorFontSize={editorFontSize}
|
|
|
|
editorIndentSize={editorIndentSize}
|
|
|
|
/>
|
|
|
|
</TabPanel>
|
2016-09-20 21:17:01 +00:00
|
|
|
</Tabs>
|
2017-03-06 17:51:58 +00:00
|
|
|
<ResponseTimer
|
|
|
|
handleCancel={cancelCurrentRequest}
|
|
|
|
loadStartTime={loadStartTime}
|
|
|
|
/>
|
2016-09-20 21:17:01 +00:00
|
|
|
</section>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-09-17 05:46:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ResponsePane.propTypes = {
|
|
|
|
// Functions
|
2016-11-16 17:18:39 +00:00
|
|
|
handleSetFilter: PropTypes.func.isRequired,
|
2016-09-17 05:46:44 +00:00
|
|
|
showCookiesModal: PropTypes.func.isRequired,
|
2016-11-16 17:18:39 +00:00
|
|
|
handleSetPreviewMode: PropTypes.func.isRequired,
|
2016-11-27 21:42:38 +00:00
|
|
|
handleSetActiveResponse: PropTypes.func.isRequired,
|
|
|
|
handleDeleteResponses: PropTypes.func.isRequired,
|
2017-06-12 21:48:17 +00:00
|
|
|
handleDeleteResponse: PropTypes.func.isRequired,
|
2017-03-29 23:09:28 +00:00
|
|
|
handleShowRequestSettings: PropTypes.func.isRequired,
|
2016-09-17 05:46:44 +00:00
|
|
|
|
|
|
|
// Required
|
|
|
|
previewMode: PropTypes.string.isRequired,
|
2016-11-16 17:18:39 +00:00
|
|
|
filter: PropTypes.string.isRequired,
|
2017-06-08 02:42:16 +00:00
|
|
|
filterHistory: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired,
|
2016-09-17 05:46:44 +00:00
|
|
|
editorFontSize: PropTypes.number.isRequired,
|
2017-04-14 21:47:15 +00:00
|
|
|
editorIndentSize: PropTypes.number.isRequired,
|
2017-01-24 22:18:11 +00:00
|
|
|
editorKeyMap: PropTypes.string.isRequired,
|
2016-09-17 05:46:44 +00:00
|
|
|
editorLineWrapping: PropTypes.bool.isRequired,
|
2016-11-16 17:18:39 +00:00
|
|
|
loadStartTime: PropTypes.number.isRequired,
|
2017-07-17 18:20:38 +00:00
|
|
|
responses: PropTypes.arrayOf(PropTypes.object),
|
2016-09-17 05:46:44 +00:00
|
|
|
|
|
|
|
// Other
|
2017-07-17 18:20:38 +00:00
|
|
|
request: PropTypes.object,
|
|
|
|
response: PropTypes.object
|
2016-09-17 05:46:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ResponsePane;
|