2016-09-17 05:46:44 +00:00
|
|
|
import React, {PropTypes, Component} from 'react';
|
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';
|
2016-09-17 05:46:44 +00:00
|
|
|
import PreviewModeDropdown from './dropdowns/PreviewModeDropdown';
|
|
|
|
import ResponseViewer from './viewers/ResponseViewer';
|
|
|
|
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-10 02:40:53 +00:00
|
|
|
import {REQUEST_TIME_TO_SHOW_COUNTER, MOD_SYM, PREVIEW_MODE_SOURCE, getPreviewModeName} from '../../common/constants';
|
|
|
|
import {getSetCookieHeaders} 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
|
|
|
|
|
|
|
class ResponsePane extends Component {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
response: null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
async _getResponse (request) {
|
2016-09-17 05:46:44 +00:00
|
|
|
if (!request) {
|
|
|
|
this.setState({response: null});
|
2016-10-02 20:57:00 +00:00
|
|
|
} else {
|
2016-11-10 01:15:27 +00:00
|
|
|
const response = await models.response.getLatestByParentId(request._id);
|
2016-09-17 05:46:44 +00:00
|
|
|
this.setState({response});
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-17 05:46:44 +00:00
|
|
|
}
|
|
|
|
|
2016-10-28 17:51:08 +00:00
|
|
|
async _handleDownloadResponseBody () {
|
|
|
|
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: [{
|
|
|
|
name: 'Download', extensions: [extension],
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
remote.dialog.showSaveDialog(options, filename => {
|
|
|
|
if (!filename) {
|
2016-10-29 05:40:09 +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-10-29 05:40:09 +00:00
|
|
|
trackEvent('Response', 'Save', 'Failure');
|
2016-10-28 17:51:08 +00:00
|
|
|
} else {
|
2016-10-29 05:40:09 +00:00
|
|
|
trackEvent('Response', 'Save', 'Success');
|
2016-10-28 17:51:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-17 05:46:44 +00:00
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
this._getResponse(nextProps.request);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
|
|
|
this._getResponse(this.props.request);
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const {
|
|
|
|
request,
|
|
|
|
previewMode,
|
2016-11-16 17:18:39 +00:00
|
|
|
handleSetPreviewMode,
|
|
|
|
handleSetFilter,
|
|
|
|
loadStartTime,
|
2016-09-17 05:46:44 +00:00
|
|
|
editorLineWrapping,
|
|
|
|
editorFontSize,
|
2016-11-16 17:18:39 +00:00
|
|
|
filter,
|
2016-09-17 05:46:44 +00:00
|
|
|
showCookiesModal
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const {response} = this.state;
|
|
|
|
|
|
|
|
let timer = null;
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
if (loadStartTime >= 0) {
|
2016-09-17 05:46:44 +00:00
|
|
|
// Set a timer to update the UI again soon
|
|
|
|
// TODO: Move this into a child component so we don't rerender too much
|
|
|
|
setTimeout(() => {
|
|
|
|
this.forceUpdate();
|
|
|
|
}, 100);
|
|
|
|
|
|
|
|
// NOTE: subtract 200ms because the request has some time on either end
|
|
|
|
const millis = Date.now() - loadStartTime - 200;
|
|
|
|
const elapsedTime = Math.round(millis / 100) / 10;
|
|
|
|
|
|
|
|
timer = (
|
|
|
|
<div className="response-pane__overlay">
|
|
|
|
{elapsedTime > REQUEST_TIME_TO_SHOW_COUNTER ? (
|
|
|
|
<h2>{elapsedTime} seconds...</h2>
|
|
|
|
) : (
|
|
|
|
<h2>Loading...</h2>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<br/>
|
|
|
|
<i className="fa fa-refresh fa-spin"></i>
|
|
|
|
|
|
|
|
<br/>
|
|
|
|
<div className="pad">
|
|
|
|
<button className="btn btn--super-compact btn--outlined"
|
|
|
|
onClick={() => cancelCurrentRequest()}>
|
|
|
|
Cancel Request
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
2016-09-17 05:46:44 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!response) {
|
|
|
|
return (
|
2016-09-20 21:17:01 +00:00
|
|
|
<section className="response-pane pane">
|
|
|
|
{timer}
|
|
|
|
|
|
|
|
<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>
|
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">
|
|
|
|
{timer}
|
|
|
|
{!response ? null : (
|
2016-11-07 20:24:38 +00:00
|
|
|
<header className="pane__header">
|
|
|
|
<StatusTag
|
|
|
|
statusCode={response.statusCode}
|
|
|
|
statusMessage={response.statusMessage}
|
|
|
|
/>
|
|
|
|
<TimeTag milliseconds={response.elapsedTime}/>
|
|
|
|
<SizeTag bytes={response.bytesRead}/>
|
|
|
|
</header>
|
2016-09-20 21:17:01 +00:00
|
|
|
)}
|
|
|
|
<Tabs className="pane__body">
|
|
|
|
<TabList>
|
|
|
|
<Tab>
|
2016-10-27 03:41:30 +00:00
|
|
|
<button>
|
2016-09-20 21:17:01 +00:00
|
|
|
{getPreviewModeName(previewMode)}
|
|
|
|
</button>
|
|
|
|
<PreviewModeDropdown
|
2016-10-28 17:51:08 +00:00
|
|
|
download={this._handleDownloadResponseBody.bind(this)}
|
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>
|
|
|
|
<Tab>
|
2016-10-27 03:41:30 +00:00
|
|
|
<button>
|
2016-09-20 21:17:01 +00:00
|
|
|
Cookies {cookieHeaders.length ? (
|
|
|
|
<span className="txt-sm">
|
2016-09-17 05:46:44 +00:00
|
|
|
({cookieHeaders.length})
|
|
|
|
</span>
|
2016-09-20 21:17:01 +00:00
|
|
|
) : null}
|
|
|
|
</button>
|
|
|
|
</Tab>
|
|
|
|
<Tab>
|
2016-10-27 03:41:30 +00:00
|
|
|
<button>
|
2016-09-20 21:17:01 +00:00
|
|
|
Headers {response.headers.length ? (
|
|
|
|
<span className="txt-sm">
|
2016-09-17 05:46:44 +00:00
|
|
|
({response.headers.length})
|
|
|
|
</span>
|
2016-09-20 21:17:01 +00:00
|
|
|
) : null}
|
|
|
|
</button>
|
|
|
|
</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-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}
|
|
|
|
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>
|
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-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,
|
|
|
|
editorLineWrapping: PropTypes.bool.isRequired,
|
2016-11-16 17:18:39 +00:00
|
|
|
loadStartTime: PropTypes.number.isRequired,
|
2016-09-17 05:46:44 +00:00
|
|
|
|
|
|
|
// Other
|
2016-11-16 17:18:39 +00:00
|
|
|
request: PropTypes.object,
|
2016-09-17 05:46:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ResponsePane;
|