Commit a5f84544 authored by Andrew Bernstein's avatar Andrew Bernstein

Adding support for validating and hashing keys that are too long when performing getMulti

parent 337a3903
......@@ -867,6 +867,8 @@ Client.config = {
, multi:true
, type: 'get'
, command: 'get ' + key.join(' ')
, key: keys
, validate: [['key', Array], ['callback', Function]]
};
}, server);
});
......
......@@ -29,7 +29,17 @@ exports.validateArg = function validateArg (args, config) {
if (toString.call(value) !== '[object Array]') {
err = 'Argument "' + key + '" is not a valid Array.';
}
if(!err && key === 'key') {
for(var vKey in value) {
var vValue = value[vKey];
var result = validateKeySize(config, vKey, vValue);
if(result.err) {
err = result.err;
} else {
args.command = args.command.replace(vValue, result['value']);
}
}
}
break;
case Object:
......@@ -52,17 +62,11 @@ exports.validateArg = function validateArg (args, config) {
}
if (!err && key === 'key') {
if (value.length > config.maxKeySize) {
if (config.keyCompression){
args[key] = createHash('md5').update(value).digest('hex');
// also make sure you update the command
args.command = args.command.replace(value, args[key]);
} else {
err = 'Argument "' + key + '" is longer than the maximum allowed length of ' + config.maxKeySize;
}
} else if (/[\s\n\r]/.test(value)) {
err = 'The key should not contain any whitespace or new lines';
var result = validateKeySize(config, key, value);
if(result.err) {
err = result.err;
} else {
args.command = args.command.replace(value, result['value']);
}
}
break;
......@@ -82,6 +86,20 @@ exports.validateArg = function validateArg (args, config) {
return true;
};
var validateKeySize = function validateKeySize(config, key, value) {
if (value.length > config.maxKeySize) {
if (config.keyCompression){
return { err: false, value: createHash('md5').update(value).digest('hex') };
} else {
return { err: 'Argument "' + key + '" is longer than the maximum allowed length of ' + config.maxKeySize };
}
} else if (/[\s\n\r]/.test(value)) {
return { err: 'The key should not contain any whitespace or new lines' };
} else {
return { err: false, value: value };
}
};
// a small util to use an object for eventEmitter
exports.fuse = function fuse (target, handlers) {
for(var i in handlers)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment