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-04-04 23:06:43 +00:00
|
|
|
import {getPreviewModeName, 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
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
response: null
|
|
|
|
};
|
|
|
|
}
|
2016-09-17 05:46:44 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_trackTab (name) {
|
|
|
|
trackEvent('Response Pane', 'View', name);
|
|
|
|
}
|
2017-02-28 21:32:23 +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) {
|
|
|
|
response = await models.response.getLatestByParentId(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 () {
|
2016-10-28 17:51:08 +00:00
|
|
|
if (!this.state.response) {
|
|
|
|
// Should never happen
|
|
|
|
console.warn('No response to download');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {body, encoding, contentType} = this.state.response;
|
|
|
|
const bodyBuffer = new Buffer(body, encoding);
|
|
|
|
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
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
remote.dialog.showSaveDialog(options, filename => {
|
|
|
|
if (!filename) {
|
2016-11-23 19:33:24 +00:00
|
|
|
trackEvent('Response', 'Save Cancel');
|
2016-10-28 17:51:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFile(filename, bodyBuffer, {}, err => {
|
|
|
|
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-05-15 19:44:12 +00:00
|
|
|
async _handleDownloadFullResponseBody () {
|
|
|
|
if (!this.state.response) {
|
|
|
|
// Should never happen
|
|
|
|
console.warn('No response to download');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {body, timeline, encoding} = this.state.response;
|
|
|
|
const headers = timeline
|
2017-05-25 18:31:36 +00:00
|
|
|
.filter(v => v.name === 'HEADER_IN')
|
|
|
|
.map(v => v.value)
|
|
|
|
.join('');
|
2017-05-15 19:44:12 +00:00
|
|
|
const bodyBuffer = new Buffer(body, encoding);
|
|
|
|
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
|
|
|
componentWillReceiveProps (nextProps) {
|
2016-11-27 21:42:38 +00:00
|
|
|
const activeRequestId = nextProps.request ? nextProps.request._id : null;
|
|
|
|
const activeResponseId = nextProps.activeResponseId;
|
|
|
|
this._getResponse(activeRequestId, activeResponseId);
|
2016-09-17 05:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
2016-11-27 21:42:38 +00:00
|
|
|
const activeRequestId = this.props.request ? this.props.request._id : null;
|
|
|
|
const activeResponseId = this.props.activeResponseId;
|
|
|
|
this._getResponse(activeRequestId, activeResponseId);
|
2016-09-17 05:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const {
|
|
|
|
request,
|
|
|
|
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,
|
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-11-27 21:42:38 +00:00
|
|
|
activeResponseId,
|
2016-09-17 05:46:44 +00:00
|
|
|
showCookiesModal
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const {response} = this.state;
|
|
|
|
|
|
|
|
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
|
|
|
|
requestId={request._id}
|
|
|
|
isLatestResponseActive={!activeResponseId}
|
|
|
|
activeResponseId={response._id}
|
|
|
|
handleSetActiveResponse={handleSetActiveResponse}
|
|
|
|
handleDeleteResponses={handleDeleteResponses}
|
|
|
|
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>
|
|
|
|
<Button onClick={this._trackTab} value="Response">
|
2016-09-20 21:17:01 +00:00
|
|
|
{getPreviewModeName(previewMode)}
|
2017-02-28 21:32:23 +00:00
|
|
|
</Button>
|
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-05-25 18:31:36 +00:00
|
|
|
{(response.timeline && response.timeline.length > 0) && (
|
2017-03-28 22:45:23 +00:00
|
|
|
<Tab>
|
|
|
|
<Button onClick={this._trackTab} value="Timeline">Timeline</Button>
|
|
|
|
</Tab>
|
2017-05-17 19:02:09 +00:00
|
|
|
)}
|
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-29 20:55:31 +00:00
|
|
|
statusCode={response.statusCode}
|
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}
|
2016-09-20 21:17:01 +00:00
|
|
|
body={response.error ? response.error : response.body}
|
2016-10-26 23:22:15 +00:00
|
|
|
encoding={response.encoding}
|
2016-09-20 21:17:01 +00:00
|
|
|
error={!!response.error}
|
2016-10-02 22:10:43 +00:00
|
|
|
responseId={response._id}
|
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-05-25 18:31:36 +00:00
|
|
|
{(response.timeline && response.timeline.length > 0) && (
|
2017-03-28 22:45:23 +00:00
|
|
|
<TabPanel>
|
2017-05-25 18:31:36 +00:00
|
|
|
<ResponseTimelineViewer
|
|
|
|
key={response._id}
|
|
|
|
timeline={response.timeline || []}
|
|
|
|
editorLineWrapping={editorLineWrapping}
|
|
|
|
editorFontSize={editorFontSize}
|
|
|
|
editorIndentSize={editorIndentSize}
|
|
|
|
/>
|
2017-03-28 22:45:23 +00:00
|
|
|
</TabPanel>
|
2017-05-17 19:02:09 +00:00
|
|
|
)}
|
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-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,
|
2016-11-27 21:42:38 +00:00
|
|
|
activeResponseId: PropTypes.string.isRequired,
|
2016-09-17 05:46:44 +00:00
|
|
|
|
|
|
|
// Other
|
2017-03-03 20:09:08 +00:00
|
|
|
request: PropTypes.object
|
2016-09-17 05:46:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ResponsePane;
|