incoming_form.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. if (global.GENTLY) require = GENTLY.hijack(require);
  2. var crypto = require('crypto');
  3. var fs = require('fs');
  4. var util = require('util'),
  5. path = require('path'),
  6. File = require('./file'),
  7. MultipartParser = require('./multipart_parser').MultipartParser,
  8. QuerystringParser = require('./querystring_parser').QuerystringParser,
  9. OctetParser = require('./octet_parser').OctetParser,
  10. JSONParser = require('./json_parser').JSONParser,
  11. StringDecoder = require('string_decoder').StringDecoder,
  12. EventEmitter = require('events').EventEmitter,
  13. Stream = require('stream').Stream,
  14. os = require('os');
  15. function IncomingForm(opts) {
  16. if (!(this instanceof IncomingForm)) return new IncomingForm(opts);
  17. EventEmitter.call(this);
  18. opts=opts||{};
  19. this.error = null;
  20. this.ended = false;
  21. this.maxFields = opts.maxFields || 1000;
  22. this.maxFieldsSize = opts.maxFieldsSize || 20 * 1024 * 1024;
  23. this.maxFileSize = opts.maxFileSize || 200 * 1024 * 1024;
  24. this.keepExtensions = opts.keepExtensions || false;
  25. this.uploadDir = opts.uploadDir || (os.tmpdir && os.tmpdir()) || os.tmpDir();
  26. this.encoding = opts.encoding || 'utf-8';
  27. this.headers = null;
  28. this.type = null;
  29. this.hash = opts.hash || false;
  30. this.multiples = opts.multiples || false;
  31. this.bytesReceived = null;
  32. this.bytesExpected = null;
  33. this._parser = null;
  34. this._flushing = 0;
  35. this._fieldsSize = 0;
  36. this._fileSize = 0;
  37. this.openedFiles = [];
  38. return this;
  39. }
  40. util.inherits(IncomingForm, EventEmitter);
  41. exports.IncomingForm = IncomingForm;
  42. IncomingForm.prototype.parse = function(req, cb) {
  43. this.pause = function() {
  44. try {
  45. req.pause();
  46. } catch (err) {
  47. // the stream was destroyed
  48. if (!this.ended) {
  49. // before it was completed, crash & burn
  50. this._error(err);
  51. }
  52. return false;
  53. }
  54. return true;
  55. };
  56. this.resume = function() {
  57. try {
  58. req.resume();
  59. } catch (err) {
  60. // the stream was destroyed
  61. if (!this.ended) {
  62. // before it was completed, crash & burn
  63. this._error(err);
  64. }
  65. return false;
  66. }
  67. return true;
  68. };
  69. // Setup callback first, so we don't miss anything from data events emitted
  70. // immediately.
  71. if (cb) {
  72. var fields = {}, files = {};
  73. this
  74. .on('field', function(name, value) {
  75. fields[name] = value;
  76. })
  77. .on('file', function(name, file) {
  78. if (this.multiples) {
  79. if (files[name]) {
  80. if (!Array.isArray(files[name])) {
  81. files[name] = [files[name]];
  82. }
  83. files[name].push(file);
  84. } else {
  85. files[name] = file;
  86. }
  87. } else {
  88. files[name] = file;
  89. }
  90. })
  91. .on('error', function(err) {
  92. cb(err, fields, files);
  93. })
  94. .on('end', function() {
  95. cb(null, fields, files);
  96. });
  97. }
  98. // Parse headers and setup the parser, ready to start listening for data.
  99. this.writeHeaders(req.headers);
  100. // Start listening for data.
  101. var self = this;
  102. req
  103. .on('error', function(err) {
  104. self._error(err);
  105. })
  106. .on('aborted', function() {
  107. self.emit('aborted');
  108. self._error(new Error('Request aborted'));
  109. })
  110. .on('data', function(buffer) {
  111. self.write(buffer);
  112. })
  113. .on('end', function() {
  114. if (self.error) {
  115. return;
  116. }
  117. var err = self._parser.end();
  118. if (err) {
  119. self._error(err);
  120. }
  121. });
  122. return this;
  123. };
  124. IncomingForm.prototype.writeHeaders = function(headers) {
  125. this.headers = headers;
  126. this._parseContentLength();
  127. this._parseContentType();
  128. };
  129. IncomingForm.prototype.write = function(buffer) {
  130. if (this.error) {
  131. return;
  132. }
  133. if (!this._parser) {
  134. this._error(new Error('uninitialized parser'));
  135. return;
  136. }
  137. if (typeof this._parser.write !== 'function') {
  138. this._error(new Error('did not expect data'));
  139. return;
  140. }
  141. this.bytesReceived += buffer.length;
  142. this.emit('progress', this.bytesReceived, this.bytesExpected);
  143. var bytesParsed = this._parser.write(buffer);
  144. if (bytesParsed !== buffer.length) {
  145. this._error(new Error('parser error, '+bytesParsed+' of '+buffer.length+' bytes parsed'));
  146. }
  147. return bytesParsed;
  148. };
  149. IncomingForm.prototype.pause = function() {
  150. // this does nothing, unless overwritten in IncomingForm.parse
  151. return false;
  152. };
  153. IncomingForm.prototype.resume = function() {
  154. // this does nothing, unless overwritten in IncomingForm.parse
  155. return false;
  156. };
  157. IncomingForm.prototype.onPart = function(part) {
  158. // this method can be overwritten by the user
  159. this.handlePart(part);
  160. };
  161. IncomingForm.prototype.handlePart = function(part) {
  162. var self = this;
  163. // This MUST check exactly for undefined. You can not change it to !part.filename.
  164. if (part.filename === undefined) {
  165. var value = ''
  166. , decoder = new StringDecoder(this.encoding);
  167. part.on('data', function(buffer) {
  168. self._fieldsSize += buffer.length;
  169. if (self._fieldsSize > self.maxFieldsSize) {
  170. self._error(new Error('maxFieldsSize exceeded, received '+self._fieldsSize+' bytes of field data'));
  171. return;
  172. }
  173. value += decoder.write(buffer);
  174. });
  175. part.on('end', function() {
  176. self.emit('field', part.name, value);
  177. });
  178. return;
  179. }
  180. this._flushing++;
  181. var file = new File({
  182. path: this._uploadPath(part.filename),
  183. name: part.filename,
  184. type: part.mime,
  185. hash: self.hash
  186. });
  187. this.emit('fileBegin', part.name, file);
  188. file.open();
  189. this.openedFiles.push(file);
  190. part.on('data', function(buffer) {
  191. self._fileSize += buffer.length;
  192. if (self._fileSize > self.maxFileSize) {
  193. self._error(new Error('maxFileSize exceeded, received '+self._fileSize+' bytes of file data'));
  194. return;
  195. }
  196. if (buffer.length == 0) {
  197. return;
  198. }
  199. self.pause();
  200. file.write(buffer, function() {
  201. self.resume();
  202. });
  203. });
  204. part.on('end', function() {
  205. file.end(function() {
  206. self._flushing--;
  207. self.emit('file', part.name, file);
  208. self._maybeEnd();
  209. });
  210. });
  211. };
  212. function dummyParser(self) {
  213. return {
  214. end: function () {
  215. self.ended = true;
  216. self._maybeEnd();
  217. return null;
  218. }
  219. };
  220. }
  221. IncomingForm.prototype._parseContentType = function() {
  222. if (this.bytesExpected === 0) {
  223. this._parser = dummyParser(this);
  224. return;
  225. }
  226. if (!this.headers['content-type']) {
  227. this._error(new Error('bad content-type header, no content-type'));
  228. return;
  229. }
  230. if (this.headers['content-type'].match(/octet-stream/i)) {
  231. this._initOctetStream();
  232. return;
  233. }
  234. if (this.headers['content-type'].match(/urlencoded/i)) {
  235. this._initUrlencoded();
  236. return;
  237. }
  238. if (this.headers['content-type'].match(/multipart/i)) {
  239. var m = this.headers['content-type'].match(/boundary=(?:"([^"]+)"|([^;]+))/i);
  240. if (m) {
  241. this._initMultipart(m[1] || m[2]);
  242. } else {
  243. this._error(new Error('bad content-type header, no multipart boundary'));
  244. }
  245. return;
  246. }
  247. if (this.headers['content-type'].match(/json/i)) {
  248. this._initJSONencoded();
  249. return;
  250. }
  251. this._error(new Error('bad content-type header, unknown content-type: '+this.headers['content-type']));
  252. };
  253. IncomingForm.prototype._error = function(err) {
  254. if (this.error || this.ended) {
  255. return;
  256. }
  257. this.error = err;
  258. this.emit('error', err);
  259. if (Array.isArray(this.openedFiles)) {
  260. this.openedFiles.forEach(function(file) {
  261. file._writeStream
  262. .on('error', function() {})
  263. .destroy();
  264. setTimeout(fs.unlink, 0, file.path, function(error) { });
  265. });
  266. }
  267. };
  268. IncomingForm.prototype._parseContentLength = function() {
  269. this.bytesReceived = 0;
  270. if (this.headers['content-length']) {
  271. this.bytesExpected = parseInt(this.headers['content-length'], 10);
  272. } else if (this.headers['transfer-encoding'] === undefined) {
  273. this.bytesExpected = 0;
  274. }
  275. if (this.bytesExpected !== null) {
  276. this.emit('progress', this.bytesReceived, this.bytesExpected);
  277. }
  278. };
  279. IncomingForm.prototype._newParser = function() {
  280. return new MultipartParser();
  281. };
  282. IncomingForm.prototype._initMultipart = function(boundary) {
  283. this.type = 'multipart';
  284. var parser = new MultipartParser(),
  285. self = this,
  286. headerField,
  287. headerValue,
  288. part;
  289. parser.initWithBoundary(boundary);
  290. parser.onPartBegin = function() {
  291. part = new Stream();
  292. part.readable = true;
  293. part.headers = {};
  294. part.name = null;
  295. part.filename = null;
  296. part.mime = null;
  297. part.transferEncoding = 'binary';
  298. part.transferBuffer = '';
  299. headerField = '';
  300. headerValue = '';
  301. };
  302. parser.onHeaderField = function(b, start, end) {
  303. headerField += b.toString(self.encoding, start, end);
  304. };
  305. parser.onHeaderValue = function(b, start, end) {
  306. headerValue += b.toString(self.encoding, start, end);
  307. };
  308. parser.onHeaderEnd = function() {
  309. headerField = headerField.toLowerCase();
  310. part.headers[headerField] = headerValue;
  311. // matches either a quoted-string or a token (RFC 2616 section 19.5.1)
  312. var m = headerValue.match(/\bname=("([^"]*)"|([^\(\)<>@,;:\\"\/\[\]\?=\{\}\s\t/]+))/i);
  313. if (headerField == 'content-disposition') {
  314. if (m) {
  315. part.name = m[2] || m[3] || '';
  316. }
  317. part.filename = self._fileName(headerValue);
  318. } else if (headerField == 'content-type') {
  319. part.mime = headerValue;
  320. } else if (headerField == 'content-transfer-encoding') {
  321. part.transferEncoding = headerValue.toLowerCase();
  322. }
  323. headerField = '';
  324. headerValue = '';
  325. };
  326. parser.onHeadersEnd = function() {
  327. switch(part.transferEncoding){
  328. case 'binary':
  329. case '7bit':
  330. case '8bit':
  331. parser.onPartData = function(b, start, end) {
  332. part.emit('data', b.slice(start, end));
  333. };
  334. parser.onPartEnd = function() {
  335. part.emit('end');
  336. };
  337. break;
  338. case 'base64':
  339. parser.onPartData = function(b, start, end) {
  340. part.transferBuffer += b.slice(start, end).toString('ascii');
  341. /*
  342. four bytes (chars) in base64 converts to three bytes in binary
  343. encoding. So we should always work with a number of bytes that
  344. can be divided by 4, it will result in a number of buytes that
  345. can be divided vy 3.
  346. */
  347. var offset = parseInt(part.transferBuffer.length / 4, 10) * 4;
  348. part.emit('data', new Buffer(part.transferBuffer.substring(0, offset), 'base64'));
  349. part.transferBuffer = part.transferBuffer.substring(offset);
  350. };
  351. parser.onPartEnd = function() {
  352. part.emit('data', new Buffer(part.transferBuffer, 'base64'));
  353. part.emit('end');
  354. };
  355. break;
  356. default:
  357. return self._error(new Error('unknown transfer-encoding'));
  358. }
  359. self.onPart(part);
  360. };
  361. parser.onEnd = function() {
  362. self.ended = true;
  363. self._maybeEnd();
  364. };
  365. this._parser = parser;
  366. };
  367. IncomingForm.prototype._fileName = function(headerValue) {
  368. // matches either a quoted-string or a token (RFC 2616 section 19.5.1)
  369. var m = headerValue.match(/\bfilename=("(.*?)"|([^\(\)<>@,;:\\"\/\[\]\?=\{\}\s\t/]+))($|;\s)/i);
  370. if (!m) return;
  371. var match = m[2] || m[3] || '';
  372. var filename = match.substr(match.lastIndexOf('\\') + 1);
  373. filename = filename.replace(/%22/g, '"');
  374. filename = filename.replace(/&#([\d]{4});/g, function(m, code) {
  375. return String.fromCharCode(code);
  376. });
  377. return filename;
  378. };
  379. IncomingForm.prototype._initUrlencoded = function() {
  380. this.type = 'urlencoded';
  381. var parser = new QuerystringParser(this.maxFields)
  382. , self = this;
  383. parser.onField = function(key, val) {
  384. self.emit('field', key, val);
  385. };
  386. parser.onEnd = function() {
  387. self.ended = true;
  388. self._maybeEnd();
  389. };
  390. this._parser = parser;
  391. };
  392. IncomingForm.prototype._initOctetStream = function() {
  393. this.type = 'octet-stream';
  394. var filename = this.headers['x-file-name'];
  395. var mime = this.headers['content-type'];
  396. var file = new File({
  397. path: this._uploadPath(filename),
  398. name: filename,
  399. type: mime
  400. });
  401. this.emit('fileBegin', filename, file);
  402. file.open();
  403. this.openedFiles.push(file);
  404. this._flushing++;
  405. var self = this;
  406. self._parser = new OctetParser();
  407. //Keep track of writes that haven't finished so we don't emit the file before it's done being written
  408. var outstandingWrites = 0;
  409. self._parser.on('data', function(buffer){
  410. self.pause();
  411. outstandingWrites++;
  412. file.write(buffer, function() {
  413. outstandingWrites--;
  414. self.resume();
  415. if(self.ended){
  416. self._parser.emit('doneWritingFile');
  417. }
  418. });
  419. });
  420. self._parser.on('end', function(){
  421. self._flushing--;
  422. self.ended = true;
  423. var done = function(){
  424. file.end(function() {
  425. self.emit('file', 'file', file);
  426. self._maybeEnd();
  427. });
  428. };
  429. if(outstandingWrites === 0){
  430. done();
  431. } else {
  432. self._parser.once('doneWritingFile', done);
  433. }
  434. });
  435. };
  436. IncomingForm.prototype._initJSONencoded = function() {
  437. this.type = 'json';
  438. var parser = new JSONParser(this)
  439. , self = this;
  440. parser.onField = function(key, val) {
  441. self.emit('field', key, val);
  442. };
  443. parser.onEnd = function() {
  444. self.ended = true;
  445. self._maybeEnd();
  446. };
  447. this._parser = parser;
  448. };
  449. IncomingForm.prototype._uploadPath = function(filename) {
  450. var buf = crypto.randomBytes(16);
  451. var name = 'upload_' + buf.toString('hex');
  452. if (this.keepExtensions) {
  453. var ext = path.extname(filename);
  454. ext = ext.replace(/(\.[a-z0-9]+).*/i, '$1');
  455. name += ext;
  456. }
  457. return path.join(this.uploadDir, name);
  458. };
  459. IncomingForm.prototype._maybeEnd = function() {
  460. if (!this.ended || this._flushing || this.error) {
  461. return;
  462. }
  463. this.emit('end');
  464. };