Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions endPoint/h2HttpHostEndPoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default class H2HttpHostEndPoint extends SecureHttpHostEndPoint {
return http2
.createSecureServer(this.#options)
.on("stream", async (stream, headers) => {
let bodyFields = {};
/** @type {Request} */
let cms = null;
/**@type {BinaryContent[]} */
Expand All @@ -73,13 +74,28 @@ export default class H2HttpHostEndPoint extends SecureHttpHostEndPoint {
const method = headers[":method"];
const url = headers[":path"];
const createCmsAndCreateResponseAsync = async () => {
let date = new Date();
console.log(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this after test and before create pr

"HTTP2" +
" => " +
date.getHours() +
":" +
date.getMinutes() +
":" +
date.getSeconds() +
") " +
method +
"::" +
url
);
cms = await this._createCmsObjectAsync(
url,
method,
headers,
formFields,
jsonHeaders,
stream.session.socket
stream.session.socket,
bodyFields
);
const result = await this.#service.processAsync(cms, fileContents);
const [code, headerList, body] = await result.getResultAsync();
Expand All @@ -94,7 +110,10 @@ export default class H2HttpHostEndPoint extends SecureHttpHostEndPoint {
stream.destroy(ex);
});
try {
if (method === "POST") {
if (
method === "POST" &&
headers["content-type"] == "multipart/form-data"
) {
/**@type {Array<BinaryContent>}*/
const bb = busboy({ headers: headers });
bb.on("file", (name, file, info) => {
Expand All @@ -117,6 +136,16 @@ export default class H2HttpHostEndPoint extends SecureHttpHostEndPoint {
});
bb.on("close", createCmsAndCreateResponseAsync);
stream.pipe(bb);
} else if (method === "POST") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check this type of code for read json.

busboy.on('finish', function() {
  // use req.body
})

let raw_body = "";
stream.on("data", (chunk) => {
raw_body += chunk.toString();
});

stream.on("end", () => {
bodyFields = JSON.stringify(raw_body);
console.log("body" + bodyFields);
});
} else {
createCmsAndCreateResponseAsync();
}
Expand Down
6 changes: 4 additions & 2 deletions endPoint/httpHostEndPoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class HttpHostEndPoint extends HostEndPoint {
headers,
formFields,
jsonHeaders,
socket
socket,
bodyFields
) {
const rawUrl = urlStr.substring(1);
const urlObject = url.parse(rawUrl, true);
Expand All @@ -58,7 +59,7 @@ class HttpHostEndPoint extends HostEndPoint {
if (Object.keys(jsonHeaders).length > 0) {
headers["json"] = ObjectUtil.convertObjectToNestedStructure(jsonHeaders);
} else {
headers["json"] = {};
headers["json"] = bodyFields;
}
const now = dayjs();
const request = new Request();
Expand All @@ -82,6 +83,7 @@ class HttpHostEndPoint extends HostEndPoint {
}
}
request["Form"] = formFields;
request["form"] = bodyFields;
return request;
}
}
Expand Down
116 changes: 63 additions & 53 deletions endPoint/nonSecureHttpHostEndPoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { StatusCodes } from "http-status-codes";
import HttpHostEndPoint from "./HttpHostEndPoint.js";
import { HostService } from "../services/hostServices.js";
import BinaryContent from "../fileStreamer/Models/BinaryContent.js";

import bodyParser from "body-parser"
export default class NonSecureHttpHostEndPoint extends HttpHostEndPoint {
/** @type {HostService} */
#service;
Expand All @@ -22,60 +22,70 @@ export default class NonSecureHttpHostEndPoint extends HttpHostEndPoint {

_createServer() {
return http.createServer(async (req, res) => {
try {
/** @type {Request} */
let cms = null;
/**@type {BinaryContent[]} */
const fileContents = [];
/**@type {NodeJS.Dict<string>} */
const formFields = {};
/**@type {NodeJS.Dict<string>} */
const jsonHeaders = {};
const createCmsAndCreateResponseAsync = async () => {
cms = await this._createCmsObjectAsync(
req.url,
req.method,
req.headers,
formFields,
jsonHeaders,
req.socket
);
const result = await this.#service.processAsync(cms, fileContents);
const [code, headers, body] = await result.getResultAsync();
res.writeHead(code, headers);
res.end(body);
};
if (req.method === "POST") {
/**@type {Array<BinaryContent>}*/
const bb = busboy({ headers: req.headers });
bb.on("file", (name, file, info) => {
const ContentParts = [];
file.on("data", (x) => ContentParts.push(x));
file.on("end", async () => {
const content = new BinaryContent();
content.url = `${req.headers["host"]}${req.url}`;
content.mime = info.mimeType.toLowerCase();
content.name = info.filename;
content.payload = Buffer.concat(ContentParts);
fileContents.push(content);
bodyParser.json()(req, res, (err) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other case (non json content) this code work? it is tested?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes; it works on non secure version of fingerfood but needs to write some unit tests for it to make sure

if (err) {
throw err;
}
try {
/** @type {Request} */
let cms = null;
/**@type {BinaryContent[]} */
const fileContents = [];
/**@type {NodeJS.Dict<string>} */
const formFields = {};
/**@type {NodeJS.Dict<string>} */
const jsonHeaders = {};
const createCmsAndCreateResponseAsync = async () => {
let date = new Date();
cms = await this._createCmsObjectAsync(
req.url,
req.method,
req.headers,
formFields,
jsonHeaders,
req.socket,
req.body
);
const result = await this.#service.processAsync(cms, fileContents);
const [code, headers, body] = await result.getResultAsync();
res.writeHead(code, headers);
res.end(body);
};
if (
req.method === "POST" &&
req.headers["content-type"] == "multipart/form-data"
) {
/**@type {Array<BinaryContent>}*/
const bb = busboy({ headers: req.headers });
bb.on("file", (name, file, info) => {
const ContentParts = [];
file.on("data", (x) => ContentParts.push(x));
file.on("end", async () => {
const content = new BinaryContent();
content.url = `${req.headers["host"]}${req.url}`;
content.mime = info.mimeType.toLowerCase();
content.name = info.filename;
content.payload = Buffer.concat(ContentParts);
fileContents.push(content);
});
});
bb.on("field", (name, val, info) => {
formFields[name] = val;
if (name.startsWith("_")) {
jsonHeaders[name] = val;
}
});
});
bb.on("field", (name, val, info) => {
formFields[name] = val;
if (name.startsWith("_")) {
jsonHeaders[name] = val;
}
});
bb.on("close", createCmsAndCreateResponseAsync);
req.pipe(bb);
} else {
createCmsAndCreateResponseAsync();
bb.on("close", createCmsAndCreateResponseAsync);
req.pipe(bb);
} else {
createCmsAndCreateResponseAsync();
}
} catch (ex) {
console.error(ex);
res.writeHead(StatusCodes.INTERNAL_SERVER_ERROR);
res.end(ex.toString());
}
} catch (ex) {
console.error(ex);
res.writeHead(StatusCodes.INTERNAL_SERVER_ERROR);
res.end(ex.toString());
}
});
});
}
}
115 changes: 63 additions & 52 deletions endPoint/secureHttpHostEndPoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { StatusCodes } from "http-status-codes";
import HttpHostEndPoint from "./HttpHostEndPoint.js";
import { HostService } from "../services/hostServices.js";
import BinaryContent from "../fileStreamer/Models/BinaryContent.js";
import bodyParser from "body-parser";

export default class SecureHttpHostEndPoint extends HttpHostEndPoint {
/** @type {HostService} */
Expand All @@ -26,60 +27,70 @@ export default class SecureHttpHostEndPoint extends HttpHostEndPoint {
_createServer() {
return https
.createServer(this.#options, async (req, res) => {
/** @type {Request} */
let cms = null;
/**@type {BinaryContent[]} */
const fileContents = [];
/**@type {NodeJS.Dict<string>} */
const formFields = {};
/**@type {NodeJS.Dict<string>} */
const jsonHeaders = {};
const createCmsAndCreateResponseAsync = async () => {
cms = await this._createCmsObjectAsync(
req.url,
req.method,
req.headers,
formFields,
jsonHeaders,
req.socket
);
const result = await this.#service.processAsync(cms, fileContents);
const [code, headers, body] = await result.getResultAsync();
res.writeHead(code, headers);
res.end(body);
};
try {
if (req.method === "POST") {
/**@type {Array<BinaryContent>}*/
const bb = busboy({ headers: req.headers });
bb.on("file", (name, file, info) => {
const ContentParts = [];
file.on("data", (x) => ContentParts.push(x));
file.on("end", async () => {
const content = new BinaryContent();
content.url = `${req.headers["host"]}${req.url}`;
content.mime = info.mimeType.toLowerCase();
content.name = info.filename;
content.payload = Buffer.concat(ContentParts);
fileContents.push(content);
bodyParser.json()(req, res, (err) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other case (non json content) this code work? it is tested?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i check that; with some test

if (err) {
throw err;
}
/** @type {Request} */
let cms = null;
/**@type {BinaryContent[]} */
const fileContents = [];
/**@type {NodeJS.Dict<string>} */
const formFields = {};
/**@type {NodeJS.Dict<string>} */
const jsonHeaders = {};
const createCmsAndCreateResponseAsync = async () => {
let date = new Date();
cms = await this._createCmsObjectAsync(
req.url,
req.method,
req.headers,
formFields,
jsonHeaders,
req.socket,
req.body
);
const result = await this.#service.processAsync(cms, fileContents);
const [code, headers, body] = await result.getResultAsync();
res.writeHead(code, headers);
res.end(body);
};
try {
if (
req.method === "POST" &&
req.headers["content-type"] == "multipart/form-data"
) {
/**@type {Array<BinaryContent>}*/
const bb = busboy({ headers: req.headers });
bb.on("file", (name, file, info) => {
const ContentParts = [];
file.on("data", (x) => ContentParts.push(x));
file.on("end", async () => {
const content = new BinaryContent();
content.url = `${req.headers["host"]}${req.url}`;
content.mime = info.mimeType.toLowerCase();
content.name = info.filename;
content.payload = Buffer.concat(ContentParts);
fileContents.push(content);
});
});
bb.on("field", (name, val, info) => {
formFields[name] = val;
if (name.startsWith("_")) {
jsonHeaders[name] = val;
}
});
});
bb.on("field", (name, val, info) => {
formFields[name] = val;
if (name.startsWith("_")) {
jsonHeaders[name] = val;
}
});
bb.on("close", createCmsAndCreateResponseAsync);
req.pipe(bb);
} else {
createCmsAndCreateResponseAsync();
bb.on("close", createCmsAndCreateResponseAsync);
req.pipe(bb);
} else {
createCmsAndCreateResponseAsync();
}
} catch (ex) {
console.error(ex);
res.writeHead(StatusCodes.INTERNAL_SERVER_ERROR);
res.end(ex.toString());
}
} catch (ex) {
console.error(ex);
res.writeHead(StatusCodes.INTERNAL_SERVER_ERROR);
res.end(ex.toString());
}
});
})
.on("error", (er) => console.error(er))
.on("clientError", (er) => console.error(er))
Expand Down
Loading