Browse Source

Throw exception for invalid multipart streams

When using the multipart parser with an regular http request, the
parser did not complain and just never finished.
v0.7.4-release
Felix Geisendörfer 15 years ago
parent
commit
d9a81374b2
  1. 15
      lib/multipart.js
  2. 32
      test/mjsunit/test-multipart.js

15
lib/multipart.js

@ -43,11 +43,16 @@ proto.init = function(options) {
if ('headers' in options) {
var req = options, contentType = req.headers['content-type'];
if (contentType) {
contentType = contentType.split(/; ?boundary=/)
this.boundary = '--'+contentType[1];
if (!contentType) {
throw new Error('Content-Type header not set');
}
var boundary = contentType.match(/^multipart\/form-data; ?boundary=(.+)$/i)
if (!boundary) {
throw new Error('Content-Type is not multipart: "'+contentType+'"');
}
this.boundary = '--'+boundary[1];
this.bytesTotal = req.headers['content-length'];
var self = this;
@ -59,6 +64,10 @@ proto.init = function(options) {
self.emit('complete');
});
} else {
if (!options.boundary) {
throw new Error('No boundary option given');
}
this.boundary = options.boundary;
this.write(options.data || '');
}

32
test/mjsunit/test-multipart.js

@ -7,10 +7,28 @@ var fixture = require('./fixtures/multipart');
var port = 8222;
var parts_reveived = 0;
var parts_complete = 0;
var requests = 0;
var badRequests = 0;
var parts = {};
var respond = function(res, text) {
requests++;
if (requests == 2) {
server.close();
}
res.sendHeader(200, {"Content-Type": "text/plain"});
res.sendBody(text);
res.finish();
};
var server = http.createServer(function(req, res) {
var stream = new multipart.Stream(req);
try {
var stream = new multipart.Stream(req);
} catch (e) {
badRequests++;
respond(res, 'no thanks');
return;
}
stream.addListener('part', function(part) {
parts_reveived++;
@ -39,10 +57,7 @@ var server = http.createServer(function(req, res) {
});
stream.addListener('complete', function() {
res.sendHeader(200, {"Content-Type": "text/plain"});
res.sendBody('thanks');
res.finish();
server.close();
respond(res, 'thanks');
});
});
server.listen(port);
@ -52,8 +67,13 @@ var request = client.request('POST', '/', {'Content-Type': 'multipart/form-data;
request.sendBody(fixture.reply, 'binary');
request.finish();
var badRequest = client.request('POST', '/', {'Content-Type': 'something', 'Content-Length': fixture.reply.length});
badRequest.sendBody(fixture.reply, 'binary');
badRequest.finish();
process.addListener('exit', function() {
puts("done");
assert.equal(2, parts_complete);
assert.equal(2, parts_reveived);
});
assert.equal(1, badRequests);
});
Loading…
Cancel
Save