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 asynckit = require('asynckit');
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 return new FormData();
30 this._overheadLength = 0;
31 this._valueLength = 0;
32 this._valuesToMeasure = [];
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._valuesToMeasure.push(value);
108 FormData.prototype._lengthRetriever = function(value, callback) {
110 if (value.hasOwnProperty('fd')) {
112 // take read range into a account
113 // `end` = Infinity –> read file till the end
115 // TODO: Looks like there is bug in Node fs.createReadStream
116 // it doesn't respect `end` options without `start` options
117 // Fix it when node fixes it.
118 // https://github.com/joyent/node/issues/7819
119 if (value.end != undefined && value.end != Infinity && value.start != undefined) {
121 // when end specified
122 // no need to calculate range
123 // inclusive, starts with 0
124 callback(null, value.end + 1 - (value.start ? value.start : 0));
126 // not that fast snoopy
128 // still need to fetch file size from fs
129 fs.stat(value.path, function(err, stat) {
138 // update final size based on the range options
139 fileSize = stat.size - (value.start ? value.start : 0);
140 callback(null, fileSize);
145 } else if (value.hasOwnProperty('httpVersion')) {
146 callback(null, +value.headers['content-length']);
148 // or request stream http://github.com/mikeal/request
149 } else if (value.hasOwnProperty('httpModule')) {
150 // wait till response come back
151 value.on('response', function(response) {
153 callback(null, +response.headers['content-length']);
159 callback('Unknown stream');
163 FormData.prototype._multiPartHeader = function(field, value, options) {
164 // custom header specified (as string)?
165 // it becomes responsible for boundary
166 // (e.g. to handle extra CRLFs on .NET servers)
167 if (typeof options.header == 'string') {
168 return options.header;
171 var contentDisposition = this._getContentDisposition(value, options);
172 var contentType = this._getContentType(value, options);
176 // add custom disposition as third element or keep it two elements if not
177 'Content-Disposition': ['form-data', 'name="' + field + '"'].concat(contentDisposition || []),
178 // if no content type. allow it to be empty array
179 'Content-Type': [].concat(contentType || [])
182 // allow custom headers.
183 if (typeof options.header == 'object') {
184 populate(headers, options.header);
188 for (var prop in headers) {
189 header = headers[prop];
191 // skip nullish headers.
192 if (header == null) {
196 // convert all headers to arrays.
197 if (!Array.isArray(header)) {
201 // add non-empty headers.
203 contents += prop + ': ' + header.join('; ') + FormData.LINE_BREAK;
207 return '--' + this.getBoundary() + FormData.LINE_BREAK + contents + FormData.LINE_BREAK;
210 FormData.prototype._getContentDisposition = function(value, options) {
212 var contentDisposition;
214 // custom filename takes precedence
215 // fs- and request- streams have path property
216 // formidable and the browser add a name property.
217 var filename = options.filename || value.name || value.path;
219 // or try http response
220 if (!filename && value.readable && value.hasOwnProperty('httpVersion')) {
221 filename = value.client._httpMessage.path;
225 contentDisposition = 'filename="' + path.basename(filename) + '"';
228 return contentDisposition;
231 FormData.prototype._getContentType = function(value, options) {
233 // use custom content-type above all
234 var contentType = options.contentType;
236 // or try `name` from formidable, browser
237 if (!contentType && value.name) {
238 contentType = mime.lookup(value.name);
241 // or try `path` from fs-, request- streams
242 if (!contentType && value.path) {
243 contentType = mime.lookup(value.path);
246 // or if it's http-reponse
247 if (!contentType && value.readable && value.hasOwnProperty('httpVersion')) {
248 contentType = value.headers['content-type'];
251 // or guess it from the filename
252 if (!contentType && options.filename) {
253 contentType = mime.lookup(options.filename);
256 // fallback to the default content type if `value` is not simple value
257 if (!contentType && typeof value == 'object') {
258 contentType = FormData.DEFAULT_CONTENT_TYPE;
264 FormData.prototype._multiPartFooter = function() {
265 return function(next) {
266 var footer = FormData.LINE_BREAK;
268 var lastPart = (this._streams.length === 0);
270 footer += this._lastBoundary();
277 FormData.prototype._lastBoundary = function() {
278 return '--' + this.getBoundary() + '--' + FormData.LINE_BREAK;
281 FormData.prototype.getHeaders = function(userHeaders) {
284 'content-type': 'multipart/form-data; boundary=' + this.getBoundary()
287 for (header in userHeaders) {
288 if (userHeaders.hasOwnProperty(header)) {
289 formHeaders[header.toLowerCase()] = userHeaders[header];
296 FormData.prototype.getBoundary = function() {
297 if (!this._boundary) {
298 this._generateBoundary();
301 return this._boundary;
304 FormData.prototype._generateBoundary = function() {
305 // This generates a 50 character boundary similar to those used by Firefox.
306 // They are optimized for boyer-moore parsing.
307 var boundary = '--------------------------';
308 for (var i = 0; i < 24; i++) {
309 boundary += Math.floor(Math.random() * 10).toString(16);
312 this._boundary = boundary;
315 // Note: getLengthSync DOESN'T calculate streams length
316 // As workaround one can calculate file size manually
317 // and add it as knownLength option
318 FormData.prototype.getLengthSync = function() {
319 var knownLength = this._overheadLength + this._valueLength;
321 // Don't get confused, there are 3 "internal" streams for each keyval pair
322 // so it basically checks if there is any value added to the form
323 if (this._streams.length) {
324 knownLength += this._lastBoundary().length;
327 // https://github.com/form-data/form-data/issues/40
328 if (!this.hasKnownLength()) {
329 // Some async length retrievers are present
330 // therefore synchronous length calculation is false.
331 // Please use getLength(callback) to get proper length
332 this._error(new Error('Cannot calculate proper length in synchronous way.'));
338 // Public API to check if length of added values is known
339 // https://github.com/form-data/form-data/issues/196
340 // https://github.com/form-data/form-data/issues/262
341 FormData.prototype.hasKnownLength = function() {
342 var hasKnownLength = true;
344 if (this._valuesToMeasure.length) {
345 hasKnownLength = false;
348 return hasKnownLength;
351 FormData.prototype.getLength = function(cb) {
352 var knownLength = this._overheadLength + this._valueLength;
354 if (this._streams.length) {
355 knownLength += this._lastBoundary().length;
358 if (!this._valuesToMeasure.length) {
359 process.nextTick(cb.bind(this, null, knownLength));
363 asynckit.parallel(this._valuesToMeasure, this._lengthRetriever, function(err, values) {
369 values.forEach(function(length) {
370 knownLength += length;
373 cb(null, knownLength);
377 FormData.prototype.submit = function(params, cb) {
380 , defaults = {method: 'post'}
383 // parse provided url if it's string
384 // or treat it as options object
385 if (typeof params == 'string') {
387 params = parseUrl(params);
390 path: params.pathname,
391 host: params.hostname
397 options = populate(params, defaults);
398 // if no port provided use default one
400 options.port = options.protocol == 'https:' ? 443 : 80;
404 // put that good code in getHeaders to some use
405 options.headers = this.getHeaders(params.headers);
407 // https if specified, fallback to http in any other case
408 if (options.protocol == 'https:') {
409 request = https.request(options);
411 request = http.request(options);
414 // get content length and fire away
415 this.getLength(function(err, length) {
421 // add content length
422 request.setHeader('Content-Length', length);
426 request.on('error', cb);
427 request.on('response', cb.bind(this, null));
434 FormData.prototype._error = function(err) {
438 this.emit('error', err);