Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / form-data / lib / form_data.js
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');
11
12 // Public API
13 module.exports = FormData;
14
15 // make it a Stream
16 util.inherits(FormData, CombinedStream);
17
18 /**
19  * Create readable "multipart/form-data" streams.
20  * Can be used to submit forms
21  * and file uploads to other web applications.
22  *
23  * @constructor
24  */
25 function FormData() {
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.');
28   }
29
30   this._overheadLength = 0;
31   this._valueLength = 0;
32   this._lengthRetrievers = [];
33
34   CombinedStream.call(this);
35 }
36
37 FormData.LINE_BREAK = '\r\n';
38 FormData.DEFAULT_CONTENT_TYPE = 'application/octet-stream';
39
40 FormData.prototype.append = function(field, value, options) {
41
42   options = options || {};
43
44   // allow filename as single option
45   if (typeof options == 'string') {
46     options = {filename: options};
47   }
48
49   var append = CombinedStream.prototype.append.bind(this);
50
51   // all that streamy business can't handle numbers
52   if (typeof value == 'number') {
53     value = '' + value;
54   }
55
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.'));
61     return;
62   }
63
64   var header = this._multiPartHeader(field, value, options);
65   var footer = this._multiPartFooter();
66
67   append(header);
68   append(value);
69   append(footer);
70
71   // pass along options.knownLength
72   this._trackLength(header, value, options);
73 };
74
75 FormData.prototype._trackLength = function(header, value, options) {
76   var valueLength = 0;
77
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);
88   }
89
90   this._valueLength += valueLength;
91
92   // @check why add CRLF? does this account for custom/multiple CRLFs?
93   this._overheadLength +=
94     Buffer.byteLength(header) +
95     FormData.LINE_BREAK.length;
96
97   // empty or either doesn't have path or not an http response
98   if (!value || ( !value.path && !(value.readable && value.hasOwnProperty('httpVersion')) )) {
99     return;
100   }
101
102   // no need to bother with the length
103   if (!options.knownLength) {
104     this._lengthRetrievers.push(function(next) {
105
106       if (value.hasOwnProperty('fd')) {
107
108         // take read range into a account
109         // `end` = Infinity –> read file till the end
110         //
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) {
116
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));
121
122         // not that fast snoopy
123         } else {
124           // still need to fetch file size from fs
125           fs.stat(value.path, function(err, stat) {
126
127             var fileSize;
128
129             if (err) {
130               next(err);
131               return;
132             }
133
134             // update final size based on the range options
135             fileSize = stat.size - (value.start ? value.start : 0);
136             next(null, fileSize);
137           });
138         }
139
140       // or http response
141       } else if (value.hasOwnProperty('httpVersion')) {
142         next(null, +value.headers['content-length']);
143
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) {
148           value.pause();
149           next(null, +response.headers['content-length']);
150         });
151         value.resume();
152
153       // something else
154       } else {
155         next('Unknown stream');
156       }
157     });
158   }
159 };
160
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;
167   }
168
169   var contentDisposition = this._getContentDisposition(value, options);
170   var contentType = this._getContentType(value, options);
171
172   var contents = '';
173   var headers  = {
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 || [])
178   };
179
180   // allow custom headers.
181   if (typeof options.header == 'object') {
182     populate(headers, options.header);
183   }
184
185   var header;
186   for (var prop in headers) {
187     header = headers[prop];
188
189     // skip nullish headers.
190     if (header == null) {
191       continue;
192     }
193
194     // convert all headers to arrays.
195     if (!Array.isArray(header)) {
196       header = [header];
197     }
198
199     // add non-empty headers.
200     if (header.length) {
201       contents += prop + ': ' + header.join('; ') + FormData.LINE_BREAK;
202     }
203   }
204
205   return '--' + this.getBoundary() + FormData.LINE_BREAK + contents + FormData.LINE_BREAK;
206 };
207
208 FormData.prototype._getContentDisposition = function(value, options) {
209
210   var contentDisposition;
211
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;
216
217   // or try http response
218   if (!filename && value.readable && value.hasOwnProperty('httpVersion')) {
219     filename = value.client._httpMessage.path;
220   }
221
222   if (filename) {
223     contentDisposition = 'filename="' + path.basename(filename) + '"';
224   }
225
226   return contentDisposition;
227 };
228
229 FormData.prototype._getContentType = function(value, options) {
230
231   // use custom content-type above all
232   var contentType = options.contentType;
233
234   // or try `name` from formidable, browser
235   if (!contentType && value.name) {
236     contentType = mime.lookup(value.name);
237   }
238
239   // or try `path` from fs-, request- streams
240   if (!contentType && value.path) {
241     contentType = mime.lookup(value.path);
242   }
243
244   // or if it's http-reponse
245   if (!contentType && value.readable && value.hasOwnProperty('httpVersion')) {
246     contentType = value.headers['content-type'];
247   }
248
249   // or guess it from the filename
250   if (!contentType && options.filename) {
251     contentType = mime.lookup(options.filename);
252   }
253
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;
257   }
258
259   return contentType;
260 };
261
262 FormData.prototype._multiPartFooter = function() {
263   return function(next) {
264     var footer = FormData.LINE_BREAK;
265
266     var lastPart = (this._streams.length === 0);
267     if (lastPart) {
268       footer += this._lastBoundary();
269     }
270
271     next(footer);
272   }.bind(this);
273 };
274
275 FormData.prototype._lastBoundary = function() {
276   return '--' + this.getBoundary() + '--' + FormData.LINE_BREAK;
277 };
278
279 FormData.prototype.getHeaders = function(userHeaders) {
280   var header;
281   var formHeaders = {
282     'content-type': 'multipart/form-data; boundary=' + this.getBoundary()
283   };
284
285   for (header in userHeaders) {
286     if (userHeaders.hasOwnProperty(header)) {
287       formHeaders[header.toLowerCase()] = userHeaders[header];
288     }
289   }
290
291   return formHeaders;
292 };
293
294 // TODO: Looks like unused function
295 FormData.prototype.getCustomHeaders = function(contentType) {
296   contentType = contentType ? contentType : 'multipart/form-data';
297
298   var formHeaders = {
299     'content-type': contentType + '; boundary=' + this.getBoundary(),
300     'content-length': this.getLengthSync()
301   };
302
303   return formHeaders;
304 };
305
306 FormData.prototype.getBoundary = function() {
307   if (!this._boundary) {
308     this._generateBoundary();
309   }
310
311   return this._boundary;
312 };
313
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);
320   }
321
322   this._boundary = boundary;
323 };
324
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;
330
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;
335   }
336
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.'));
343   }
344
345   return knownLength;
346 };
347
348 FormData.prototype.getLength = function(cb) {
349   var knownLength = this._overheadLength + this._valueLength;
350
351   if (this._streams.length) {
352     knownLength += this._lastBoundary().length;
353   }
354
355   if (!this._lengthRetrievers.length) {
356     process.nextTick(cb.bind(this, null, knownLength));
357     return;
358   }
359
360   async.parallel(this._lengthRetrievers, function(err, values) {
361     if (err) {
362       cb(err);
363       return;
364     }
365
366     values.forEach(function(length) {
367       knownLength += length;
368     });
369
370     cb(null, knownLength);
371   });
372 };
373
374 FormData.prototype.submit = function(params, cb) {
375   var request
376     , options
377     , defaults = {method: 'post'}
378     ;
379
380   // parse provided url if it's string
381   // or treat it as options object
382   if (typeof params == 'string') {
383
384     params = parseUrl(params);
385     options = populate({
386       port: params.port,
387       path: params.pathname,
388       host: params.hostname
389     }, defaults);
390
391   // use custom params
392   } else {
393
394     options = populate(params, defaults);
395     // if no port provided use default one
396     if (!options.port) {
397       options.port = options.protocol == 'https:' ? 443 : 80;
398     }
399   }
400
401   // put that good code in getHeaders to some use
402   options.headers = this.getHeaders(params.headers);
403
404   // https if specified, fallback to http in any other case
405   if (options.protocol == 'https:') {
406     request = https.request(options);
407   } else {
408     request = http.request(options);
409   }
410
411   // get content length and fire away
412   this.getLength(function(err, length) {
413     if (err) {
414       this._error(err);
415       return;
416     }
417
418     // add content length
419     request.setHeader('Content-Length', length);
420
421     this.pipe(request);
422     if (cb) {
423       request.on('error', cb);
424       request.on('response', cb.bind(this, null));
425     }
426   }.bind(this));
427
428   return request;
429 };
430
431 FormData.prototype._error = function(err) {
432   if (!this.error) {
433     this.error = err;
434     this.pause();
435     this.emit('error', err);
436   }
437 };