[Pkg-javascript-commits] [sockjs-client] 211/350: try/catch JSON.parse
tonnerre at ancient-solutions.com
tonnerre at ancient-solutions.com
Fri Aug 5 01:04:22 UTC 2016
This is an automated email from the git hooks/post-receive script.
tonnerre-guest pushed a commit to branch upstream
in repository sockjs-client.
commit e57ca1f3e3b2eb2e3c11d79d6b4a5a07383237d3
Author: Bryce Kahle <bkahle at gmail.com>
Date: Mon Oct 20 23:54:54 2014 -0400
try/catch JSON.parse
---
lib/main.js | 53 ++++++++++++++++++++++++++++++++++---------------
lib/transport/iframe.js | 15 +++++++++-----
2 files changed, 47 insertions(+), 21 deletions(-)
diff --git a/lib/main.js b/lib/main.js
index 3998d52..156cfbf 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -229,32 +229,53 @@ SockJS.prototype._transportTimeout = function() {
SockJS.prototype._transportMessage = function(msg) {
debug('_transportMessage', msg);
- var self = this;
- var type = msg.slice(0, 1);
- var payload;
+ var self = this
+ , type = msg.slice(0, 1)
+ , content = msg.slice(1)
+ , payload
+ ;
+
+ // first check for messages that don't need a payload
switch (type) {
case 'o':
this._open();
- break;
+ return;
+ case 'h':
+ this.dispatchEvent(new Event('heartbeat'));
+ debug('heartbeat', this.transport);
+ return;
+ }
+
+ if (content) {
+ try {
+ payload = JSON.parse(content);
+ } catch (e) {
+ debug('bad json', content);
+ }
+ }
+
+ if (typeof payload === 'undefined') {
+ debug('empty payload', content);
+ return;
+ }
+
+ switch (type) {
case 'a':
- payload = JSON3.parse(msg.slice(1) || '[]');
- payload.forEach(function(p) {
- debug('message', self.transport, p);
- self.dispatchEvent(new TransportMessageEvent(p));
- });
+ if (Array.isArray(payload)) {
+ payload.forEach(function(p) {
+ debug('message', self.transport, p);
+ self.dispatchEvent(new TransportMessageEvent(p));
+ });
+ }
break;
case 'm':
- payload = JSON3.parse(msg.slice(1) || 'null');
debug('message', this.transport, payload);
this.dispatchEvent(new TransportMessageEvent(payload));
break;
case 'c':
- payload = JSON3.parse(msg.slice(1) || '[]');
- this._close(payload[0], payload[1], true);
- break;
- case 'h':
- this.dispatchEvent(new Event('heartbeat'));
- debug('heartbeat', this.transport);
+ if (Array.isArray(payload) && payload.length === 2) {
+ this._close(payload[0], payload[1], true);
+ }
break;
}
};
diff --git a/lib/transport/iframe.js b/lib/transport/iframe.js
index 3373f29..c81f5c4 100644
--- a/lib/transport/iframe.js
+++ b/lib/transport/iframe.js
@@ -72,11 +72,10 @@ IframeTransport.prototype._message = function(e) {
}
var iframeMessage;
- try
- {
+ try {
iframeMessage = JSON3.parse(e.data);
- }
- catch (ignored) {
+ } catch (ignored) {
+ debug('bad json', e.data);
return;
}
@@ -100,7 +99,13 @@ IframeTransport.prototype._message = function(e) {
this.emit('message', iframeMessage.data);
break;
case 'c':
- var cdata = JSON.parse(iframeMessage.data);
+ var cdata;
+ try {
+ cdata = JSON.parse(iframeMessage.data);
+ } catch (ignored) {
+ debug('bad json', iframeMessage.data);
+ return;
+ }
this.emit('close', cdata[0], cdata[1]);
break;
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/sockjs-client.git
More information about the Pkg-javascript-commits
mailing list