package/package.json 000644 000765 000024 0000001371 12121143632 013012 0 ustar 00 000000 000000 { "name": "formidable", "description": "A node.js module for parsing form data, especially file uploads.", "homepage": "https://github.com/felixge/node-formidable", "version": "1.0.13", "devDependencies": { "gently": "0.8.0", "findit": "0.1.1", "hashish": "0.0.4", "urun": "~0.0.6", "utest": "0.0.3", "request": "~2.11.4" }, "directories": { "lib": "./lib" }, "main": "./lib/index", "scripts": { "test": "node test/run.js", "clean": "rm test/tmp/*" }, "engines": { "node": "<0.9.0" }, "repository": { "type": "git", "url": "git://github.com/felixge/node-formidable.git" }, "bugs": { "url": "http://github.com/felixge/node-formidable/issues" }, "optionalDependencies": {} } package/.npmignore 000644 000765 000024 0000000041 12117571742 012530 0 ustar 00 000000 000000 /test/tmp/ *.upload *.un~ *.http package/LICENSE 000644 000765 000024 0000002047 12117571742 011546 0 ustar 00 000000 000000 Copyright (C) 2011 Felix Geisendörfer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package/index.js 000644 000765 000024 0000000042 12117571742 012177 0 ustar 00 000000 000000 module.exports = require('./lib'); package/benchmark/bench-multipart-parser.js 000644 000765 000024 0000003210 12117571742 017412 0 ustar 00 000000 000000 var assert = require('assert'); require('../test/common'); var multipartParser = require('../lib/multipart_parser'), MultipartParser = multipartParser.MultipartParser, parser = new MultipartParser(), Buffer = require('buffer').Buffer, boundary = '-----------------------------168072824752491622650073', mb = 100, buffer = createMultipartBuffer(boundary, mb * 1024 * 1024), callbacks = { partBegin: -1, partEnd: -1, headerField: -1, headerValue: -1, partData: -1, end: -1, }; parser.initWithBoundary(boundary); parser.onHeaderField = function() { callbacks.headerField++; }; parser.onHeaderValue = function() { callbacks.headerValue++; }; parser.onPartBegin = function() { callbacks.partBegin++; }; parser.onPartData = function() { callbacks.partData++; }; parser.onPartEnd = function() { callbacks.partEnd++; }; parser.onEnd = function() { callbacks.end++; }; var start = +new Date(), nparsed = parser.write(buffer), duration = +new Date - start, mbPerSec = (mb / (duration / 1000)).toFixed(2); console.log(mbPerSec+' mb/sec'); assert.equal(nparsed, buffer.length); function createMultipartBuffer(boundary, size) { var head = '--'+boundary+'\r\n' + 'content-disposition: form-data; name="field1"\r\n' + '\r\n' , tail = '\r\n--'+boundary+'--\r\n' , buffer = new Buffer(size); buffer.write(head, 'ascii', 0); buffer.write(tail, 'ascii', buffer.length - tail.length); return buffer; } process.on('exit', function() { for (var k in callbacks) { assert.equal(0, callbacks[k], k+' count off by '+callbacks[k]); } }); package/.travis.yml 000644 000765 000024 0000000066 12117602754 012647 0 ustar 00 000000 000000 language: node_js node_js: - 0.8 - 0.9 - "0.10" package/tool/record.js 000644 000765 000024 0000002360 12117571742 013330 0 ustar 00 000000 000000 var http = require('http'); var fs = require('fs'); var connections = 0; var server = http.createServer(function(req, res) { var socket = req.socket; console.log('Request: %s %s -> %s', req.method, req.url, socket.filename); req.on('end', function() { if (req.url !== '/') { res.end(JSON.stringify({ method: req.method, url: req.url, filename: socket.filename, })); return; } res.writeHead(200, {'content-type': 'text/html'}); res.end( '
' ); }); }); server.on('connection', function(socket) { connections++; socket.id = connections; socket.filename = 'connection-' + socket.id + '.http'; socket.file = fs.createWriteStream(socket.filename); socket.pipe(socket.file); console.log('--> %s', socket.filename); socket.on('close', function() { console.log('<-- %s', socket.filename); }); }); var port = process.env.PORT || 8080; server.listen(port, function() { console.log('Recording connections on port %s', port); }); package/lib/file.js 000644 000765 000024 0000003020 12121124047 012537 0 ustar 00 000000 000000 if (global.GENTLY) require = GENTLY.hijack(require); var util = require('util'), WriteStream = require('fs').WriteStream, EventEmitter = require('events').EventEmitter, crypto = require('crypto'); function File(properties) { EventEmitter.call(this); this.size = 0; this.path = null; this.name = null; this.type = null; this.hash = null; this.lastModifiedDate = null; this._writeStream = null; for (var key in properties) { this[key] = properties[key]; } if(typeof this.hash === 'string') { this.hash = crypto.createHash(properties.hash); } } module.exports = File; util.inherits(File, EventEmitter); File.prototype.open = function() { this._writeStream = new WriteStream(this.path); }; File.prototype.toJSON = function() { return { size: this.size, path: this.path, name: this.name, type: this.type, mtime: this.lastModifiedDate, length: this.length, filename: this.filename, mime: this.mime }; }; File.prototype.write = function(buffer, cb) { var self = this; this._writeStream.write(buffer, function() { if (self.hash) { if (self.hash.hasOwnProperty('update')) { self.hash.update(buffer); } } self.lastModifiedDate = new Date(); self.size += buffer.length; self.emit('progress', self.size); cb(); }); }; File.prototype.end = function(cb) { var self = this; this._writeStream.end(function() { if(self.hash) { self.hash = self.hash.digest('hex'); } self.emit('end'); cb(); }); }; package/lib/incoming_form.js 000644 000765 000024 0000027716 12117571742 014505 0 ustar 00 000000 000000 if (global.GENTLY) require = GENTLY.hijack(require); var fs = require('fs'); var util = require('util'), path = require('path'), File = require('./file'), MultipartParser = require('./multipart_parser').MultipartParser, QuerystringParser = require('./querystring_parser').QuerystringParser, OctetParser = require('./octet_parser').OctetParser, JSONParser = require('./json_parser').JSONParser, StringDecoder = require('string_decoder').StringDecoder, EventEmitter = require('events').EventEmitter, Stream = require('stream').Stream, os = require('os'); function IncomingForm(opts) { if (!(this instanceof IncomingForm)) return new IncomingForm(opts); EventEmitter.call(this); opts=opts||{}; this.error = null; this.ended = false; this.maxFields = opts.maxFields || 1000; this.maxFieldsSize = opts.maxFieldsSize || 2 * 1024 * 1024; this.keepExtensions = opts.keepExtensions || false; this.uploadDir = opts.uploadDir || os.tmpDir(); this.encoding = opts.encoding || 'utf-8'; this.headers = null; this.type = null; this.hash = false; this.bytesReceived = null; this.bytesExpected = null; this._parser = null; this._flushing = 0; this._fieldsSize = 0; this.openedFiles = []; return this; }; util.inherits(IncomingForm, EventEmitter); exports.IncomingForm = IncomingForm; IncomingForm.prototype.parse = function(req, cb) { this.pause = function() { try { req.pause(); } catch (err) { // the stream was destroyed if (!this.ended) { // before it was completed, crash & burn this._error(err); } return false; } return true; }; this.resume = function() { try { req.resume(); } catch (err) { // the stream was destroyed if (!this.ended) { // before it was completed, crash & burn this._error(err); } return false; } return true; }; var self = this; req .on('error', function(err) { self._error(err); }) .on('aborted', function() { self.emit('aborted'); self._error(new Error('Request aborted')); }) .on('data', function(buffer) { self.write(buffer); }) .on('end', function() { if (self.error) { return; } var err = self._parser.end(); if (err) { self._error(err); } }); if (cb) { var fields = {}, files = {}; this .on('field', function(name, value) { fields[name] = value; }) .on('file', function(name, file) { files[name] = file; }) .on('error', function(err) { cb(err, fields, files); }) .on('end', function() { cb(null, fields, files); }); } this.writeHeaders(req.headers); return this; }; IncomingForm.prototype.writeHeaders = function(headers) { this.headers = headers; this._parseContentLength(); this._parseContentType(); }; IncomingForm.prototype.write = function(buffer) { if (!this._parser) { this._error(new Error('unintialized parser')); return; } this.bytesReceived += buffer.length; this.emit('progress', this.bytesReceived, this.bytesExpected); var bytesParsed = this._parser.write(buffer); if (bytesParsed !== buffer.length) { this._error(new Error('parser error, '+bytesParsed+' of '+buffer.length+' bytes parsed')); } return bytesParsed; }; IncomingForm.prototype.pause = function() { // this does nothing, unless overwritten in IncomingForm.parse return false; }; IncomingForm.prototype.resume = function() { // this does nothing, unless overwritten in IncomingForm.parse return false; }; IncomingForm.prototype.onPart = function(part) { // this method can be overwritten by the user this.handlePart(part); }; IncomingForm.prototype.handlePart = function(part) { var self = this; if (part.filename === undefined) { var value = '' , decoder = new StringDecoder(this.encoding); part.on('data', function(buffer) { self._fieldsSize += buffer.length; if (self._fieldsSize > self.maxFieldsSize) { self._error(new Error('maxFieldsSize exceeded, received '+self._fieldsSize+' bytes of field data')); return; } value += decoder.write(buffer); }); part.on('end', function() { self.emit('field', part.name, value); }); return; } this._flushing++; var file = new File({ path: this._uploadPath(part.filename), name: part.filename, type: part.mime, hash: self.hash }); this.emit('fileBegin', part.name, file); file.open(); this.openedFiles.push(file); part.on('data', function(buffer) { self.pause(); file.write(buffer, function() { self.resume(); }); }); part.on('end', function() { file.end(function() { self._flushing--; self.emit('file', part.name, file); self._maybeEnd(); }); }); }; function dummyParser(self) { return { end: function () { self.ended = true; self._maybeEnd(); return null; } }; } IncomingForm.prototype._parseContentType = function() { if (this.bytesExpected === 0) { this._parser = dummyParser(this); return; } if (!this.headers['content-type']) { this._error(new Error('bad content-type header, no content-type')); return; } if (this.headers['content-type'].match(/octet-stream/i)) { this._initOctetStream(); return; } if (this.headers['content-type'].match(/urlencoded/i)) { this._initUrlencoded(); return; } if (this.headers['content-type'].match(/multipart/i)) { var m; if (m = this.headers['content-type'].match(/boundary=(?:"([^"]+)"|([^;]+))/i)) { this._initMultipart(m[1] || m[2]); } else { this._error(new Error('bad content-type header, no multipart boundary')); } return; } if (this.headers['content-type'].match(/json/i)) { this._initJSONencoded(); return; } this._error(new Error('bad content-type header, unknown content-type: '+this.headers['content-type'])); }; IncomingForm.prototype._error = function(err) { if (this.error || this.ended) { return; } this.error = err; this.pause(); this.emit('error', err); if (Array.isArray(this.openedFiles)) { this.openedFiles.forEach(function(file) { file._writeStream.destroy(); setTimeout(fs.unlink, 0, file.path); }); } }; IncomingForm.prototype._parseContentLength = function() { if (this.headers['content-length']) { this.bytesReceived = 0; this.bytesExpected = parseInt(this.headers['content-length'], 10); this.emit('progress', this.bytesReceived, this.bytesExpected); } }; IncomingForm.prototype._newParser = function() { return new MultipartParser(); }; IncomingForm.prototype._initMultipart = function(boundary) { this.type = 'multipart'; var parser = new MultipartParser(), self = this, headerField, headerValue, part; parser.initWithBoundary(boundary); parser.onPartBegin = function() { part = new Stream(); part.readable = true; part.headers = {}; part.name = null; part.filename = null; part.mime = null; part.transferEncoding = 'binary'; part.transferBuffer = ''; headerField = ''; headerValue = ''; }; parser.onHeaderField = function(b, start, end) { headerField += b.toString(self.encoding, start, end); }; parser.onHeaderValue = function(b, start, end) { headerValue += b.toString(self.encoding, start, end); }; parser.onHeaderEnd = function() { headerField = headerField.toLowerCase(); part.headers[headerField] = headerValue; var m; if (headerField == 'content-disposition') { if (m = headerValue.match(/\bname="([^"]+)"/i)) { part.name = m[1]; } part.filename = self._fileName(headerValue); } else if (headerField == 'content-type') { part.mime = headerValue; } else if (headerField == 'content-transfer-encoding') { part.transferEncoding = headerValue.toLowerCase(); } headerField = ''; headerValue = ''; }; parser.onHeadersEnd = function() { switch(part.transferEncoding){ case 'binary': case '7bit': case '8bit': parser.onPartData = function(b, start, end) { part.emit('data', b.slice(start, end)); }; parser.onPartEnd = function() { part.emit('end'); }; break; case 'base64': parser.onPartData = function(b, start, end) { part.transferBuffer += b.slice(start, end).toString('ascii'); /* four bytes (chars) in base64 converts to three bytes in binary encoding. So we should always work with a number of bytes that can be divided by 4, it will result in a number of buytes that can be divided vy 3. */ var offset = parseInt(part.transferBuffer.length / 4) * 4; part.emit('data', new Buffer(part.transferBuffer.substring(0, offset), 'base64')) part.transferBuffer = part.transferBuffer.substring(offset); }; parser.onPartEnd = function() { part.emit('data', new Buffer(part.transferBuffer, 'base64')) part.emit('end'); }; break; default: return self._error(new Error('unknown transfer-encoding')); } self.onPart(part); }; parser.onEnd = function() { self.ended = true; self._maybeEnd(); }; this._parser = parser; }; IncomingForm.prototype._fileName = function(headerValue) { var m = headerValue.match(/\bfilename="(.*?)"($|; )/i); if (!m) return; var filename = m[1].substr(m[1].lastIndexOf('\\') + 1); filename = filename.replace(/%22/g, '"'); filename = filename.replace(/([\d]{4});/g, function(m, code) { return String.fromCharCode(code); }); return filename; }; IncomingForm.prototype._initUrlencoded = function() { this.type = 'urlencoded'; var parser = new QuerystringParser(this.maxFields) , self = this; parser.onField = function(key, val) { self.emit('field', key, val); }; parser.onEnd = function() { self.ended = true; self._maybeEnd(); }; this._parser = parser; }; IncomingForm.prototype._initOctetStream = function() { this.type = 'octet-stream'; var filename = this.headers['x-file-name']; var mime = this.headers['content-type']; var file = new File({ path: this._uploadPath(filename), name: filename, type: mime }); file.open(); this.emit('fileBegin', filename, file); this._flushing++; var self = this; self._parser = new OctetParser(); //Keep track of writes that haven't finished so we don't emit the file before it's done being written var outstandingWrites = 0; self._parser.on('data', function(buffer){ self.pause(); outstandingWrites++; file.write(buffer, function() { outstandingWrites--; self.resume(); if(self.ended){ self._parser.emit('doneWritingFile'); } }); }); self._parser.on('end', function(){ self._flushing--; self.ended = true; var done = function(){ self.emit('file', 'file', file); self._maybeEnd(); }; if(outstandingWrites === 0){ done(); } else { self._parser.once('doneWritingFile', done); } }); }; IncomingForm.prototype._initJSONencoded = function() { this.type = 'json'; var parser = new JSONParser() , self = this; if (this.bytesExpected) { parser.initWithLength(this.bytesExpected); } parser.onField = function(key, val) { self.emit('field', key, val); } parser.onEnd = function() { self.ended = true; self._maybeEnd(); }; this._parser = parser; }; IncomingForm.prototype._uploadPath = function(filename) { var name = ''; for (var i = 0; i < 32; i++) { name += Math.floor(Math.random() * 16).toString(16); } if (this.keepExtensions) { var ext = path.extname(filename); ext = ext.replace(/(\.[a-z0-9]+).*/, '$1'); name += ext; } return path.join(this.uploadDir, name); }; IncomingForm.prototype._maybeEnd = function() { if (!this.ended || this._flushing || this.error) { return; } this.emit('end'); }; package/lib/index.js 000644 000765 000024 0000000205 12117571742 012746 0 ustar 00 000000 000000 var IncomingForm = require('./incoming_form').IncomingForm; IncomingForm.IncomingForm = IncomingForm; module.exports = IncomingForm; package/lib/json_parser.js 000644 000765 000024 0000001500 12117571742 014163 0 ustar 00 000000 000000 if (global.GENTLY) require = GENTLY.hijack(require); var Buffer = require('buffer').Buffer function JSONParser() { this.data = new Buffer(''); this.bytesWritten = 0; }; exports.JSONParser = JSONParser; JSONParser.prototype.initWithLength = function(length) { this.data = new Buffer(length); } JSONParser.prototype.write = function(buffer) { if (this.data.length >= this.bytesWritten + buffer.length) { buffer.copy(this.data, this.bytesWritten); } else { this.data = Buffer.concat([this.data, buffer]); } this.bytesWritten += buffer.length; return buffer.length; } JSONParser.prototype.end = function() { try { var fields = JSON.parse(this.data.toString('utf8')) for (var field in fields) { this.onField(field, fields[field]); } } catch (e) {} this.data = null; this.onEnd(); } package/lib/multipart_parser.js 000644 000765 000024 0000017765 12117571742 015257 0 ustar 00 000000 000000 var Buffer = require('buffer').Buffer, s = 0, S = { PARSER_UNINITIALIZED: s++, START: s++, START_BOUNDARY: s++, HEADER_FIELD_START: s++, HEADER_FIELD: s++, HEADER_VALUE_START: s++, HEADER_VALUE: s++, HEADER_VALUE_ALMOST_DONE: s++, HEADERS_ALMOST_DONE: s++, PART_DATA_START: s++, PART_DATA: s++, PART_END: s++, END: s++ }, f = 1, F = { PART_BOUNDARY: f, LAST_BOUNDARY: f *= 2 }, LF = 10, CR = 13, SPACE = 32, HYPHEN = 45, COLON = 58, A = 97, Z = 122, lower = function(c) { return c | 0x20; }; for (s in S) { exports[s] = S[s]; } function MultipartParser() { this.boundary = null; this.boundaryChars = null; this.lookbehind = null; this.state = S.PARSER_UNINITIALIZED; this.index = null; this.flags = 0; }; exports.MultipartParser = MultipartParser; MultipartParser.stateToString = function(stateNumber) { for (var state in S) { var number = S[state]; if (number === stateNumber) return state; } }; MultipartParser.prototype.initWithBoundary = function(str) { this.boundary = new Buffer(str.length+4); this.boundary.write('\r\n--', 'ascii', 0); this.boundary.write(str, 'ascii', 4); this.lookbehind = new Buffer(this.boundary.length+8); this.state = S.START; this.boundaryChars = {}; for (var i = 0; i < this.boundary.length; i++) { this.boundaryChars[this.boundary[i]] = true; } }; MultipartParser.prototype.write = function(buffer) { var self = this, i = 0, len = buffer.length, prevIndex = this.index, index = this.index, state = this.state, flags = this.flags, lookbehind = this.lookbehind, boundary = this.boundary, boundaryChars = this.boundaryChars, boundaryLength = this.boundary.length, boundaryEnd = boundaryLength - 1, bufferLength = buffer.length, c, cl, mark = function(name) { self[name+'Mark'] = i; }, clear = function(name) { delete self[name+'Mark']; }, callback = function(name, buffer, start, end) { if (start !== undefined && start === end) { return; } var callbackSymbol = 'on'+name.substr(0, 1).toUpperCase()+name.substr(1); if (callbackSymbol in self) { self[callbackSymbol](buffer, start, end); } }, dataCallback = function(name, clear) { var markSymbol = name+'Mark'; if (!(markSymbol in self)) { return; } if (!clear) { callback(name, buffer, self[markSymbol], buffer.length); self[markSymbol] = 0; } else { callback(name, buffer, self[markSymbol], i); delete self[markSymbol]; } }; for (i = 0; i < len; i++) { c = buffer[i]; switch (state) { case S.PARSER_UNINITIALIZED: return i; case S.START: index = 0; state = S.START_BOUNDARY; case S.START_BOUNDARY: if (index == boundary.length - 2) { if (c != CR) { return i; } index++; break; } else if (index - 1 == boundary.length - 2) { if (c != LF) { return i; } index = 0; callback('partBegin'); state = S.HEADER_FIELD_START; break; } if (c != boundary[index+2]) { index = -2; } if (c == boundary[index+2]) { index++; } break; case S.HEADER_FIELD_START: state = S.HEADER_FIELD; mark('headerField'); index = 0; case S.HEADER_FIELD: if (c == CR) { clear('headerField'); state = S.HEADERS_ALMOST_DONE; break; } index++; if (c == HYPHEN) { break; } if (c == COLON) { if (index == 1) { // empty header field return i; } dataCallback('headerField', true); state = S.HEADER_VALUE_START; break; } cl = lower(c); if (cl < A || cl > Z) { return i; } break; case S.HEADER_VALUE_START: if (c == SPACE) { break; } mark('headerValue'); state = S.HEADER_VALUE; case S.HEADER_VALUE: if (c == CR) { dataCallback('headerValue', true); callback('headerEnd'); state = S.HEADER_VALUE_ALMOST_DONE; } break; case S.HEADER_VALUE_ALMOST_DONE: if (c != LF) { return i; } state = S.HEADER_FIELD_START; break; case S.HEADERS_ALMOST_DONE: if (c != LF) { return i; } callback('headersEnd'); state = S.PART_DATA_START; break; case S.PART_DATA_START: state = S.PART_DATA; mark('partData'); case S.PART_DATA: prevIndex = index; if (index == 0) { // boyer-moore derrived algorithm to safely skip non-boundary data i += boundaryEnd; while (i < bufferLength && !(buffer[i] in boundaryChars)) { i += boundaryLength; } i -= boundaryEnd; c = buffer[i]; } if (index < boundary.length) { if (boundary[index] == c) { if (index == 0) { dataCallback('partData', true); } index++; } else { index = 0; } } else if (index == boundary.length) { index++; if (c == CR) { // CR = part boundary flags |= F.PART_BOUNDARY; } else if (c == HYPHEN) { // HYPHEN = end boundary flags |= F.LAST_BOUNDARY; } else { index = 0; } } else if (index - 1 == boundary.length) { if (flags & F.PART_BOUNDARY) { index = 0; if (c == LF) { // unset the PART_BOUNDARY flag flags &= ~F.PART_BOUNDARY; callback('partEnd'); callback('partBegin'); state = S.HEADER_FIELD_START; break; } } else if (flags & F.LAST_BOUNDARY) { if (c == HYPHEN) { callback('partEnd'); callback('end'); state = S.END; } else { index = 0; } } else { index = 0; } } if (index > 0) { // when matching a possible boundary, keep a lookbehind reference // in case it turns out to be a false lead lookbehind[index-1] = c; } else if (prevIndex > 0) { // if our boundary turned out to be rubbish, the captured lookbehind // belongs to partData callback('partData', lookbehind, 0, prevIndex); prevIndex = 0; mark('partData'); // reconsider the current character even so it interrupted the sequence // it could be the beginning of a new sequence i--; } break; case S.END: break; default: return i; } } dataCallback('headerField'); dataCallback('headerValue'); dataCallback('partData'); this.index = index; this.state = state; this.flags = flags; return len; }; MultipartParser.prototype.end = function() { var callback = function(self, name) { var callbackSymbol = 'on'+name.substr(0, 1).toUpperCase()+name.substr(1); if (callbackSymbol in self) { self[callbackSymbol](); } }; if ((this.state == S.HEADER_FIELD_START && this.index == 0) || (this.state == S.PART_DATA && this.index == this.boundary.length)) { callback(this, 'partEnd'); callback(this, 'end'); } else if (this.state != S.END) { return new Error('MultipartParser.end(): stream ended unexpectedly: ' + this.explain()); } }; MultipartParser.prototype.explain = function() { return 'state = ' + MultipartParser.stateToString(this.state); }; package/lib/octet_parser.js 000644 000765 000024 0000000710 12117571742 014332 0 ustar 00 000000 000000 var EventEmitter = require('events').EventEmitter , util = require('util'); function OctetParser(options){ if(!(this instanceof OctetParser)) return new OctetParser(options); EventEmitter.call(this); } util.inherits(OctetParser, EventEmitter); exports.OctetParser = OctetParser; OctetParser.prototype.write = function(buffer) { this.emit('data', buffer); return buffer.length; }; OctetParser.prototype.end = function() { this.emit('end'); }; package/lib/querystring_parser.js 000644 000765 000024 0000001345 12117571742 015615 0 ustar 00 000000 000000 if (global.GENTLY) require = GENTLY.hijack(require); // This is a buffering parser, not quite as nice as the multipart one. // If I find time I'll rewrite this to be fully streaming as well var querystring = require('querystring'); function QuerystringParser(maxKeys) { this.maxKeys = maxKeys; this.buffer = ''; }; exports.QuerystringParser = QuerystringParser; QuerystringParser.prototype.write = function(buffer) { this.buffer += buffer.toString('ascii'); return buffer.length; }; QuerystringParser.prototype.end = function() { var fields = querystring.parse(this.buffer, '&', '=', { maxKeys: this.maxKeys }); for (var field in fields) { this.onField(field, fields[field]); } this.buffer = ''; this.onEnd(); }; package/Readme.md 000644 000765 000024 0000031074 12121143370 012245 0 ustar 00 000000 000000 # Formidable [](http://travis-ci.org/felixge/node-formidable) ## Purpose A node.js module for parsing form data, especially file uploads. ## Current status This module was developed for [Transloadit](http://transloadit.com/), a service focused on uploading and encoding images and videos. It has been battle-tested against hundreds of GB of file uploads from a large variety of clients and is considered production-ready. ## Features * Fast (~500mb/sec), non-buffering multipart parser * Automatically writing file uploads to disk * Low memory footprint * Graceful error handling * Very high test coverage ## Installation Via [npm](http://github.com/isaacs/npm): ``` npm install formidable@latest ``` Manually: ``` git clone git://github.com/felixge/node-formidable.git formidable vim my.js # var formidable = require('./formidable'); ``` Note: Formidable requires [gently](http://github.com/felixge/node-gently) to run the unit tests, but you won't need it for just using the library. ## Example Parse an incoming file upload. ```javascript var formidable = require('formidable'), http = require('http'), util = require('util'); http.createServer(function(req, res) { if (req.url == '/upload' && req.method.toLowerCase() == 'post') { // parse a file upload var form = new formidable.IncomingForm(); form.parse(req, function(err, fields, files) { res.writeHead(200, {'content-type': 'text/plain'}); res.write('received upload:\n\n'); res.end(util.inspect({fields: fields, files: files})); }); return; } // show a file upload form res.writeHead(200, {'content-type': 'text/html'}); res.end( '' ); }).listen(8080); ``` ## API ### Formidable.IncomingForm ```javascript var form = new formidable.IncomingForm() ``` Creates a new incoming form. ```javascript form.encoding = 'utf-8'; ``` Sets encoding for incoming form fields. ```javascript form.uploadDir = process.env.TMP || process.env.TMPDIR || process.env.TEMP || '/tmp' || process.cwd(); ``` The directory for placing file uploads in. You can move them later on using `fs.rename()`. The default directory is picked at module load time depending on the first existing directory from those listed above. ```javascript form.keepExtensions = false; ``` If you want the files written to `form.uploadDir` to include the extensions of the original files, set this property to `true`. ```javascript form.type ``` Either 'multipart' or 'urlencoded' depending on the incoming request. ```javascript form.maxFieldsSize = 2 * 1024 * 1024; ``` Limits the amount of memory a field (not file) can allocate in bytes. If this value is exceeded, an `'error'` event is emitted. The default size is 2MB. ```javascript form.maxFields = 0; ``` Limits the number of fields that the querystring parser will decode. Defaults to 0 (unlimited). ```javascript form.hash = false; ``` If you want checksums calculated for incoming files, set this to either `'sha1'` or `'md5'`. ```javascript form.bytesReceived ``` The amount of bytes received for this form so far. ```javascript form.bytesExpected ``` The expected number of bytes in this form. ```javascript form.parse(request, [cb]); ``` Parses an incoming node.js `request` containing form data. If `cb` is provided, all fields an files are collected and passed to the callback: ```javascript form.parse(req, function(err, fields, files) { // ... }); form.onPart(part); ``` You may overwrite this method if you are interested in directly accessing the multipart stream. Doing so will disable any `'field'` / `'file'` events processing which would occur otherwise, making you fully responsible for handling the processing. ```javascript form.onPart = function(part) { part.addListener('data', function() { // ... }); } ``` If you want to use formidable to only handle certain parts for you, you can do so: ```javascript form.onPart = function(part) { if (!part.filename) { // let formidable handle all non-file parts form.handlePart(part); } } ``` Check the code in this method for further inspiration. ### Formidable.File ```javascript file.size = 0 ``` The size of the uploaded file in bytes. If the file is still being uploaded (see `'fileBegin'` event), this property says how many bytes of the file have been written to disk yet. ```javascript file.path = null ``` The path this file is being written to. You can modify this in the `'fileBegin'` event in case you are unhappy with the way formidable generates a temporary path for your files. ```javascript file.name = null ``` The name this file had according to the uploading client. ```javascript file.type = null ``` The mime type of this file, according to the uploading client. ```javascript file.lastModifiedDate = null ``` A date object (or `null`) containing the time this file was last written to. Mostly here for compatibility with the [W3C File API Draft](http://dev.w3.org/2006/webapi/FileAPI/). ```javascript file.hash = null ``` If hash calculation was set, you can read the hex digest out of this var. #### Formidable.File#toJSON() This method returns a JSON-representation of the file, allowing you to `JSON.stringify()` the file which is useful for logging and responding to requests. ### Events #### 'progress' ```javascript form.on('progress', function(bytesReceived, bytesExpected) { }); ``` Emitted after each incoming chunk of data that has been parsed. Can be used to roll your own progress bar. #### 'field' ```javascript form.on('field', function(name, value) { }); ``` #### 'fileBegin' Emitted whenever a field / value pair has been received. ```javascript form.on('fileBegin', function(name, file) { }); ``` #### 'file' Emitted whenever a new file is detected in the upload stream. Use this even if you want to stream the file to somewhere else while buffering the upload on the file system. Emitted whenever a field / file pair has been received. `file` is an instance of `File`. ```javascript form.on('file', function(name, file) { }); ``` #### 'error' Emitted when there is an error processing the incoming form. A request that experiences an error is automatically paused, you will have to manually call `request.resume()` if you want the request to continue firing `'data'` events. ```javascript form.on('error', function(err) { }); ``` #### 'aborted' Emitted when the request was aborted by the user. Right now this can be due to a 'timeout' or 'close' event on the socket. In the future there will be a separate 'timeout' event (needs a change in the node core). ```javascript form.on('aborted', function() { }); ``` ##### 'end' ```javascript form.on('end', function() { }); ``` Emitted when the entire request has been received, and all contained files have finished flushing to disk. This is a great place for you to send your response. ## Changelog ### v1.0.13 * Only update hash if update method exists (Sven Lito) * According to travis v0.10 needs to go quoted (Sven Lito) * Bumping build node versions (Sven Lito) * Change the default to 1000, to match the new Node behaviour. (OrangeDog) * Add ability to control maxKeys in the querystring parser. (OrangeDog) * Adjust test case to work with node 0.9.x (Eugene Girshov) * Update package.json (Sven Lito) * Path adjustment according to eb4468b (Markus Ast) ### v1.0.12 * Emit error on aborted connections (Eugene Girshov) * Add support for empty requests (Eugene Girshov) * Fix name/filename handling in Content-Disposition (jesperp) * Tolerate malformed closing boundary in multipart (Eugene Girshov) * Ignore preamble in multipart messages (Eugene Girshov) * Add support for application/json (Mike Frey, Carlos Rodriguez) * Add support for Base64 encoding (Elmer Bulthuis) * Add File#toJSON (TJ Holowaychuk) * Remove support for Node.js 0.4 & 0.6 (Andrew Kelley) * Documentation improvements (Sven Lito, Andre Azevedo) * Add support for application/octet-stream (Ion Lupascu, Chris Scribner) * Use os.tmpDir() to get tmp directory (Andrew Kelley) * Improve package.json (Andrew Kelley, Sven Lito) * Fix benchmark script (Andrew Kelley) * Fix scope issue in incoming_forms (Sven Lito) * Fix file handle leak on error (OrangeDog) ### v1.0.11 * Calculate checksums for incoming files (sreuter) * Add definition parameters to "IncomingForm" as an argument (Math-) ### v1.0.10 * Make parts to be proper Streams (Matt Robenolt) ### v1.0.9 * Emit progress when content length header parsed (Tim Koschützki) * Fix Readme syntax due to GitHub changes (goob) * Replace references to old 'sys' module in Readme with 'util' (Peter Sugihara) ### v1.0.8 * Strip potentially unsafe characters when using `keepExtensions: true`. * Switch to utest / urun for testing * Add travis build ### v1.0.7 * Remove file from package that was causing problems when installing on windows. (#102) * Fix typos in Readme (Jason Davies). ### v1.0.6 * Do not default to the default to the field name for file uploads where filename="". ### v1.0.5 * Support filename="" in multipart parts * Explain unexpected end() errors in parser better **Note:** Starting with this version, formidable emits 'file' events for empty file input fields. Previously those were incorrectly emitted as regular file input fields with value = "". ### v1.0.4 * Detect a good default tmp directory regardless of platform. (#88) ### v1.0.3 * Fix problems with utf8 characters (#84) / semicolons in filenames (#58) * Small performance improvements * New test suite and fixture system ### v1.0.2 * Exclude node\_modules folder from git * Implement new `'aborted'` event * Fix files in example folder to work with recent node versions * Make gently a devDependency [See Commits](https://github.com/felixge/node-formidable/compare/v1.0.1...v1.0.2) ### v1.0.1 * Fix package.json to refer to proper main directory. (#68, Dean Landolt) [See Commits](https://github.com/felixge/node-formidable/compare/v1.0.0...v1.0.1) ### v1.0.0 * Add support for multipart boundaries that are quoted strings. (Jeff Craig) This marks the beginning of development on version 2.0 which will include several architectural improvements. [See Commits](https://github.com/felixge/node-formidable/compare/v0.9.11...v1.0.0) ### v0.9.11 * Emit `'progress'` event when receiving data, regardless of parsing it. (Tim Koschützki) * Use [W3C FileAPI Draft](http://dev.w3.org/2006/webapi/FileAPI/) properties for File class **Important:** The old property names of the File class will be removed in a future release. [See Commits](https://github.com/felixge/node-formidable/compare/v0.9.10...v0.9.11) ### Older releases These releases were done before starting to maintain the above Changelog: * [v0.9.10](https://github.com/felixge/node-formidable/compare/v0.9.9...v0.9.10) * [v0.9.9](https://github.com/felixge/node-formidable/compare/v0.9.8...v0.9.9) * [v0.9.8](https://github.com/felixge/node-formidable/compare/v0.9.7...v0.9.8) * [v0.9.7](https://github.com/felixge/node-formidable/compare/v0.9.6...v0.9.7) * [v0.9.6](https://github.com/felixge/node-formidable/compare/v0.9.5...v0.9.6) * [v0.9.5](https://github.com/felixge/node-formidable/compare/v0.9.4...v0.9.5) * [v0.9.4](https://github.com/felixge/node-formidable/compare/v0.9.3...v0.9.4) * [v0.9.3](https://github.com/felixge/node-formidable/compare/v0.9.2...v0.9.3) * [v0.9.2](https://github.com/felixge/node-formidable/compare/v0.9.1...v0.9.2) * [v0.9.1](https://github.com/felixge/node-formidable/compare/v0.9.0...v0.9.1) * [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0) * [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0) * [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0) * [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0) * [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0) * [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0) * [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0) * [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0) * [v0.1.0](https://github.com/felixge/node-formidable/commits/v0.1.0) ## License Formidable is licensed under the MIT license. ## Ports * [multipart-parser](http://github.com/FooBarWidget/multipart-parser): a C++ parser based on formidable ## Credits * [Ryan Dahl](http://twitter.com/ryah) for his work on [http-parser](http://github.com/ry/http-parser) which heavily inspired multipart_parser.js package/test/common.js 000644 000765 000024 0000000602 12117571742 013341 0 ustar 00 000000 000000 var path = require('path'); var root = path.join(__dirname, '../'); exports.dir = { root : root, lib : root + '/lib', fixture : root + '/test/fixture', tmp : root + '/test/tmp', }; exports.port = 13532; exports.formidable = require('..'); exports.assert = require('assert'); exports.require = function(lib) { return require(exports.dir.lib + '/' + lib); }; package/test/run.js 000755 000765 000024 0000000033 12117571742 012656 0 ustar 00 000000 000000 require('urun')(__dirname) package/test/fixture/multipart.js 000644 000765 000024 0000004016 12117571742 015563 0 ustar 00 000000 000000 exports['rfc1867'] = { boundary: 'AaB03x', raw: '--AaB03x\r\n'+ 'content-disposition: form-data; name="field1"\r\n'+ '\r\n'+ 'Joe Blow\r\nalmost tricked you!\r\n'+ '--AaB03x\r\n'+ 'content-disposition: form-data; name="pics"; filename="file1.txt"\r\n'+ 'Content-Type: text/plain\r\n'+ '\r\n'+ '... contents of file1.txt ...\r\r\n'+ '--AaB03x--\r\n', parts: [ { headers: { 'content-disposition': 'form-data; name="field1"', }, data: 'Joe Blow\r\nalmost tricked you!', }, { headers: { 'content-disposition': 'form-data; name="pics"; filename="file1.txt"', 'Content-Type': 'text/plain', }, data: '... contents of file1.txt ...\r', } ] }; exports['noTrailing\r\n'] = { boundary: 'AaB03x', raw: '--AaB03x\r\n'+ 'content-disposition: form-data; name="field1"\r\n'+ '\r\n'+ 'Joe Blow\r\nalmost tricked you!\r\n'+ '--AaB03x\r\n'+ 'content-disposition: form-data; name="pics"; filename="file1.txt"\r\n'+ 'Content-Type: text/plain\r\n'+ '\r\n'+ '... contents of file1.txt ...\r\r\n'+ '--AaB03x--', parts: [ { headers: { 'content-disposition': 'form-data; name="field1"', }, data: 'Joe Blow\r\nalmost tricked you!', }, { headers: { 'content-disposition': 'form-data; name="pics"; filename="file1.txt"', 'Content-Type': 'text/plain', }, data: '... contents of file1.txt ...\r', } ] }; exports['emptyHeader'] = { boundary: 'AaB03x', raw: '--AaB03x\r\n'+ 'content-disposition: form-data; name="field1"\r\n'+ ': foo\r\n'+ '\r\n'+ 'Joe Blow\r\nalmost tricked you!\r\n'+ '--AaB03x\r\n'+ 'content-disposition: form-data; name="pics"; filename="file1.txt"\r\n'+ 'Content-Type: text/plain\r\n'+ '\r\n'+ '... contents of file1.txt ...\r\r\n'+ '--AaB03x--\r\n', expectError: true, }; package/test/fixture/file/beta-sticker-1.png 000644 000765 000024 0000003174 12117571742 017350 0 ustar 00 000000 000000 PNG IHDR $ $ tEXtSoftware Adobe ImageReadyqe<