|
1 | | -var http = require('http'); |
2 | | -var oop = require('oop'); |
3 | | -var Stream = require('stream').Stream; |
| 1 | +var CombinedStream = require('combined-stream'); |
| 2 | +var util = require('util'); |
| 3 | +var path = require('path'); |
| 4 | +var mime = require('mime'); |
4 | 5 |
|
5 | 6 | module.exports = FormData; |
6 | | -oop.extend(FormData, Stream); |
7 | | -var formData = FormData.prototype; |
8 | | - |
9 | 7 | function FormData() { |
10 | | - Stream.call(this); |
11 | | - |
12 | | - this.readable = true; |
13 | | - this.paused = true; |
14 | | - |
15 | | - this._parts = []; |
16 | | - this._streamPart = null; |
17 | | - |
18 | | - this._boundary = null; |
19 | | - this._partBegin = null; |
| 8 | + CombinedStream.call(this); |
20 | 9 | } |
| 10 | +util.inherits(FormData, CombinedStream); |
21 | 11 |
|
22 | | -formData._init = function() { |
23 | | - // This generates a 50 character boundary similar to those used by Firefox. |
24 | | - // They are optimized for boyer-moore parsing. |
25 | | - var boundary = '--------------------------'; |
26 | | - for (var i = 0; i < 24; i++) { |
27 | | - boundary += Math.floor(Math.random() * 10).toString(16); |
28 | | - } |
| 12 | +FormData.prototype.append = function(field, value) { |
| 13 | + var parentAppend = CombinedStream.prototype.append; |
29 | 14 |
|
30 | | - this._partBegin = new Buffer('--' + boundary + '\r\n'); |
31 | | - this._boundary = boundary; |
| 15 | + parentAppend.call(this, this._multiPartHeader.bind(this, field, value)); |
| 16 | + parentAppend.call(this, value); |
| 17 | + parentAppend.call(this, this._multiPartFooter.bind(this, field, value)); |
32 | 18 | }; |
33 | 19 |
|
34 | | -formData.getBoundary = function() { |
35 | | - if (!this._boundary) { |
36 | | - this._init(); |
| 20 | +FormData.prototype._multiPartHeader = function(field, value, next) { |
| 21 | + var boundary = this.getBoundary(); |
| 22 | + |
| 23 | + var header = |
| 24 | + '--' + boundary |
| 25 | + + '\r\n' |
| 26 | + + 'Content-Disposition: form-data; name="' + field + '"'; |
| 27 | + |
| 28 | + if (value.path) { |
| 29 | + header += |
| 30 | + '; filename="' + path.basename(value.path) + '"\r\n' |
| 31 | + + 'Content-Type: ' + mime.lookup(value.path); |
37 | 32 | } |
38 | 33 |
|
39 | | - return this._boundary; |
40 | | -}; |
| 34 | + header += '\r\n\r\n'; |
41 | 35 |
|
42 | | -formData.append = function(name, value) { |
43 | | - this._parts.push({ |
44 | | - name: name, |
45 | | - value: value, |
46 | | - }); |
| 36 | + next(header); |
47 | 37 | }; |
48 | 38 |
|
49 | | -formData.pipe = function(destination) { |
50 | | - var r = Stream.prototype.pipe.call(this, destination); |
51 | | - this.resume(); |
52 | | - return r; |
53 | | -}; |
| 39 | +FormData.prototype._multiPartFooter = function(field, value, next) { |
| 40 | + var footer = '\r\n'; |
54 | 41 |
|
55 | | -formData.resume = function() { |
56 | | - if (!this._boundary) { |
57 | | - this._init(); |
| 42 | + var lastPart = (this._streams.length === 0); |
| 43 | + if (lastPart) { |
| 44 | + footer += '--' + this.getBoundary() + '--'; |
58 | 45 | } |
59 | 46 |
|
60 | | - this.paused = false; |
61 | | - process.nextTick(this._emitData.bind(this)); |
| 47 | + next(footer); |
62 | 48 | }; |
63 | 49 |
|
64 | | -formData.pause = function() { |
65 | | - this.paused = true; |
66 | | - //if (this._streamPart) { |
67 | | - //this._streamPart.pause(); |
68 | | - //} |
69 | | -}; |
| 50 | +FormData.prototype.getHeaders = function(userHeaders) { |
| 51 | + var formHeaders = { |
| 52 | + 'content-type': 'multipart/form-data; boundary=' + this.getBoundary(), |
| 53 | + }; |
70 | 54 |
|
71 | | -formData._emitData = function() { |
72 | | - if (this.paused) { |
73 | | - return; |
| 55 | + for (var header in userHeaders) { |
| 56 | + formHeaders[header.toLowerCase()] = userHeaders[header]; |
74 | 57 | } |
75 | 58 |
|
76 | | - var part = this._parts.shift(); |
77 | | - if (!part) { |
78 | | - return; |
79 | | - } |
| 59 | + return formHeaders; |
| 60 | +} |
80 | 61 |
|
81 | | - if (part.value instanceof Stream) { |
82 | | - this._emitStreamPart(part); |
83 | | - } else { |
84 | | - this._emitRegularPart(part); |
| 62 | +FormData.prototype.getBoundary = function() { |
| 63 | + if (!this._boundary) { |
| 64 | + this._generateBoundary(); |
85 | 65 | } |
86 | | -}; |
87 | | - |
88 | | -formData._emitRegularPart = function(part) { |
89 | | - var header = 'Content-Disposition: form-data; name="' + part.name + '"'; |
90 | | - header += '\r\n\r\n'; |
91 | 66 |
|
92 | | - this.emit('data', this._partBegin); |
93 | | - this.emit('data', new Buffer(header + part.value + '\r\n', 'utf8')); |
| 67 | + return this._boundary; |
| 68 | +}; |
94 | 69 |
|
95 | | - if (this._parts.length === 0) { |
96 | | - this._emitEndBoundary(); |
97 | | - this.readable = false; |
98 | | - } else { |
99 | | - this._emitData(); |
| 70 | +FormData.prototype._generateBoundary = function() { |
| 71 | + // This generates a 50 character boundary similar to those used by Firefox. |
| 72 | + // They are optimized for boyer-moore parsing. |
| 73 | + var boundary = '--------------------------'; |
| 74 | + for (var i = 0; i < 24; i++) { |
| 75 | + boundary += Math.floor(Math.random() * 10).toString(16); |
100 | 76 | } |
101 | | -}; |
102 | 77 |
|
103 | | -formData._emitEndBoundary = function() { |
104 | | - this.emit('data', new Buffer('--' + this._boundary + '--\r\n')); |
| 78 | + this._boundary = boundary; |
105 | 79 | }; |
0 commit comments