This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
multipart.js
317 lines (289 loc) · 10.2 KB
/
multipart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Simple multipart parser for file uploads
// Based loosly on Felix's node-formidable library
// https://github.com/felixge/node-formidable
var Stream = require('stream').Stream;
var EventEmitter = require('events').EventEmitter;
// Given an http request, it returns an event emitter that emits readable streams.
module.exports = newParser
function newParser(req, str) {
var boundary = new Buffer(str);
return new Parser(req, boundary);
};
// Parser states
var DONE = 0x00;
var BOUNDARY_START = 0x01;
var BOUNDARY = 0x02;
var BOUNDARY_END = 0x03;
var HEADER_FIELD = 0x11;
var HEADER_VALUE = 0x12;
var HEADER_VALUE_END = 0x13;
var HEADERS_ALMOST_DONE = 0x14;
var PART_DATA = 0x21;
var PART_END = 0x22;
var END = 0x31;
var states = {};
states[DONE] = "DONE";
states[BOUNDARY_START] = "BOUNDARY_START";
states[BOUNDARY] = "BOUNDARY";
states[BOUNDARY_END] = "BOUNDARY_END";
states[HEADER_FIELD] = "HEADER_FIELD";
states[HEADER_VALUE] = "HEADER_VALUE";
states[HEADER_VALUE_END] = "HEADER_VALUE_END";
states[HEADERS_ALMOST_DONE] = "HEADERS_ALMOST_DONE";
states[PART_DATA] = "PART_DATA";
states[PART_END] = "PART_END";
states[END] = "END";
var inherits = require('util').inherits;
inherits(PartStream, Stream);
function PartStream(input, headers) {
this.headers = headers;
this.readable = true;
this.input = input;
this.events = [];
}
PartStream.prototype.pause = function pause() {
this.input.pause();
};
PartStream.prototype.resume = function resume() {
this.input.resume();
};
inherits(Parser, EventEmitter);
function Parser(input, boundary) {
this.boundary = boundary;
this.input = input;
this.state = BOUNDARY_START;
this.offset = 0; // The current offset in the entire stream
this.index = 0; // index of bytes in current substream
this.chunks = [];
input.on("data", this.onData.bind(this));
input.on("end", this.onEnd.bind(this));
}
Parser.prototype.error = function error(message) {
this.emit("error", new Error(message));
};
// Flush the buffer
Parser.prototype.flush = function flush() {
for (var i = 0, l = this.chunks.length; i < l; i++) {
this.stream.emit("data", this.chunks[i]);
}
this.chunks.length = 0;
};
Parser.prototype.onData = function onData(chunk) {
var boundary = this.boundary;
var stream = this.stream;
var len = chunk.length;
var start; // offset within chunk of start of current piece
var partStart; // offset within chunk of data start
var partEnd;
var leftover;
if (this.leftover !== undefined) {
leftover = this.leftover;
this.leftover = undefined;
start = 0;
}
for (var i = 0; i < len; i++,this.offset++) {
var c = chunk[i];
// console.log({i:i,c:new Buffer([c]).toString(),s:states[this.state],l:leftover,ch:this.chunks});
switch (this.state) {
case BOUNDARY_START: // Matches the "--" prefix before the boundary string
if (c !== 0x2d) {
if (partEnd !== undefined) {
partEnd = undefined;
this.state = PART_DATA;
}
if (this.chunks.length) {
this.flush(); // Flush any pending chunks that we weren't sure about
this.state = PART_DATA;
}
if (this.state === PART_DATA) {
i--;
continue;
}
return this.error("Missing -- before boundary " + this.index);
}
if (this.index === 1) {
this.state = BOUNDARY;
this.index = 0;
} else {
this.index++;
}
break;
case BOUNDARY: // Matches the boundary string
if (c !== boundary[this.index]) {
if (partEnd !== undefined) {
partEnd = undefined;
this.state = PART_DATA;
}
if (this.chunks.length) {
this.flush(); // Flush any pending chunks that we weren't sure about
this.state = PART_DATA;
}
if (this.state === PART_DATA) {
i--;
continue;
}
return this.error("Boundary mismatch " + this.index);
}
if (this.index === boundary.length - 1) {
this.chunks.length = 0; // It was a boundary, throw away the buffer
if (partEnd !== undefined) {
partStart = partStart || 0;
if (partStart < partEnd) {
stream.emit("data", chunk.slice(partStart, partEnd));
}
partStart = undefined;
partEnd = undefined;
}
if (stream) {
stream.emit("end");
this.stream = undefined;
stream = undefined;
}
this.state = BOUNDARY_END;
this.index = 0;
} else {
this.index++;
}
break;
case BOUNDARY_END: // Matches the \r\n after the boundary
if (c === 0x2d) { // -
this.state = END;
this.index = 0;
continue;
}
if (c !== (this.index === 0 ? 0x0d : 0x0a)) {
return this.error("Missing \\r\\n after boundary " + this.index);
}
if (this.index === 1) {
this.state = HEADER_FIELD;
this.index = 0;
this.headers = {};
start = i + 1;
} else {
this.index++;
}
break;
case HEADER_FIELD:
if (start === i && c === 0x0d) { // \r
this.state = HEADERS_ALMOST_DONE;
start = undefined;
continue;
}
if (c !== 0x3a) continue; // Eat everything up to :
var field = chunk.toString("ascii", start, i);
if (leftover !== undefined) {
field = leftover + field;
leftover = undefined;
}
this.field = field;
this.state = HEADER_VALUE;
start = i + 1;
break;
case HEADER_VALUE:
if (c === 0x20 && start === i && !leftover) {
start = i + 1; // left trim
}
if (c !== 0x0d) continue; // Eat everything up to \r
var value = chunk.toString("ascii", start, i);
if (leftover !== undefined) {
value = leftover + value;
leftover = undefined;
}
this.headers[this.field.toLowerCase()] = value;
this.field = undefined;
start = undefined;
this.state = HEADER_VALUE_END;
break;
case HEADER_VALUE_END:
if (c !== 0x0a) {
return this.error("Missing \\r\\n after header");
}
start = i + 1;
this.state = HEADER_FIELD;
break;
case HEADERS_ALMOST_DONE:
if (c !== 0x0a) {
return this.error("Missing \\r\\n after headers");
}
stream = new PartStream(this.input, this.headers);
this.stream = stream;
this.headers = undefined;
this.emit("part", stream);
this.state = PART_DATA;
partStart = i + 1;
break;
case PART_DATA:
if (c !== 0x0d) continue; // \r
// This might be the end of the data, we're not sure yet
partEnd = i;
// Start checking if this is the end of the body
this.state = PART_END;
break;
case PART_END:
if (c === 0x0a) {
this.state = BOUNDARY_START;
this.index = 0;
} else {
if (partEnd !== undefined) {
partEnd = undefined;
}
if (this.chunks.length) {
this.flush(); // Flush any pending chunks that we weren't sure about
}
this.state = PART_DATA;
i--;
}
break;
case END:
if (c !== (this.index === 0 ? 0x2d : this.index === 1 ? 0x0d : 0x0a)) {
return this.error("Missing --\r\n after closing boundary");
}
if (this.index === 2) {
this.state = DONE;
continue;
}
this.index++;
break;
case DONE:
return this.error("Trailing data after end");
default:
this.error("Unknown parser state " + this.state);
break;
}
}
// At end of input chunk, we need to handle leftovers.
// If we were parsing a body, just emit what we got.
if (this.state === PART_DATA) {
if (!partStart) { // The entire chunk was data
stream.emit("data", chunk);
} else if (partStart < chunk.length) { // The data started within this chunk
stream.emit("data", chunk.slice(partStart));
}
} else {
// We're still checking to see if chunk bytes are boundary or data
if (partEnd !== undefined) {
// Flush the part we're sure is body
partStart = partStart || 0;
if (partStart < partEnd) {
stream.emit("data", chunk.slice(partStart, partEnd));
}
// Buffer the rest
if (partEnd < chunk.length) {
this.chunks.push(chunk.slice(partEnd));
}
} else if (this.chunks.length) {
this.chunks.push(chunk);
}
}
if (start !== undefined) {
this.leftover = (leftover || "") + chunk.toString("ascii", start);
} else if (leftover) {
this.leftover = leftover;
}
};
Parser.prototype.onEnd = function onEnd() {
if (this.state !== DONE) {
this.error("Unexpected EOF in input stream");
}
this.emit("end");
};