]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/request/node_modules/form-data/lib/form_data.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / request / 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 (options.header) {
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   for (var prop in headers) {
181     if (headers[prop].length) {
182       contents += prop + ': ' + headers[prop].join('; ') + FormData.LINE_BREAK;
183     }
184   }
185
186   return '--' + this.getBoundary() + FormData.LINE_BREAK + contents + FormData.LINE_BREAK;
187 };
188
189 FormData.prototype._getContentDisposition = function(value, options) {
190
191   var contentDisposition;
192
193   // custom filename takes precedence
194   // fs- and request- streams have path property
195   var filename = options.filename || value.path;
196
197   // or try http response
198   if (!filename && value.readable && value.hasOwnProperty('httpVersion')) {
199     filename = value.client._httpMessage.path;
200   }
201
202   if (filename) {
203     contentDisposition = 'filename="' + path.basename(filename) + '"';
204   }
205
206   return contentDisposition;
207 };
208
209 FormData.prototype._getContentType = function(value, options) {
210
211   // use custom content-type above all
212   var contentType = options.contentType;
213
214   // or try `path` from fs-, request- streams
215   if (!contentType && value.path) {
216     contentType = mime.lookup(value.path);
217   }
218
219   // or if it's http-reponse
220   if (!contentType && value.readable && value.hasOwnProperty('httpVersion')) {
221     contentType = value.headers['content-type'];
222   }
223
224   // or guess it from the filename
225   if (!contentType && options.filename) {
226     contentType = mime.lookup(options.filename);
227   }
228
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;
232   }
233
234   return contentType;
235 };
236
237 FormData.prototype._multiPartFooter = function() {
238   return function(next) {
239     var footer = FormData.LINE_BREAK;
240
241     var lastPart = (this._streams.length === 0);
242     if (lastPart) {
243       footer += this._lastBoundary();
244     }
245
246     next(footer);
247   }.bind(this);
248 };
249
250 FormData.prototype._lastBoundary = function() {
251   return '--' + this.getBoundary() + '--' + FormData.LINE_BREAK;
252 };
253
254 FormData.prototype.getHeaders = function(userHeaders) {
255   var header;
256   var formHeaders = {
257     'content-type': 'multipart/form-data; boundary=' + this.getBoundary()
258   };
259
260   for (header in userHeaders) {
261     if (userHeaders.hasOwnProperty(header)) {
262       formHeaders[header.toLowerCase()] = userHeaders[header];
263     }
264   }
265
266   return formHeaders;
267 };
268
269 FormData.prototype.getCustomHeaders = function(contentType) {
270   contentType = contentType ? contentType : 'multipart/form-data';
271
272   var formHeaders = {
273     'content-type': contentType + '; boundary=' + this.getBoundary(),
274     'content-length': this.getLengthSync()
275   };
276
277   return formHeaders;
278 };
279
280 FormData.prototype.getBoundary = function() {
281   if (!this._boundary) {
282     this._generateBoundary();
283   }
284
285   return this._boundary;
286 };
287
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);
294   }
295
296   this._boundary = boundary;
297 };
298
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;
304
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;
309   }
310
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.'));
317   }
318
319   return knownLength;
320 };
321
322 FormData.prototype.getLength = function(cb) {
323   var knownLength = this._overheadLength + this._valueLength;
324
325   if (this._streams.length) {
326     knownLength += this._lastBoundary().length;
327   }
328
329   if (!this._lengthRetrievers.length) {
330     process.nextTick(cb.bind(this, null, knownLength));
331     return;
332   }
333
334   async.parallel(this._lengthRetrievers, function(err, values) {
335     if (err) {
336       cb(err);
337       return;
338     }
339
340     values.forEach(function(length) {
341       knownLength += length;
342     });
343
344     cb(null, knownLength);
345   });
346 };
347
348 FormData.prototype.submit = function(params, cb) {
349   var request
350     , options
351     , defaults = {method: 'post'}
352     ;
353
354   // parse provided url if it's string
355   // or treat it as options object
356   if (typeof params == 'string') {
357
358     params = parseUrl(params);
359     options = populate({
360       port: params.port,
361       path: params.pathname,
362       host: params.hostname
363     }, defaults);
364
365   // use custom params
366   } else {
367
368     options = populate(params, defaults);
369     // if no port provided use default one
370     if (!options.port) {
371       options.port = options.protocol == 'https:' ? 443 : 80;
372     }
373   }
374
375   // put that good code in getHeaders to some use
376   options.headers = this.getHeaders(params.headers);
377
378   // https if specified, fallback to http in any other case
379   if (options.protocol == 'https:') {
380     request = https.request(options);
381   } else {
382     request = http.request(options);
383   }
384
385   // get content length and fire away
386   this.getLength(function(err, length) {
387     if (err) {
388       this._error(err);
389       return;
390     }
391
392     // add content length
393     request.setHeader('Content-Length', length);
394
395     this.pipe(request);
396     if (cb) {
397       request.on('error', cb);
398       request.on('response', cb.bind(this, null));
399     }
400   }.bind(this));
401
402   return request;
403 };
404
405 FormData.prototype._error = function(err) {
406   if (!this.error) {
407     this.error = err;
408     this.pause();
409     this.emit('error', err);
410   }
411 };