-
Notifications
You must be signed in to change notification settings - Fork 0
adding json content type support #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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[]} */ | ||
|
|
@@ -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( | ||
| "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(); | ||
|
|
@@ -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) => { | ||
|
|
@@ -117,6 +136,16 @@ export default class H2HttpHostEndPoint extends SecureHttpHostEndPoint { | |
| }); | ||
| bb.on("close", createCmsAndCreateResponseAsync); | ||
| stream.pipe(bb); | ||
| } else if (method === "POST") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In other case (non json content) this code work? it is tested?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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} */ | ||
|
|
@@ -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) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In other case (non json content) this code work? it is tested?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
|
|
||
There was a problem hiding this comment.
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