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';
|
2016-09-17 05:46:44 +00:00
|
|
|
import {Tab, Tabs, TabList, TabPanel} from 'react-tabs';
|
2016-11-07 20:24:38 +00:00
|
|
|
import SizeTag from './tags/SizeTag';
|
|
|
|
import StatusTag from './tags/StatusTag';
|
|
|
|
import TimeTag from './tags/TimeTag';
|
2017-02-28 21:32:23 +00:00
|
|
|
import Button from './base/Button';
|
2016-09-17 05:46:44 +00:00
|
|
|
import PreviewModeDropdown from './dropdowns/PreviewModeDropdown';
|
|
|
|
import ResponseViewer from './viewers/ResponseViewer';
|
2016-11-27 21:42:38 +00:00
|
|
|
import ResponseHistoryDropdown from './dropdowns/ResponseHistoryDropdown';
|
|
|
|
import ResponseTimer from './ResponseTimer';
|
2016-09-17 05:46:44 +00:00
|
|
|
import ResponseHeadersViewer from './viewers/ResponseHeadersViewer';
|
|
|
|
import ResponseCookiesViewer from './viewers/ResponseCookiesViewer';
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as models from '../../models';
|
2016-11-27 21:42:38 +00:00
|
|
|
import {MOD_SYM, PREVIEW_MODE_SOURCE, getPreviewModeName} from '../../common/constants';
|
2017-02-28 21:32:23 +00:00
|
|
|
import {getSetCookieHeaders, nullFn} from '../../common/misc';
|
2016-11-10 05:56:23 +00:00
|
|
|
import {cancelCurrentRequest} from '../../common/network';
|
2016-11-10 05:47:20 +00:00
|
|
|
import {trackEvent} from '../../analytics';
|
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) {
|
|
|
|
let response = await models.response.getById(responseId);
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
title: 'Save Response',
|
|
|
|
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
|
|
|
|
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,
|
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-01-24 22:18:11 +00:00
|
|
|
editorKeyMap,
|
2016-11-16 17:18:39 +00:00
|
|
|
filter,
|
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">
|
2016-11-27 21:42:38 +00:00
|
|
|
<ResponseTimer
|
2017-01-23 22:41:31 +00:00
|
|
|
className="overlay"
|
2016-11-27 21:42:38 +00:00
|
|
|
handleCancel={cancelCurrentRequest}
|
|
|
|
loadStartTime={loadStartTime}
|
|
|
|
/>
|
2016-09-20 21:17:01 +00:00
|
|
|
|
|
|
|
<header className="pane__header"></header>
|
|
|
|
<div className="pane__body pane__body--placeholder">
|
|
|
|
<div>
|
|
|
|
<table>
|
|
|
|
<tbody>
|
|
|
|
<tr>
|
|
|
|
<td>Send Request</td>
|
|
|
|
<td className="text-right">
|
|
|
|
<code>{MOD_SYM}Enter</code>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>Focus Url Bar</td>
|
|
|
|
<td className="text-right">
|
|
|
|
<code>{MOD_SYM}L</code>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>Manage Cookies</td>
|
|
|
|
<td className="text-right">
|
|
|
|
<code>{MOD_SYM}K</code>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>Edit Environments</td>
|
|
|
|
<td className="text-right">
|
|
|
|
<code>{MOD_SYM}E</code>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2016-09-17 05:46:44 +00:00
|
|
|
</div>
|
2016-09-20 21:17:01 +00:00
|
|
|
</div>
|
|
|
|
</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">
|
2016-11-27 21:42:38 +00:00
|
|
|
<ResponseTimer
|
2017-01-23 22:41:31 +00:00
|
|
|
className="overlay"
|
2016-11-27 21:42:38 +00:00
|
|
|
handleCancel={cancelCurrentRequest}
|
|
|
|
loadStartTime={loadStartTime}
|
|
|
|
/>
|
2016-09-20 21:17:01 +00:00
|
|
|
{!response ? null : (
|
2017-03-03 01:44:07 +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}
|
|
|
|
/>
|
|
|
|
<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
|
2016-11-27 21:42:38 +00:00
|
|
|
/>
|
2017-03-03 01:44:07 +00:00
|
|
|
</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}
|
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>
|
|
|
|
<Button onClick={this._trackTab} value="Cookies">
|
2016-09-20 21:17:01 +00:00
|
|
|
Cookies {cookieHeaders.length ? (
|
2017-03-03 01:44:07 +00:00
|
|
|
<span className="txt-sm">
|
2016-09-17 05:46:44 +00:00
|
|
|
({cookieHeaders.length})
|
|
|
|
</span>
|
2017-03-03 01:44:07 +00:00
|
|
|
) : null}
|
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>
|
|
|
|
<Button onClick={this._trackTab} value="Headers">
|
2016-09-20 21:17:01 +00:00
|
|
|
Headers {response.headers.length ? (
|
2017-03-03 01:44:07 +00:00
|
|
|
<span className="txt-sm">
|
2016-09-17 05:46:44 +00:00
|
|
|
({response.headers.length})
|
|
|
|
</span>
|
2017-03-03 01:44:07 +00:00
|
|
|
) : null}
|
2017-02-28 21:32:23 +00:00
|
|
|
</Button>
|
2016-09-20 21:17:01 +00:00
|
|
|
</Tab>
|
|
|
|
</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}
|
|
|
|
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-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">
|
|
|
|
<ResponseCookiesViewer
|
|
|
|
showCookiesModal={showCookiesModal}
|
|
|
|
key={response._id}
|
|
|
|
headers={cookieHeaders}
|
|
|
|
/>
|
|
|
|
</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">
|
|
|
|
<ResponseHeadersViewer
|
|
|
|
key={response._id}
|
|
|
|
headers={response.headers}
|
|
|
|
/>
|
|
|
|
</div>
|
2016-09-20 21:17:01 +00:00
|
|
|
</TabPanel>
|
|
|
|
</Tabs>
|
|
|
|
</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,
|
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,
|
2016-09-17 05:46:44 +00:00
|
|
|
editorFontSize: 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;
|