3use Psr\Log\LoggerInterface;
95 $params[
'segmentationSize'] = $params[
'segmentationSize'] ?? INF;
96 if ( empty( $params[
'url'] ) ) {
97 throw new InvalidArgumentException(
'URL parameter is required' );
100 if ( empty( $params[
'client'] ) ) {
106 foreach ( [
'caBundlePath',
'proxy' ] as $key ) {
107 if ( isset( $params[$key] ) ) {
108 $clientParams[$key] = $params[$key];
113 $this->client = $params[
'client'];
116 $this->httpParams[
'writeMethod'] = $params[
'httpParams'][
'writeMethod'] ??
'PUT';
117 $this->httpParams[
'readHeaders'] = $params[
'httpParams'][
'readHeaders'] ?? [];
118 $this->httpParams[
'writeHeaders'] = $params[
'httpParams'][
'writeHeaders'] ?? [];
119 $this->httpParams[
'deleteHeaders'] = $params[
'httpParams'][
'deleteHeaders'] ?? [];
120 $this->extendedErrorBodyFields = $params[
'extendedErrorBodyFields'] ?? [];
121 $this->serializationType = $params[
'serialization_type'] ??
'PHP';
122 $this->hmacKey = $params[
'hmac_key'] ??
'';
125 parent::__construct( $params );
128 $this->url = rtrim( $params[
'url'],
'/' ) .
'/';
131 $this->attrMap[self::ATTR_SYNCWRITES] = self::QOS_SYNCWRITES_QC;
136 $this->client->setLogger(
$logger );
139 protected function doGet( $key, $flags = 0, &$casToken =
null ) {
144 'url' => $this->url . rawurlencode( $key ),
145 'headers' => $this->httpParams[
'readHeaders'],
148 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
149 if ( $rcode === 200 ) {
150 if ( is_string( $rbody ) ) {
153 $casToken = ( $value !== false ) ? $rbody :
null;
159 if ( $rcode === 0 || ( $rcode >= 400 && $rcode != 404 ) ) {
160 return $this->
handleError(
"Failed to fetch $key", $rcode, $rerr, $rhdrs, $rbody );
165 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
169 'method' => $this->httpParams[
'writeMethod'],
170 'url' => $this->url . rawurlencode( $key ),
172 'headers' => $this->httpParams[
'writeHeaders'],
175 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
176 if ( $rcode === 200 || $rcode === 201 || $rcode === 204 ) {
179 return $this->
handleError(
"Failed to store $key", $rcode, $rerr, $rhdrs, $rbody );
182 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
184 if ( $this->
get( $key ) ===
false ) {
185 return $this->
set( $key, $value, $exptime, $flags );
194 'method' =>
'DELETE',
195 'url' => $this->url . rawurlencode( $key ),
196 'headers' => $this->httpParams[
'deleteHeaders'],
199 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
200 if ( in_array( $rcode, [ 200, 204, 205, 404, 410 ] ) ) {
203 return $this->
handleError(
"Failed to delete $key", $rcode, $rerr, $rhdrs, $rbody );
206 public function incr( $key, $value = 1, $flags = 0 ) {
210 $n = max( $n + (
int)$value, 0 );
212 return $this->
set( $key, $n ) ? $n :
false;
218 public function decr( $key, $value = 1, $flags = 0 ) {
219 return $this->
incr( $key, -$value, $flags );
229 $pieces = explode(
'.', $body, 3 );
230 if ( count( $pieces ) !== 3 || $pieces[0] !== $this->serializationType ) {
234 if ( $this->hmacKey !==
'' ) {
235 $checkHmac = hash_hmac(
'sha256',
$serialized, $this->hmacKey,
true );
236 if ( !hash_equals( $checkHmac, base64_decode( $hmac ) ) ) {
241 switch ( $this->serializationType ) {
244 return ( json_last_error() === JSON_ERROR_NONE ) ? $value :
false;
250 throw new \DomainException(
251 "Unknown serialization type: $this->serializationType"
264 switch ( $this->serializationType ) {
266 $value = json_encode( $body );
267 if ( $value ===
false ) {
268 throw new InvalidArgumentException( __METHOD__ .
": body could not be encoded." );
277 throw new \DomainException(
278 "Unknown serialization type: $this->serializationType"
282 if ( $this->hmacKey !==
'' ) {
283 $hmac = base64_encode(
284 hash_hmac(
'sha256', $value, $this->hmacKey,
true )
289 return $this->serializationType .
'.' . $hmac .
'.' . $value;
301 protected function handleError( $msg, $rcode, $rerr, $rhdrs, $rbody ) {
302 $message =
"$msg : ({code}) {error}";
308 if ( $this->extendedErrorBodyFields !== [] ) {
312 foreach ( $this->extendedErrorBodyFields as $field ) {
313 if ( isset( $body[$field] ) ) {
314 $extraFields .=
" : ({$field}) {$body[$field]}";
317 if ( $extraFields !==
'' ) {
318 $message .=
" {extra_fields}";
319 $context[
'extra_fields'] = $extraFields;
324 $this->logger->error( $message, $context );
325 $this->
setLastError( $rcode === 0 ? self::ERR_UNREACHABLE : self::ERR_UNEXPECTED );
const READ_LATEST
Bitfield constants for get()/getMulti(); these are only advisory.
Storage medium specific cache for storing items (e.g.
isInteger( $value)
Check if a value is an integer.
setLastError( $err)
Set the "last error" registry.
Class to handle multiple HTTP requests.
Interface to key-value storage behind an HTTP server.
array $httpParams
http parameters: readHeaders, writeHeaders, deleteHeaders, writeMethod
string $serializationType
Optional serialization type to use.
setLogger(LoggerInterface $logger)
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
const DEFAULT_CONN_TIMEOUT
Default connection timeout in seconds.
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
decodeBody( $body)
Processes the response body.
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
const DEFAULT_REQ_TIMEOUT
Default request timeout.
doGet( $key, $flags=0, &$casToken=null)
doDelete( $key, $flags=0)
Delete an item.
array $extendedErrorBodyFields
additional body fields to log on error, if possible
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
string $hmacKey
Optional HMAC Key for protecting the serialized blob.
encodeBody( $body)
Prepares the request body (the "value" portion of our key/value store) for transmission.
handleError( $msg, $rcode, $rerr, $rhdrs, $rbody)
Handle storage error.
string $url
REST URL to use for storage.
foreach( $res as $row) $serialized