diff options
author | Jason <jason.daurus@gmail.com> | 2016-06-18 14:18:24 +0800 |
---|---|---|
committer | Jason <jason.daurus@gmail.com> | 2016-06-18 14:18:24 +0800 |
commit | 9cb5b0af9db83d84af0bdb45d56a9755b400c212 (patch) | |
tree | a2f2c6985f8a2230be3b8796567e678733e03b7f /web/src/js/ducks | |
parent | 6c0511b06fdc359ec4c48879f803c80d7fbeb34c (diff) | |
parent | 23b976a999f41439e83c1010474ec9dc680b8486 (diff) | |
download | mitmproxy-9cb5b0af9db83d84af0bdb45d56a9755b400c212.tar.gz mitmproxy-9cb5b0af9db83d84af0bdb45d56a9755b400c212.tar.bz2 mitmproxy-9cb5b0af9db83d84af0bdb45d56a9755b400c212.zip |
Merge branch 'master' of github.com:mitmproxy/mitmproxy into websocket
Conflicts:
mitmproxy/web/static/app.js
web/src/js/components/ProxyApp.jsx
web/src/js/connection.js
Diffstat (limited to 'web/src/js/ducks')
-rw-r--r-- | web/src/js/ducks/index.js | 2 | ||||
-rw-r--r-- | web/src/js/ducks/settings.js | 77 | ||||
-rw-r--r-- | web/src/js/ducks/websocket.js | 29 |
3 files changed, 96 insertions, 12 deletions
diff --git a/web/src/js/ducks/index.js b/web/src/js/ducks/index.js index fee4d792..ffde1a64 100644 --- a/web/src/js/ducks/index.js +++ b/web/src/js/ducks/index.js @@ -2,12 +2,14 @@ import {combineReducers} from 'redux' import eventLog from './eventLog' import websocket from './websocket' import flows from './flows' +import settings from './settings' import ui from './ui' const rootReducer = combineReducers({ eventLog, websocket, flows, + settings, ui }) diff --git a/web/src/js/ducks/settings.js b/web/src/js/ducks/settings.js new file mode 100644 index 00000000..73c62120 --- /dev/null +++ b/web/src/js/ducks/settings.js @@ -0,0 +1,77 @@ +import {fetchApi} from "../utils"; + +export const REQUEST_SETTINGS = "REQUEST_SETTINGS" +export const RECEIVE_SETTINGS = "RECEIVE_SETTINGS" +export const UPDATE_SETTINGS = "UPDATE_SETTINGS" + +const defaultState = { + settings: {}, + isFetching: false, + actionsDuringFetch: [], +} + +export default function reducer(state = defaultState, action) { + switch (action.type) { + + case REQUEST_SETTINGS: + return { + ...state, + isFetching: true + } + + case RECEIVE_SETTINGS: + let s = { + settings: action.settings, + isFetching: false, + actionsDuringFetch: [], + } + for (action of state.actionsDuringFetch) { + s = reducer(s, action) + } + return s + + case UPDATE_SETTINGS: + if (state.isFetching) { + return { + ...state, + actionsDuringFetch: [...state.actionsDuringFetch, action] + } + } + return { + ...state, + settings: {...state.settings, ...action.settings} + } + + default: + return state + } +} + +export function updateSettings(event) { + /* This action creator takes all WebSocket events */ + if (event.cmd === "update") { + return { + type: UPDATE_SETTINGS, + settings: event.data + } + } + console.error("unknown settings update", event) +} + +export function fetchSettings() { + return dispatch => { + dispatch({type: REQUEST_SETTINGS}) + + return fetchApi("/settings") + .then(response => response.json()) + .then(json => + dispatch({type: RECEIVE_SETTINGS, settings: json.data}) + ) + // TODO: Error handling + } +} + +export function setInterceptPattern(intercept) { + return dispatch => + fetchApi.put("/settings", {intercept}) +} diff --git a/web/src/js/ducks/websocket.js b/web/src/js/ducks/websocket.js index 0d0556ad..766f7236 100644 --- a/web/src/js/ducks/websocket.js +++ b/web/src/js/ducks/websocket.js @@ -1,7 +1,8 @@ import { ConnectionActions } from '../actions.js' import { AppDispatcher } from '../dispatcher.js' import * as eventLogActions from './eventLog' -import * as flowActions from './flows' +import * as flowsActions from './flows' +import * as settingsActions from './settings' export const CONNECT = 'WEBSOCKET_CONNECT' export const CONNECTED = 'WEBSOCKET_CONNECTED' @@ -62,15 +63,8 @@ export function onConnect() { // workaround to make sure that our state is already available. return dispatch => { dispatch({ type: CONNECTED }) - dispatch(flowActions.fetchFlows()).then(() => ConnectionActions.open()) - } -} - -export function onDisconnect() { - return dispatch => { - ConnectionActions.close() - dispatch(eventLogActions.addLogEntry('WebSocket connection closed.')) - dispatch({ type: DISCONNECTED }) + dispatch(settingsActions.fetchSettings()) + dispatch(flowsActions.fetchFlows()).then(() => ConnectionActions.open()) } } @@ -85,8 +79,11 @@ export function onMessage(msg) { case eventLogActions.UPDATE_LOG: return dispatch(eventLogActions.updateLogEntries(data)) - case flowActions.UPDATE_FLOWS: - return dispatch(flowActions.updateFlows(data)) + case flowsActions.UPDATE_FLOWS: + return dispatch(flowsActions.updateFlows(data)) + + case settingsActions.UPDATE_SETTINGS: + return dispatch(settingsActions.updateSettings(message)) default: console.warn('unknown message', data) @@ -96,6 +93,14 @@ export function onMessage(msg) { } } +export function onDisconnect() { + return dispatch => { + ConnectionActions.close() + dispatch(eventLogActions.addLogEntry('WebSocket connection closed.')) + dispatch({ type: DISCONNECTED }) + } +} + export function onError(error) { // @todo let event log subscribe WebSocketActions.ERROR return dispatch => { |