-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathparser.js
146 lines (132 loc) · 3.95 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
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
var sys = require('sys');
var events = require('events');
var StompFrame = require('./frame').StompFrame;
var ParserStates = {
COMMAND: 0,
HEADERS: 1,
BODY: 2,
ERROR: 3,
};
function StompFrameEmitter(commands) {
events.EventEmitter.call(this);
this.state = ParserStates.COMMAND;
this.frame = new StompFrame();
this.frames = [];
this.buffer = '';
this.commands = commands;
};
sys.inherits(StompFrameEmitter, events.EventEmitter);
StompFrameEmitter.prototype.incrementState = function() {
if (this.state == ParserStates.BODY || this.state == ParserStates.ERROR){
this.state = ParserStates.COMMAND;
}
else {
this.state++;
}
};
StompFrameEmitter.prototype.handleData = function(data) {
this.buffer += data;
do {
if (this.state == ParserStates.COMMAND) {
this.parseCommand();
}
if (this.state == ParserStates.HEADERS) {
this.parseHeaders();
}
if (this.state == ParserStates.BODY) {
this.parseBody();
}
if (this.state == ParserStates.ERROR) {
this.parseError();
}
} while (this.state == ParserStates.COMMAND && this.hasLine());
};
StompFrameEmitter.prototype.hasLine = function() {
return (this.buffer.indexOf('\n') > -1);
};
StompFrameEmitter.prototype.popLine = function () {
var index = this.buffer.indexOf('\n');
var line = this.buffer.slice(0, index);
this.buffer = this.buffer.substr(index + 1);
return line;
};
StompFrameEmitter.prototype.error = function (err) {
this.emit('error', err);
this.state = ParserStates.ERROR;
};
StompFrameEmitter.prototype.parseCommand = function() {
while (this.hasLine()) {
var line = this.popLine();
if (line != '') {
if (this.commands.indexOf(line) == -1) {
this.error({
message: 'No such command',
details: 'Unrecognized Command \'' + line + '\'',
});
break;
}
this.frame.setCommand(line);
this.incrementState();
break;
}
}
};
StompFrameEmitter.prototype.parseHeaders = function() {
while (this.hasLine()) {
var line = this.popLine();
if (line == '') {
this.incrementState();
break;
}
else {
var kv = line.split(':', 2);
if (kv.length != 2) {
this.error({
message: 'Error parsing header',
details: 'No ":" in line "' + line + '"',
});
break;
}
this.frame.setHeader(kv[0], kv[1]);
}
}
};
StompFrameEmitter.prototype.parseBody = function() {
if (this.frame.contentLength > -1) {
var remainingLength = this.frame.contentLength - this.frame.body.length;
this.frame.appendToBody(this.buffer.slice(0, remainingLength));
this.buffer = this.buffer.substr(remainingLength);
if (this.frame.contentLength == this.frame.body.length) {
this.frame.contentLength = -1;
}
else {
return;
}
}
var index = this.buffer.indexOf('\0');
if (index == -1) {
this.frame.appendToBody(this.buffer);
this.buffer = '';
}
else {
// The end of the frame has been identified, finish creating it
this.frame.appendToBody(this.buffer.slice(0, index));
// Emit the frame and reset
this.emit('frame', this.frame);
this.frame = new StompFrame();
this.incrementState();
this.buffer = this.buffer.substr(index + 1);
}
};
StompFrameEmitter.prototype.parseError = function () {
var index = this.buffer.indexOf('\0');
if (index > -1) {
this.buffer = this.buffer.substr(index + 1);
this.incrementState();
}
else {
this.buffer = "";
}
};
new StompFrameEmitter();
exports.StompFrameEmitter = StompFrameEmitter;