1
1
Fork 0

Fix redirect url in Guzzle Responses

This commit is contained in:
Philipp Holzer 2021-12-09 14:59:25 +01:00
parent e2ff48375f
commit 2234d6fa91
Signed by: nupplaPhil
GPG key ID: 24A7501396EB5432

View file

@ -23,8 +23,8 @@ namespace Friendica\Network\HTTPClient\Response;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses; use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
use Friendica\Network\HTTPException\NotImplementedException;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use GuzzleHttp\RedirectMiddleware;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
/** /**
@ -48,6 +48,11 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon
*/ */
private $error; private $error;
/** @var string */
private $redirectUrl = '';
/** @var bool */
private $isRedirectUrl = false;
public function __construct(ResponseInterface $response, string $url, $errorNumber = 0, $error = '') public function __construct(ResponseInterface $response, string $url, $errorNumber = 0, $error = '')
{ {
parent::__construct($response->getStatusCode(), $response->getHeaders(), $response->getBody(), $response->getProtocolVersion(), $response->getReasonPhrase()); 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->errorNumber = $errorNumber;
$this->checkSuccess(); $this->checkSuccess();
$this->checkRedirect($response);
} }
private function checkSuccess() 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} */ /** {@inheritDoc} */
public function getReturnCode(): string public function getReturnCode(): string
{ {
@ -119,16 +135,14 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getRedirectUrl(): string public function getRedirectUrl(): string
{ {
return $this->url; return $this->redirectUrl;
} }
/** {@inheritDoc} /** {@inheritDoc}
*
* @throws NotImplementedException
*/ */
public function isRedirectUrl(): bool public function isRedirectUrl(): bool
{ {
throw new NotImplementedException(); return $this->isRedirectUrl;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */