From 2b7c07126be2b9a8e88e030fcc71f4beeec6fb9f Mon Sep 17 00:00:00 2001 From: Douglas Greenshields Date: Mon, 17 Apr 2017 21:16:59 +0100 Subject: [PATCH] refactor out gotos to avoid raptor attack --- src/Contentful.php | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/Contentful.php b/src/Contentful.php index c251425..4274380 100644 --- a/src/Contentful.php +++ b/src/Contentful.php @@ -6,6 +6,7 @@ use GuzzleHttp\Exception\RequestException; use function GuzzleHttp\Promise\coroutine; use GuzzleHttp\Promise\FulfilledPromise; +use function GuzzleHttp\Promise\promise_for; use GuzzleHttp\Promise\PromiseInterface; use Markup\Contentful\Cache\NullCacheItemPool; use Markup\Contentful\Decorator\AssetDecoratorInterface; @@ -385,7 +386,6 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api, return $pool->getItem($cacheKey); }; $assetDecorator = $this->ensureAssetDecorator($spaceData['asset_decorator']); - $finalPromise = null; /** * Returns a built response if it passes test, or null if it doesn't. * @@ -430,8 +430,8 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api, $builtResponse = $buildResponseFromJson($cacheItemJson); if ($builtResponse) { - $finalPromise = new FulfilledPromise($builtResponse); - goto finalYield; + yield promise_for($builtResponse); + return; } } if ($this->cacheFailResponses) { @@ -449,8 +449,8 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api, ); $builtResponse = $buildResponseFromJson($fallbackJson); if ($builtResponse) { - $finalPromise = new FulfilledPromise($builtResponse); - goto finalYield; + yield promise_for($builtResponse); + return; } } } @@ -499,22 +499,20 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api, $writeCacheItem->set($fallbackJson); $writeCache->save($writeCacheItem); - $finalPromise = new FulfilledPromise( - $this->buildResponseFromRaw( - json_decode($fallbackJson, true), - $spaceData['name'], - $assetDecorator, - $shouldBuildTypedResources - ) - ); - goto finalYield; + yield promise_for($this->buildResponseFromRaw( + json_decode($fallbackJson, true), + $spaceData['name'], + $assetDecorator, + $shouldBuildTypedResources + )); + return; } } //if there is a rate limit error, wait (if applicable) if ($e->hasResponse() && $e->getResponse()->getStatusCode() === 429 && $spaceData['retry_time_after_rate_limit_in_ms']) { usleep(intval($spaceData['retry_time_after_rate_limit_in_ms'])); - $finalPromise = (yield $this->doRequest( + yield $this->doRequest( $spaceData, $spaceName, $endpointUrl, @@ -524,8 +522,8 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api, $cacheDisambiguator, $parameters, array_merge($options, ['async' => true]) - )); - goto finalYield; + ); + return; } $unavailableException = new ResourceUnavailableException($e->getResponse(), $exceptionMessage, 0, $e); } @@ -594,8 +592,8 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api, ); $builtResponse = $buildResponseFromJson($fallbackJson); if ($builtResponse) { - $finalPromise = new FulfilledPromise($builtResponse); - goto finalYield; + yield promise_for($builtResponse); + return; } } } @@ -613,9 +611,7 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api, LogInterface::TYPE_RESPONSE ); - finalYield: - - yield $finalPromise ?: new FulfilledPromise($builtResponse); + yield promise_for($builtResponse); } );