UNPKG

strata

Version:

A modular, streaming HTTP server

71 lines (56 loc) 1.67 kB
var util = require('util'); var EventEmitter = require('events').EventEmitter; module.exports = Part; /** * A container class for data pertaining to one part of a multipart message. */ function Part() { this.headers = {}; } util.inherits(Part, EventEmitter); /** * Returns the name of this part. */ Part.prototype.__defineGetter__('name', function () { var disposition = this.headers['content-disposition'], match; if (disposition && (match = disposition.match(/name="([^"]+)"/i))) { return match[1]; } return this.headers['content-id'] || null; }); /** * Returns the filename of this part if it originated from a file upload. */ Part.prototype.__defineGetter__('filename', function () { var disposition = this.headers['content-disposition']; if (disposition) { var match = disposition.match(/filename="([^;]*)"/i), filename; if (match) { filename = decodeURIComponent(match[1].replace(/\\"/g, '"')); } else { // Match unquoted filename. match = disposition.match(/filename=([^;]+)/i); if (match) { filename = decodeURIComponent(match[1]); } } if (filename) { // Take the last part of the filename. This handles full Windows // paths given by IE (and possibly other dumb clients). return filename.substr(filename.lastIndexOf('\\') + 1); } } return null; }); /** * Returns the Content-Type of this part. */ Part.prototype.__defineGetter__('type', function () { return this.headers['content-type'] || null; }); Part.prototype.write = function (buffer) { this.emit('data', buffer); }; Part.prototype.end = function () { this.emit('end'); };