/usr/share/php/Predis/Connection/WebdisConnection.php is in libphp-predis 0.8.3-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | <?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Connection;
use Predis\NotSupportedException;
use Predis\ResponseError;
use Predis\Command\CommandInterface;
use Predis\Connection\ConnectionException;
use Predis\Protocol\ProtocolException;
/**
* This class implements a Predis connection that actually talks with Webdis
* instead of connecting directly to Redis. It relies on the cURL extension to
* communicate with the web server and the phpiredis extension to parse the
* protocol of the replies returned in the http response bodies.
*
* Some features are not yet available or they simply cannot be implemented:
* - Pipelining commands.
* - Publish / Subscribe.
* - MULTI / EXEC transactions (not yet supported by Webdis).
*
* The connection parameters supported by this class are:
*
* - scheme: must be 'http'.
* - host: hostname or IP address of the server.
* - port: TCP port of the server.
* - timeout: timeout to perform the connection.
* - user: username for authentication.
* - pass: password for authentication.
*
* @link http://webd.is
* @link http://github.com/nicolasff/webdis
* @link http://github.com/seppo0010/phpiredis
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class WebdisConnection implements SingleConnectionInterface
{
const ERR_MSG_EXTENSION = 'The %s extension must be loaded in order to be able to use this connection class';
private $parameters;
private $resource;
private $reader;
/**
* @param ConnectionParametersInterface $parameters Parameters used to initialize the connection.
*/
public function __construct(ConnectionParametersInterface $parameters)
{
$this->checkExtensions();
if ($parameters->scheme !== 'http') {
throw new \InvalidArgumentException("Invalid scheme: {$parameters->scheme}");
}
$this->parameters = $parameters;
$this->resource = $this->initializeCurl($parameters);
$this->reader = $this->initializeReader($parameters);
}
/**
* Frees the underlying cURL and protocol reader resources when PHP's
* garbage collector kicks in.
*/
public function __destruct()
{
curl_close($this->resource);
phpiredis_reader_destroy($this->reader);
}
/**
* Helper method used to throw on unsupported methods.
*/
private function throwNotSupportedException($function)
{
$class = __CLASS__;
throw new NotSupportedException("The method $class::$function() is not supported");
}
/**
* Checks if the cURL and phpiredis extensions are loaded in PHP.
*/
private function checkExtensions()
{
if (!function_exists('curl_init')) {
throw new NotSupportedException(sprintf(self::ERR_MSG_EXTENSION, 'curl'));
}
if (!function_exists('phpiredis_reader_create')) {
throw new NotSupportedException(sprintf(self::ERR_MSG_EXTENSION, 'phpiredis'));
}
}
/**
* Initializes cURL.
*
* @param ConnectionParametersInterface $parameters Parameters used to initialize the connection.
* @return resource
*/
private function initializeCurl(ConnectionParametersInterface $parameters)
{
$options = array(
CURLOPT_FAILONERROR => true,
CURLOPT_CONNECTTIMEOUT_MS => $parameters->timeout * 1000,
CURLOPT_URL => "{$parameters->scheme}://{$parameters->host}:{$parameters->port}",
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_POST => true,
CURLOPT_WRITEFUNCTION => array($this, 'feedReader'),
);
if (isset($parameters->user, $parameters->pass)) {
$options[CURLOPT_USERPWD] = "{$parameters->user}:{$parameters->pass}";
}
curl_setopt_array($resource = curl_init(), $options);
return $resource;
}
/**
* Initializes phpiredis' protocol reader.
*
* @param ConnectionParametersInterface $parameters Parameters used to initialize the connection.
* @return resource
*/
private function initializeReader(ConnectionParametersInterface $parameters)
{
$reader = phpiredis_reader_create();
phpiredis_reader_set_status_handler($reader, $this->getStatusHandler());
phpiredis_reader_set_error_handler($reader, $this->getErrorHandler());
return $reader;
}
/**
* Gets the handler used by the protocol reader to handle status replies.
*
* @return \Closure
*/
protected function getStatusHandler()
{
return function ($payload) {
return $payload === 'OK' ? true : $payload;
};
}
/**
* Gets the handler used by the protocol reader to handle Redis errors.
*
* @return \Closure
*/
protected function getErrorHandler()
{
return function ($errorMessage) {
return new ResponseError($errorMessage);
};
}
/**
* Feeds phpredis' reader resource with the data read from the network.
*
* @param resource $resource Reader resource.
* @param string $buffer Buffer with the reply read from the network.
* @return int
*/
protected function feedReader($resource, $buffer)
{
phpiredis_reader_feed($this->reader, $buffer);
return strlen($buffer);
}
/**
* {@inheritdoc}
*/
public function connect()
{
// NOOP
}
/**
* {@inheritdoc}
*/
public function disconnect()
{
// NOOP
}
/**
* {@inheritdoc}
*/
public function isConnected()
{
return true;
}
/**
* Checks if the specified command is supported by this connection class.
*
* @param CommandInterface $command The instance of a Redis command.
* @return string
*/
protected function getCommandId(CommandInterface $command)
{
switch (($commandId = $command->getId())) {
case 'AUTH':
case 'SELECT':
case 'MULTI':
case 'EXEC':
case 'WATCH':
case 'UNWATCH':
case 'DISCARD':
case 'MONITOR':
throw new NotSupportedException("Disabled command: {$command->getId()}");
default:
return $commandId;
}
}
/**
* {@inheritdoc}
*/
public function writeCommand(CommandInterface $command)
{
$this->throwNotSupportedException(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function readResponse(CommandInterface $command)
{
$this->throwNotSupportedException(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function executeCommand(CommandInterface $command)
{
$resource = $this->resource;
$commandId = $this->getCommandId($command);
if ($arguments = $command->getArguments()) {
$arguments = implode('/', array_map('urlencode', $arguments));
$serializedCommand = "$commandId/$arguments.raw";
} else {
$serializedCommand = "$commandId.raw";
}
curl_setopt($resource, CURLOPT_POSTFIELDS, $serializedCommand);
if (curl_exec($resource) === false) {
$error = curl_error($resource);
$errno = curl_errno($resource);
throw new ConnectionException($this, trim($error), $errno);
}
if (phpiredis_reader_get_state($this->reader) !== PHPIREDIS_READER_STATE_COMPLETE) {
throw new ProtocolException($this, phpiredis_reader_get_error($this->reader));
}
return phpiredis_reader_get_reply($this->reader);
}
/**
* {@inheritdoc}
*/
public function getResource()
{
return $this->resource;
}
/**
* {@inheritdoc}
*/
public function getParameters()
{
return $this->parameters;
}
/**
* {@inheritdoc}
*/
public function pushInitCommand(CommandInterface $command)
{
$this->throwNotSupportedException(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function read()
{
$this->throwNotSupportedException(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function __toString()
{
return "{$this->parameters->host}:{$this->parameters->port}";
}
/**
* {@inheritdoc}
*/
public function __sleep()
{
return array('parameters');
}
/**
* {@inheritdoc}
*/
public function __wakeup()
{
$this->checkExtensions();
$parameters = $this->getParameters();
$this->resource = $this->initializeCurl($parameters);
$this->reader = $this->initializeReader($parameters);
}
}
|