-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiparser.js
302 lines (249 loc) · 9.06 KB
/
multiparser.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
'use strict';
var MessagePart = require('./messagepart.js');
var ContentType = require('content-type');
var EventEmitter = require('events');
var HTTPParser = process.binding('http_parser').HTTPParser;
var singleNewline = new Buffer('\r\n');
var doubleNewline = new Buffer('\r\n\r\n');
var MultiParser = function (boundary) {
// Set up a path of arrays representing the parts of each of the ancestors of
// the current message part. The first element is a root array containing the
// root HTTP message as its only part.
this.path = [];
// And don't forget to keep track of the boundaries separating their parts.
this.boundaries = [];
// Initialize a parser for the root message.
this.initialize();
if (boundary) {
// Skip the headers.
this.parser.execute(doubleNewline);
this.state = MultiParser.states.start;
this.buffer = new Buffer(singleNewline);
this.multi(new Buffer('\r\n--' + boundary));
} else {
throw Error('A boundary should be supplied to the multiparser.');
}
this.message = this.current;
};
MultiParser.prototype = Object.create(EventEmitter.prototype);
var onHeaders = function (list) {
var key;
var content;
for (var i = 0; i < list.length; i = i + 2) {
key = list[i].toString().toLowerCase();
this.multiparser.current.headers[key] = list[i + 1];
}
};
var onHeadersComplete = function (major, minor, list) {
var message = this.multiparser.current;
var content, boundary, last;
onHeaders.call(this, list);
// Check whether a content-type header was received.
if (message.headers['content-type']) {
content = ContentType.parse(message.headers['content-type']);
boundary = content.parameters.boundary;
if (content.type.slice(0, 9) === 'multipart' && boundary) {
// The CR and LF characters should be considered part of the boundary.
this.multiparser.multi(new Buffer('\r\n--' + boundary));
}
}
// Communicate the completion of the headers tot the multiparser.
this.multiparser.headers();
};
var onBody = function (chunk, start, length) {
this.multiparser.current.write(chunk.slice(start, start + length));
};
/**
* Allow pipe chains by forwarding a pipe call on the parser to the underlying
* root message.
*/
MultiParser.prototype.pipe = function (stream, options) {
this.message.pipe(stream, options);
};
/**
* Initialize a new message parser.
*/
MultiParser.prototype.initialize = function () {
// Reset the parser.
this.parser = new HTTPParser(HTTPParser.RESPONSE);
this.parser.multiparser = this;
this.parser.execute(new Buffer('HTTP/1.1 200 OK'));
this.current = new MessagePart();
// Set up the body callbacks.
this.parser[HTTPParser.kOnBody] = onBody;
};
MultiParser.prototype.part = function () {
// Initialize the HTTP parser.
this.initialize();
// Set up the header callbacks.
this.parser[HTTPParser.kOnHeaders] = onHeaders;
this.parser[HTTPParser.kOnHeadersComplete] = onHeadersComplete;
// Put the Parser in the headers state.
this.state = MultiParser.states.headers;
// Only three bytes of context are needed to find the end of the
// headers in the next body part.
this.margin = 3;
};
MultiParser.prototype.headers = function () {
// Transition the Parser to the start state.
this.state = MultiParser.states.start;
// Let the parent message know that we found another part.
this.path[this.path.length - 1].part(this.current);
};
/**
* Initialize a new multipart message. This method should be called when
* encountering a multipart content type, as this requires the body to include
* the closing boundary.
*/
MultiParser.prototype.multi = function (boundary) {
// Add the parts array to the current path in the message tree.
this.path.push(this.current);
this.boundaries.push(boundary);
this.boundary = boundary;
this.current.emit('multi');
// Allow for four extra characters after the boundary (two dashes, a
// carriage return and a line feed at most). Like this we can check the
// presence of a boundary at once and we don't have to cycle the parser
// through different states.
this.margin = this.boundary.length + 3;
};
/**
* Close the current multipart message. To be called when encountering the
* closing multipart boundary.
*/
MultiParser.prototype.pop = function () {
// The parser is currently in the body parsing state. This means we can
// continue using this parser for parsing the body of the parent message.
this.current = this.path.pop();
this.boundaries.pop();
this.margin = this.boundary.length + 3;
};
MultiParser.prototype.trailer = function () {
this.current = this.path[this.path.length - 1].trailer();
};
var onData = function (chunk, start, end) {
var data = chunk.slice(start, end);
var result, consumed;
var error = '';
if (data.length > 0) {
result = this.parser.execute(data);
} else {
result = 0;
}
if (typeof result === 'number') {
consumed = result;
} else {
error += result.toString() + ': ' + result.code;
error += ' after ' + result.bytesParsed + ' bytes';
this.emit('error', Error(error));
consumed = result.bytesParsed;
}
return consumed;
};
/**
* The process function contains the inner loop of the parsing infrastructure.
* It consumes any data which can be guaranteed not to contain a boundary and
* processes the next boundary if one can be found. This method should generally
* not be called externally. When the length of the chunk of data provided is
* shorter than the current parser margin, the behaviour is undefined. The write
* function guarantees this is never the case.
*/
MultiParser.prototype.process = function (data, start) {
// The index is a positional variable inside the buffer, while start
// represents the number of bytes processed from the new chunk of data.
var index, stop;
var offset = 0;
switch (this.state) {
case MultiParser.states.start:
this.state = MultiParser.states.body;
offset = 2;
// Continue processing the data in the next case.
case MultiParser.states.body:
// Find the next boundary occurence.
index = data.indexOf(this.boundary, start);
if (index >= 0 && index < data.length - this.margin) {
var a, b, c, d;
stop = index;
index += this.boundary.length;
a = data[index + 0];
b = data[index + 1];
c = data[index + 2];
d = data[index + 3];
if (a === 13 && b === 10) {
start += onData.call(this, data, start + offset, stop);
// Initialize a parser to accept a new body part.
this.part();
// Advance the position in the current data chunk.
start += this.boundary.length;
} else if (a === 45 && b === 45 && c === 13 && d === 10) {
start += onData.call(this, data, start + offset, stop);
// Warn the multipart message about an upcoming trailer.
this.trailer();
// End the current message part.
this.pop();
// Advance the position in the current data chunk.
start += this.boundary.length + 4 + offset;
} else {
start += onData.call(this, data, start + offset, index) + offset;
}
} else {
start += onData.call(this, data, start + offset, data.length - this.margin) + offset;
}
break;
case MultiParser.states.headers:
// Find the next boundary occurence.
index = data.indexOf(doubleNewline, start);
if (index >= 0 && index < data.length - this.margin) {
start += onData.call(this, data, start, index + 4) - 2;
if (this.state !== MultiParser.states.start) {
throw new Error('Failed to transition into headers state to parse a new body part');
}
this.margin = this.boundary.length + 3;
} else {
start += onData.call(this, data, start, data.length - this.margin);
}
}
return start;
};
/**
* Write a chunk of data to the parser.
*/
MultiParser.prototype.write = function (chunk, encoding, callback) {
chunk = new Buffer(chunk);
// The index is a positional variable inside the buffer, while start
// represents the number of bytes processed from the new chunk of data.
var start = 0, shift;
// The center is a temporary buffer to work with boundaries split over
// multiple chunks of data.
var center;
center = Buffer.concat([this.buffer, chunk.slice(0, this.margin)]);
// The starting position is negative if the lookbehind buffer is not empty.
shift = this.buffer.length;
start = 0;
while (start < center.length - this.margin) {
start = this.process(center, start);
}
start -= shift;
while (start < chunk.length - this.margin) {
start = this.process(chunk, start);
}
// Set a new lookbehind buffer, adding the data that wasn't parsed yet.
center = Buffer.concat([this.buffer, chunk.slice(Math.max(0, start))]);
this.buffer = center.slice(center.length - chunk.length + start);
};
MultiParser.prototype.end = function (chunk, encoding, callback) {
if (chunk) {
this.write(chunk, encoding, callback);
}
if (this.path.length > 0) {
this.emit('error', Error('Not all messages were closed'));
} else {
this.message.end();
}
};
MultiParser.states = {
headers: 0,
start: 1,
body: 2
};
module.exports = MultiParser;