From 2234d6fa91dbacbf4ac55f0142cbad918e615269 Mon Sep 17 00:00:00 2001 From: Philipp Date: Thu, 9 Dec 2021 14:59:25 +0100 Subject: [PATCH] Fix redirect url in Guzzle Responses --- .../HTTPClient/Response/GuzzleResponse.php | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Network/HTTPClient/Response/GuzzleResponse.php b/src/Network/HTTPClient/Response/GuzzleResponse.php index aa92309e5..722ec5331 100644 --- a/src/Network/HTTPClient/Response/GuzzleResponse.php +++ b/src/Network/HTTPClient/Response/GuzzleResponse.php @@ -23,8 +23,8 @@ namespace Friendica\Network\HTTPClient\Response; use Friendica\Core\Logger; use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses; -use Friendica\Network\HTTPException\NotImplementedException; use GuzzleHttp\Psr7\Response; +use GuzzleHttp\RedirectMiddleware; use Psr\Http\Message\ResponseInterface; /** @@ -48,6 +48,11 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon */ private $error; + /** @var string */ + private $redirectUrl = ''; + /** @var bool */ + private $isRedirectUrl = false; + public function __construct(ResponseInterface $response, string $url, $errorNumber = 0, $error = '') { parent::__construct($response->getStatusCode(), $response->getHeaders(), $response->getBody(), $response->getProtocolVersion(), $response->getReasonPhrase()); @@ -56,6 +61,7 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon $this->errorNumber = $errorNumber; $this->checkSuccess(); + $this->checkRedirect($response); } private function checkSuccess() @@ -78,6 +84,16 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon } } + private function checkRedirect(ResponseInterface $response) + { + $headersRedirect = $response->getHeader(RedirectMiddleware::HISTORY_HEADER) ?? []; + + if (count($headersRedirect) > 0) { + $this->redirectUrl = $headersRedirect[0]; + $this->isRedirectUrl = true; + } + } + /** {@inheritDoc} */ public function getReturnCode(): string { @@ -119,16 +135,14 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon /** {@inheritDoc} */ public function getRedirectUrl(): string { - return $this->url; + return $this->redirectUrl; } /** {@inheritDoc} - * - * @throws NotImplementedException */ public function isRedirectUrl(): bool { - throw new NotImplementedException(); + return $this->isRedirectUrl; } /** {@inheritDoc} */