package/package.json000644 000765 000024 0000001371 12121143632013012 0ustar00000000 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/.npmignore000644 000765 000024 0000000041 12117571742012530 0ustar00000000 000000 /test/tmp/ *.upload *.un~ *.http package/LICENSE000644 000765 000024 0000002047 12117571742011546 0ustar00000000 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.js000644 000765 000024 0000000042 12117571742012177 0ustar00000000 000000 module.exports = require('./lib');package/benchmark/bench-multipart-parser.js000644 000765 000024 0000003210 12117571742017412 0ustar00000000 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.yml000644 000765 000024 0000000066 12117602754012647 0ustar00000000 000000 language: node_js node_js: - 0.8 - 0.9 - "0.10" package/tool/record.js000644 000765 000024 0000002360 12117571742013330 0ustar00000000 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.js000644 000765 000024 0000003020 12121124047012537 0ustar00000000 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.js000644 000765 000024 0000027716 12117571742014505 0ustar00000000 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.js000644 000765 000024 0000000205 12117571742012746 0ustar00000000 000000 var IncomingForm = require('./incoming_form').IncomingForm; IncomingForm.IncomingForm = IncomingForm; module.exports = IncomingForm; package/lib/json_parser.js000644 000765 000024 0000001500 12117571742014163 0ustar00000000 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.js000644 000765 000024 0000017765 12117571742015257 0ustar00000000 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.js000644 000765 000024 0000000710 12117571742014332 0ustar00000000 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.js000644 000765 000024 0000001345 12117571742015615 0ustar00000000 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.md000644 000765 000024 0000031074 12121143370012245 0ustar00000000 000000 # Formidable [![Build Status](https://secure.travis-ci.org/felixge/node-formidable.png?branch=master)](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.js000644 000765 000024 0000000602 12117571742013341 0ustar00000000 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.js000755 000765 000024 0000000033 12117571742012656 0ustar00000000 000000 require('urun')(__dirname) package/test/fixture/multipart.js000644 000765 000024 0000004016 12117571742015563 0ustar00000000 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.png000644 000765 000024 0000003174 12117571742017350 0ustar00000000 000000 PNG  IHDR$$tEXtSoftwareAdobe ImageReadyqe<IDATx̘{HGg#pjĈM5D W?bPhӢi[i)H-O?>>~رcw`qs>&A0eOEEɨ*JOHAYYXX0vwwwttX`yr `0xMMM)A(-˕Ǝ[n=%¹EPj31_p!磣K:G<ຬݻg9&_1%0/^---tIž-(((}ihhEOOj744Ftyyy9fy`~~ECot gB5~K ى?!mmm_FiOGzzzyeee L#5E VD¼vaKAR}iLsX" ?P"K.ek4ƆQoY1%~RS u~+Ⱦ^4_$d@>xA/:<IENDB`package/test/fixture/file/binaryfile.tar.gz000644 000765 000024 0000000455 12117571742017401 0ustar00000000 000000 h!On0`}eD1ދ8jV3c| mYve-wS@ʽuID,̗bv*2b!2>UĽ4ut^YWZ]G]fFՔ>tZW8PYK ӊZ2R ^>B [2p3.!hSiǒ!)ngC~G5.΢-qElx1J13[~Wĺq6ӽLSu]/ċ(package/test/fixture/file/blank.gif000755 000765 000024 0000000061 12117571742015700 0ustar00000000 000000 GIF89a!,T;package/test/fixture/file/funkyfilename.txt000644 000765 000024 0000000044 12117571742017516 0ustar00000000 000000 I am a text file with a funky name! package/test/fixture/file/menu_separator.png000644 000765 000024 0000001643 12117571742017660 0ustar00000000 000000 PNG  IHDR_tEXtSoftwareAdobe ImageReadyqe<"iTXtXML:com.adobe.xmp kmIDATxbqbbKVZIENDB`package/test/fixture/file/plain.txt000644 000765 000024 0000000027 12117571742015765 0ustar00000000 000000 I am a plain text file package/test/fixture/http/special-chars-in-filename/info.md000644 000765 000024 0000000343 12117571742022337 0ustar00000000 000000 * Opera does not allow submitting this file, it shows a warning to the user that the file could not be found instead. Tested in 9.8, 11.51 on OSX. Reported to Opera on 08.09.2011 (tracking email DSK-346009@bugs.opera.com). package/test/fixture/js/encoding.js000644 000765 000024 0000001232 12117571742015741 0ustar00000000 000000 module.exports['menu_seperator.png.http'] = [ {type: 'file', name: 'image', filename: 'menu_separator.png', fixture: 'menu_separator.png'} ]; module.exports['beta-sticker-1.png.http'] = [ {type: 'file', name: 'sticker', filename: 'beta-sticker-1.png', fixture: 'beta-sticker-1.png'} ]; module.exports['blank.gif.http'] = [ {type: 'file', name: 'file', filename: 'blank.gif', fixture: 'blank.gif'} ]; module.exports['binaryfile.tar.gz.http'] = [ {type: 'file', name: 'file', filename: 'binaryfile.tar.gz', fixture: 'binaryfile.tar.gz'} ]; module.exports['plain.txt.http'] = [ {type: 'file', name: 'file', filename: 'plain.txt', fixture: 'plain.txt'} ]; package/test/fixture/js/misc.js000644 000765 000024 0000000147 12117571742015112 0ustar00000000 000000 module.exports = { 'empty.http': [], 'empty-urlencoded.http': [], 'empty-multipart.http': [], }; package/test/fixture/js/no-filename.js000644 000765 000024 0000000350 12117571742016345 0ustar00000000 000000 module.exports['generic.http'] = [ {type: 'file', name: 'upload', filename: '', fixture: 'plain.txt'}, ]; module.exports['filename-name.http'] = [ {type: 'file', name: 'upload', filename: 'plain.txt', fixture: 'plain.txt'}, ]; package/test/fixture/js/preamble.js000644 000765 000024 0000000351 12117571742015743 0ustar00000000 000000 module.exports['crlf.http'] = [ {type: 'file', name: 'upload', filename: 'plain.txt', fixture: 'plain.txt'}, ]; module.exports['preamble.http'] = [ {type: 'file', name: 'upload', filename: 'plain.txt', fixture: 'plain.txt'}, ]; package/test/fixture/js/special-chars-in-filename.js000644 000765 000024 0000001313 12117571742021053 0ustar00000000 000000 var properFilename = 'funkyfilename.txt'; function expect(filename) { return [ {type: 'field', name: 'title', value: 'Weird filename'}, {type: 'file', name: 'upload', filename: filename, fixture: properFilename}, ]; }; var webkit = " ? % * | \" < > . ? ; ' @ # $ ^ & ( ) - _ = + { } [ ] ` ~.txt"; var ffOrIe = " ? % * | \" < > . ☃ ; ' @ # $ ^ & ( ) - _ = + { } [ ] ` ~.txt"; module.exports = { 'osx-chrome-13.http' : expect(webkit), 'osx-firefox-3.6.http' : expect(ffOrIe), 'osx-safari-5.http' : expect(webkit), 'xp-chrome-12.http' : expect(webkit), 'xp-ie-7.http' : expect(ffOrIe), 'xp-ie-8.http' : expect(ffOrIe), 'xp-safari-5.http' : expect(webkit), }; package/test/fixture/js/workarounds.js000644 000765 000024 0000000374 12117571742016537 0ustar00000000 000000 module.exports['missing-hyphens1.http'] = [ {type: 'file', name: 'upload', filename: 'plain.txt', fixture: 'plain.txt'}, ]; module.exports['missing-hyphens2.http'] = [ {type: 'file', name: 'upload', filename: 'plain.txt', fixture: 'plain.txt'}, ]; package/test/integration/test-fixtures.js000644 000765 000024 0000004440 12117571742017226 0ustar00000000 000000 var hashish = require('hashish'); var fs = require('fs'); var findit = require('findit'); var path = require('path'); var http = require('http'); var net = require('net'); var assert = require('assert'); var common = require('../common'); var formidable = common.formidable; var server = http.createServer(); server.listen(common.port, findFixtures); function findFixtures() { var fixtures = []; findit .sync(common.dir.fixture + '/js') .forEach(function(jsPath) { if (!/\.js$/.test(jsPath)) return; var group = path.basename(jsPath, '.js'); hashish.forEach(require(jsPath), function(fixture, name) { fixtures.push({ name : group + '/' + name, fixture : fixture, }); }); }); testNext(fixtures); } function testNext(fixtures) { var fixture = fixtures.shift(); if (!fixture) return server.close(); var name = fixture.name; var fixture = fixture.fixture; uploadFixture(name, function(err, parts) { if (err) throw err; fixture.forEach(function(expectedPart, i) { var parsedPart = parts[i]; assert.equal(parsedPart.type, expectedPart.type); assert.equal(parsedPart.name, expectedPart.name); if (parsedPart.type === 'file') { var filename = parsedPart.value.name; assert.equal(filename, expectedPart.filename); } }); testNext(fixtures); }); }; function uploadFixture(name, cb) { server.once('request', function(req, res) { var form = new formidable.IncomingForm(); form.uploadDir = common.dir.tmp; form.parse(req); function callback() { var realCallback = cb; cb = function() {}; realCallback.apply(null, arguments); } var parts = []; form .on('error', callback) .on('fileBegin', function(name, value) { parts.push({type: 'file', name: name, value: value}); }) .on('field', function(name, value) { parts.push({type: 'field', name: name, value: value}); }) .on('end', function() { res.end('OK'); callback(null, parts); }); }); var socket = net.createConnection(common.port); var file = fs.createReadStream(common.dir.fixture + '/http/' + name); file.pipe(socket, {end: false}); socket.on('data', function () { socket.end(); }); } package/test/integration/test-json.js000644 000765 000024 0000001414 12117571742016324 0ustar00000000 000000 var common = require('../common'); var formidable = common.formidable; var http = require('http'); var assert = require('assert'); var testData = { numbers: [1, 2, 3, 4, 5], nested: { key: 'value' } }; var server = http.createServer(function(req, res) { var form = new formidable.IncomingForm(); form.parse(req, function(err, fields, files) { assert.deepEqual(fields, testData); res.end(); server.close(); }); }); var port = common.port; server.listen(port, function(err){ assert.equal(err, null); var request = http.request({ port: port, method: 'POST', headers: { 'Content-Type': 'application/json' } }); request.write(JSON.stringify(testData)); request.end(); }); package/test/integration/test-octet-stream.js000644 000765 000024 0000002105 12117571742017760 0ustar00000000 000000 var common = require('../common'); var formidable = common.formidable; var http = require('http'); var fs = require('fs'); var path = require('path'); var hashish = require('hashish'); var assert = require('assert'); var testFilePath = path.join(__dirname, '../fixture/file/binaryfile.tar.gz'); var server = http.createServer(function(req, res) { var form = new formidable.IncomingForm(); form.parse(req, function(err, fields, files) { assert.equal(hashish(files).length, 1); var file = files.file; assert.equal(file.size, 301); var uploaded = fs.readFileSync(file.path); var original = fs.readFileSync(testFilePath); assert.deepEqual(uploaded, original); res.end(); server.close(); }); }); var port = common.port; server.listen(port, function(err){ assert.equal(err, null); var request = http.request({ port: port, method: 'POST', headers: { 'Content-Type': 'application/octet-stream' } }); fs.createReadStream(testFilePath).pipe(request); }); package/test/legacy/common.js000644 000765 000024 0000001245 12117571742014611 0ustar00000000 000000 var path = require('path'), fs = require('fs'); try { global.Gently = require('gently'); } catch (e) { throw new Error('this test suite requires node-gently'); } exports.lib = path.join(__dirname, '../../lib'); global.GENTLY = new Gently(); global.assert = require('assert'); global.TEST_PORT = 13532; global.TEST_FIXTURES = path.join(__dirname, '../fixture'); global.TEST_TMP = path.join(__dirname, '../tmp'); // Stupid new feature in node that complains about gently attaching too many // listeners to process 'exit'. This is a workaround until I can think of a // better way to deal with this. if (process.setMaxListeners) { process.setMaxListeners(10000); } package/test/legacy/integration/test-multipart-parser.js000644 000765 000024 0000004035 12117571742022134 0ustar00000000 000000 var common = require('../common'); var CHUNK_LENGTH = 10, multipartParser = require(common.lib + '/multipart_parser'), MultipartParser = multipartParser.MultipartParser, parser = new MultipartParser(), fixtures = require(TEST_FIXTURES + '/multipart'), Buffer = require('buffer').Buffer; Object.keys(fixtures).forEach(function(name) { var fixture = fixtures[name], buffer = new Buffer(Buffer.byteLength(fixture.raw, 'binary')), offset = 0, chunk, nparsed, parts = [], part = null, headerField, headerValue, endCalled = ''; parser.initWithBoundary(fixture.boundary); parser.onPartBegin = function() { part = {headers: {}, data: ''}; parts.push(part); headerField = ''; headerValue = ''; }; parser.onHeaderField = function(b, start, end) { headerField += b.toString('ascii', start, end); }; parser.onHeaderValue = function(b, start, end) { headerValue += b.toString('ascii', start, end); } parser.onHeaderEnd = function() { part.headers[headerField] = headerValue; headerField = ''; headerValue = ''; }; parser.onPartData = function(b, start, end) { var str = b.toString('ascii', start, end); part.data += b.slice(start, end); } parser.onEnd = function() { endCalled = true; } buffer.write(fixture.raw, 'binary', 0); while (offset < buffer.length) { if (offset + CHUNK_LENGTH < buffer.length) { chunk = buffer.slice(offset, offset+CHUNK_LENGTH); } else { chunk = buffer.slice(offset, buffer.length); } offset = offset + CHUNK_LENGTH; nparsed = parser.write(chunk); if (nparsed != chunk.length) { if (fixture.expectError) { return; } puts('-- ERROR --'); p(chunk.toString('ascii')); throw new Error(chunk.length+' bytes written, but only '+nparsed+' bytes parsed!'); } } if (fixture.expectError) { throw new Error('expected parse error did not happen'); } assert.ok(endCalled); assert.deepEqual(parts, fixture.parts); }); package/test/legacy/simple/test-file.js000644 000765 000024 0000004414 12117571742016507 0ustar00000000 000000 var common = require('../common'); var WriteStreamStub = GENTLY.stub('fs', 'WriteStream'); var File = require(common.lib + '/file'), EventEmitter = require('events').EventEmitter, file, gently; function test(test) { gently = new Gently(); file = new File(); test(); gently.verify(test.name); } test(function constructor() { assert.ok(file instanceof EventEmitter); assert.strictEqual(file.size, 0); assert.strictEqual(file.path, null); assert.strictEqual(file.name, null); assert.strictEqual(file.type, null); assert.strictEqual(file.lastModifiedDate, null); assert.strictEqual(file._writeStream, null); (function testSetProperties() { var file2 = new File({foo: 'bar'}); assert.equal(file2.foo, 'bar'); })(); }); test(function open() { var WRITE_STREAM; file.path = '/foo'; gently.expect(WriteStreamStub, 'new', function (path) { WRITE_STREAM = this; assert.strictEqual(path, file.path); }); file.open(); assert.strictEqual(file._writeStream, WRITE_STREAM); }); test(function write() { var BUFFER = {length: 10}, CB_STUB, CB = function() { CB_STUB.apply(this, arguments); }; file._writeStream = {}; gently.expect(file._writeStream, 'write', function (buffer, cb) { assert.strictEqual(buffer, BUFFER); gently.expect(file, 'emit', function (event, bytesWritten) { assert.ok(file.lastModifiedDate instanceof Date); assert.equal(event, 'progress'); assert.equal(bytesWritten, file.size); }); CB_STUB = gently.expect(function writeCb() { assert.equal(file.size, 10); }); cb(); gently.expect(file, 'emit', function (event, bytesWritten) { assert.equal(event, 'progress'); assert.equal(bytesWritten, file.size); }); CB_STUB = gently.expect(function writeCb() { assert.equal(file.size, 20); }); cb(); }); file.write(BUFFER, CB); }); test(function end() { var CB_STUB, CB = function() { CB_STUB.apply(this, arguments); }; file._writeStream = {}; gently.expect(file._writeStream, 'end', function (cb) { gently.expect(file, 'emit', function (event) { assert.equal(event, 'end'); }); CB_STUB = gently.expect(function endCb() { }); cb(); }); file.end(CB); }); package/test/legacy/simple/test-incoming-form.js000644 000765 000024 0000045716 12117571742020346 0ustar00000000 000000 var common = require('../common'); var MultipartParserStub = GENTLY.stub('./multipart_parser', 'MultipartParser'), QuerystringParserStub = GENTLY.stub('./querystring_parser', 'QuerystringParser'), EventEmitterStub = GENTLY.stub('events', 'EventEmitter'), StreamStub = GENTLY.stub('stream', 'Stream'), FileStub = GENTLY.stub('./file'); var formidable = require(common.lib + '/index'), IncomingForm = formidable.IncomingForm, events = require('events'), fs = require('fs'), path = require('path'), Buffer = require('buffer').Buffer, fixtures = require(TEST_FIXTURES + '/multipart'), form, gently; function test(test) { gently = new Gently(); gently.expect(EventEmitterStub, 'call'); form = new IncomingForm(); test(); gently.verify(test.name); } test(function constructor() { assert.strictEqual(form.error, null); assert.strictEqual(form.ended, false); assert.strictEqual(form.type, null); assert.strictEqual(form.headers, null); assert.strictEqual(form.keepExtensions, false); // Can't assume dir === '/tmp' for portability // assert.strictEqual(form.uploadDir, '/tmp'); // Make sure it is a directory instead assert.doesNotThrow(function () { assert(fs.statSync(form.uploadDir).isDirectory()); }); assert.strictEqual(form.encoding, 'utf-8'); assert.strictEqual(form.bytesReceived, null); assert.strictEqual(form.bytesExpected, null); assert.strictEqual(form.maxFieldsSize, 2 * 1024 * 1024); assert.strictEqual(form._parser, null); assert.strictEqual(form._flushing, 0); assert.strictEqual(form._fieldsSize, 0); assert.ok(form instanceof EventEmitterStub); assert.equal(form.constructor.name, 'IncomingForm'); (function testSimpleConstructor() { gently.expect(EventEmitterStub, 'call'); var form = IncomingForm(); assert.ok(form instanceof IncomingForm); })(); (function testSimpleConstructorShortcut() { gently.expect(EventEmitterStub, 'call'); var form = formidable(); assert.ok(form instanceof IncomingForm); })(); }); test(function parse() { var REQ = {headers: {}} , emit = {}; var events = ['error', 'aborted', 'data', 'end']; gently.expect(REQ, 'on', events.length, function(event, fn) { assert.equal(event, events.shift()); emit[event] = fn; return this; }); gently.expect(form, 'writeHeaders', function(headers) { assert.strictEqual(headers, REQ.headers); }); form.parse(REQ); (function testPause() { gently.expect(REQ, 'pause'); assert.strictEqual(form.pause(), true); })(); (function testPauseCriticalException() { form.ended = false; var ERR = new Error('dasdsa'); gently.expect(REQ, 'pause', function() { throw ERR; }); gently.expect(form, '_error', function(err) { assert.strictEqual(err, ERR); }); assert.strictEqual(form.pause(), false); })(); (function testPauseHarmlessException() { form.ended = true; var ERR = new Error('dasdsa'); gently.expect(REQ, 'pause', function() { throw ERR; }); assert.strictEqual(form.pause(), false); })(); (function testResume() { gently.expect(REQ, 'resume'); assert.strictEqual(form.resume(), true); })(); (function testResumeCriticalException() { form.ended = false; var ERR = new Error('dasdsa'); gently.expect(REQ, 'resume', function() { throw ERR; }); gently.expect(form, '_error', function(err) { assert.strictEqual(err, ERR); }); assert.strictEqual(form.resume(), false); })(); (function testResumeHarmlessException() { form.ended = true; var ERR = new Error('dasdsa'); gently.expect(REQ, 'resume', function() { throw ERR; }); assert.strictEqual(form.resume(), false); })(); (function testEmitError() { var ERR = new Error('something bad happened'); gently.expect(form, '_error',function(err) { assert.strictEqual(err, ERR); }); emit.error(ERR); })(); (function testEmitAborted() { gently.expect(form, 'emit',function(event) { assert.equal(event, 'aborted'); }); gently.expect(form, '_error'); emit.aborted(); })(); (function testEmitData() { var BUFFER = [1, 2, 3]; gently.expect(form, 'write', function(buffer) { assert.strictEqual(buffer, BUFFER); }); emit.data(BUFFER); })(); (function testEmitEnd() { form._parser = {}; (function testWithError() { var ERR = new Error('haha'); gently.expect(form._parser, 'end', function() { return ERR; }); gently.expect(form, '_error', function(err) { assert.strictEqual(err, ERR); }); emit.end(); })(); (function testWithoutError() { gently.expect(form._parser, 'end'); emit.end(); })(); (function testAfterError() { form.error = true; emit.end(); })(); })(); (function testWithCallback() { gently.expect(EventEmitterStub, 'call'); var form = new IncomingForm(), REQ = {headers: {}}, parseCalled = 0; gently.expect(REQ, 'on', 4, function() { return this; }); gently.expect(form, 'on', 4, function(event, fn) { if (event == 'field') { fn('field1', 'foo'); fn('field1', 'bar'); fn('field2', 'nice'); } if (event == 'file') { fn('file1', '1'); fn('file1', '2'); fn('file2', '3'); } if (event == 'end') { fn(); } return this; }); var parseCbOk = function (err, fields, files) { assert.deepEqual(fields, {field1: 'bar', field2: 'nice'}); assert.deepEqual(files, {file1: '2', file2: '3'}); }; gently.expect(form, 'writeHeaders'); form.parse(REQ, parseCbOk); gently.expect(REQ, 'on', 4, function() { return this; }); var ERR = new Error('test'); gently.expect(form, 'on', 3, function(event, fn) { if (event == 'field') { fn('foo', 'bar'); } if (event == 'error') { fn(ERR); gently.expect(form, 'on'); gently.expect(form, 'writeHeaders'); } return this; }); form.parse(REQ, function parseCbErr(err, fields, files) { assert.strictEqual(err, ERR); assert.deepEqual(fields, {foo: 'bar'}); }); })(); }); test(function pause() { assert.strictEqual(form.pause(), false); }); test(function resume() { assert.strictEqual(form.resume(), false); }); test(function writeHeaders() { var HEADERS = {}; gently.expect(form, '_parseContentLength'); gently.expect(form, '_parseContentType'); form.writeHeaders(HEADERS); assert.strictEqual(form.headers, HEADERS); }); test(function write() { var parser = {}, BUFFER = [1, 2, 3]; form._parser = parser; form.bytesExpected = 523423; (function testBasic() { gently.expect(form, 'emit', function(event, bytesReceived, bytesExpected) { assert.equal(event, 'progress'); assert.equal(bytesReceived, BUFFER.length); assert.equal(bytesExpected, form.bytesExpected); }); gently.expect(parser, 'write', function(buffer) { assert.strictEqual(buffer, BUFFER); return buffer.length; }); assert.equal(form.write(BUFFER), BUFFER.length); assert.equal(form.bytesReceived, BUFFER.length); })(); (function testParserError() { gently.expect(form, 'emit'); gently.expect(parser, 'write', function(buffer) { assert.strictEqual(buffer, BUFFER); return buffer.length - 1; }); gently.expect(form, '_error', function(err) { assert.ok(err.message.match(/parser error/i)); }); assert.equal(form.write(BUFFER), BUFFER.length - 1); assert.equal(form.bytesReceived, BUFFER.length + BUFFER.length); })(); (function testUninitialized() { delete form._parser; gently.expect(form, '_error', function(err) { assert.ok(err.message.match(/unintialized parser/i)); }); form.write(BUFFER); })(); }); test(function parseContentType() { var HEADERS = {}; form.headers = {'content-type': 'application/x-www-form-urlencoded'}; gently.expect(form, '_initUrlencoded'); form._parseContentType(); // accept anything that has 'urlencoded' in it form.headers = {'content-type': 'broken-client/urlencoded-stupid'}; gently.expect(form, '_initUrlencoded'); form._parseContentType(); var BOUNDARY = '---------------------------57814261102167618332366269'; form.headers = {'content-type': 'multipart/form-data; boundary='+BOUNDARY}; gently.expect(form, '_initMultipart', function(boundary) { assert.equal(boundary, BOUNDARY); }); form._parseContentType(); (function testQuotedBoundary() { form.headers = {'content-type': 'multipart/form-data; boundary="' + BOUNDARY + '"'}; gently.expect(form, '_initMultipart', function(boundary) { assert.equal(boundary, BOUNDARY); }); form._parseContentType(); })(); (function testNoBoundary() { form.headers = {'content-type': 'multipart/form-data'}; gently.expect(form, '_error', function(err) { assert.ok(err.message.match(/no multipart boundary/i)); }); form._parseContentType(); })(); (function testNoContentType() { form.headers = {}; gently.expect(form, '_error', function(err) { assert.ok(err.message.match(/no content-type/i)); }); form._parseContentType(); })(); (function testUnknownContentType() { form.headers = {'content-type': 'invalid'}; gently.expect(form, '_error', function(err) { assert.ok(err.message.match(/unknown content-type/i)); }); form._parseContentType(); })(); }); test(function parseContentLength() { var HEADERS = {}; form.headers = {}; form._parseContentLength(); assert.strictEqual(form.bytesReceived, null); assert.strictEqual(form.bytesExpected, null); form.headers['content-length'] = '8'; gently.expect(form, 'emit', function(event, bytesReceived, bytesExpected) { assert.equal(event, 'progress'); assert.equal(bytesReceived, 0); assert.equal(bytesExpected, 8); }); form._parseContentLength(); assert.strictEqual(form.bytesReceived, 0); assert.strictEqual(form.bytesExpected, 8); // JS can be evil, lets make sure we are not form.headers['content-length'] = '08'; gently.expect(form, 'emit', function(event, bytesReceived, bytesExpected) { assert.equal(event, 'progress'); assert.equal(bytesReceived, 0); assert.equal(bytesExpected, 8); }); form._parseContentLength(); assert.strictEqual(form.bytesExpected, 8); }); test(function _initMultipart() { var BOUNDARY = '123', PARSER; gently.expect(MultipartParserStub, 'new', function() { PARSER = this; }); gently.expect(MultipartParserStub.prototype, 'initWithBoundary', function(boundary) { assert.equal(boundary, BOUNDARY); }); form._initMultipart(BOUNDARY); assert.equal(form.type, 'multipart'); assert.strictEqual(form._parser, PARSER); (function testRegularField() { var PART; gently.expect(StreamStub, 'new', function() { PART = this; }); gently.expect(form, 'onPart', function(part) { assert.strictEqual(part, PART); assert.deepEqual ( part.headers , { 'content-disposition': 'form-data; name="field1"' , 'foo': 'bar' } ); assert.equal(part.name, 'field1'); var strings = ['hello', ' world']; gently.expect(part, 'emit', 2, function(event, b) { assert.equal(event, 'data'); assert.equal(b.toString(), strings.shift()); }); gently.expect(part, 'emit', function(event, b) { assert.equal(event, 'end'); }); }); PARSER.onPartBegin(); PARSER.onHeaderField(new Buffer('content-disposition'), 0, 10); PARSER.onHeaderField(new Buffer('content-disposition'), 10, 19); PARSER.onHeaderValue(new Buffer('form-data; name="field1"'), 0, 14); PARSER.onHeaderValue(new Buffer('form-data; name="field1"'), 14, 24); PARSER.onHeaderEnd(); PARSER.onHeaderField(new Buffer('foo'), 0, 3); PARSER.onHeaderValue(new Buffer('bar'), 0, 3); PARSER.onHeaderEnd(); PARSER.onHeadersEnd(); PARSER.onPartData(new Buffer('hello world'), 0, 5); PARSER.onPartData(new Buffer('hello world'), 5, 11); PARSER.onPartEnd(); })(); (function testFileField() { var PART; gently.expect(StreamStub, 'new', function() { PART = this; }); gently.expect(form, 'onPart', function(part) { assert.deepEqual ( part.headers , { 'content-disposition': 'form-data; name="field2"; filename="C:\\Documents and Settings\\IE\\Must\\Die\\Sun"et.jpg"' , 'content-type': 'text/plain' } ); assert.equal(part.name, 'field2'); assert.equal(part.filename, 'Sun"et.jpg'); assert.equal(part.mime, 'text/plain'); gently.expect(part, 'emit', function(event, b) { assert.equal(event, 'data'); assert.equal(b.toString(), '... contents of file1.txt ...'); }); gently.expect(part, 'emit', function(event, b) { assert.equal(event, 'end'); }); }); PARSER.onPartBegin(); PARSER.onHeaderField(new Buffer('content-disposition'), 0, 19); PARSER.onHeaderValue(new Buffer('form-data; name="field2"; filename="C:\\Documents and Settings\\IE\\Must\\Die\\Sun"et.jpg"'), 0, 85); PARSER.onHeaderEnd(); PARSER.onHeaderField(new Buffer('Content-Type'), 0, 12); PARSER.onHeaderValue(new Buffer('text/plain'), 0, 10); PARSER.onHeaderEnd(); PARSER.onHeadersEnd(); PARSER.onPartData(new Buffer('... contents of file1.txt ...'), 0, 29); PARSER.onPartEnd(); })(); (function testEnd() { gently.expect(form, '_maybeEnd'); PARSER.onEnd(); assert.ok(form.ended); })(); }); test(function _fileName() { // TODO return; }); test(function _initUrlencoded() { var PARSER; gently.expect(QuerystringParserStub, 'new', function() { PARSER = this; }); form._initUrlencoded(); assert.equal(form.type, 'urlencoded'); assert.strictEqual(form._parser, PARSER); (function testOnField() { var KEY = 'KEY', VAL = 'VAL'; gently.expect(form, 'emit', function(field, key, val) { assert.equal(field, 'field'); assert.equal(key, KEY); assert.equal(val, VAL); }); PARSER.onField(KEY, VAL); })(); (function testOnEnd() { gently.expect(form, '_maybeEnd'); PARSER.onEnd(); assert.equal(form.ended, true); })(); }); test(function _error() { var ERR = new Error('bla'); gently.expect(form, 'pause'); gently.expect(form, 'emit', function(event, err) { assert.equal(event, 'error'); assert.strictEqual(err, ERR); }); form._error(ERR); assert.strictEqual(form.error, ERR); // make sure _error only does its thing once form._error(ERR); }); test(function onPart() { var PART = {}; gently.expect(form, 'handlePart', function(part) { assert.strictEqual(part, PART); }); form.onPart(PART); }); test(function handlePart() { (function testUtf8Field() { var PART = new events.EventEmitter(); PART.name = 'my_field'; gently.expect(form, 'emit', function(event, field, value) { assert.equal(event, 'field'); assert.equal(field, 'my_field'); assert.equal(value, 'hello world: €'); }); form.handlePart(PART); PART.emit('data', new Buffer('hello')); PART.emit('data', new Buffer(' world: ')); PART.emit('data', new Buffer([0xE2])); PART.emit('data', new Buffer([0x82, 0xAC])); PART.emit('end'); })(); (function testBinaryField() { var PART = new events.EventEmitter(); PART.name = 'my_field2'; gently.expect(form, 'emit', function(event, field, value) { assert.equal(event, 'field'); assert.equal(field, 'my_field2'); assert.equal(value, 'hello world: '+new Buffer([0xE2, 0x82, 0xAC]).toString('binary')); }); form.encoding = 'binary'; form.handlePart(PART); PART.emit('data', new Buffer('hello')); PART.emit('data', new Buffer(' world: ')); PART.emit('data', new Buffer([0xE2])); PART.emit('data', new Buffer([0x82, 0xAC])); PART.emit('end'); })(); (function testFieldSize() { form.maxFieldsSize = 8; var PART = new events.EventEmitter(); PART.name = 'my_field'; gently.expect(form, '_error', function(err) { assert.equal(err.message, 'maxFieldsSize exceeded, received 9 bytes of field data'); }); form.handlePart(PART); form._fieldsSize = 1; PART.emit('data', new Buffer(7)); PART.emit('data', new Buffer(1)); })(); (function testFilePart() { var PART = new events.EventEmitter(), FILE = new events.EventEmitter(), PATH = '/foo/bar'; PART.name = 'my_file'; PART.filename = 'sweet.txt'; PART.mime = 'sweet.txt'; gently.expect(form, '_uploadPath', function(filename) { assert.equal(filename, PART.filename); return PATH; }); gently.expect(FileStub, 'new', function(properties) { assert.equal(properties.path, PATH); assert.equal(properties.name, PART.filename); assert.equal(properties.type, PART.mime); FILE = this; gently.expect(form, 'emit', function (event, field, file) { assert.equal(event, 'fileBegin'); assert.strictEqual(field, PART.name); assert.strictEqual(file, FILE); }); gently.expect(FILE, 'open'); }); form.handlePart(PART); assert.equal(form._flushing, 1); var BUFFER; gently.expect(form, 'pause'); gently.expect(FILE, 'write', function(buffer, cb) { assert.strictEqual(buffer, BUFFER); gently.expect(form, 'resume'); // @todo handle cb(new Err) cb(); }); PART.emit('data', BUFFER = new Buffer('test')); gently.expect(FILE, 'end', function(cb) { gently.expect(form, 'emit', function(event, field, file) { assert.equal(event, 'file'); assert.strictEqual(file, FILE); }); gently.expect(form, '_maybeEnd'); cb(); assert.equal(form._flushing, 0); }); PART.emit('end'); })(); }); test(function _uploadPath() { (function testUniqueId() { var UUID_A, UUID_B; gently.expect(GENTLY.hijacked.path, 'join', function(uploadDir, uuid) { assert.equal(uploadDir, form.uploadDir); UUID_A = uuid; }); form._uploadPath(); gently.expect(GENTLY.hijacked.path, 'join', function(uploadDir, uuid) { UUID_B = uuid; }); form._uploadPath(); assert.notEqual(UUID_A, UUID_B); })(); (function testFileExtension() { form.keepExtensions = true; var FILENAME = 'foo.jpg', EXT = '.bar'; gently.expect(GENTLY.hijacked.path, 'extname', function(filename) { assert.equal(filename, FILENAME); gently.restore(path, 'extname'); return EXT; }); gently.expect(GENTLY.hijacked.path, 'join', function(uploadDir, name) { assert.equal(path.extname(name), EXT); }); form._uploadPath(FILENAME); })(); }); test(function _maybeEnd() { gently.expect(form, 'emit', 0); form._maybeEnd(); form.ended = true; form._flushing = 1; form._maybeEnd(); gently.expect(form, 'emit', function(event) { assert.equal(event, 'end'); }); form.ended = true; form._flushing = 0; form._maybeEnd(); }); package/test/legacy/simple/test-multipart-parser.js000644 000765 000024 0000002716 12117571742021106 0ustar00000000 000000 var common = require('../common'); var multipartParser = require(common.lib + '/multipart_parser'), MultipartParser = multipartParser.MultipartParser, events = require('events'), Buffer = require('buffer').Buffer, parser; function test(test) { parser = new MultipartParser(); test(); } test(function constructor() { assert.equal(parser.boundary, null); assert.equal(parser.state, 0); assert.equal(parser.flags, 0); assert.equal(parser.boundaryChars, null); assert.equal(parser.index, null); assert.equal(parser.lookbehind, null); assert.equal(parser.constructor.name, 'MultipartParser'); }); test(function initWithBoundary() { var boundary = 'abc'; parser.initWithBoundary(boundary); assert.deepEqual(Array.prototype.slice.call(parser.boundary), [13, 10, 45, 45, 97, 98, 99]); assert.equal(parser.state, multipartParser.START); assert.deepEqual(parser.boundaryChars, {10: true, 13: true, 45: true, 97: true, 98: true, 99: true}); }); test(function parserError() { var boundary = 'abc', buffer = new Buffer(5); parser.initWithBoundary(boundary); buffer.write('--ad', 'ascii', 0); assert.equal(parser.write(buffer), 5); }); test(function end() { (function testError() { assert.equal(parser.end().message, 'MultipartParser.end(): stream ended unexpectedly: ' + parser.explain()); })(); (function testRegular() { parser.state = multipartParser.END; assert.strictEqual(parser.end(), undefined); })(); }); package/test/legacy/simple/test-querystring-parser.js000644 000765 000024 0000002075 12117571742021457 0ustar00000000 000000 var common = require('../common'); var QuerystringParser = require(common.lib + '/querystring_parser').QuerystringParser, Buffer = require('buffer').Buffer, gently, parser; function test(test) { gently = new Gently(); parser = new QuerystringParser(); test(); gently.verify(test.name); } test(function constructor() { assert.equal(parser.buffer, ''); assert.equal(parser.constructor.name, 'QuerystringParser'); }); test(function write() { var a = new Buffer('a=1'); assert.equal(parser.write(a), a.length); var b = new Buffer('&b=2'); parser.write(b); assert.equal(parser.buffer, a + b); }); test(function end() { var FIELDS = {a: ['b', {c: 'd'}], e: 'f'}; gently.expect(GENTLY.hijacked.querystring, 'parse', function(str) { assert.equal(str, parser.buffer); return FIELDS; }); gently.expect(parser, 'onField', Object.keys(FIELDS).length, function(key, val) { assert.deepEqual(FIELDS[key], val); }); gently.expect(parser, 'onEnd'); parser.buffer = 'my buffer'; parser.end(); assert.equal(parser.buffer, ''); }); package/test/legacy/system/test-multi-video-upload.js000644 000765 000024 0000004415 12121123733021330 0ustar00000000 000000 var common = require('../common'); var BOUNDARY = '---------------------------10102754414578508781458777923', FIXTURE = TEST_FIXTURES+'/multi_video.upload', fs = require('fs'), http = require('http'), formidable = require(common.lib + '/index'), server = http.createServer(); server.on('request', function(req, res) { var form = new formidable.IncomingForm(), uploads = {}; form.uploadDir = TEST_TMP; form.hash = 'sha1'; form.parse(req); form .on('fileBegin', function(field, file) { assert.equal(field, 'upload'); var tracker = {file: file, progress: [], ended: false}; uploads[file.name] = tracker; file .on('progress', function(bytesReceived) { tracker.progress.push(bytesReceived); assert.equal(bytesReceived, file.size); }) .on('end', function() { tracker.ended = true; }); }) .on('field', function(field, value) { assert.equal(field, 'title'); assert.equal(value, ''); }) .on('file', function(field, file) { assert.equal(field, 'upload'); assert.strictEqual(uploads[file.name].file, file); }) .on('end', function() { assert.ok(uploads['shortest_video.flv']); assert.ok(uploads['shortest_video.flv'].ended); assert.ok(uploads['shortest_video.flv'].progress.length > 3); assert.equal(uploads['shortest_video.flv'].file.hash, 'da39a3ee5e6b4b0d3255bfef95601890afd80709'); assert.equal(uploads['shortest_video.flv'].progress.slice(-1), uploads['shortest_video.flv'].file.size); assert.ok(uploads['shortest_video.mp4']); assert.ok(uploads['shortest_video.mp4'].ended); assert.ok(uploads['shortest_video.mp4'].progress.length > 3); assert.equal(uploads['shortest_video.mp4'].file.hash, 'da39a3ee5e6b4b0d3255bfef95601890afd80709'); server.close(); res.writeHead(200); res.end('good'); }); }); server.listen(TEST_PORT, function() { var stat, headers, request, fixture; stat = fs.statSync(FIXTURE); request = http.request({ port: TEST_PORT, path: '/', method: 'POST', headers: { 'content-type': 'multipart/form-data; boundary='+BOUNDARY, 'content-length': stat.size, }, }); fs.createReadStream(FIXTURE).pipe(request); }); package/test/standalone/test-connection-aborted.js000644 000765 000024 0000001452 12117571742020737 0ustar00000000 000000 var assert = require('assert'); var http = require('http'); var net = require('net'); var formidable = require('../../lib/index'); var server = http.createServer(function (req, res) { var form = new formidable.IncomingForm(); var aborted_received = false; form.on('aborted', function () { aborted_received = true; }); form.on('error', function () { assert(aborted_received, 'Error event should follow aborted'); server.close(); }); form.on('end', function () { throw new Error('Unexpected "end" event'); }); form.parse(req); }).listen(0, 'localhost', function () { var client = net.connect(server.address().port); client.write( "POST / HTTP/1.1\r\n" + "Content-Length: 70\r\n" + "Content-Type: multipart/form-data; boundary=foo\r\n\r\n"); client.end(); }); package/test/standalone/test-content-transfer-encoding.js000644 000765 000024 0000002505 12117571742022242 0ustar00000000 000000 var assert = require('assert'); var common = require('../common'); var formidable = require('../../lib/index'); var http = require('http'); var server = http.createServer(function(req, res) { var form = new formidable.IncomingForm(); form.uploadDir = common.dir.tmp; form.on('end', function () { throw new Error('Unexpected "end" event'); }); form.on('error', function (e) { res.writeHead(500); res.end(e.message); }); form.parse(req); }); server.listen(0, function() { var body = '--foo\r\n' + 'Content-Disposition: form-data; name="file1"; filename="file1"\r\n' + 'Content-Type: application/octet-stream\r\n' + '\r\nThis is the first file\r\n' + '--foo\r\n' + 'Content-Type: application/octet-stream\r\n' + 'Content-Disposition: form-data; name="file2"; filename="file2"\r\n' + 'Content-Transfer-Encoding: unknown\r\n' + '\r\nThis is the second file\r\n' + '--foo--\r\n'; var req = http.request({ method: 'POST', port: server.address().port, headers: { 'Content-Length': body.length, 'Content-Type': 'multipart/form-data; boundary=foo' } }); req.on('response', function (res) { assert.equal(res.statusCode, 500); res.on('data', function () {}); res.on('end', function () { server.close(); }); }); req.end(body); }); package/test/standalone/test-issue-46.js000644 000765 000024 0000002435 12117571742016543 0ustar00000000 000000 var http = require('http'), formidable = require('../../lib/index'), request = require('request'), assert = require('assert'); var host = 'localhost'; var index = [ '
', ' ', ' ', '
' ].join("\n"); var server = http.createServer(function(req, res) { // Show a form for testing purposes. if (req.method == 'GET') { res.writeHead(200, {'content-type': 'text/html'}); res.end(index); return; } // Parse form and write results to response. var form = new formidable.IncomingForm(); form.parse(req, function(err, fields, files) { res.writeHead(200, {'content-type': 'text/plain'}); res.write(JSON.stringify({err: err, fields: fields, files: files})); res.end(); }); }).listen(0, host, function() { console.log("Server up and running..."); var server = this, url = 'http://' + host + ':' + server.address().port; var parts = [ {'Content-Disposition': 'form-data; name="foo"', 'body': 'bar'} ] var req = request({method: 'POST', url: url, multipart: parts}, function(e, res, body) { var obj = JSON.parse(body); assert.equal("bar", obj.fields.foo); server.close(); }); }); package/test/tools/base64.html000644 000765 000024 0000003521 12117571742014630 0ustar00000000 000000 Convert a file to a base64 request




Don't forget to save the output with windows (CRLF) line endings!

package/test/unit/test-file.js000644 000765 000024 0000001506 12117720274014724 0ustar00000000 000000 var common = require('../common'); var test = require('utest'); var assert = common.assert; var File = common.require('file'); var file; var now = new Date; test('IncomingForm', { before: function() { file = new File({ size: 1024, path: '/tmp/cat.png', name: 'cat.png', type: 'image/png', lastModifiedDate: now, filename: 'cat.png', mime: 'image/png' }) }, '#toJSON()': function() { var obj = file.toJSON(); var len = Object.keys(obj).length; assert.equal(1024, obj.size); assert.equal('/tmp/cat.png', obj.path); assert.equal('cat.png', obj.name); assert.equal('image/png', obj.type); assert.equal('image/png', obj.mime); assert.equal('cat.png', obj.filename); assert.equal(now, obj.mtime); assert.equal(len, 8); } });package/test/unit/test-incoming-form.js000644 000765 000024 0000003577 12117571742016567 0ustar00000000 000000 var common = require('../common'); var test = require('utest'); var assert = common.assert; var IncomingForm = common.require('incoming_form').IncomingForm; var path = require('path'); var form; test('IncomingForm', { before: function() { form = new IncomingForm(); }, '#_fileName with regular characters': function() { var filename = 'foo.txt'; assert.equal(form._fileName(makeHeader(filename)), 'foo.txt'); }, '#_fileName with unescaped quote': function() { var filename = 'my".txt'; assert.equal(form._fileName(makeHeader(filename)), 'my".txt'); }, '#_fileName with escaped quote': function() { var filename = 'my%22.txt'; assert.equal(form._fileName(makeHeader(filename)), 'my".txt'); }, '#_fileName with bad quote and additional sub-header': function() { var filename = 'my".txt'; var header = makeHeader(filename) + '; foo="bar"'; assert.equal(form._fileName(header), filename); }, '#_fileName with semicolon': function() { var filename = 'my;.txt'; assert.equal(form._fileName(makeHeader(filename)), 'my;.txt'); }, '#_fileName with utf8 character': function() { var filename = 'my☃.txt'; assert.equal(form._fileName(makeHeader(filename)), 'my☃.txt'); }, '#_uploadPath strips harmful characters from extension when keepExtensions': function() { form.keepExtensions = true; var ext = path.extname(form._uploadPath('fine.jpg?foo=bar')); assert.equal(ext, '.jpg'); var ext = path.extname(form._uploadPath('fine?foo=bar')); assert.equal(ext, ''); var ext = path.extname(form._uploadPath('super.cr2+dsad')); assert.equal(ext, '.cr2'); var ext = path.extname(form._uploadPath('super.bar')); assert.equal(ext, '.bar'); }, }); function makeHeader(filename) { return 'Content-Disposition: form-data; name="upload"; filename="' + filename + '"'; } package/example/json.js000644 000765 000024 0000003405 12117571742013502 0ustar00000000 000000 var common = require('../test/common'), http = require('http'), util = require('util'), formidable = common.formidable, Buffer = require('buffer').Buffer, port = common.port, server; server = http.createServer(function(req, res) { if (req.method !== 'POST') { res.writeHead(200, {'content-type': 'text/plain'}) res.end('Please POST a JSON payload to http://localhost:'+port+'/') return; } var form = new formidable.IncomingForm(), fields = {}; form .on('error', function(err) { res.writeHead(500, {'content-type': 'text/plain'}); res.end('error:\n\n'+util.inspect(err)); console.error(err); }) .on('field', function(field, value) { console.log(field, value); fields[field] = value; }) .on('end', function() { console.log('-> post done'); res.writeHead(200, {'content-type': 'text/plain'}); res.end('received fields:\n\n '+util.inspect(fields)); }); form.parse(req); }); server.listen(port); console.log('listening on http://localhost:'+port+'/'); var request = http.request({ host: 'localhost', path: '/', port: port, method: 'POST', headers: { 'content-type':'application/json', 'content-length':48 } }, function(response) { var data = ''; console.log('\nServer responded with:'); console.log('Status:', response.statusCode); response.pipe(process.stdout); response.on('end', function() { console.log('\n') process.exit(); }); // response.on('data', function(chunk) { // data += chunk.toString('utf8'); // }); // response.on('end', function() { // console.log('Response Data:') // console.log(data); // process.exit(); // }); }) request.write('{"numbers":[1,2,3,4,5],"nested":{"key":"value"}}'); request.end(); package/example/post.js000644 000765 000024 0000002402 12117571742013512 0ustar00000000 000000 require('../test/common'); var http = require('http'), util = require('util'), formidable = require('formidable'), server; server = http.createServer(function(req, res) { if (req.url == '/') { res.writeHead(200, {'content-type': 'text/html'}); res.end( '
'+ '
'+ '
'+ ''+ '
' ); } else if (req.url == '/post') { var form = new formidable.IncomingForm(), fields = []; form .on('error', function(err) { res.writeHead(200, {'content-type': 'text/plain'}); res.end('error:\n\n'+util.inspect(err)); }) .on('field', function(field, value) { console.log(field, value); fields.push([field, value]); }) .on('end', function() { console.log('-> post done'); res.writeHead(200, {'content-type': 'text/plain'}); res.end('received fields:\n\n '+util.inspect(fields)); }); form.parse(req); } else { res.writeHead(404, {'content-type': 'text/plain'}); res.end('404'); } }); server.listen(TEST_PORT); console.log('listening on http://localhost:'+TEST_PORT+'/'); package/example/upload.js000644 000765 000024 0000002642 12117571742014017 0ustar00000000 000000 require('../test/common'); var http = require('http'), util = require('util'), formidable = require('formidable'), server; server = http.createServer(function(req, res) { if (req.url == '/') { res.writeHead(200, {'content-type': 'text/html'}); res.end( '
'+ '
'+ '
'+ ''+ '
' ); } else if (req.url == '/upload') { var form = new formidable.IncomingForm(), files = [], fields = []; form.uploadDir = TEST_TMP; form .on('field', function(field, value) { console.log(field, value); fields.push([field, value]); }) .on('file', function(field, file) { console.log(field, file); files.push([field, file]); }) .on('end', function() { console.log('-> upload done'); res.writeHead(200, {'content-type': 'text/plain'}); res.write('received fields:\n\n '+util.inspect(fields)); res.write('\n\n'); res.end('received files:\n\n '+util.inspect(files)); }); form.parse(req); } else { res.writeHead(404, {'content-type': 'text/plain'}); res.end('404'); } }); server.listen(TEST_PORT); console.log('listening on http://localhost:'+TEST_PORT+'/');