|
| 1 | +var http = require('http'); |
| 2 | +var oop = require('oop'); |
| 3 | +var Stream = require('stream').Stream; |
| 4 | + |
| 5 | +module.exports = FormData; |
| 6 | +oop.extend(FormData, Stream); |
| 7 | +var formData = FormData.prototype; |
| 8 | + |
| 9 | +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; |
| 20 | +} |
| 21 | + |
| 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 | + } |
| 29 | + |
| 30 | + this._partBegin = new Buffer('--' + boundary + '\r\n'); |
| 31 | + this._boundary = boundary; |
| 32 | +}; |
| 33 | + |
| 34 | +formData.getBoundary = function() { |
| 35 | + if (!this._boundary) { |
| 36 | + this._init(); |
| 37 | + } |
| 38 | + |
| 39 | + return this._boundary; |
| 40 | +}; |
| 41 | + |
| 42 | +formData.append = function(name, value) { |
| 43 | + this._parts.push({ |
| 44 | + name: name, |
| 45 | + value: value, |
| 46 | + }); |
| 47 | +}; |
| 48 | + |
| 49 | +formData.pipe = function(destination) { |
| 50 | + var r = Stream.prototype.pipe.call(this, destination); |
| 51 | + this.resume(); |
| 52 | + return r; |
| 53 | +}; |
| 54 | + |
| 55 | +formData.resume = function() { |
| 56 | + if (!this._boundary) { |
| 57 | + this._init(); |
| 58 | + } |
| 59 | + |
| 60 | + this.paused = false; |
| 61 | + process.nextTick(this._emitData.bind(this)); |
| 62 | +}; |
| 63 | + |
| 64 | +formData.pause = function() { |
| 65 | + this.paused = true; |
| 66 | + //if (this._streamPart) { |
| 67 | + //this._streamPart.pause(); |
| 68 | + //} |
| 69 | +}; |
| 70 | + |
| 71 | +formData._emitData = function() { |
| 72 | + if (this.paused) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + var part = this._parts.shift(); |
| 77 | + if (!part) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + if (part.value instanceof Stream) { |
| 82 | + this._emitStreamPart(part); |
| 83 | + } else { |
| 84 | + this._emitRegularPart(part); |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +formData._emitRegularPart = function(part) { |
| 89 | + var header = 'Content-Disposition: form-data; name="' + part.name + '"'; |
| 90 | + header += '\r\n\r\n'; |
| 91 | + |
| 92 | + this.emit('data', this._partBegin); |
| 93 | + this.emit('data', new Buffer(header + part.value + '\r\n', 'utf8')); |
| 94 | + |
| 95 | + if (this._parts.length === 0) { |
| 96 | + this._emitEndBoundary(); |
| 97 | + this.readable = false; |
| 98 | + } |
| 99 | +}; |
| 100 | + |
| 101 | +formData._emitEndBoundary = function() { |
| 102 | + this.emit('data', new Buffer('--' + this._boundary + '--\r\n')); |
| 103 | +}; |
0 commit comments