Skip to content

Commit

Permalink
Prevent redundant request body processing/events. Add HTTP content-ty…
Browse files Browse the repository at this point in the history
…pe header for form bodies.
  • Loading branch information
coreybutler committed Mar 31, 2021
1 parent 8d553b0 commit c2ce9e7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ngnjs/net",
"version": "1.0.0-alpha.15",
"version": "1.0.0-alpha.16",
"description": "A network communications plugin for NGN.",
"type": "module",
"author": "Corey Butler\n",
Expand Down
21 changes: 15 additions & 6 deletions src/lib/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export default class Request extends NGN.EventEmitter { // eslint-disable-line n
* following syntax:
*
* ```json
* {
* body: {
* form: {
* form_field_1: "value",
* form_field_2: "value",
Expand Down Expand Up @@ -276,6 +276,7 @@ export default class Request extends NGN.EventEmitter { // eslint-disable-line n
}

this.#body = dataString.join('&')
this.setHeader('Content-Type', 'application/x-www-form-urlencoded')
} else {
this.#body = JSON.stringify(this.#body).trim()
this.setHeader('Content-Length', this.#body.length)
Expand All @@ -293,7 +294,9 @@ export default class Request extends NGN.EventEmitter { // eslint-disable-line n
if (match !== null && this.#body.trim().substr(0, 5).toLowerCase() !== 'data:' && this.#body.trim().substr(0, 1).toLowerCase() !== '<') {
this.setHeader('Content-Type', 'application/x-www-form-urlencoded')
} else {
this.setHeader('Content-Type', 'text/plain')
if (contentType === null) {
this.setHeader('Content-Type', 'text/plain')
}

if (this.#body.trim().substr(0, 5).toLowerCase() === 'data:') {
// Crude Data URL mimetype detection
Expand All @@ -310,7 +313,7 @@ export default class Request extends NGN.EventEmitter { // eslint-disable-line n
this.setHeader('Content-Type', 'text/html')
}
}
} else {
} else if (contentType === null) {
this.setHeader('Content-Type', 'text/plain')
}

Expand Down Expand Up @@ -703,9 +706,15 @@ export default class Request extends NGN.EventEmitter { // eslint-disable-line n
}

set body (value) {
const old = this.#body
this.#body = value
if (old !== this.#body) {
if (this.#body !== value) {
if (value && typeof value === 'string') {
this.setHeader('content-type', 'text/plain')
} else {
this.removeHeader('content-type')
}

const old = this.#body
this.#body = value
this.prepareBody()
this.emit('update.body', { old, new: this.body })
}
Expand Down
2 changes: 1 addition & 1 deletion tests/03-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ test('NGN HTTP Request Configuration', t => {
t.expect('text/html', request.getHeader('content-type'), 'Correctly identified HTML body type.')

request.body = 'Basic text body.'
t.expect('text/plain', request.getHeader('content-type'), 'Correctly identified HTML body type.')
t.expect('text/plain', request.getHeader('content-type'), 'Correctly identified plain text body type.')

// request = new Request({
// url: uri.get,
Expand Down

0 comments on commit c2ce9e7

Please sign in to comment.