Skip to content

Commit 27bb4a7

Browse files
authored
Merge pull request #32 from nickhealy/preload
Refactor HTTP2 portion of HTTP Controller to disallow Node Integration
2 parents 91c0409 + 0e90b60 commit 27bb4a7

File tree

8 files changed

+590
-43
lines changed

8 files changed

+590
-43
lines changed

httpMainController.js

Lines changed: 524 additions & 0 deletions
Large diffs are not rendered by default.

main.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ const protoParserFunc = require("./src/client/protoParser.js");
4545

4646
// require menu file
4747
require("./menu/mainMenu");
48+
// require http controller file
49+
require('./httpMainController.js')();
50+
4851

4952
// configure logging
5053
// autoUpdater.logger = log;
@@ -664,3 +667,6 @@ ipcMain.on("open-gql", (event, args) => {
664667
});
665668
}
666669
});
670+
671+
// export main window so we can access ipcMain from other files
672+
module.exports = mainWindow;

package-lock.json

Lines changed: 21 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

preload.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
const { ipcRenderer, contextBridge } = require("electron");
22

33
contextBridge.exposeInMainWorld("api", {
4-
send: (channel, data) => {
4+
send: (channel, ...data) => {
55
// allowlist channels
66
const allowedChannels = [
77
"toMain",
88
"confirm-clear-history",
99
"import-proto",
1010
"quit-and-install",
11+
'open-http',
1112
];
1213
if (allowedChannels.includes(channel)) {
13-
ipcRenderer.send(channel, data);
14+
ipcRenderer.send(channel, ...data);
1415
}
1516
},
1617
receive: (channel, cb) => {
@@ -22,6 +23,7 @@ contextBridge.exposeInMainWorld("api", {
2223
"clear-history-response",
2324
"proto-info",
2425
"message",
26+
'reqResUpdate'
2527
];
2628
if (allowedChannels.includes(channel)) {
2729
ipcRenderer.on(channel, (event, ...args) => cb(...args));

src/client/components/containers/App.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Component } from "react";
22
import "../../../assets/style/App.scss";
33
const { api } = window;
4-
// import ContentsContainer from './ContentsContainer.jsx';
4+
import ContentsContainer from './ContentsContainer.jsx';
55
// import ReqResCtrl from '../../controllers/reqResController';
66
import SidebarContainer from "./SidebarContainer.jsx";
77
import UpdatePopUpContainer from "./UpdatePopUpContainer.jsx";
@@ -56,7 +56,7 @@ class App extends Component {
5656
<div id="app">
5757
<UpdatePopUpContainer />
5858
<SidebarContainer />
59-
{/* <ContentsContainer /> */}
59+
<ContentsContainer />
6060
</div>
6161
);
6262
}

src/client/components/containers/ContentsContainer.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { Component } from 'react';
22

3-
import GraphContainer from './GraphContainer.jsx';
3+
// import GraphContainer from './GraphContainer.jsx';
44
import ReqResContainer from './ReqResContainer.jsx';
55
import NavBarContainer from './NavBarContainer.jsx';
66

@@ -12,7 +12,7 @@ class Contents extends Component {
1212
render() {
1313
return(
1414
<div className={'contents'}>
15-
<GraphContainer/>
15+
{/* <GraphContainer/> */}
1616
<NavBarContainer/>
1717
<ReqResContainer/>
1818
</div>

src/client/controllers/httpController.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const httpController = {
7777
clearInterval(interval);
7878
if (foundHTTP2Connection.status === "initialized") {
7979
reqResObj.connection = "error";
80+
// LISTEN FOR ERROR EVENTS FROM MAIN PROCESS HERE
8081
store.default.dispatch(actions.reqResUpdate(reqResObj));
8182
}
8283
}, 10000);
@@ -141,7 +142,8 @@ const httpController = {
141142
reqResObj.connection = "pending";
142143
reqResObj.timeSent = Date.now();
143144
store.default.dispatch(actions.reqResUpdate(reqResObj));
144-
145+
146+
// format headers in chosen reqResObj so we can add them to our request
145147
const formattedHeaders = {};
146148
reqResObj.request.headers.forEach((head) => {
147149
formattedHeaders[head.key] = head.value;
@@ -150,15 +152,11 @@ const httpController = {
150152

151153
// initiate request
152154
const reqStream = client.request(formattedHeaders, {
155+
// do not immediately close the *writable* side of the http2 stream (i.e. what the request sends over), in case we are using a request method that sends a payload body
153156
endStream: false,
154157
});
155158

156-
console.log("reqStream at THIS moment : ", {
157-
...reqStream,
158-
});
159-
160-
// endStream false means we can continue to send more data, which we would for a body;
161-
// Send body depending on method;
159+
// we can now close the writable side of our stream, either sending our request body or not, depending on our method
162160
if (
163161
reqResObj.request.method !== "GET" &&
164162
reqResObj.request.method !== "HEAD"
@@ -169,10 +167,11 @@ const httpController = {
169167
reqStream.end();
170168
}
171169

172-
console.log("reqStream at THIS moment : ", {
173-
...reqStream,
174-
});
170+
// console.log("reqStream at THIS moment : ", {
171+
// ...reqStream,
172+
// });
175173

174+
// create an object that represents our open connection.
176175
const openConnectionObj = {
177176
stream: reqStream,
178177
protocol: "HTTP2",
@@ -185,11 +184,14 @@ const httpController = {
185184
" openConnectionObj : ",
186185
JSON.parse(JSON.stringify(openConnectionObj))
187186
);
187+
188+
// this is the connection array that was passed into these controller functions from reqResController.js
188189
connectionArray.push(openConnectionObj);
189190

190191
let isSSE;
191192

192193
reqStream.on("response", (headers, flags) => {
194+
// first argumnet of callback to response listener in ClientHttp2Stream is an object containing the receieved HTTP/2 Headers Object, as well as the flags associated with those headers
193195
isSSE = headers["content-type"].includes("stream");
194196

195197
if (isSSE) {
@@ -201,24 +203,28 @@ const httpController = {
201203
}
202204
reqResObj.isHTTP2 = true;
203205
reqResObj.timeReceived = Date.now();
206+
console.log('this is the time inside the response event listener : ', Date.now())
204207
reqResObj.response.headers = headers;
205208
// reqResObj.response.events = []; // passing empty array to handleSingleEvent
206209
// below instead of running this line. This invocation is different from
207210
// when it is run below to Check if the URL provided is a stream (near line 327)
208211

209212
// if cookies exists, parse the cookie(s)
210213
if (setCookie.parse(headers["set-cookie"])) {
214+
console.log('made it into cookies')
211215
reqResObj.response.cookies = this.cookieFormatter(
212216
setCookie.parse(headers["set-cookie"])
213217
);
214218
store.default.dispatch(actions.reqResUpdate(reqResObj));
219+
// RECEIVE A "RESPONSE EVENT"
215220
this.handleSingleEvent([], reqResObj, headers);
216221
}
217222
});
218223

219224
reqStream.setEncoding("utf8");
220225
let data = "";
221226
reqStream.on("data", (chunk) => {
227+
console.log('is this a server sent event? ', isSSE)
222228
data += chunk;
223229
if (isSSE) {
224230
let couldBeEvents = true;
@@ -252,6 +258,7 @@ const httpController = {
252258
reqStream.on("end", () => {
253259
if (isSSE) {
254260
const receivedEventFields = this.parseSSEFields(data);
261+
console.log('SSE triggered')
255262
receivedEventFields.timeReceived = Date.now();
256263
reqResObj.connection = "closed";
257264
reqResObj.response.events.push(receivedEventFields);
@@ -399,6 +406,8 @@ const httpController = {
399406
newObj.connection = "closed";
400407
newObj.connectionType = "plain";
401408
newObj.timeReceived = Date.now();
409+
console.log('Date.now() inside handleSingleEvent : ', Date.now())
410+
console.log('this is what was added : ', newObj.timeReceived)
402411
newObj.response.events.push(response);
403412
store.default.dispatch(actions.reqResUpdate(newObj));
404413
},

0 commit comments

Comments
 (0)