form_data.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. 'use strict';
  2. var CombinedStream = require('combined-stream');
  3. var util = require('util');
  4. var path = require('path');
  5. var http = require('http');
  6. var https = require('https');
  7. var parseUrl = require('url').parse;
  8. var fs = require('fs');
  9. var Stream = require('stream').Stream;
  10. var crypto = require('crypto');
  11. var mime = require('mime-types');
  12. var asynckit = require('asynckit');
  13. var setToStringTag = require('es-set-tostringtag');
  14. var hasOwn = require('hasown');
  15. var populate = require('./populate.js');
  16. /**
  17. * Escape CR, LF, and `"` in a multipart `name`/`filename` parameter, so a field
  18. * name or filename can not break out of its header line to inject headers or
  19. * smuggle additional parts. Matches the WHATWG HTML multipart/form-data encoding.
  20. *
  21. * @param {string} str - the parameter value to escape
  22. * @returns {string} the escaped value
  23. */
  24. function escapeHeaderParam(str) {
  25. return String(str).replace(/\r/g, '%0D').replace(/\n/g, '%0A').replace(/"/g, '%22');
  26. }
  27. /**
  28. * Create readable "multipart/form-data" streams.
  29. * Can be used to submit forms
  30. * and file uploads to other web applications.
  31. *
  32. * @constructor
  33. * @param {object} options - Properties to be added/overriden for FormData and CombinedStream
  34. */
  35. function FormData(options) {
  36. if (!(this instanceof FormData)) {
  37. return new FormData(options);
  38. }
  39. this._overheadLength = 0;
  40. this._valueLength = 0;
  41. this._valuesToMeasure = [];
  42. CombinedStream.call(this);
  43. options = options || {}; // eslint-disable-line no-param-reassign
  44. for (var option in options) { // eslint-disable-line no-restricted-syntax
  45. this[option] = options[option];
  46. }
  47. }
  48. // make it a Stream
  49. util.inherits(FormData, CombinedStream);
  50. FormData.LINE_BREAK = '\r\n';
  51. FormData.DEFAULT_CONTENT_TYPE = 'application/octet-stream';
  52. FormData.prototype.append = function (field, value, options) {
  53. options = options || {}; // eslint-disable-line no-param-reassign
  54. // allow filename as single option
  55. if (typeof options === 'string') {
  56. options = { filename: options }; // eslint-disable-line no-param-reassign
  57. }
  58. var append = CombinedStream.prototype.append.bind(this);
  59. // all that streamy business can't handle numbers
  60. if (typeof value === 'number' || value == null) {
  61. value = String(value); // eslint-disable-line no-param-reassign
  62. }
  63. // https://github.com/felixge/node-form-data/issues/38
  64. if (Array.isArray(value)) {
  65. /*
  66. * Please convert your array into string
  67. * the way web server expects it
  68. */
  69. this._error(new Error('Arrays are not supported.'));
  70. return;
  71. }
  72. var header = this._multiPartHeader(field, value, options);
  73. var footer = this._multiPartFooter();
  74. append(header);
  75. append(value);
  76. append(footer);
  77. // pass along options.knownLength
  78. this._trackLength(header, value, options);
  79. };
  80. FormData.prototype._trackLength = function (header, value, options) {
  81. var valueLength = 0;
  82. /*
  83. * used w/ getLengthSync(), when length is known.
  84. * e.g. for streaming directly from a remote server,
  85. * w/ a known file a size, and not wanting to wait for
  86. * incoming file to finish to get its size.
  87. */
  88. if (options.knownLength != null) {
  89. valueLength += Number(options.knownLength);
  90. } else if (Buffer.isBuffer(value)) {
  91. valueLength = value.length;
  92. } else if (typeof value === 'string') {
  93. valueLength = Buffer.byteLength(value);
  94. }
  95. this._valueLength += valueLength;
  96. // @check why add CRLF? does this account for custom/multiple CRLFs?
  97. this._overheadLength += Buffer.byteLength(header) + FormData.LINE_BREAK.length;
  98. // empty or either doesn't have path or not an http response or not a stream
  99. if (!value || (!value.path && !(value.readable && hasOwn(value, 'httpVersion')) && !(value instanceof Stream))) {
  100. return;
  101. }
  102. // no need to bother with the length
  103. if (!options.knownLength) {
  104. this._valuesToMeasure.push(value);
  105. }
  106. };
  107. FormData.prototype._lengthRetriever = function (value, callback) {
  108. if (hasOwn(value, 'fd')) {
  109. // take read range into a account
  110. // `end` = Infinity –> read file till the end
  111. //
  112. // TODO: Looks like there is bug in Node fs.createReadStream
  113. // it doesn't respect `end` options without `start` options
  114. // Fix it when node fixes it.
  115. // https://github.com/joyent/node/issues/7819
  116. if (value.end != undefined && value.end != Infinity && value.start != undefined) {
  117. // when end specified
  118. // no need to calculate range
  119. // inclusive, starts with 0
  120. callback(null, value.end + 1 - (value.start ? value.start : 0)); // eslint-disable-line callback-return
  121. // not that fast snoopy
  122. } else {
  123. // still need to fetch file size from fs
  124. fs.stat(value.path, function (err, stat) {
  125. if (err) {
  126. callback(err);
  127. return;
  128. }
  129. // update final size based on the range options
  130. var fileSize = stat.size - (value.start ? value.start : 0);
  131. callback(null, fileSize);
  132. });
  133. }
  134. // or http response
  135. } else if (hasOwn(value, 'httpVersion')) {
  136. callback(null, Number(value.headers['content-length'])); // eslint-disable-line callback-return
  137. // or request stream http://github.com/mikeal/request
  138. } else if (hasOwn(value, 'httpModule')) {
  139. // wait till response come back
  140. value.on('response', function (response) {
  141. value.pause();
  142. callback(null, Number(response.headers['content-length']));
  143. });
  144. value.resume();
  145. // something else
  146. } else {
  147. callback('Unknown stream'); // eslint-disable-line callback-return
  148. }
  149. };
  150. FormData.prototype._multiPartHeader = function (field, value, options) {
  151. /*
  152. * custom header specified (as string)?
  153. * it becomes responsible for boundary
  154. * (e.g. to handle extra CRLFs on .NET servers)
  155. */
  156. if (typeof options.header === 'string') {
  157. return options.header;
  158. }
  159. var contentDisposition = this._getContentDisposition(value, options);
  160. var contentType = this._getContentType(value, options);
  161. var contents = '';
  162. var headers = {
  163. // add custom disposition as third element or keep it two elements if not
  164. 'Content-Disposition': ['form-data', 'name="' + escapeHeaderParam(field) + '"'].concat(contentDisposition || []),
  165. // if no content type. allow it to be empty array
  166. 'Content-Type': [].concat(contentType || [])
  167. };
  168. // allow custom headers.
  169. if (typeof options.header === 'object') {
  170. populate(headers, options.header);
  171. }
  172. var header;
  173. for (var prop in headers) { // eslint-disable-line no-restricted-syntax
  174. if (hasOwn(headers, prop)) {
  175. header = headers[prop];
  176. // skip nullish headers.
  177. if (header == null) {
  178. continue; // eslint-disable-line no-restricted-syntax, no-continue
  179. }
  180. // convert all headers to arrays.
  181. if (!Array.isArray(header)) {
  182. header = [header];
  183. }
  184. // add non-empty headers.
  185. if (header.length) {
  186. contents += prop + ': ' + header.join('; ') + FormData.LINE_BREAK;
  187. }
  188. }
  189. }
  190. return '--' + this.getBoundary() + FormData.LINE_BREAK + contents + FormData.LINE_BREAK;
  191. };
  192. FormData.prototype._getContentDisposition = function (value, options) { // eslint-disable-line consistent-return
  193. var filename;
  194. if (typeof options.filepath === 'string') {
  195. // custom filepath for relative paths
  196. filename = path.normalize(options.filepath).replace(/\\/g, '/');
  197. } else if (options.filename || (value && (value.name || value.path))) {
  198. /*
  199. * custom filename take precedence
  200. * formidable and the browser add a name property
  201. * fs- and request- streams have path property
  202. */
  203. filename = path.basename(options.filename || (value && (value.name || value.path)));
  204. } else if (value && value.readable && hasOwn(value, 'httpVersion')) {
  205. // or try http response
  206. filename = path.basename(value.client._httpMessage.path || '');
  207. }
  208. if (filename) {
  209. return 'filename="' + escapeHeaderParam(filename) + '"';
  210. }
  211. };
  212. FormData.prototype._getContentType = function (value, options) {
  213. // use custom content-type above all
  214. var contentType = options.contentType;
  215. // or try `name` from formidable, browser
  216. if (!contentType && value && value.name) {
  217. contentType = mime.lookup(value.name);
  218. }
  219. // or try `path` from fs-, request- streams
  220. if (!contentType && value && value.path) {
  221. contentType = mime.lookup(value.path);
  222. }
  223. // or if it's http-reponse
  224. if (!contentType && value && value.readable && hasOwn(value, 'httpVersion')) {
  225. contentType = value.headers['content-type'];
  226. }
  227. // or guess it from the filepath or filename
  228. if (!contentType && (options.filepath || options.filename)) {
  229. contentType = mime.lookup(options.filepath || options.filename);
  230. }
  231. // fallback to the default content type if `value` is not simple value
  232. if (!contentType && value && typeof value === 'object') {
  233. contentType = FormData.DEFAULT_CONTENT_TYPE;
  234. }
  235. return contentType;
  236. };
  237. FormData.prototype._multiPartFooter = function () {
  238. return function (next) {
  239. var footer = FormData.LINE_BREAK;
  240. var lastPart = this._streams.length === 0;
  241. if (lastPart) {
  242. footer += this._lastBoundary();
  243. }
  244. next(footer);
  245. }.bind(this);
  246. };
  247. FormData.prototype._lastBoundary = function () {
  248. return '--' + this.getBoundary() + '--' + FormData.LINE_BREAK;
  249. };
  250. FormData.prototype.getHeaders = function (userHeaders) {
  251. var header;
  252. var formHeaders = {
  253. 'content-type': 'multipart/form-data; boundary=' + this.getBoundary()
  254. };
  255. for (header in userHeaders) { // eslint-disable-line no-restricted-syntax
  256. if (hasOwn(userHeaders, header)) {
  257. formHeaders[header.toLowerCase()] = userHeaders[header];
  258. }
  259. }
  260. return formHeaders;
  261. };
  262. FormData.prototype.setBoundary = function (boundary) {
  263. if (typeof boundary !== 'string') {
  264. throw new TypeError('FormData boundary must be a string');
  265. }
  266. this._boundary = boundary;
  267. };
  268. FormData.prototype.getBoundary = function () {
  269. if (!this._boundary) {
  270. this._generateBoundary();
  271. }
  272. return this._boundary;
  273. };
  274. FormData.prototype.getBuffer = function () {
  275. var dataBuffer = new Buffer.alloc(0); // eslint-disable-line new-cap
  276. var boundary = this.getBoundary();
  277. // Create the form content. Add Line breaks to the end of data.
  278. for (var i = 0, len = this._streams.length; i < len; i++) {
  279. if (typeof this._streams[i] !== 'function') {
  280. // Add content to the buffer.
  281. if (Buffer.isBuffer(this._streams[i])) {
  282. dataBuffer = Buffer.concat([dataBuffer, this._streams[i]]);
  283. } else {
  284. dataBuffer = Buffer.concat([dataBuffer, Buffer.from(this._streams[i])]);
  285. }
  286. // Add break after content.
  287. if (typeof this._streams[i] !== 'string' || this._streams[i].substring(2, boundary.length + 2) !== boundary) {
  288. dataBuffer = Buffer.concat([dataBuffer, Buffer.from(FormData.LINE_BREAK)]);
  289. }
  290. }
  291. }
  292. // Add the footer and return the Buffer object.
  293. return Buffer.concat([dataBuffer, Buffer.from(this._lastBoundary())]);
  294. };
  295. FormData.prototype._generateBoundary = function () {
  296. // This generates a 50 character boundary similar to those used by Firefox.
  297. // They are optimized for boyer-moore parsing.
  298. this._boundary = '--------------------------' + crypto.randomBytes(12).toString('hex');
  299. };
  300. // Note: getLengthSync DOESN'T calculate streams length
  301. // As workaround one can calculate file size manually and add it as knownLength option
  302. FormData.prototype.getLengthSync = function () {
  303. var knownLength = this._overheadLength + this._valueLength;
  304. // Don't get confused, there are 3 "internal" streams for each keyval pair so it basically checks if there is any value added to the form
  305. if (this._streams.length) {
  306. knownLength += this._lastBoundary().length;
  307. }
  308. // https://github.com/form-data/form-data/issues/40
  309. if (!this.hasKnownLength()) {
  310. /*
  311. * Some async length retrievers are present
  312. * therefore synchronous length calculation is false.
  313. * Please use getLength(callback) to get proper length
  314. */
  315. this._error(new Error('Cannot calculate proper length in synchronous way.'));
  316. }
  317. return knownLength;
  318. };
  319. // Public API to check if length of added values is known
  320. // https://github.com/form-data/form-data/issues/196
  321. // https://github.com/form-data/form-data/issues/262
  322. FormData.prototype.hasKnownLength = function () {
  323. var hasKnownLength = true;
  324. if (this._valuesToMeasure.length) {
  325. hasKnownLength = false;
  326. }
  327. return hasKnownLength;
  328. };
  329. FormData.prototype.getLength = function (cb) {
  330. var knownLength = this._overheadLength + this._valueLength;
  331. if (this._streams.length) {
  332. knownLength += this._lastBoundary().length;
  333. }
  334. if (!this._valuesToMeasure.length) {
  335. process.nextTick(cb.bind(this, null, knownLength));
  336. return;
  337. }
  338. asynckit.parallel(this._valuesToMeasure, this._lengthRetriever, function (err, values) {
  339. if (err) {
  340. cb(err);
  341. return;
  342. }
  343. values.forEach(function (length) {
  344. knownLength += length;
  345. });
  346. cb(null, knownLength);
  347. });
  348. };
  349. FormData.prototype.submit = function (params, cb) {
  350. var request;
  351. var options;
  352. var defaults = { method: 'post' };
  353. // parse provided url if it's string or treat it as options object
  354. if (typeof params === 'string') {
  355. params = parseUrl(params); // eslint-disable-line no-param-reassign
  356. /* eslint sort-keys: 0 */
  357. options = populate({
  358. port: params.port,
  359. path: params.pathname,
  360. host: params.hostname,
  361. protocol: params.protocol
  362. }, defaults);
  363. } else { // use custom params
  364. options = populate(params, defaults);
  365. // if no port provided use default one
  366. if (!options.port) {
  367. options.port = options.protocol === 'https:' ? 443 : 80;
  368. }
  369. }
  370. // put that good code in getHeaders to some use
  371. options.headers = this.getHeaders(params.headers);
  372. // https if specified, fallback to http in any other case
  373. if (options.protocol === 'https:') {
  374. request = https.request(options);
  375. } else {
  376. request = http.request(options);
  377. }
  378. // get content length and fire away
  379. this.getLength(function (err, length) {
  380. if (err && err !== 'Unknown stream') {
  381. this._error(err);
  382. return;
  383. }
  384. // add content length
  385. if (length) {
  386. request.setHeader('Content-Length', length);
  387. }
  388. this.pipe(request);
  389. if (cb) {
  390. var onResponse;
  391. var callback = function (error, responce) {
  392. request.removeListener('error', callback);
  393. request.removeListener('response', onResponse);
  394. return cb.call(this, error, responce);
  395. };
  396. onResponse = callback.bind(this, null);
  397. request.on('error', callback);
  398. request.on('response', onResponse);
  399. }
  400. }.bind(this));
  401. return request;
  402. };
  403. FormData.prototype._error = function (err) {
  404. if (!this.error) {
  405. this.error = err;
  406. this.pause();
  407. this.emit('error', err);
  408. }
  409. };
  410. FormData.prototype.toString = function () {
  411. return '[object FormData]';
  412. };
  413. setToStringTag(FormData.prototype, 'FormData');
  414. // Public API
  415. module.exports = FormData;