lib/commandCenter/httpBatchHandler.js
function parseMultipart(req, boundary, cb) {
	// multipart, the first part has to be the same format as single part post data

	var parser = MultipartParser.create(boundary);
	var files = {};
	var cmdData = '';

	parser.on('part', function (part) {
		var m, disp, partName, fileName, isCmdData;

		disp = part.headers['content-disposition'];
		if (!disp) {
			// unidentifiable parts cannot be used

			logger.warning.data('partHeaders', part.headers).log('Received an unidentifyable part, skipping.');
			return;
		}

		m = disp.match(/name="(.+?)"/);
		if (!m) {
			// unnamed parts cannot be used

			logger.warning.data('partHeaders', part.headers).log('Received an unnamed part, skipping.');
			return;
		}

		partName = m[1];

		logger.verbose('Receiving multipart-part:', partName);

		isCmdData = (partName === 'cmddata');

		m = disp.match(/filename="(.+?)"/);
		if (m) {
			// a filename is optional

			fileName = m[1];
		}

		// the first part is ready and is expected to be the same format as single part post data

		var data = [];

		part.on('data', function (chunk) {
			if (isCmdData) {
				// treat as utf8

				cmdData += chunk.toString('utf8');
			} else {
				data.push(chunk);
			}
		});

		if (!isCmdData) {
			part.on('end', function () {
				// create the files object for the following files, the command center can take care of the rest

				files[partName] = {
					data: data,
					partName: partName,
					fileName: fileName,
					type: part.headers['content-type']
				};
			});
		}
	});

	// It has been observed that the "end" event on the parser would fire more than once. This might
	// be related to Node.js 0.10 streams. To avoid the situation, we use once('end') instead of
	// on('end').

	parser.once('end', function () {
		logger.verbose('Finished reading multipart', req.method, 'request.');

		cb(null, cmdData, files);
	});

	// pipe all incoming data straight into the multipart parser

	req.pipe(parser);
}
function parseMultipart(req, boundary, cb) {
	// multipart, the first part has to be the same format as single part post data

	var parser = MultipartParser.create(boundary);
	var files = {};
	var cmdData = '';

	parser.on('part', function (part) {
		var m, disp, partName, fileName, isCmdData;

		disp = part.headers['content-disposition'];
		if (!disp) {
			// unidentifiable parts cannot be used

			logger.warning.data('partHeaders', part.headers).log('Received an unidentifyable part, skipping.');
			return;
		}

		m = disp.match(/name="(.+?)"/);
		if (!m) {
			// unnamed parts cannot be used

			logger.warning.data('partHeaders', part.headers).log('Received an unnamed part, skipping.');
			return;
		}

		partName = m[1];

		logger.verbose('Receiving multipart-part:', partName);

		isCmdData = (partName === 'cmddata');

		m = disp.match(/filename="(.+?)"/);
		if (m) {
			// a filename is optional

			fileName = m[1];
		}

		// the first part is ready and is expected to be the same format as single part post data

		var data = [];

		part.on('data', function (chunk) {
			if (isCmdData) {
				// treat as utf8

				cmdData += chunk.toString('utf8');
			} else {
				data.push(chunk);
			}
		});

		if (!isCmdData) {
			part.on('end', function () {
				// create the files object for the following files, the command center can take care of the rest

				files[partName] = {
					data: data,
					partName: partName,
					fileName: fileName,
					type: part.headers['content-type']
				};
			});
		}
	});

	// It has been observed that the "end" event on the parser would fire more than once. This might
	// be related to Node.js 0.10 streams. To avoid the situation, we use once('end') instead of
	// on('end').

	parser.once('end', function () {
		logger.verbose('Finished reading multipart', req.method, 'request.');

		cb(null, cmdData, files);
	});

	// pipe all incoming data straight into the multipart parser

	req.pipe(parser);
}
function parseMultipart ( req , boundary , cb ) { // multipart, the first part has to be the same format as single part post data var parser = MultipartParser . create ( boundary ) ; var files = { } ; var cmdData = '' ; parser . on ( 'part' , function ( part ) { var m , disp , partName , fileName , isCmdData ; disp = part . headers [ 'content-disposition' ] ; if ( ! disp ) { // unidentifiable parts cannot be used logger . warning . data ( 'partHeaders' , part . headers ) . log ( 'Received an unidentifyable part, skipping.' ) ; return ; } m = disp . match ( / name="(.+?)" / ) ; if ( ! m ) { // unnamed parts cannot be used logger . warning . data ( 'partHeaders' , part . headers ) . log ( 'Received an unnamed part, skipping.' ) ; return ; } partName = m [ 1 ] ; logger . verbose ( 'Receiving multipart-part:' , partName ) ; isCmdData = ( partName === 'cmddata' ) ; m = disp . match ( / filename="(.+?)" / ) ; if ( m ) { // a filename is optional fileName = m [ 1 ] ; } // the first part is ready and is expected to be the same format as single part post data var data = [ ] ; part . on ( 'data' , function ( chunk ) { if ( isCmdData ) { // treat as utf8 cmdData += chunk . toString ( 'utf8' ) ; } else { data . push ( chunk ) ; } } ) ; if ( ! isCmdData ) { part . on ( 'end' , function ( ) { // create the files object for the following files, the command center can take care of the rest files [ partName ] = { data : data , partName : partName , fileName : fileName , type : part . headers [ 'content-type' ] } ; } ) ; } } ) ; // It has been observed that the "end" event on the parser would fire more than once. This might // be related to Node.js 0.10 streams. To avoid the situation, we use once('end') instead of // on('end'). parser . once ( 'end' , function ( ) { logger . verbose ( 'Finished reading multipart' , req . method , 'request.' ) ; cb ( null , cmdData , files ) ; } ) ; // pipe all incoming data straight into the multipart parser req . pipe ( parser ) ; }
Streams and parses a multipart request

@param {Object}   req       The HTTP request.
@param {string}   boundary  The boundary between the parts.
@param {Function} cb        Callback on completion.
Streams and parses a multipart request