From fb8669892c1c5b6c9ec74cbf33efa95fe2a71b47 Mon Sep 17 00:00:00 2001 From: Cooper Bell Date: Mon, 15 Oct 2018 12:54:54 -0500 Subject: [PATCH 1/5] Add additional multipart headers as an optional parameter --- lib/types/multipart.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/types/multipart.js b/lib/types/multipart.js index d33492a..1a835bd 100644 --- a/lib/types/multipart.js +++ b/lib/types/multipart.js @@ -20,6 +20,12 @@ var RE_BOUNDARY = /^boundary$/i, RE_FILENAME = /^filename$/i, RE_NAME = /^name$/i; +var REQUIRED_HEADERS = [ + 'content-type', + 'content-disposition', + 'content-transfer-encoding' +]; + Multipart.detect = /^multipart\/form-data/i; function Multipart(boy, cfg) { if (!(this instanceof Multipart)) @@ -121,6 +127,7 @@ function Multipart(boy, cfg) { field.removeAllListeners('end'); } + var additionalHeaders = {}; part.on('header', function(header) { var contype, fieldname, @@ -169,6 +176,10 @@ function Multipart(boy, cfg) { else encoding = '7bit'; + for (var h in header) { + if (!REQUIRED_HEADERS.includes(h)) additionalHeaders[h] = header[h][0]; + } + var onData, onEnd; if (contype === 'application/octet-stream' || filename !== undefined) { @@ -210,7 +221,9 @@ function Multipart(boy, cfg) { cb(); } }; - boy.emit('file', fieldname, file, filename, encoding, contype); + + var hdrs = Object.keys(additionalHeaders).length ? additionalHeaders : null; + boy.emit('file', fieldname, file, filename, encoding, contype, hdrs); onData = function(data) { if ((nsize += data.length) > fileSizeLimit) { @@ -258,7 +271,9 @@ function Multipart(boy, cfg) { curField = undefined; if (buffer.length) buffer = decodeText(buffer, 'binary', charset); - boy.emit('field', fieldname, buffer, false, truncated, encoding, contype); + + var hdrs = Object.keys(additionalHeaders).length ? additionalHeaders : null; + boy.emit('field', fieldname, buffer, false, truncated, encoding, contype, hdrs); --nends; checkFinished(); }; From 17e5af46ff93b8becc2ab7538b49183b8d8483d3 Mon Sep 17 00:00:00 2001 From: Cooper Bell Date: Mon, 15 Oct 2018 13:06:31 -0500 Subject: [PATCH 2/5] Remove includes method for older node.js versions --- lib/types/multipart.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/types/multipart.js b/lib/types/multipart.js index 1a835bd..34c19cc 100644 --- a/lib/types/multipart.js +++ b/lib/types/multipart.js @@ -20,12 +20,6 @@ var RE_BOUNDARY = /^boundary$/i, RE_FILENAME = /^filename$/i, RE_NAME = /^name$/i; -var REQUIRED_HEADERS = [ - 'content-type', - 'content-disposition', - 'content-transfer-encoding' -]; - Multipart.detect = /^multipart\/form-data/i; function Multipart(boy, cfg) { if (!(this instanceof Multipart)) @@ -177,7 +171,7 @@ function Multipart(boy, cfg) { encoding = '7bit'; for (var h in header) { - if (!REQUIRED_HEADERS.includes(h)) additionalHeaders[h] = header[h][0]; + if (!isRequiredHeader(h)) additionalHeaders[h] = header[h][0]; } var onData, @@ -332,6 +326,13 @@ function FileStream(opts) { this.truncated = false; } + +function isRequiredHeader(h) { + return h === 'content-type' + || h === 'content-disposition' + || h === 'content-transfer-encoding'; +} + inherits(FileStream, ReadableStream); FileStream.prototype._read = function(n) {}; From b4e29f203c0de228d25c16b352e555e27f8264a7 Mon Sep 17 00:00:00 2001 From: Cooper Bell Date: Mon, 15 Oct 2018 13:35:39 -0500 Subject: [PATCH 3/5] Always pass in additional headers object --- lib/types/multipart.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/types/multipart.js b/lib/types/multipart.js index 34c19cc..9ccb6f2 100644 --- a/lib/types/multipart.js +++ b/lib/types/multipart.js @@ -216,8 +216,7 @@ function Multipart(boy, cfg) { } }; - var hdrs = Object.keys(additionalHeaders).length ? additionalHeaders : null; - boy.emit('file', fieldname, file, filename, encoding, contype, hdrs); + boy.emit('file', fieldname, file, filename, encoding, contype, additionalHeaders); onData = function(data) { if ((nsize += data.length) > fileSizeLimit) { @@ -266,8 +265,7 @@ function Multipart(boy, cfg) { if (buffer.length) buffer = decodeText(buffer, 'binary', charset); - var hdrs = Object.keys(additionalHeaders).length ? additionalHeaders : null; - boy.emit('field', fieldname, buffer, false, truncated, encoding, contype, hdrs); + boy.emit('field', fieldname, buffer, false, truncated, encoding, contype, additionalHeaders); --nends; checkFinished(); }; From e67f154c4ea5c4c2ff4583f58f81bfbadc8d5c61 Mon Sep 17 00:00:00 2001 From: Cooper Bell Date: Tue, 16 Oct 2018 20:08:48 -0500 Subject: [PATCH 4/5] Iterate over Object.keys of header, add all header values not just the first --- lib/types/multipart.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/types/multipart.js b/lib/types/multipart.js index 9ccb6f2..47d5d0e 100644 --- a/lib/types/multipart.js +++ b/lib/types/multipart.js @@ -170,8 +170,8 @@ function Multipart(boy, cfg) { else encoding = '7bit'; - for (var h in header) { - if (!isRequiredHeader(h)) additionalHeaders[h] = header[h][0]; + for (var k of Object.keys(header)) { + if (!isRequiredHeader(k)) additionalHeaders[k] = header[k]; } var onData, From 99d64362bbf4bde69232648ab92ed5b27fc60169 Mon Sep 17 00:00:00 2001 From: Cooper Bell Date: Tue, 16 Oct 2018 20:11:29 -0500 Subject: [PATCH 5/5] Node 0.10 doesn't support "for of", replace with forEach --- lib/types/multipart.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/types/multipart.js b/lib/types/multipart.js index 47d5d0e..4e3d5d7 100644 --- a/lib/types/multipart.js +++ b/lib/types/multipart.js @@ -170,9 +170,9 @@ function Multipart(boy, cfg) { else encoding = '7bit'; - for (var k of Object.keys(header)) { + Object.keys(header).forEach(function(k) { if (!isRequiredHeader(k)) additionalHeaders[k] = header[k]; - } + }); var onData, onEnd;