Permalink
Browse files

Merge pull request #71 from sebastianseilund/master

Fix for issue with values starting with OK, VALUE etc.
  • Loading branch information...
3rd-Eden committed Nov 21, 2012
2 parents 8fdbc37 + e1faeeb commit 594853801a3d82a7bfbcedbe9c94c0f0a7460059
Showing with 108 additions and 8 deletions.
  1. +5 −8 lib/memcached.js
  2. +10 −0 lib/utils.js
  3. +93 −0 test/memcached-get-set.test.js
View
@@ -590,7 +590,7 @@ Client.config = {
var queue = []
, token
, tokenSet
- , dataSet = ''
+ , dataSet
, resultSet
, metaData
, err = []
@@ -615,11 +615,7 @@ Client.config = {
// fetch the response content
if (tokenSet[0] === 'VALUE') {
- while (S.bufferArray.length) {
- if (privates.bufferedCommands.test(S.bufferArray[0])) break;
-
- dataSet += S.bufferArray.shift();
- }
+ dataSet = Utils.unescapeValue(S.bufferArray.shift());
}
resultSet = privates.parsers[tokenSet[0]].call(S, tokenSet, dataSet || token, err, queue, this);
@@ -671,8 +667,7 @@ Client.config = {
}
// cleanup
- dataSet = '';
- tokenSet = metaData = undefined;
+ dataSet = tokenSet = metaData = undefined;
// check if we need to remove an empty item from the array, as splitting on /r/n might cause an empty
// item at the end..
@@ -771,6 +766,8 @@ Client.config = {
flag = FLAG_JSON;
value = JSON.stringify(value);
}
+
+ value = Utils.escapeValue(value);
length = Buffer.byteLength(value);
if (length > this.maxValue) {
View
@@ -129,3 +129,13 @@ exports.Iterator = function iterator (collection, callback) {
return index < maximum;
};
};
+
+//Escapes values by putting backslashes before line breaks
+exports.escapeValue = function(value) {
+ return value.replace(/(\r|\n)/g, '\\$1');
+};
+
+//Unescapes escaped values by removing backslashes before line breaks
+exports.unescapeValue = function(value) {
+ return value.replace(/\\(\r|\n)/g, '$1');
+};
@@ -445,4 +445,97 @@ describe("Memcached GET SET", function() {
});
});
});
+
+ /**
+ * Make sure that a string beginning with OK is not interpreted as
+ * a command response.
+ */
+ it("set and get a string beginning with OK", function(done) {
+ var memcached = new Memcached(common.servers.single)
+ , message = 'OK123456'
+ , testnr = ++global.testnumbers
+ , callbacks = 0;
+
+ memcached.set("test:" + testnr, message, 1000, function(error, ok){
+ ++callbacks;
+
+ assert.ok(!error);
+ ok.should.be.true;
+
+ memcached.get("test:" + testnr, function(error, answer){
+ ++callbacks;
+
+ assert.ok(!error);
+
+ assert.ok(typeof answer === 'string');
+ answer.should.eql(message);
+
+ memcached.end(); // close connections
+ assert.equal(callbacks, 2);
+ done();
+ });
+ });
+ });
+
+ /**
+ * Make sure that a string beginning with OK is not interpreted as
+ * a command response.
+ */
+ it("set and get a string beginning with VALUE", function(done) {
+ var memcached = new Memcached(common.servers.single)
+ , message = 'VALUE hello, I\'m not really a value.'
+ , testnr = ++global.testnumbers
+ , callbacks = 0;
+
+ memcached.set("test:" + testnr, message, 1000, function(error, ok){
+ ++callbacks;
+
+ assert.ok(!error);
+ ok.should.be.true;
+
+ memcached.get("test:" + testnr, function(error, answer){
+ ++callbacks;
+
+ assert.ok(!error);
+
+ assert.ok(typeof answer === 'string');
+ answer.should.eql(message);
+
+ memcached.end(); // close connections
+ assert.equal(callbacks, 2);
+ done();
+ });
+ });
+ });
+
+ /**
+ * Make sure that a string containing line breaks are escaped and
+ * unescaped correctly.
+ */
+ it("set and get a string with line breaks", function(done) {
+ var memcached = new Memcached(common.servers.single)
+ , message = '1\n2\r\n3\n\r4\\n5\\r\\n6\\n\\r7'
+ , testnr = ++global.testnumbers
+ , callbacks = 0;
+
+ memcached.set("test:" + testnr, message, 1000, function(error, ok){
+ ++callbacks;
+
+ assert.ok(!error);
+ ok.should.be.true;
+
+ memcached.get("test:" + testnr, function(error, answer){
+ ++callbacks;
+
+ assert.ok(!error);
+
+ assert.ok(typeof answer === 'string');
+ answer.should.eql(message);
+
+ memcached.end(); // close connections
+ assert.equal(callbacks, 2);
+ done();
+ });
+ });
+ });
});

0 comments on commit 5948538

Please sign in to comment.