Skip to content

Commit

Permalink
Merge pull request #11 from tinkertim/develop
Browse files Browse the repository at this point in the history
This patch fixes an issue in _encode_request(), which tokenizes a given request into the Redis wire protocol by spaces. This is problematic if assigning a string value to a key that contains spaces.
  • Loading branch information
joelcox committed May 4, 2012
2 parents 44da4f5 + 22c402d commit 0c52347
Showing 1 changed file with 47 additions and 19 deletions.
66 changes: 47 additions & 19 deletions libraries/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,30 @@ function __construct()
* @param mixed arguments to be passed
* @return mixed
*/
public function __call($method, $arguments)
{
return $this->command(strtoupper($method) . ' ' . $arguments[0]);
}
public function __call($method, $arguments) {
if (! isset($arguments[0])) {
return $this->command(strtoupper($method));
}
if (is_array($arguments[0])) {
return $this->command(strtoupper($method), $arguments[0]);
}
if (isset($arguments[1])) {
return $this->command(strtoupper($method) . ' ' . $arguments[0], $arguments[1]);
}
return $this->command(strtoupper($method) . ' ' . $arguments[0]);
}

/**
* Command
*
* Generic command function, just like redis-cli
* @param string command to be executed
* @return mixed
* @param string $cmd command to be executed
* @param mixed $data Extra data to send (string, array)
* @return mixed
*/
public function command($cmd)
public function command($cmd, $data = NULL)
{
$request = $this->_encode_request($cmd);
$request = $this->_encode_request($cmd, $data);
return $this->_write_request($request);
}

Expand Down Expand Up @@ -294,23 +303,42 @@ private function _multi_bulk_reply()
*
* Encode plain-text request to Redis protocol format
* @link http://redis.io/topics/protocol
* @param string request in plain-text
* @return string encoded according to Redis protocol
* @param string $request request in plain-text
* @param string $data additional data (string or array, depending on the request)
* @return string encoded according to Redis protocol
*/
private function _encode_request($request)
{
$slices = explode(' ', $request);
private function _encode_request($request, $data = NULL)
{
$slices = explode(' ', rtrim($request, ' '));
$arguments = count($slices);

if ($data !== NULL)
{
if (is_array($data))
{
$arguments += (count($data) * 2);
} else
{
$arguments ++;
}
}
$request = "*" . $arguments . "\r\n";

foreach ($slices as $slice)
{
foreach ($slices as $slice) {
$request .= "$" . strlen($slice) . "\r\n" . $slice ."\r\n";
}

if ($data !== NULL) {
if (is_array($data))
{
foreach ($data as $key => $value)
{
$request .= "$" . strlen($key) . "\r\n" . $key . "\r\n";
$request .= "$" . strlen($value) . "\r\n" . $value . "\r\n";
}
} else
{
$request .= "$" . strlen($data) . "\r\n" . $data . "\r\n";
}
}
return $request;

}

/**
Expand Down

0 comments on commit 0c52347

Please sign in to comment.