Front end of the Slack clone application.

incoming_form.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. this.bytesReceived += buffer.length;
  138. this.emit('progress', this.bytesReceived, this.bytesExpected);
  139. var bytesParsed = this._parser.write(buffer);
  140. if (bytesParsed !== buffer.length) {
  141. this._error(new Error('parser error, '+bytesParsed+' of '+buffer.length+' bytes parsed'));
  142. }
  143. return bytesParsed;
  144. };
  145. IncomingForm.prototype.pause = function() {
  146. // this does nothing, unless overwritten in IncomingForm.parse
  147. return false;
  148. };
  149. IncomingForm.prototype.resume = function() {
  150. // this does nothing, unless overwritten in IncomingForm.parse
  151. return false;
  152. };
  153. IncomingForm.prototype.onPart = function(part) {
  154. // this method can be overwritten by the user
  155. this.handlePart(part);
  156. };
  157. IncomingForm.prototype.handlePart = function(part) {
  158. var self = this;
  159. // This MUST check exactly for undefined. You can not change it to !part.filename.
  160. if (part.filename === undefined) {
  161. var value = ''
  162. , decoder = new StringDecoder(this.encoding);
  163. part.on('data', function(buffer) {
  164. self._fieldsSize += buffer.length;
  165. if (self._fieldsSize > self.maxFieldsSize) {
  166. self._error(new Error('maxFieldsSize exceeded, received '+self._fieldsSize+' bytes of field data'));
  167. return;
  168. }
  169. value += decoder.write(buffer);
  170. });
  171. part.on('end', function() {
  172. self.emit('field', part.name, value);
  173. });
  174. return;
  175. }
  176. this._flushing++;
  177. var file = new File({
  178. path: this._uploadPath(part.filename),
  179. name: part.filename,
  180. type: part.mime,
  181. hash: self.hash
  182. });
  183. this.emit('fileBegin', part.name, file);
  184. file.open();
  185. this.openedFiles.push(file);
  186. part.on('data', function(buffer) {
  187. self._fileSize += buffer.length;
  188. if (self._fileSize > self.maxFileSize) {
  189. self._error(new Error('maxFileSize exceeded, received '+self._fileSize+' bytes of file data'));
  190. return;
  191. }
  192. if (buffer.length == 0) {
  193. return;
  194. }
  195. self.pause();
  196. file.write(buffer, function() {
  197. self.resume();
  198. });
  199. });
  200. part.on('end', function() {
  201. file.end(function() {
  202. self._flushing--;
  203. self.emit('file', part.name, file);
  204. self._maybeEnd();
  205. });
  206. });
  207. };
  208. function dummyParser(self) {
  209. return {
  210. end: function () {
  211. self.ended = true;
  212. self._maybeEnd();
  213. return null;
  214. }
  215. };
  216. }
  217. IncomingForm.prototype._parseContentType = function() {
  218. if (this.bytesExpected === 0) {
  219. this._parser = dummyParser(this);
  220. return;
  221. }
  222. if (!this.headers['content-type']) {
  223. this._error(new Error('bad content-type header, no content-type'));
  224. return;
  225. }
  226. if (this.headers['content-type'].match(/octet-stream/i)) {
  227. this._initOctetStream();
  228. return;
  229. }
  230. if (this.headers['content-type'].match(/urlencoded/i)) {
  231. this._initUrlencoded();
  232. return;
  233. }
  234. if (this.headers['content-type'].match(/multipart/i)) {
  235. var m = this.headers['content-type'].match(/boundary=(?:"([^"]+)"|([^;]+))/i);
  236. if (m) {
  237. this._initMultipart(m[1] || m[2]);
  238. } else {
  239. this._error(new Error('bad content-type header, no multipart boundary'));
  240. }
  241. return;
  242. }
  243. if (this.headers['content-type'].match(/json/i)) {
  244. this._initJSONencoded();
  245. return;
  246. }
  247. this._error(new Error('bad content-type header, unknown content-type: '+this.headers['content-type']));
  248. };
  249. IncomingForm.prototype._error = function(err) {
  250. if (this.error || this.ended) {
  251. return;
  252. }
  253. this.error = err;
  254. this.emit('error', err);
  255. if (Array.isArray(this.openedFiles)) {
  256. this.openedFiles.forEach(function(file) {
  257. file._writeStream.destroy();
  258. setTimeout(fs.unlink, 0, file.path, function(error) { });
  259. });
  260. }
  261. };
  262. IncomingForm.prototype._parseContentLength = function() {
  263. this.bytesReceived = 0;
  264. if (this.headers['content-length']) {
  265. this.bytesExpected = parseInt(this.headers['content-length'], 10);
  266. } else if (this.headers['transfer-encoding'] === undefined) {
  267. this.bytesExpected = 0;
  268. }
  269. if (this.bytesExpected !== null) {
  270. this.emit('progress', this.bytesReceived, this.bytesExpected);
  271. }
  272. };
  273. IncomingForm.prototype._newParser = function() {
  274. return new MultipartParser();
  275. };
  276. IncomingForm.prototype._initMultipart = function(boundary) {
  277. this.type = 'multipart';
  278. var parser = new MultipartParser(),
  279. self = this,
  280. headerField,
  281. headerValue,
  282. part;
  283. parser.initWithBoundary(boundary);
  284. parser.onPartBegin = function() {
  285. part = new Stream();
  286. part.readable = true;
  287. part.headers = {};
  288. part.name = null;
  289. part.filename = null;
  290. part.mime = null;
  291. part.transferEncoding = 'binary';
  292. part.transferBuffer = '';
  293. headerField = '';
  294. headerValue = '';
  295. };
  296. parser.onHeaderField = function(b, start, end) {
  297. headerField += b.toString(self.encoding, start, end);
  298. };
  299. parser.onHeaderValue = function(b, start, end) {
  300. headerValue += b.toString(self.encoding, start, end);
  301. };
  302. parser.onHeaderEnd = function() {
  303. headerField = headerField.toLowerCase();
  304. part.headers[headerField] = headerValue;
  305. // matches either a quoted-string or a token (RFC 2616 section 19.5.1)
  306. var m = headerValue.match(/\bname=("([^"]*)"|([^\(\)<>@,;:\\"\/\[\]\?=\{\}\s\t/]+))/i);
  307. if (headerField == 'content-disposition') {
  308. if (m) {
  309. part.name = m[2] || m[3] || '';
  310. }
  311. part.filename = self._fileName(headerValue);
  312. } else if (headerField == 'content-type') {
  313. part.mime = headerValue;
  314. } else if (headerField == 'content-transfer-encoding') {
  315. part.transferEncoding = headerValue.toLowerCase();
  316. }
  317. headerField = '';
  318. headerValue = '';
  319. };
  320. parser.onHeadersEnd = function() {
  321. switch(part.transferEncoding){
  322. case 'binary':
  323. case '7bit':
  324. case '8bit':
  325. parser.onPartData = function(b, start, end) {
  326. part.emit('data', b.slice(start, end));
  327. };
  328. parser.onPartEnd = function() {
  329. part.emit('end');
  330. };
  331. break;
  332. case 'base64':
  333. parser.onPartData = function(b, start, end) {
  334. part.transferBuffer += b.slice(start, end).toString('ascii');
  335. /*
  336. four bytes (chars) in base64 converts to three bytes in binary
  337. encoding. So we should always work with a number of bytes that
  338. can be divided by 4, it will result in a number of buytes that
  339. can be divided vy 3.
  340. */
  341. var offset = parseInt(part.transferBuffer.length / 4, 10) * 4;
  342. part.emit('data', new Buffer(part.transferBuffer.substring(0, offset), 'base64'));
  343. part.transferBuffer = part.transferBuffer.substring(offset);
  344. };
  345. parser.onPartEnd = function() {
  346. part.emit('data', new Buffer(part.transferBuffer, 'base64'));
  347. part.emit('end');
  348. };
  349. break;
  350. default:
  351. return self._error(new Error('unknown transfer-encoding'));
  352. }
  353. self.onPart(part);
  354. };
  355. parser.onEnd = function() {
  356. self.ended = true;
  357. self._maybeEnd();
  358. };
  359. this._parser = parser;
  360. };
  361. IncomingForm.prototype._fileName = function(headerValue) {
  362. // matches either a quoted-string or a token (RFC 2616 section 19.5.1)
  363. var m = headerValue.match(/\bfilename=("(.*?)"|([^\(\)<>@,;:\\"\/\[\]\?=\{\}\s\t/]+))($|;\s)/i);
  364. if (!m) return;
  365. var match = m[2] || m[3] || '';
  366. var filename = match.substr(match.lastIndexOf('\\') + 1);
  367. filename = filename.replace(/%22/g, '"');
  368. filename = filename.replace(/&#([\d]{4});/g, function(m, code) {
  369. return String.fromCharCode(code);
  370. });
  371. return filename;
  372. };
  373. IncomingForm.prototype._initUrlencoded = function() {
  374. this.type = 'urlencoded';
  375. var parser = new QuerystringParser(this.maxFields)
  376. , self = this;
  377. parser.onField = function(key, val) {
  378. self.emit('field', key, val);
  379. };
  380. parser.onEnd = function() {
  381. self.ended = true;
  382. self._maybeEnd();
  383. };
  384. this._parser = parser;
  385. };
  386. IncomingForm.prototype._initOctetStream = function() {
  387. this.type = 'octet-stream';
  388. var filename = this.headers['x-file-name'];
  389. var mime = this.headers['content-type'];
  390. var file = new File({
  391. path: this._uploadPath(filename),
  392. name: filename,
  393. type: mime
  394. });
  395. this.emit('fileBegin', filename, file);
  396. file.open();
  397. this.openedFiles.push(file);
  398. this._flushing++;
  399. var self = this;
  400. self._parser = new OctetParser();
  401. //Keep track of writes that haven't finished so we don't emit the file before it's done being written
  402. var outstandingWrites = 0;
  403. self._parser.on('data', function(buffer){
  404. self.pause();
  405. outstandingWrites++;
  406. file.write(buffer, function() {
  407. outstandingWrites--;
  408. self.resume();
  409. if(self.ended){
  410. self._parser.emit('doneWritingFile');
  411. }
  412. });
  413. });
  414. self._parser.on('end', function(){
  415. self._flushing--;
  416. self.ended = true;
  417. var done = function(){
  418. file.end(function() {
  419. self.emit('file', 'file', file);
  420. self._maybeEnd();
  421. });
  422. };
  423. if(outstandingWrites === 0){
  424. done();
  425. } else {
  426. self._parser.once('doneWritingFile', done);
  427. }
  428. });
  429. };
  430. IncomingForm.prototype._initJSONencoded = function() {
  431. this.type = 'json';
  432. var parser = new JSONParser(this)
  433. , self = this;
  434. parser.onField = function(key, val) {
  435. self.emit('field', key, val);
  436. };
  437. parser.onEnd = function() {
  438. self.ended = true;
  439. self._maybeEnd();
  440. };
  441. this._parser = parser;
  442. };
  443. IncomingForm.prototype._uploadPath = function(filename) {
  444. var buf = crypto.randomBytes(16);
  445. var name = 'upload_' + buf.toString('hex');
  446. if (this.keepExtensions) {
  447. var ext = path.extname(filename);
  448. ext = ext.replace(/(\.[a-z0-9]+).*/i, '$1');
  449. name += ext;
  450. }
  451. return path.join(this.uploadDir, name);
  452. };
  453. IncomingForm.prototype._maybeEnd = function() {
  454. if (!this.ended || this._flushing || this.error) {
  455. return;
  456. }
  457. this.emit('end');
  458. };