From 528c449901a685e74f31eca9f35a84982eeae048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisend=C3=B6rfer?= Date: Sat, 21 Nov 2009 16:37:29 +0100 Subject: [PATCH] Multipart improvements Multipart parts now have a name and filename property. Those are the same as: part.headers['content-disposition'].name part.headers['content-disposition'].filename This patch also updates and improves the docs for the multipart module. --- doc/api.txt | 14 ++++++++++++-- lib/multipart.js | 10 +++++++++- test/mjsunit/test-multipart.js | 2 +- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/doc/api.txt b/doc/api.txt index 7692f4987e..c8880801cc 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -1069,7 +1069,6 @@ var stream = new multipart.Stream(options); var parts = {}; stream.addListener("part", function (part) { - var name = part.headers["Content-Disposition"].name; var buffer = ""; part.addListener("body", function(chunk) { @@ -1077,7 +1076,7 @@ stream.addListener("part", function (part) { }); part.addListener("complete", function() { - parts[name] = buffer; + parts[part.name] = buffer; }); }); @@ -1095,6 +1094,12 @@ stream.addListener("complete", function() { |+"complete"+ | | Emitted when the end of the stream is reached. |========================================================= ++stream.bytesTotal+:: +The amount of bytes this stream is expected to have. + ++stream.bytesReceived+:: +The amount of bytes received by this stream so far. + ==== +multipart.Part+ [cols="1,2,10",options="header"] @@ -1104,6 +1109,11 @@ stream.addListener("complete", function() { |+"complete"+ | | Emitted when the end of the part is reached. |========================================================= ++part.name+:: +The field name of this part. + ++part.filename+:: +The filename of this part. Only set for file uploads. === TCP diff --git a/lib/multipart.js b/lib/multipart.js index f72a4be1e8..9b2d7b5c16 100644 --- a/lib/multipart.js +++ b/lib/multipart.js @@ -6,7 +6,7 @@ exports.parse = function(options) { var parts = {}; stream.addListener('part', function(part) { - var name = part.headers['content-disposition'].name; + var name = part.name; var buffer = ''; part.addListener('body', function(chunk) { @@ -101,6 +101,8 @@ function Part(stream) { process.EventEmitter.call(this); this.headers = {}; + this.name = null; + this.filename = null; this.buffer = ''; this.bytesReceived = 0; @@ -128,6 +130,12 @@ Part.prototype.parsedHeaders = function() { parsedHeader[key] = val; } + + if (header == 'content-disposition') { + this.name = parsedHeader.name || null; + this.filename = parsedHeader.filename || null; + } + this.headers[header] = parsedHeader; } }; diff --git a/test/mjsunit/test-multipart.js b/test/mjsunit/test-multipart.js index d0472c4647..a44ea6d55c 100644 --- a/test/mjsunit/test-multipart.js +++ b/test/mjsunit/test-multipart.js @@ -15,7 +15,7 @@ var server = http.createServer(function(req, res) { stream.addListener('part', function(part) { parts_reveived++; - var name = part.headers['content-disposition'].name; + var name = part.name; if (parts_reveived == 1) { assertEquals('reply', name);