From 988295e04de29fe1b9f6b79fa89ecd688da55a02 Mon Sep 17 00:00:00 2001 From: Jaakko Manninen Date: Mon, 11 Apr 2011 12:41:21 +0300 Subject: [PATCH 1/2] more parsing fixes: match VALUE and END more accurately --- lib/memcache.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/memcache.js b/lib/memcache.js index 3c80dba..841a687 100644 --- a/lib/memcache.js +++ b/lib/memcache.js @@ -271,14 +271,15 @@ Client.prototype.handle_get = function(buffer) { var end_indicator_len = 3; var result_len = 0; - if (buffer.indexOf('END') == 0) { + if (buffer.indexOf('END'+crlf) == 0) { return [result_value, end_indicator_len + crlf_len]; - } else if (buffer.indexOf('VALUE') == 0 && buffer.indexOf('END') != -1) { - first_line_len = buffer.indexOf(crlf) + crlf_len; - var end_indicator_start = buffer.indexOf('END'); - result_len = end_indicator_start - first_line_len - crlf_len; + } else if (buffer.indexOf('VALUE ') == 0 && buffer.indexOf(crlf+'END'+crlf) != -1) { + var first_line_len = buffer.indexOf(crlf) + crlf_len; + var end_indicator_start = buffer.indexOf(crlf+'END'+crlf); + result_len = end_indicator_start - first_line_len; result_value = buffer.substr(first_line_len, result_len); - return [result_value, first_line_len + parseInt(result_len, 10) + crlf_len + end_indicator_len + crlf_len] + next_result_at = first_line_len + parseInt(result_len, 10) + crlf_len + end_indicator_len + crlf_len; + return [result_value, next_result_at]; } else { var first_line_len = buffer.indexOf(crlf) + crlf_len; var result_len = buffer.substr(0, first_line_len).split(' ')[3]; @@ -291,12 +292,12 @@ Client.prototype.handle_get = function(buffer) { Client.prototype.handle_stats = function(buffer){ // special case - no stats at all - if (buffer.indexOf('END') == 0){ + if (buffer.indexOf('END'+crlf) == 0){ return [{}, 5]; } // find the terminator - var idx = buffer.indexOf('\r\nEND\r\n'); + var idx = buffer.indexOf(crlf+'END'+crlf); if (idx == -1){ // wait for more data if we don't have an end yet return null; From fcbe062fcd705271a52a49593c0877c94d882bd9 Mon Sep 17 00:00:00 2001 From: Jaakko Manninen Date: Fri, 11 Nov 2011 14:52:35 +0200 Subject: [PATCH 2/2] fix crash when querying on closed connection --- lib/memcache.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/memcache.js b/lib/memcache.js index 51aeac9..1a1be03 100644 --- a/lib/memcache.js +++ b/lib/memcache.js @@ -107,7 +107,8 @@ Client.prototype.dispatchHandles = function() { Client.prototype.query = function(query, type, callback) { this.callbacks.push({ type: type, fun: callback }); this.sends++; - this.conn.write(query + crlf); + if (this.conn) + this.conn.write(query + crlf); }; Client.prototype.close = function() {