diff --git a/lib/memcache.js b/lib/memcache.js index 8b826ad..a757b4f 100644 --- a/lib/memcache.js +++ b/lib/memcache.js @@ -270,22 +270,18 @@ Client.prototype.handle_get = function(buffer) { var result_value = null; var end_indicator_len = 3; var result_len = 0; + var first_line_len; - if (buffer.indexOf('END') == 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; - 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] - } else { - var first_line_len = buffer.indexOf(crlf) + crlf_len; - var result_len = buffer.substr(0, first_line_len).split(' ')[3]; - result_value = buffer.substr(first_line_len, result_len); - - return [result_value, first_line_len + parseInt(result_len ) + crlf_len + end_indicator_len + crlf_len]; + if (buffer.indexOf('END' + crlf) == 0) { // No result, key not found + return [null, end_indicator_len + crlf_len]; } + first_line_len = buffer.indexOf(crlf) + crlf_len; + result_len = parseInt(buffer.substr(0, first_line_len).split(' ')[3], 10); + if (buffer.length < (result_len + first_line_len + crlf_len + end_indicator_len + crlf_len)) { + return null; + } + result_value = buffer.substr(first_line_len, result_len); + return [result_value, first_line_len + result_len + crlf_len + end_indicator_len + crlf_len]; }; Client.prototype.handle_stats = function(buffer){ @@ -295,27 +291,27 @@ Client.prototype.handle_stats = function(buffer){ return [{}, 5]; } - // find the terminator - var idx = buffer.indexOf('\r\nEND\r\n'); - if (idx == -1){ - // wait for more data if we don't have an end yet - return null; - } + // find the terminator + var idx = buffer.indexOf('\r\nEND\r\n'); + if (idx == -1){ + // wait for more data if we don't have an end yet + return null; + } - // read the lines - var our_data = buffer.substr(0, idx+2); - var out = {}; - var line = null; - var i=0; - while (line = readLine(our_data)){ - our_data = our_data.substr(line.length + 2); - if (line.substr(0, 5) == 'STAT '){ - var idx2 = line.indexOf(' ', 5); - var k = line.substr(5, idx2-5); - var v = line.substr(idx2+1); - out[k] = v; - } - } + // read the lines + var our_data = buffer.substr(0, idx+2); + var out = {}; + var line = null; + var i=0; + while ((line = readLine(our_data))){ + our_data = our_data.substr(line.length + 2); + if (line.substr(0, 5) == 'STAT '){ + var idx2 = line.indexOf(' ', 5); + var k = line.substr(5, idx2-5); + var v = line.substr(idx2+1); + out[k] = v; + } + } return [out, idx + 7, null]; };