1 var CombinedStream = require('combined-stream');
2 var util = require('util');
3 var path = require('path');
4 var http = require('http');
5 var https = require('https');
6 var parseUrl = require('url').parse;
7 var fs = require('fs');
8 var mime = require('mime-types');
9 var async = require('async');
10 var populate = require('./populate.js');
13 module.exports = FormData;
16 util.inherits(FormData, CombinedStream);
19 * Create readable "multipart/form-data" streams.
20 * Can be used to submit forms
21 * and file uploads to other web applications.
26 if (!(this instanceof FormData)) {
27 throw new TypeError('Failed to construct FormData: Please use the _new_ operator, this object constructor cannot be called as a function.');
30 this._overheadLength = 0;
31 this._valueLength = 0;
32 this._lengthRetrievers = [];
34 CombinedStream.call(this);
37 FormData.LINE_BREAK = '\r\n';
38 FormData.DEFAULT_CONTENT_TYPE = 'application/octet-stream';
40 FormData.prototype.append = function(field, value, options) {
42 options = options || {};
44 // allow filename as single option
45 if (typeof options == 'string') {
46 options = {filename: options};
49 var append = CombinedStream.prototype.append.bind(this);
51 // all that streamy business can't handle numbers
52 if (typeof value == 'number') {
56 // https://github.com/felixge/node-form-data/issues/38
57 if (util.isArray(value)) {
58 // Please convert your array into string
59 // the way web server expects it
60 this._error(new Error('Arrays are not supported.'));
64 var header = this._multiPartHeader(field, value, options);
65 var footer = this._multiPartFooter();
71 // pass along options.knownLength
72 this._trackLength(header, value, options);
75 FormData.prototype._trackLength = function(header, value, options) {
78 // used w/ getLengthSync(), when length is known.
79 // e.g. for streaming directly from a remote server,
80 // w/ a known file a size, and not wanting to wait for
81 // incoming file to finish to get its size.
82 if (options.knownLength != null) {
83 valueLength += +options.knownLength;
84 } else if (Buffer.isBuffer(value)) {
85 valueLength = value.length;
86 } else if (typeof value === 'string') {
87 valueLength = Buffer.byteLength(value);
90 this._valueLength += valueLength;
92 // @check why add CRLF? does this account for custom/multiple CRLFs?
93 this._overheadLength +=
94 Buffer.byteLength(header) +
95 FormData.LINE_BREAK.length;
97 // empty or either doesn't have path or not an http response
98 if (!value || ( !value.path && !(value.readable && value.hasOwnProperty('httpVersion')) )) {
102 // no need to bother with the length
103 if (!options.knownLength) {
104 this._lengthRetrievers.push(function(next) {
106 if (value.hasOwnProperty('fd')) {
108 // take read range into a account
109 // `end` = Infinity –> read file till the end
111 // TODO: Looks like there is bug in Node fs.createReadStream
112 // it doesn't respect `end` options without `start` options
113 // Fix it when node fixes it.
114 // https://github.com/joyent/node/issues/7819
115 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 next(null, value.end + 1 - (value.start ? value.start : 0));
122 // not that fast snoopy
124 // still need to fetch file size from fs
125 fs.stat(value.path, function(err, stat) {
134 // update final size based on the range options
135 fileSize = stat.size - (value.start ? value.start : 0);
136 next(null, fileSize);
141 } else if (value.hasOwnProperty('httpVersion')) {
142 next(null, +value.headers['content-length']);
144 // or request stream http://github.com/mikeal/request
145 } else if (value.hasOwnProperty('httpModule')) {
146 // wait till response come back
147 value.on('response', function(response) {
149 next(null, +response.headers['content-length']);
155 next('Unknown stream');
161 FormData.prototype._multiPartHeader = function(field, value, options) {
162 // custom header specified (as string)?
163 // it becomes responsible for boundary
164 // (e.g. to handle extra CRLFs on .NET servers)
165 if (typeof options.header == 'string') {
166 return options.header;
169 var contentDisposition = this._getContentDisposition(value, options);
170 var contentType = this._getContentType(value, options);
174 // add custom disposition as third element or keep it two elements if not
175 'Content-Disposition': ['form-data', 'name="' + field + '"'].concat(contentDisposition || []),
176 // if no content type. allow it to be empty array
177 'Content-Type': [].concat(contentType || [])
180 // allow custom headers.
181 if (typeof options.header == 'object') {
182 populate(headers, options.header);
186 for (var prop in headers) {
187 header = headers[prop];
189 // skip nullish headers.
190 if (header == null) {
194 // convert all headers to arrays.
195 if (!Array.isArray(header)) {
199 // add non-empty headers.
201 contents += prop + ': ' + header.join('; ') + FormData.LINE_BREAK;
205 return '--' + this.getBoundary() + FormData.LINE_BREAK + contents + FormData.LINE_BREAK;
208 FormData.prototype._getContentDisposition = function(value, options) {
210 var contentDisposition;
212 // custom filename takes precedence
213 // fs- and request- streams have path property
214 // formidable and the browser add a name property.
215 var filename = options.filename || value.name || value.path;
217 // or try http response
218 if (!filename && value.readable && value.hasOwnProperty('httpVersion')) {
219 filename = value.client._httpMessage.path;
223 contentDisposition = 'filename="' + path.basename(filename) + '"';
226 return contentDisposition;
229 FormData.prototype._getContentType = function(value, options) {
231 // use custom content-type above all
232 var contentType = options.contentType;
234 // or try `name` from formidable, browser
235 if (!contentType && value.name) {
236 contentType = mime.lookup(value.name);
239 // or try `path` from fs-, request- streams
240 if (!contentType && value.path) {
241 contentType = mime.lookup(value.path);
244 // or if it's http-reponse
245 if (!contentType && value.readable && value.hasOwnProperty('httpVersion')) {
246 contentType = value.headers['content-type'];
249 // or guess it from the filename
250 if (!contentType && options.filename) {
251 contentType = mime.lookup(options.filename);
254 // fallback to the default content type if `value` is not simple value
255 if (!contentType && typeof value == 'object') {
256 contentType = FormData.DEFAULT_CONTENT_TYPE;
262 FormData.prototype._multiPartFooter = function() {
263 return function(next) {
264 var footer = FormData.LINE_BREAK;
266 var lastPart = (this._streams.length === 0);
268 footer += this._lastBoundary();
275 FormData.prototype._lastBoundary = function() {
276 return '--' + this.getBoundary() + '--' + FormData.LINE_BREAK;
279 FormData.prototype.getHeaders = function(userHeaders) {
282 'content-type': 'multipart/form-data; boundary=' + this.getBoundary()
285 for (header in userHeaders) {
286 if (userHeaders.hasOwnProperty(header)) {
287 formHeaders[header.toLowerCase()] = userHeaders[header];
294 // TODO: Looks like unused function
295 FormData.prototype.getCustomHeaders = function(contentType) {
296 contentType = contentType ? contentType : 'multipart/form-data';
299 'content-type': contentType + '; boundary=' + this.getBoundary(),
300 'content-length': this.getLengthSync()
306 FormData.prototype.getBoundary = function() {
307 if (!this._boundary) {
308 this._generateBoundary();
311 return this._boundary;
314 FormData.prototype._generateBoundary = function() {
315 // This generates a 50 character boundary similar to those used by Firefox.
316 // They are optimized for boyer-moore parsing.
317 var boundary = '--------------------------';
318 for (var i = 0; i < 24; i++) {
319 boundary += Math.floor(Math.random() * 10).toString(16);
322 this._boundary = boundary;
325 // Note: getLengthSync DOESN'T calculate streams length
326 // As workaround one can calculate file size manually
327 // and add it as knownLength option
328 FormData.prototype.getLengthSync = function() {
329 var knownLength = this._overheadLength + this._valueLength;
331 // Don't get confused, there are 3 "internal" streams for each keyval pair
332 // so it basically checks if there is any value added to the form
333 if (this._streams.length) {
334 knownLength += this._lastBoundary().length;
337 // https://github.com/form-data/form-data/issues/40
338 if (this._lengthRetrievers.length) {
339 // Some async length retrievers are present
340 // therefore synchronous length calculation is false.
341 // Please use getLength(callback) to get proper length
342 this._error(new Error('Cannot calculate proper length in synchronous way.'));
348 FormData.prototype.getLength = function(cb) {
349 var knownLength = this._overheadLength + this._valueLength;
351 if (this._streams.length) {
352 knownLength += this._lastBoundary().length;
355 if (!this._lengthRetrievers.length) {
356 process.nextTick(cb.bind(this, null, knownLength));
360 async.parallel(this._lengthRetrievers, function(err, values) {
366 values.forEach(function(length) {
367 knownLength += length;
370 cb(null, knownLength);
374 FormData.prototype.submit = function(params, cb) {
377 , defaults = {method: 'post'}
380 // parse provided url if it's string
381 // or treat it as options object
382 if (typeof params == 'string') {
384 params = parseUrl(params);
387 path: params.pathname,
388 host: params.hostname
394 options = populate(params, defaults);
395 // if no port provided use default one
397 options.port = options.protocol == 'https:' ? 443 : 80;
401 // put that good code in getHeaders to some use
402 options.headers = this.getHeaders(params.headers);
404 // https if specified, fallback to http in any other case
405 if (options.protocol == 'https:') {
406 request = https.request(options);
408 request = http.request(options);
411 // get content length and fire away
412 this.getLength(function(err, length) {
418 // add content length
419 request.setHeader('Content-Length', length);
423 request.on('error', cb);
424 request.on('response', cb.bind(this, null));
431 FormData.prototype._error = function(err) {
435 this.emit('error', err);