import React, {Component} from 'react'; import './ConnectionContent.scss'; import {Row} from 'react-bootstrap'; import MessageAction from "./MessageAction"; import backend from "../backend"; import ButtonField from "./fields/ButtonField"; import ChoiceField from "./fields/ChoiceField"; const classNames = require('classnames'); class ConnectionContent extends Component { constructor(props) { super(props); this.state = { loading: false, connectionContent: null, format: "default", tryParse: true, messageActionDialog: null }; this.validFormats = ["default", "hex", "hexdump", "base32", "base64", "ascii", "binary", "decimal", "octal"]; this.setFormat = this.setFormat.bind(this); } componentDidMount() { if (this.props.connection != null) { this.loadStream(); } } componentDidUpdate(prevProps, prevState, snapshot) { if (this.props.connection != null && ( this.props.connection !== prevProps.connection || this.state.format !== prevState.format)) { this.loadStream(); } } loadStream = () => { this.setState({loading: true}); // TODO: limit workaround. backend.get(`/api/streams/${this.props.connection.id}?format=${this.state.format}&limit=999999`).then(res => { this.setState({ connectionContent: res.json, loading: false }); }); }; setFormat(format) { if (this.validFormats.includes(format)) { this.setState({format: format}); } } tryParseConnectionMessage(connectionMessage) { if (connectionMessage.metadata == null) { return connectionMessage.content; } if (connectionMessage["is_metadata_continuation"]) { return **already parsed in previous messages**; } let unrollMap = (obj) => obj == null ? null : Object.entries(obj).map(([key, value]) =>
{key}: {value}
); let m = connectionMessage.metadata; switch (m.type) { case "http-request": let url = {m.host}{m.url}; return{m.method} {url} {m.protocol}
{unrollMap(m.headers)}{m.protocol} {m.status}
{unrollMap(m.headers)}{payload}{this.state.messageActionDialog}