From 7908cbad4f439584519877ecc42663168fb971ee Mon Sep 17 00:00:00 2001
From: "xiaofeng.mxf" <xiaofeng.mxf@taobao.com>
Date: Mon, 20 Jan 2020 20:27:41 +0800
Subject: [PATCH] fix problem with file name

---
 web/src/common/ApiUtil.js                     |  4 +-
 web/src/common/WsUtil.js                      |  1 +
 web/src/common/apiUtil.js                     | 62 +++++++++++++++++++
 web/src/common/constant.js                    |  9 +++
 web/src/common/wsUtil.js                      | 42 +++++++++++++
 web/src/component/download-root-ca.jsx        |  2 +-
 web/src/component/header-menu.jsx             |  2 +-
 .../component/record-ws-message-detail.jsx    |  2 +-
 web/src/component/ws-listener.jsx             |  4 +-
 web/src/saga/rootSaga.js                      |  2 +-
 10 files changed, 122 insertions(+), 8 deletions(-)
 create mode 100644 web/src/common/apiUtil.js
 create mode 100644 web/src/common/constant.js
 create mode 100644 web/src/common/wsUtil.js

diff --git a/web/src/common/ApiUtil.js b/web/src/common/ApiUtil.js
index ab227b7..4962dab 100644
--- a/web/src/common/ApiUtil.js
+++ b/web/src/common/ApiUtil.js
@@ -53,10 +53,10 @@ export function isApiSuccess (response) {
     return response.status === 'success';
 }
 
-const ApiUtil = {
+const apiUtil = {
     getJSON,
     postJSON,
     isApiSuccess
 };
 
-export default ApiUtil;
+export default apiUtil;
diff --git a/web/src/common/WsUtil.js b/web/src/common/WsUtil.js
index 833d8e8..eb94b59 100644
--- a/web/src/common/WsUtil.js
+++ b/web/src/common/WsUtil.js
@@ -39,3 +39,4 @@ export function initWs(wsPort = location.port, path = 'do-not-proxy') {
 export default {
   initWs: initWs
 };
+
diff --git a/web/src/common/apiUtil.js b/web/src/common/apiUtil.js
new file mode 100644
index 0000000..4962dab
--- /dev/null
+++ b/web/src/common/apiUtil.js
@@ -0,0 +1,62 @@
+/**
+* AJAX操作工具类
+*/
+import PromiseUtil from './promiseUtil';
+export function getJSON(url, data) {
+    const d = PromiseUtil.defer();
+    fetch(url + serializeQuery(data))
+        .then((data) => {
+            d.resolve(data.json());
+        })
+        .catch((error) => {
+            console.error(error);
+            d.reject(error);
+        });
+    return d.promise;
+}
+
+export function postJSON(url, data) {
+    const d = PromiseUtil.defer();
+    fetch(url, {
+        method: 'POST',
+        headers: {
+            'Accept': 'application/json',
+            'Content-Type': 'application/json'
+        },
+        body: JSON.stringify(data)
+    })
+        .then((data) => {
+
+            d.resolve(data.json());
+        })
+        .catch((error) => {
+            console.error(error);
+            d.reject(error);
+        });
+    return d.promise;
+}
+
+function serializeQuery (data = {}) {
+    data['__t'] = Date.now();// disable the cache
+    const queryArray = [];
+
+    for (let key in data) {
+        queryArray.push(`${key}=${data[key]}`);
+    }
+
+    const queryStr = queryArray.join('&');
+
+    return queryStr ? '?' + queryStr : '';
+}
+
+export function isApiSuccess (response) {
+    return response.status === 'success';
+}
+
+const apiUtil = {
+    getJSON,
+    postJSON,
+    isApiSuccess
+};
+
+export default apiUtil;
diff --git a/web/src/common/constant.js b/web/src/common/constant.js
new file mode 100644
index 0000000..c0ec500
--- /dev/null
+++ b/web/src/common/constant.js
@@ -0,0 +1,9 @@
+/**
+* define all constant variables here
+*/
+
+module.exports.MenuKeyMap = {
+    RECORD_FILTER: 'RECORD_FILTER',
+    MAP_LOCAL: 'MAP_LOCAL',
+    ROOT_CA: 'ROOT_CA'
+};
diff --git a/web/src/common/wsUtil.js b/web/src/common/wsUtil.js
new file mode 100644
index 0000000..eb94b59
--- /dev/null
+++ b/web/src/common/wsUtil.js
@@ -0,0 +1,42 @@
+/*
+* Utility for websocket
+*
+*/
+import { message } from 'antd';
+
+/**
+* Initiate a ws connection.
+* The default path `do-not-proxy` means the ws do not need to be proxied.
+* This is very important for AnyProxy‘s own server, such as WEB UI, 
+* and the websocket detail panel in it, to prevent a recursive proxy.
+* @param {wsPort} wsPort the port of websocket
+* @param {key} path the path of the ws url
+*
+*/
+export function initWs(wsPort = location.port, path = 'do-not-proxy') {
+  if(!WebSocket){
+    throw (new Error('WebSocket is not supported on this browser'));
+  }
+
+  const wsClient = new WebSocket(`ws://${location.hostname}:${wsPort}/${path}`);
+
+  wsClient.onerror = (error) => {
+    console.error(error);
+    message.error('error happened when setup websocket');
+  };
+
+  wsClient.onopen = (e) => {
+    console.info('websocket opened: ', e);
+  };
+
+  wsClient.onclose = (e) => {
+    console.info('websocket closed: ', e);
+  };
+
+  return wsClient;
+}
+
+export default {
+  initWs: initWs
+};
+
diff --git a/web/src/component/download-root-ca.jsx b/web/src/component/download-root-ca.jsx
index 22b855e..f530432 100644
--- a/web/src/component/download-root-ca.jsx
+++ b/web/src/component/download-root-ca.jsx
@@ -11,7 +11,7 @@ import { message, Button, Spin } from 'antd';
 import ResizablePanel from 'component/resizable-panel';
 import { hideRootCA, updateIsRootCAExists } from 'action/globalStatusAction';
 import { MenuKeyMap } from 'common/constant';
-import { getJSON, ajaxGet, postJSON } from 'common/ApiUtil';
+import { getJSON, ajaxGet, postJSON } from 'common/apiUtil';
 
 import Style from './download-root-ca.less';
 import CommonStyle from '../style/common.less';
diff --git a/web/src/component/header-menu.jsx b/web/src/component/header-menu.jsx
index 48d51c4..7b58f29 100644
--- a/web/src/component/header-menu.jsx
+++ b/web/src/component/header-menu.jsx
@@ -27,7 +27,7 @@ const {
   RECORD_FILTER: RECORD_FILTER_MENU_KEY
 } = MenuKeyMap;
 
-import { getJSON } from 'common/ApiUtil';
+import { getJSON } from 'common/apiUtil';
 
 import Style from './header-menu.less';
 
diff --git a/web/src/component/record-ws-message-detail.jsx b/web/src/component/record-ws-message-detail.jsx
index b253fd5..1113df3 100644
--- a/web/src/component/record-ws-message-detail.jsx
+++ b/web/src/component/record-ws-message-detail.jsx
@@ -6,7 +6,7 @@
 import React, { PropTypes } from 'react';
 import { message, Button, Icon } from 'antd';
 import { formatDate } from 'common/commonUtil';
-import { initWs } from 'common/WsUtil';
+import { initWs } from 'common/wsUtil';
 import ClassBind from 'classnames/bind';
 
 import Style from './record-ws-message-detail.less';
diff --git a/web/src/component/ws-listener.jsx b/web/src/component/ws-listener.jsx
index d5da6a4..cd87056 100644
--- a/web/src/component/ws-listener.jsx
+++ b/web/src/component/ws-listener.jsx
@@ -5,14 +5,14 @@
 
 import React, { PropTypes } from 'react';
 import { message } from 'antd';
-import { initWs } from 'common/WsUtil';
+import { initWs } from 'common/wsUtil';
 import { updateWholeRequest } from 'action/recordAction';
 import {
   updateShouldClearRecord,
   updateShowNewRecordTip
 } from 'action/globalStatusAction';
 const RecordWorker = require('worker-loader?inline!./record-worker.jsx');
-import { getJSON } from 'common/ApiUtil';
+import { getJSON } from 'common/apiUtil';
 
 const myRecordWorker = new RecordWorker(window.URL.createObjectURL(new Blob([RecordWorker.toString()])));
 const fetchLatestLog = function () {
diff --git a/web/src/saga/rootSaga.js b/web/src/saga/rootSaga.js
index a334e19..5e9525f 100644
--- a/web/src/saga/rootSaga.js
+++ b/web/src/saga/rootSaga.js
@@ -29,7 +29,7 @@ import {
   updateLocalGlobalProxyFlag
 } from 'action/globalStatusAction';
 
-import { getJSON, postJSON, isApiSuccess } from 'common/ApiUtil';
+import { getJSON, postJSON, isApiSuccess } from 'common/apiUtil';
 
 function* doFetchRequestList() {
   const data = yield call(getJSON, '/latestLog');