From 59892e567cf0af99e1ee6990b687d15998ff1bde Mon Sep 17 00:00:00 2001 From: Christian Salazar Date: Fri, 7 Jul 2017 09:19:26 -0400 Subject: [PATCH] #4 fixing a scenario having a part with no content-type and either a file-type payload. --- README.md | 11 ++++++++++ multipart.js | 20 +++++++++++------- test.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 test.js diff --git a/README.md b/README.md index b4a3d8c..f702d55 100644 --- a/README.md +++ b/README.md @@ -83,4 +83,15 @@ Now, having this two key values then you can implement it: The returned data is an array of parts, each one described by a filename, a type and a data, this last one is a Buffer (see also Node Buffer). +# How to Test Locally + +A test.js file is provided in order to do a local testing, simply open +a console and type: + +``` +cd /your/project/parse-multipart; +# or: cd /your/project/node_modules/parse-multipart; +node test # please read the comments at test.js +``` + diff --git a/multipart.js b/multipart.js index 1efecfe..7b48455 100644 --- a/multipart.js +++ b/multipart.js @@ -33,14 +33,14 @@ exports.Parse = function(multipartBodyBuffer,boundary){ { value: b, writable: true, enumerable: true, configurable: true }) return o; } - var header = part.header.split(';'); - var file = obj(header[2]); - var contentType = part.info.split(':')[1].trim(); + var header = part.header.split(';'); + var file = obj(header[2] ? header[2] : 'filename=\"NOT_A_FILE\"'); + var contentType = (part.info) ? (part.info.split(':')[1].trim()) : 'text/plain'; Object.defineProperty( file , 'type' , { value: contentType, writable: true, enumerable: true, configurable: true }) Object.defineProperty( file , 'data' , { value: new Buffer(part.part), writable: true, enumerable: true, configurable: true }) - return file; + return file; } var prev = null; var lastline=''; @@ -125,12 +125,16 @@ exports.DemoData = function(){ body += "111Y\r\n"; body += "111Z\rCCCC\nCCCC\r\nCCCCC@\r\n\r\n"; body += "------WebKitFormBoundaryvef1fLxmoUdYZWXp\r\n"; - body += "Content-Disposition: form-data; name=\"uploads[]\"; filename=\"B.txt\"\r\n"; + body += "Content-Disposition: form-data; name=\"testMessage\";\r\n"; + body += "\r\n\r\n"; + body += "test message 123456\r\n"; + body += "------WebKitFormBoundaryvef1fLxmoUdYZWXp\r\n"; + body += "Content-Disposition: form-data; name=\"uploads[]\"; filename=\"C.txt\"\r\n"; body += "Content-Type: text/plain\r\n", body += "\r\n\r\n"; - body += "@22X"; - body += "222Y\r\n"; - body += "222Z\r222W\n2220\r\n666@\r\n"; + body += "@CCC"; + body += "CCCY\r\n"; + body += "CCCZ\rCCCW\nCCC0\r\n666@\r\n"; body += "------WebKitFormBoundaryvef1fLxmoUdYZWXp--\r\n"; return (new Buffer(body,'utf-8')); // returns a Buffered payload, so the it will be treated as a binary content. diff --git a/test.js b/test.js new file mode 100644 index 0000000..4ce7eae --- /dev/null +++ b/test.js @@ -0,0 +1,60 @@ +/** + this is a tester, it enables you to debug your request. + + 1. USAGE: + + Provide two values: body and contentType and put them in the + respective variables, then execute: + + $ node test + + 2. OBTAINING THE BODY AND CONTENT-TYPE UNDER AMAZON APIGATEWAY+LAMBDA: + + if you are runnig this library on Amazon Aws (ApiGateway) then put + this line in the very beggining of your lambda function: + + console.log(JSON.stringify(event)); + + it will provide you with something similar to (see logs in CloudWatch): + + ``` + 2017-07-05T23:17:58.956Z 2f01d974-61d8-11e7-9b9a-87f6b4f45038 event= + { + "body-json": "LS0tLS0tV2ViS........", + "params": { + "path": {}, + "querystring": {}, + "header": { + "Accept": "/", + "Accept-Encoding": "gzip, deflate, br", + "Accept-Language": "ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4", + "CloudFront-Is-Tablet-Viewer": "false", + "CloudFront-Viewer-Country": "JP", + "content-type": "multipart/form-data; boundary=----WebKitFormBoundaryoCaPJuwCGZB5G5Jq", + ... + } + } + } + ``` + from this log, copy and paste the values for: "body-json" and "content-type". +*/ +var multipart = require('./multipart.js'); + +var body = "LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5b0NhUEp1d0NHWkI1RzVKcQ0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJ0ZXN0TWVzc2FnZSINCg0KdGVzdCBtZXNzYWdlIDEyMzU2DQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnlvQ2FQSnV3Q0daQjVHNUpxDQpDb250ZW50LURpc3Bvc2l0aW9uOiBmb3JtLWRhdGE7IG5hbWU9InRlc3RGaWxlIjsgZmlsZW5hbWU9IjEyMy50Z3oiDQpDb250ZW50LVR5cGU6IGFwcGxpY2F0aW9uL3gtY29tcHJlc3NlZA0KDQofiwgIgd9cWQALMTIzLnRhcgDtzjEOwCAMQ1FOVBG7CRdj6PEbGNkjFr/lS55s4DO/2Sr1NNx309nEZjREvB0Da/dgtvSVLAbeviAiIhf8UCKb4gAIAAANCi0tLS0tLVdlYktpdEZvcm1Cb3VuZGFyeW9DYVBKdXdDR1pCNUc1SnEtLQ0K"; +var contentType='multipart/form-data; boundary=----WebKitFormBoundaryoCaPJuwCGZB5G5Jq'; +var boundary=null; + +// activate this line to utilize the body and content type defined above +// body = new Buffer(body,'base64'); boundary = multipart.getBoundary(contentType); + +// activate this line to utilize the demo data +body = multipart.DemoData(); boundary="----WebKitFormBoundaryvef1fLxmoUdYZWXp"; + +var parts = multipart.Parse(body,boundary); +for(var i=0;i