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 (options.header) {
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 for (var prop in headers) {
181 if (headers[prop].length) {
182 contents += prop + ': ' + headers[prop].join('; ') + FormData.LINE_BREAK;
186 return '--' + this.getBoundary() + FormData.LINE_BREAK + contents + FormData.LINE_BREAK;
189 FormData.prototype._getContentDisposition = function(value, options) {
191 var contentDisposition;
193 // custom filename takes precedence
194 // fs- and request- streams have path property
195 var filename = options.filename || value.path;
197 // or try http response
198 if (!filename && value.readable && value.hasOwnProperty('httpVersion')) {
199 filename = value.client._httpMessage.path;
203 contentDisposition = 'filename="' + path.basename(filename) + '"';
206 return contentDisposition;
209 FormData.prototype._getContentType = function(value, options) {
211 // use custom content-type above all
212 var contentType = options.contentType;
214 // or try `path` from fs-, request- streams
215 if (!contentType && value.path) {
216 contentType = mime.lookup(value.path);
219 // or if it's http-reponse
220 if (!contentType && value.readable && value.hasOwnProperty('httpVersion')) {
221 contentType = value.headers['content-type'];
224 // or guess it from the filename
225 if (!contentType && options.filename) {
226 contentType = mime.lookup(options.filename);
229 // fallback to the default content type if `value` is not simple value
230 if (!contentType && typeof value == 'object') {
231 contentType = FormData.DEFAULT_CONTENT_TYPE;
237 FormData.prototype._multiPartFooter = function() {
238 return function(next) {
239 var footer = FormData.LINE_BREAK;
241 var lastPart = (this._streams.length === 0);
243 footer += this._lastBoundary();
250 FormData.prototype._lastBoundary = function() {
251 return '--' + this.getBoundary() + '--' + FormData.LINE_BREAK;
254 FormData.prototype.getHeaders = function(userHeaders) {
257 'content-type': 'multipart/form-data; boundary=' + this.getBoundary()
260 for (header in userHeaders) {
261 if (userHeaders.hasOwnProperty(header)) {
262 formHeaders[header.toLowerCase()] = userHeaders[header];
269 FormData.prototype.getCustomHeaders = function(contentType) {
270 contentType = contentType ? contentType : 'multipart/form-data';
273 'content-type': contentType + '; boundary=' + this.getBoundary(),
274 'content-length': this.getLengthSync()
280 FormData.prototype.getBoundary = function() {
281 if (!this._boundary) {
282 this._generateBoundary();
285 return this._boundary;
288 FormData.prototype._generateBoundary = function() {
289 // This generates a 50 character boundary similar to those used by Firefox.
290 // They are optimized for boyer-moore parsing.
291 var boundary = '--------------------------';
292 for (var i = 0; i < 24; i++) {
293 boundary += Math.floor(Math.random() * 10).toString(16);
296 this._boundary = boundary;
299 // Note: getLengthSync DOESN'T calculate streams length
300 // As workaround one can calculate file size manually
301 // and add it as knownLength option
302 FormData.prototype.getLengthSync = function() {
303 var knownLength = this._overheadLength + this._valueLength;
305 // Don't get confused, there are 3 "internal" streams for each keyval pair
306 // so it basically checks if there is any value added to the form
307 if (this._streams.length) {
308 knownLength += this._lastBoundary().length;
311 // https://github.com/form-data/form-data/issues/40
312 if (this._lengthRetrievers.length) {
313 // Some async length retrievers are present
314 // therefore synchronous length calculation is false.
315 // Please use getLength(callback) to get proper length
316 this._error(new Error('Cannot calculate proper length in synchronous way.'));
322 FormData.prototype.getLength = function(cb) {
323 var knownLength = this._overheadLength + this._valueLength;
325 if (this._streams.length) {
326 knownLength += this._lastBoundary().length;
329 if (!this._lengthRetrievers.length) {
330 process.nextTick(cb.bind(this, null, knownLength));
334 async.parallel(this._lengthRetrievers, function(err, values) {
340 values.forEach(function(length) {
341 knownLength += length;
344 cb(null, knownLength);
348 FormData.prototype.submit = function(params, cb) {
351 , defaults = {method: 'post'}
354 // parse provided url if it's string
355 // or treat it as options object
356 if (typeof params == 'string') {
358 params = parseUrl(params);
361 path: params.pathname,
362 host: params.hostname
368 options = populate(params, defaults);
369 // if no port provided use default one
371 options.port = options.protocol == 'https:' ? 443 : 80;
375 // put that good code in getHeaders to some use
376 options.headers = this.getHeaders(params.headers);
378 // https if specified, fallback to http in any other case
379 if (options.protocol == 'https:') {
380 request = https.request(options);
382 request = http.request(options);
385 // get content length and fire away
386 this.getLength(function(err, length) {
392 // add content length
393 request.setHeader('Content-Length', length);
397 request.on('error', cb);
398 request.on('response', cb.bind(this, null));
405 FormData.prototype._error = function(err) {
409 this.emit('error', err);