insomnia/app/ui/components/viewers/response-viewer.js
Gregory Schier 8c0d87168e libcurl network backend (#82)
* Most things are working

* Cookies and lots of improvements

* Hoist regex

* Appveyor test

* Fix appveyor file

* Remove yarn

* Test deps

* Fix

* Remove all git deps

* fix

* Fix

* Add cash rm

* Fix command

* Fix

* Production

* try

* fix

* fix?

* Done

* Remove cache

* tweak

* Shortcut

* Fix

* Attempt

* try

* Try clone depth

* Try again

* back to shallow_clone

* Some debugging

* Another try

* Try something else

* Another

* try curl only

* Another

* fix

* Update node-gyp

* Try again

* Try

* Again

* Some pruning

* Back to the start

* Again

* try config

* Reorder test

* Try without cache

* Update dep

* Try again

* remove shallow_clone

* don't run tests

* Implement CA fetching

* Fix

* Use CAs

* Fix?

* test

* Remove build dir

* Fix dir path?

* Try again

* Aagain

* Try

* Change dep

* Add comment

* Invalidate cache

* Remove yarn

* Fix cookie expiry

* fixed tests

* Some modal fixes and unrelated stuff

* Debounce autocomplete

* Got client certificates working

* Fixed binary files

* Fixed cookies

* Got proxy and debug info working

* Got CA certs working

* Update appveyor build

* Fixed cookie date parsing and overlay z-index

* Test updates

* Autocomplete to bulk header editor

* Small CSS tweak
2017-03-16 10:51:56 -07:00

203 lines
5.3 KiB
JavaScript

import React, {PureComponent, PropTypes} from 'react';
import autobind from 'autobind-decorator';
import {shell} from 'electron';
import Editor from '../codemirror/code-editor';
import ResponseWebView from './response-webview';
import ResponseRaw from './response-raw';
import ResponseError from './response-error';
import {LARGE_RESPONSE_MB, PREVIEW_MODE_FRIENDLY, PREVIEW_MODE_SOURCE} from '../../../common/constants';
let alwaysShowLargeResponses = false;
@autobind
class ResponseViewer extends PureComponent {
constructor (props) {
super(props);
this.state = {
blockingBecauseTooLarge: false
};
}
_handleOpenLink (link) {
shell.openExternal(link);
}
_handleDismissBlocker () {
this.setState({blockingBecauseTooLarge: false});
}
_handleDisableBlocker () {
alwaysShowLargeResponses = true;
this._handleDismissBlocker();
}
_checkResponseBlocker (props) {
if (alwaysShowLargeResponses) {
return;
}
// Block the response if it's too large
if (props.bytes > LARGE_RESPONSE_MB * 1024 * 1024) {
this.setState({blockingBecauseTooLarge: true});
}
}
componentWillMount () {
this._checkResponseBlocker(this.props);
}
componentWillReceiveProps (nextProps) {
this._checkResponseBlocker(nextProps);
}
shouldComponentUpdate (nextProps, nextState) {
for (let k of Object.keys(nextProps)) {
const value = nextProps[k];
if (typeof value !== 'function' && this.props[k] !== value) {
return true;
}
}
for (let k of Object.keys(nextState)) {
const value = nextState[k];
if (typeof value !== 'function' && this.state[k] !== value) {
return true;
}
}
return false;
}
render () {
const {
previewMode,
filter,
contentType,
editorLineWrapping,
editorFontSize,
editorKeyMap,
updateFilter,
statusCode,
body: base64Body,
encoding,
url,
error
} = this.props;
const bodyBuffer = new Buffer(base64Body, encoding);
if (error) {
return (
<ResponseError
url={url}
error={bodyBuffer.toString('utf8')}
fontSize={editorFontSize}
statusCode={statusCode}
/>
);
}
const {blockingBecauseTooLarge} = this.state;
if (blockingBecauseTooLarge) {
return (
<div className="response-pane__notify">
<p className="pad faint">
Response body over {LARGE_RESPONSE_MB}MB hidden to prevent unresponsiveness
</p>
<p>
<button onClick={this._handleDismissBlocker}
className="inline-block btn btn--clicky">
Show Response
</button>
{' '}
<button className="faint inline-block btn btn--super-compact"
onClick={this._handleDisableBlocker}>
Always Show
</button>
</p>
</div>
);
}
switch (previewMode) {
case PREVIEW_MODE_FRIENDLY:
if (contentType.toLowerCase().indexOf('image/') === 0) {
const justContentType = contentType.split(';')[0];
return (
<div className="scrollable-container tall wide">
<div className="scrollable">
<img src={`data:${justContentType};base64,${base64Body}`}
className="pad block"
style={{maxWidth: '100%', maxHeight: '100%', margin: 'auto'}}/>
</div>
</div>
);
} else {
return (
<ResponseWebView
body={bodyBuffer.toString('utf8')}
contentType={contentType}
url={url}
/>
);
}
case PREVIEW_MODE_SOURCE:
let mode = contentType;
const body = bodyBuffer.toString('utf8');
try {
// FEATURE: Detect JSON even without content-type
contentType.indexOf('json') === -1 && JSON.parse(body);
mode = 'application/json';
} catch (e) {
// Nothing
}
return (
<Editor
onClickLink={this._handleOpenLink}
defaultValue={body}
updateFilter={updateFilter}
filter={filter}
autoPrettify
noMatchBrackets
readOnly
mode={mode}
lineWrapping={editorLineWrapping}
fontSize={editorFontSize}
keyMap={editorKeyMap}
placeholder="..."
/>
);
default: // Raw
return (
<ResponseRaw
value={bodyBuffer.toString('utf8')}
fontSize={editorFontSize}
/>
);
}
}
}
ResponseViewer.propTypes = {
body: PropTypes.string.isRequired,
encoding: PropTypes.string.isRequired,
previewMode: PropTypes.string.isRequired,
filter: PropTypes.string.isRequired,
editorFontSize: PropTypes.number.isRequired,
editorKeyMap: PropTypes.string.isRequired,
editorLineWrapping: PropTypes.bool.isRequired,
url: PropTypes.string.isRequired,
bytes: PropTypes.number.isRequired,
statusCode: PropTypes.number.isRequired,
responseId: PropTypes.string.isRequired,
contentType: PropTypes.string.isRequired,
// Optional
updateFilter: PropTypes.func,
error: PropTypes.bool
};
export default ResponseViewer;