Skip to content

Commit 98e13d8

Browse files
author
Adam Rudd
committed
Merge pull request mqttjs#94 from adamvr/fix-parse
Fix parse
2 parents 67404f6 + 6fbf115 commit 98e13d8

File tree

2 files changed

+108
-108
lines changed

2 files changed

+108
-108
lines changed

lib/parse.js

Lines changed: 92 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@ module.exports.header = function(buf, packet) {
1212
module.exports.connect = function(buf, packet) {
1313
var pos = 0
1414
, len = buf.length
15-
, protocol_and_len
16-
, topic_and_len
17-
, username_and_len
18-
, client_and_len
19-
, payload_and_len
20-
, password_and_len
15+
, protocolId // Protocol id
16+
, clientId // Client id
17+
, topic // Will topic
18+
, payload // Will payload
19+
, password // Password
20+
, username // Username
2121
, flags = {};
2222

23-
/* Parse protocol id */
24-
protocol_and_len = parse_string(buf, len, pos);
25-
packet.protocolId = protocol_and_len[0];
26-
if(packet.protocolId === null) return null;
27-
pos += protocol_and_len[1] + 2;
23+
// Parse protocol id
24+
protocolId = parse_string(buf, len, pos);
25+
if (protocolId === null) return new Error('Parse error - cannot parse protocol id');
26+
packet.protocolId = protocolId[0];
27+
pos += protocolId[1] + 2;
2828

29-
/* Parse protocol version number */
29+
// Parse protocol version number
3030
if(pos > len) return null;
3131
packet.protocolVersion = buf[pos];
3232
pos += 1;
3333

34-
/* Parse connect flags */
34+
// Parse connect flags
3535
flags.username = (buf[pos] & protocol.USERNAME_MASK);
3636
flags.password = (buf[pos] & protocol.PASSWORD_MASK);
3737
flags.will = (buf[pos] & protocol.WILL_FLAG_MASK);
@@ -45,49 +45,47 @@ module.exports.connect = function(buf, packet) {
4545
packet.clean = (buf[pos] & protocol.CLEAN_SESSION_MASK) !== 0;
4646
pos += 1;
4747

48-
/* Parse keepalive */
48+
// Parse keepalive
4949
packet.keepalive = parse_num(buf, len, pos);
5050
if(packet.keepalive === null) return null;
5151
pos += 2;
5252

53-
/* Parse client ID */
54-
client_and_len = parse_string(buf, len, pos);
55-
packet.clientId = client_and_len[0];
56-
if(packet.clientId === null) return null;
57-
pos += client_and_len[1] + 2;
53+
// Parse client ID
54+
clientId = parse_string(buf, len, pos);
55+
if(clientId === null) return new Error('Parse error - cannot parse client id');
56+
packet.clientId = clientId[0];
57+
pos += clientId[1] + 2;
5858

5959
if(flags.will) {
60-
/* Parse will topic */
61-
topic_and_len = parse_string(buf, len, pos);
62-
packet.will.topic = topic_and_len[0];
63-
if(packet.will.topic === null) return null;
64-
pos += topic_and_len[1] + 2;
65-
66-
/* Parse will payload */
67-
payload_and_len = parse_string(buf, len, pos);
68-
packet.will.payload = payload_and_len[0];
69-
if(packet.will.payload === null) return null;
70-
pos += payload_and_len[1] + 2;
60+
// Parse will topic
61+
topic = parse_string(buf, len, pos);
62+
if(topic === null) return new Error('Parse error - cannot parse will topic');
63+
packet.will.topic = topic[0];
64+
pos += topic[1] + 2;
65+
66+
// Parse will payload
67+
payload = parse_string(buf, len, pos);
68+
if(payload === null) return new Error('Parse error - cannot parse will payload');
69+
packet.will.payload = payload[0];
70+
pos += payload[1] + 2;
7171
}
7272

73-
/* Parse username */
73+
// Parse username
7474
if(flags.username) {
75-
username_and_len = parse_string(buf, len, pos);
76-
packet.username = username_and_len[0];
77-
if(packet.username === null) return null;
78-
pos += username_and_len[1] + 2;
75+
username = parse_string(buf, len, pos);
76+
if(username === null) return new Error('Parse error - cannot parse username');
77+
packet.username = username[0];
78+
pos += username[1] + 2;
7979
}
8080

81-
/* Parse password */
81+
// Parse password
8282
if(flags.password) {
83-
password_and_len = parse_string(buf, len, pos);
84-
packet.password = password_and_len[0];
85-
if(packet.password === null) return null;
86-
pos += password_and_len[1] + 2;
83+
password = parse_string(buf, len, pos);
84+
if(password === null) return ;
85+
packet.password = password[0];
86+
pos += password[1] + 2;
8787
}
8888

89-
pos = len = protocol_and_len = topic_and_len = username_and_len = client_and_len = payload_and_len = password_and_len = flags = buf = null;
90-
9189
return packet;
9290
};
9391

@@ -96,85 +94,78 @@ module.exports.connack = function(buf, packet) {
9694
, pos = 0;
9795

9896
packet.returnCode = parse_num(buf, len, pos);
99-
if(packet.returnCode === null) return null;
100-
101-
buf = len = pos = null;
97+
if(packet.returnCode === null) return new Error('Parse error - cannot parse return code');
10298

10399
return packet;
104100
};
105101

106102
module.exports.publish = function(buf, packet) {
107103
var len = buf.length
108104
, pos = 0
109-
, topic_and_len;
105+
, topic;
110106

111-
/* Parse topic name */
112-
topic_and_len = parse_string(buf, len, pos);
113-
packet.topic = topic_and_len[0];
114-
if(packet.topic === null) return null;
115-
pos += topic_and_len[1] + 2;
107+
// Parse topic name
108+
topic = parse_string(buf, len, pos);
109+
if(topic === null) return null;
110+
packet.topic = topic[0];
111+
pos += topic[1] + 2;
116112

117-
/* Parse message ID */
113+
// Parse message ID
118114
if (packet.qos > 0) {
119115
packet.messageId = parse_num(buf, len, pos);
120-
if(packet.messageId === null) return null;
116+
if(packet.messageId === null) return new Error('Parse error - cannot parse message id');
121117
pos += 2;
122118
}
123119

124-
/* Parse the payload */
120+
// Parse the payload
125121
/* No checks - whatever remains in the packet is the payload */
126122
packet.payload = buf.toString('utf8', pos, len);
127123

128-
buf = len = pos = topic_and_len = null;
129-
130124
return packet;
131125
}
132126

133-
var pubs = ['puback', 'pubrec', 'pubrel', 'pubcomp', 'unsuback'];
134-
135-
for (var i = 0; i < pubs.length; i++) {
136-
module.exports[pubs[i]] = function(buf, packet) {
137-
var len = buf.length
138-
, pos = 0;
127+
// Parse puback, pubrec, pubrel, pubcomp and suback
128+
module.exports.puback =
129+
module.exports.pubrec =
130+
module.exports.pubrel =
131+
module.exports.pubcomp =
132+
module.exports.unsuback = function (buf, packet) {
133+
var len = buf.length
134+
, pos = 0;
139135

140-
packet.messageId = parse_num(buf, len, pos);
141-
if (packet.messageId === null) return null;
142-
143-
buf = len = pos = null;
136+
packet.messageId = parse_num(buf, len, pos);
137+
if (packet.messageId === null) return new Error('Parse error - cannot parse message id');
144138

145-
return packet;
146-
};
147-
}
139+
return packet;
140+
};
148141

149142
module.exports.subscribe = function(buf, packet) {
150143
var len = buf.length
151144
, pos = 0;
152145

153146
packet.subscriptions = [];
154147

155-
/* Parse message ID */
148+
// Parse message ID
156149
packet.messageId = parse_num(buf, len, pos);
157-
if(packet.messageId === null) return null;
150+
if (packet.messageId === null) return new Error('Parse error - cannot parse message id');
158151
pos += 2;
159152

160153
while(pos < len) {
161-
var topic, qos, topic_and_len;
154+
var topic
155+
, qos;
162156

163-
/* Parse topic */
164-
topic_and_len = parse_string(buf, len, pos);
165-
topic = topic_and_len[0];
166-
if(topic === null) return null;
167-
pos += topic_and_len[1] + 2;
157+
// Parse topic
158+
topic = parse_string(buf, len, pos);
159+
if(topic === null) return new Error('Parse error - cannot parse topic');
160+
pos += topic[1] + 2;
168161

169-
/* Parse QoS */
162+
// Parse QoS
163+
// TODO: possible failure location
170164
qos = buf[pos++];
171165

172-
/* Push pair to subscriptions */
173-
packet.subscriptions.push({topic: topic, qos: qos});
166+
// Push pair to subscriptions
167+
packet.subscriptions.push({topic: topic[0], qos: qos});
174168
}
175-
176-
pos = len = buf = null;
177-
178169
return packet;
179170
};
180171

@@ -184,17 +175,15 @@ module.exports.suback = function(buf, packet) {
184175

185176
packet.granted = [];
186177

187-
/* Parse message ID */
178+
// Parse message ID
188179
packet.messageId = parse_num(buf, len, pos);
189-
if(packet.messageId === null) return null;
180+
if(packet.messageId === null) return new Error('Parse error - cannot parse message id');
190181
pos += 2;
191182

183+
// Parse granted QoSes
192184
while(pos < len) {
193185
packet.granted.push(buf[pos++]);
194186
}
195-
196-
pos = len = buf = null;
197-
198187
return packet;
199188
};
200189

@@ -204,26 +193,22 @@ module.exports.unsubscribe = function(buf, packet) {
204193

205194
packet.unsubscriptions = [];
206195

207-
/* Parse message ID */
196+
// Parse message ID
208197
packet.messageId = parse_num(buf, len, pos);
209-
if(packet.messageId === null) return null;
198+
if(packet.messageId === null) return new Error('Parse error - cannot parse message id');
210199
pos += 2;
211200

212201
while(pos < len) {
213-
var topic, topic_and_len;
202+
var topic;
214203

215-
/* Parse topic */
216-
topic_and_len = parse_string(buf, len, pos);
217-
topic = topic_and_len[0];
218-
if(topic === null) return null;
219-
pos += topic_and_len[1] + 2;
220-
221-
/* Push topic to unsubscriptions */
222-
packet.unsubscriptions.push(topic);
204+
// Parse topic
205+
topic = parse_string(buf, len, pos);
206+
if(topic === null) return new Error('Parse error - cannot parse topic');
207+
pos += topic[1] + 2;
208+
209+
// Push topic to unsubscriptions
210+
packet.unsubscriptions.push(topic[0]);
223211
}
224-
225-
pos = len = buf = null;
226-
227212
return packet;
228213
};
229214

@@ -233,12 +218,11 @@ module.exports.reserved = function(buf, packet) {
233218

234219
var empties = ['pingreq', 'pingresp', 'disconnect'];
235220

236-
for (var i = 0; i < empties.length; i++) {
237-
module.exports[empties[i]] = function(buf, packet) {
238-
buf = null;
239-
return packet;
240-
};
241-
}
221+
module.exports.pingreq =
222+
module.exports.pingresp =
223+
module.exports.disconnect = function (buf, packet) {
224+
return packet;
225+
};
242226

243227
var parse_num = function(buf, len, pos) {
244228
if(2 > pos + len) return null;

test/connection.parse.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ module.exports = function() {
9696
done();
9797
});
9898
});
99+
100+
describe('parse errors', function() {
101+
it('should say protocol not parseable', function(done) {
102+
var fixture = [
103+
16, 4,
104+
0, 6,
105+
77, 81
106+
];
107+
108+
this.stream.write(new Buffer(fixture));
109+
this.conn.once('error', function(err) {
110+
err.message.should.match(/cannot parse protocol id/);
111+
done();
112+
});
113+
});
114+
});
99115
});
100116

101117
describe('connack', function() {

0 commit comments

Comments
 (0)