Browse Source

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.
v0.7.4-release
Felix Geisendörfer 15 years ago
committed by Ryan Dahl
parent
commit
528c449901
  1. 14
      doc/api.txt
  2. 10
      lib/multipart.js
  3. 2
      test/mjsunit/test-multipart.js

14
doc/api.txt

@ -1069,7 +1069,6 @@ var stream = new multipart.Stream(options);
var parts = {}; var parts = {};
stream.addListener("part", function (part) { stream.addListener("part", function (part) {
var name = part.headers["Content-Disposition"].name;
var buffer = ""; var buffer = "";
part.addListener("body", function(chunk) { part.addListener("body", function(chunk) {
@ -1077,7 +1076,7 @@ stream.addListener("part", function (part) {
}); });
part.addListener("complete", function() { 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. |+"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+ ==== +multipart.Part+
[cols="1,2,10",options="header"] [cols="1,2,10",options="header"]
@ -1104,6 +1109,11 @@ stream.addListener("complete", function() {
|+"complete"+ | | Emitted when the end of the part is reached. |+"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 === TCP

10
lib/multipart.js

@ -6,7 +6,7 @@ exports.parse = function(options) {
var parts = {}; var parts = {};
stream.addListener('part', function(part) { stream.addListener('part', function(part) {
var name = part.headers['content-disposition'].name; var name = part.name;
var buffer = ''; var buffer = '';
part.addListener('body', function(chunk) { part.addListener('body', function(chunk) {
@ -101,6 +101,8 @@ function Part(stream) {
process.EventEmitter.call(this); process.EventEmitter.call(this);
this.headers = {}; this.headers = {};
this.name = null;
this.filename = null;
this.buffer = ''; this.buffer = '';
this.bytesReceived = 0; this.bytesReceived = 0;
@ -128,6 +130,12 @@ Part.prototype.parsedHeaders = function() {
parsedHeader[key] = val; parsedHeader[key] = val;
} }
if (header == 'content-disposition') {
this.name = parsedHeader.name || null;
this.filename = parsedHeader.filename || null;
}
this.headers[header] = parsedHeader; this.headers[header] = parsedHeader;
} }
}; };

2
test/mjsunit/test-multipart.js

@ -15,7 +15,7 @@ var server = http.createServer(function(req, res) {
stream.addListener('part', function(part) { stream.addListener('part', function(part) {
parts_reveived++; parts_reveived++;
var name = part.headers['content-disposition'].name; var name = part.name;
if (parts_reveived == 1) { if (parts_reveived == 1) {
assertEquals('reply', name); assertEquals('reply', name);

Loading…
Cancel
Save