forked from TooTallNate/node-cgi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
40 lines (36 loc) · 1.13 KB
/
parser.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
var Stream = require('stream').Stream;
var StreamStack = require('stream-stack').StreamStack;
var HeaderParser = require('header-stack').Parser;
/**
* Parses CGI headers (\n newlines) until a blank line,
* signifying the end of the headers. After the blank line
* is assumed to be the body, which you can use 'pipe()' with.
*/
function Parser(stream) {
StreamStack.call(this, stream, {
data: function(b) { this._onData(b); }
});
this._onData = this._parseHeader;
this._headerParser = new HeaderParser(new Stream(), {
emitFirstLine: false,
strictCRLF: false,
strictSpaceAfterColon: false,
allowFoldedHeaders: false
});
this._headerParser.on('headers', this._onHeadersComplete.bind(this));
}
require('util').inherits(Parser, StreamStack);
module.exports = Parser;
Parser.prototype._proxyData = function(b) {
this.emit('data', b);
}
Parser.prototype._parseHeader = function(chunk) {
this._headerParser.stream.emit('data', chunk);
}
Parser.prototype._onHeadersComplete = function(headers, leftover) {
this._onData = this._proxyData;
this.emit('headers', headers);
if (leftover) {
this._onData(leftover);
}
}