5use InvalidArgumentException;
6use Psr\Log\LoggerInterface;
95 private const DEFAULT_CONN_TIMEOUT = 1.2;
100 private const DEFAULT_REQ_TIMEOUT = 3.0;
126 private $serializationType;
138 private $extendedErrorBodyFields;
141 $params[
'segmentationSize'] ??= INF;
142 if ( empty(
$params[
'url'] ) ) {
143 throw new InvalidArgumentException(
'URL parameter is required' );
146 if ( empty(
$params[
'client'] ) ) {
149 'connTimeout' =>
$params[
'connTimeout'] ?? self::DEFAULT_CONN_TIMEOUT,
150 'reqTimeout' =>
$params[
'reqTimeout'] ?? self::DEFAULT_REQ_TIMEOUT,
152 foreach ( [
'caBundlePath',
'proxy',
'telemetry' ] as $key ) {
153 if ( isset(
$params[$key] ) ) {
154 $clientParams[$key] =
$params[$key];
159 $this->client =
$params[
'client'];
162 $this->httpParams[
'writeMethod'] =
$params[
'httpParams'][
'writeMethod'] ??
'PUT';
163 $this->httpParams[
'readHeaders'] =
$params[
'httpParams'][
'readHeaders'] ?? [];
164 $this->httpParams[
'writeHeaders'] =
$params[
'httpParams'][
'writeHeaders'] ?? [];
165 $this->httpParams[
'deleteHeaders'] =
$params[
'httpParams'][
'deleteHeaders'] ?? [];
166 $this->extendedErrorBodyFields =
$params[
'extendedErrorBodyFields'] ?? [];
167 $this->serializationType =
$params[
'serialization_type'] ??
'PHP';
168 $this->hmacKey =
$params[
'hmac_key'] ??
'';
171 parent::__construct(
$params );
174 $this->url = rtrim(
$params[
'url'],
'/' ) .
'/';
181 $this->client->setLogger(
$logger );
184 protected function doGet( $key, $flags = 0, &$casToken =
null ) {
190 'url' => $this->url . rawurlencode( $key ),
191 'headers' => $this->httpParams[
'readHeaders'],
196 [ $rcode, , $rhdrs, $rbody, $rerr ] = $this->client->run( $req );
197 if ( $rcode === 200 && is_string( $rbody ) ) {
198 $value = $this->decodeBody( $rbody );
199 $valueSize = strlen( $rbody );
201 if ( $getToken && $value !==
false ) {
204 } elseif ( $rcode === 0 || ( $rcode >= 400 && $rcode != 404 ) ) {
205 $this->
handleError(
'Failed to fetch {cacheKey}', $rcode, $rerr, $rhdrs, $rbody,
206 [
'cacheKey' => $key ] );
209 $this->
updateOpStats( self::METRIC_OP_GET, [ $key => [ 0, $valueSize ] ] );
214 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
216 'method' => $this->httpParams[
'writeMethod'],
217 'url' => $this->url . rawurlencode( $key ),
218 'body' => $this->encodeBody( $value ),
219 'headers' => $this->httpParams[
'writeHeaders'],
222 [ $rcode, , $rhdrs, $rbody, $rerr ] = $this->client->run( $req );
223 $res = ( $rcode === 200 || $rcode === 201 || $rcode === 204 );
225 $this->
handleError(
'Failed to store {cacheKey}', $rcode, $rerr, $rhdrs, $rbody,
226 [
'cacheKey' => $key ] );
229 $this->
updateOpStats( self::METRIC_OP_SET, [ $key => [ strlen( $rbody ), 0 ] ] );
234 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
236 if ( $this->
get( $key ) ===
false ) {
237 return $this->
set( $key, $value, $exptime, $flags );
246 'method' =>
'DELETE',
247 'url' => $this->url . rawurlencode( $key ),
248 'headers' => $this->httpParams[
'deleteHeaders'],
251 [ $rcode, , $rhdrs, $rbody, $rerr ] = $this->client->run( $req );
252 $res = in_array( $rcode, [ 200, 204, 205, 404, 410 ] );
254 $this->
handleError(
'Failed to delete {cacheKey}', $rcode, $rerr, $rhdrs, $rbody,
255 [
'cacheKey' => $key ] );
265 $curValue = $this->
doGet( $key );
266 if ( $curValue ===
false ) {
267 $newValue = $this->
doSet( $key, $init, $exptime ) ? $init :
false;
268 } elseif ( $this->
isInteger( $curValue ) ) {
269 $sum = max( $curValue + $step, 0 );
270 $newValue = $this->
doSet( $key, $sum, $exptime ) ? $sum :
false;
285 private function decodeBody( $body ) {
286 $pieces = explode(
'.', $body, 3 );
287 if ( count( $pieces ) !== 3 || $pieces[0] !== $this->serializationType ) {
290 [ , $hmac, $serialized ] = $pieces;
291 if ( $this->hmacKey !==
'' ) {
292 $checkHmac = hash_hmac(
'sha256', $serialized, $this->hmacKey,
true );
293 if ( !hash_equals( $checkHmac, base64_decode( $hmac ) ) ) {
298 switch ( $this->serializationType ) {
300 $value = json_decode( $serialized,
true );
301 return ( json_last_error() === JSON_ERROR_NONE ) ? $value :
false;
307 throw new \DomainException(
308 "Unknown serialization type: $this->serializationType"
320 private function encodeBody( $body ) {
321 switch ( $this->serializationType ) {
323 $value = json_encode( $body );
324 if ( $value ===
false ) {
325 throw new InvalidArgumentException( __METHOD__ .
": body could not be encoded." );
334 throw new \DomainException(
335 "Unknown serialization type: $this->serializationType"
339 if ( $this->hmacKey !==
'' ) {
340 $hmac = base64_encode(
341 hash_hmac(
'sha256', $value, $this->hmacKey,
true )
346 return $this->serializationType .
'.' . $hmac .
'.' . $value;
359 protected function handleError( $msg, $rcode, $rerr, $rhdrs, $rbody, $context = [] ) {
360 $message =
"$msg : ({code}) {error}";
366 if ( $this->extendedErrorBodyFields !== [] ) {
367 $body = $this->decodeBody( $rbody );
370 foreach ( $this->extendedErrorBodyFields as $field ) {
371 if ( isset( $body[$field] ) ) {
372 $extraFields .=
" : ({$field}) {$body[$field]}";
375 if ( $extraFields !==
'' ) {
376 $message .=
" {extra_fields}";
377 $context[
'extra_fields'] = $extraFields;
382 $this->logger->error( $message, $context );
388class_alias( RESTBagOStuff::class,
'RESTBagOStuff' );
array $params
The job parameters.