Skip to content

Commit

Permalink
#4 fixing a scenario having a part with no content-type and either a …
Browse files Browse the repository at this point in the history
…file-type payload.
  • Loading branch information
Christian Salazar committed Jul 7, 2017
1 parent af35cd6 commit 59892e5
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 8 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```


20 changes: 12 additions & 8 deletions multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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='';
Expand Down Expand Up @@ -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.
Expand Down
60 changes: 60 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -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<parts.length;i++){
var part = parts[i];
console.log(part);
if('NOT_A_FILE' != part.filename) require("fs").writeFile(
'/tmp/'+part.filename,part.data,function(err){
console.log('file saved at:','/tmp/'+part.filename); });
}

0 comments on commit 59892e5

Please sign in to comment.