3 use Psr\Log\LoggerInterface;
99 $params[
'segmentationSize'] = $params[
'segmentationSize'] ?? INF;
100 if ( empty( $params[
'url'] ) ) {
101 throw new InvalidArgumentException(
'URL parameter is required' );
104 if ( empty( $params[
'client'] ) ) {
110 foreach ( [
'caBundlePath',
'proxy' ] as $key ) {
111 if ( isset( $params[$key] ) ) {
112 $clientParams[$key] = $params[$key];
117 $this->client = $params[
'client'];
120 $this->httpParams[
'writeMethod'] = $params[
'httpParams'][
'writeMethod'] ??
'PUT';
121 $this->httpParams[
'readHeaders'] = $params[
'httpParams'][
'readHeaders'] ?? [];
122 $this->httpParams[
'writeHeaders'] = $params[
'httpParams'][
'writeHeaders'] ?? [];
123 $this->httpParams[
'deleteHeaders'] = $params[
'httpParams'][
'deleteHeaders'] ?? [];
124 $this->extendedErrorBodyFields = $params[
'extendedErrorBodyFields'] ?? [];
125 $this->serializationType = $params[
'serialization_type'] ??
'legacy';
126 $this->hmacKey = $params[
'hmac_key'] ??
'';
129 parent::__construct( $params );
132 $this->url = rtrim( $params[
'url'],
'/' ) .
'/';
140 $this->client->setLogger(
$logger );
143 protected function doGet( $key, $flags = 0, &$casToken =
null ) {
148 'url' => $this->url . rawurlencode( $key ),
149 'headers' => $this->httpParams[
'readHeaders'],
152 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
153 if ( $rcode === 200 ) {
154 if ( is_string( $rbody ) ) {
157 $casToken = ( $value !== false ) ? $rbody :
null;
163 if ( $rcode === 0 || ( $rcode >= 400 && $rcode != 404 ) ) {
164 return $this->
handleError(
"Failed to fetch $key", $rcode, $rerr, $rhdrs, $rbody );
169 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
173 'method' => $this->httpParams[
'writeMethod'],
174 'url' => $this->url . rawurlencode( $key ),
176 'headers' => $this->httpParams[
'writeHeaders'],
179 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
180 if ( $rcode === 200 || $rcode === 201 || $rcode === 204 ) {
183 return $this->
handleError(
"Failed to store $key", $rcode, $rerr, $rhdrs, $rbody );
186 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
188 if ( $this->
get( $key ) ===
false ) {
189 return $this->
set( $key, $value, $exptime, $flags );
198 'method' =>
'DELETE',
199 'url' => $this->url . rawurlencode( $key ),
200 'headers' => $this->httpParams[
'deleteHeaders'],
203 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
204 if ( in_array( $rcode, [ 200, 204, 205, 404, 410 ] ) ) {
207 return $this->
handleError(
"Failed to delete $key", $rcode, $rerr, $rhdrs, $rbody );
210 public function incr( $key, $value = 1, $flags = 0 ) {
214 $n = max( $n + (
int)$value, 0 );
216 return $this->
set( $key, $n ) ? $n :
false;
222 public function decr( $key, $value = 1, $flags = 0 ) {
223 return $this->
incr( $key, -$value, $flags );
233 if ( $this->serializationType ===
'legacy' ) {
236 $pieces = explode(
'.', $body, 3 );
237 if ( count( $pieces ) !== 3 || $pieces[0] !== $this->serializationType ) {
241 if ( $this->hmacKey !==
'' ) {
242 $checkHmac = hash_hmac(
'sha256',
$serialized, $this->hmacKey,
true );
243 if ( !hash_equals( $checkHmac, base64_decode( $hmac ) ) ) {
249 switch ( $this->serializationType ) {
252 return ( json_last_error() === JSON_ERROR_NONE ) ? $value :
false;
259 throw new \DomainException(
260 "Unknown serialization type: $this->serializationType"
273 switch ( $this->serializationType ) {
275 $value = json_encode( $body );
276 if ( $value ===
false ) {
277 throw new InvalidArgumentException( __METHOD__ .
": body could not be encoded." );
287 throw new \DomainException(
288 "Unknown serialization type: $this->serializationType"
292 if ( $this->serializationType !==
'legacy' ) {
293 if ( $this->hmacKey !==
'' ) {
294 $hmac = base64_encode(
295 hash_hmac(
'sha256', $value, $this->hmacKey,
true )
300 $value = $this->serializationType .
'.' . $hmac .
'.' . $value;
315 protected function handleError( $msg, $rcode, $rerr, $rhdrs, $rbody ) {
316 $message =
"$msg : ({code}) {error}";
322 if ( $this->extendedErrorBodyFields !== [] ) {
326 foreach ( $this->extendedErrorBodyFields as $field ) {
327 if ( isset( $body[$field] ) ) {
328 $extraFields .=
" : ({$field}) {$body[$field]}";
331 if ( $extraFields !==
'' ) {
332 $message .=
" {extra_fields}";
333 $context[
'extra_fields'] = $extraFields;
338 $this->logger->error( $message,
$context );
339 $this->
setLastError( $rcode === 0 ? self::ERR_UNREACHABLE : self::ERR_UNEXPECTED );