From c2ce9e78bd7e0e1fbcb1695f24563fa5984a4ba5 Mon Sep 17 00:00:00 2001 From: Corey Butler Date: Wed, 31 Mar 2021 10:40:03 -0500 Subject: [PATCH] Prevent redundant request body processing/events. Add HTTP content-type header for form bodies. --- package.json | 2 +- src/lib/Request.js | 21 +++++++++++++++------ tests/03-request.js | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 0fb50a8..fc645cc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/lib/Request.js b/src/lib/Request.js index fa5261a..f906dc7 100644 --- a/src/lib/Request.js +++ b/src/lib/Request.js @@ -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", @@ -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) @@ -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 @@ -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') } @@ -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 }) } diff --git a/tests/03-request.js b/tests/03-request.js index 40ea5b7..b15af41 100644 --- a/tests/03-request.js +++ b/tests/03-request.js @@ -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,