5use InvalidArgumentException;
7use Psr\Log\LoggerInterface;
96 private const DEFAULT_CONN_TIMEOUT = 1.2;
101 private const DEFAULT_REQ_TIMEOUT = 3.0;
127 private $serializationType;
139 private $extendedErrorBodyFields;
142 $params[
'segmentationSize'] ??= INF;
143 if ( empty(
$params[
'url'] ) ) {
144 throw new InvalidArgumentException(
'URL parameter is required' );
147 if ( empty(
$params[
'client'] ) ) {
150 'connTimeout' =>
$params[
'connTimeout'] ?? self::DEFAULT_CONN_TIMEOUT,
151 'reqTimeout' =>
$params[
'reqTimeout'] ?? self::DEFAULT_REQ_TIMEOUT,
153 foreach ( [
'caBundlePath',
'proxy',
'telemetry' ] as $key ) {
154 if ( isset(
$params[$key] ) ) {
155 $clientParams[$key] =
$params[$key];
160 $this->client =
$params[
'client'];
163 $this->httpParams[
'writeMethod'] =
$params[
'httpParams'][
'writeMethod'] ??
'PUT';
164 $this->httpParams[
'readHeaders'] =
$params[
'httpParams'][
'readHeaders'] ?? [];
165 $this->httpParams[
'writeHeaders'] =
$params[
'httpParams'][
'writeHeaders'] ?? [];
166 $this->httpParams[
'deleteHeaders'] =
$params[
'httpParams'][
'deleteHeaders'] ?? [];
167 $this->extendedErrorBodyFields =
$params[
'extendedErrorBodyFields'] ?? [];
168 $this->serializationType =
$params[
'serialization_type'] ??
'PHP';
169 $this->hmacKey =
$params[
'hmac_key'] ??
'';
172 parent::__construct(
$params );
175 $this->url = rtrim(
$params[
'url'],
'/' ) .
'/';
182 $this->client->setLogger(
$logger );
185 protected function doGet( $key, $flags = 0, &$casToken =
null ) {
191 'url' => $this->url . rawurlencode( $key ),
192 'headers' => $this->httpParams[
'readHeaders'],
197 [ $rcode, , $rhdrs, $rbody, $rerr ] = $this->client->run( $req );
198 if ( $rcode === 200 && is_string( $rbody ) ) {
199 $value = $this->decodeBody( $rbody );
200 $valueSize = strlen( $rbody );
202 if ( $getToken && $value !==
false ) {
205 } elseif ( $rcode === 0 || ( $rcode >= 400 && $rcode != 404 ) ) {
206 $this->
handleError(
'Failed to fetch {cacheKey}', $rcode, $rerr, $rhdrs, $rbody,
207 [
'cacheKey' => $key ] );
210 $this->
updateOpStats( self::METRIC_OP_GET, [ $key => [ 0, $valueSize ] ] );
215 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
217 'method' => $this->httpParams[
'writeMethod'],
218 'url' => $this->url . rawurlencode( $key ),
219 'body' => $this->encodeBody( $value ),
220 'headers' => $this->httpParams[
'writeHeaders'],
223 [ $rcode, , $rhdrs, $rbody, $rerr ] = $this->client->run( $req );
224 $res = ( $rcode === 200 || $rcode === 201 || $rcode === 204 );
226 $this->
handleError(
'Failed to store {cacheKey}', $rcode, $rerr, $rhdrs, $rbody,
227 [
'cacheKey' => $key ] );
230 $this->
updateOpStats( self::METRIC_OP_SET, [ $key => [ strlen( $rbody ), 0 ] ] );
235 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
237 if ( $this->
get( $key ) ===
false ) {
238 return $this->
set( $key, $value, $exptime, $flags );
247 'method' =>
'DELETE',
248 'url' => $this->url . rawurlencode( $key ),
249 'headers' => $this->httpParams[
'deleteHeaders'],
252 [ $rcode, , $rhdrs, $rbody, $rerr ] = $this->client->run( $req );
253 $res = in_array( $rcode, [ 200, 204, 205, 404, 410 ] );
255 $this->
handleError(
'Failed to delete {cacheKey}', $rcode, $rerr, $rhdrs, $rbody,
256 [
'cacheKey' => $key ] );
266 $curValue = $this->
doGet( $key );
267 if ( $curValue ===
false ) {
268 $newValue = $this->
doSet( $key, $init, $exptime ) ? $init :
false;
269 } elseif ( $this->
isInteger( $curValue ) ) {
270 $sum = max( $curValue + $step, 0 );
271 $newValue = $this->
doSet( $key, $sum, $exptime ) ? $sum :
false;
286 private function decodeBody( $body ) {
287 $pieces = explode(
'.', $body, 3 );
288 if ( count( $pieces ) !== 3 || $pieces[0] !== $this->serializationType ) {
291 [ , $hmac, $serialized ] = $pieces;
292 if ( $this->hmacKey !==
'' ) {
293 $checkHmac = hash_hmac(
'sha256', $serialized, $this->hmacKey,
true );
294 if ( !hash_equals( $checkHmac, base64_decode( $hmac ) ) ) {
299 switch ( $this->serializationType ) {
301 $value = json_decode( $serialized,
true );
302 return ( json_last_error() === JSON_ERROR_NONE ) ? $value :
false;
308 throw new \DomainException(
309 "Unknown serialization type: $this->serializationType"
322 private function encodeBody( $body ) {
323 switch ( $this->serializationType ) {
325 $value = json_encode( $body );
326 if ( $value ===
false ) {
327 throw new InvalidArgumentException( __METHOD__ .
": body could not be encoded." );
336 throw new \DomainException(
337 "Unknown serialization type: $this->serializationType"
341 if ( $this->hmacKey !==
'' ) {
342 $hmac = base64_encode(
343 hash_hmac(
'sha256', $value, $this->hmacKey,
true )
348 return $this->serializationType .
'.' . $hmac .
'.' . $value;
361 protected function handleError( $msg, $rcode, $rerr, $rhdrs, $rbody, $context = [] ) {
362 $message =
"$msg : ({code}) {error}";
368 if ( $this->extendedErrorBodyFields !== [] ) {
369 $body = $this->decodeBody( $rbody );
372 foreach ( $this->extendedErrorBodyFields as $field ) {
373 if ( isset( $body[$field] ) ) {
374 $extraFields .=
" : ({$field}) {$body[$field]}";
377 if ( $extraFields !==
'' ) {
378 $message .=
" {extra_fields}";
379 $context[
'extra_fields'] = $extraFields;
384 $this->logger->error( $message, $context );
390class_alias( RESTBagOStuff::class,
'RESTBagOStuff' );
array $params
The job parameters.