mirror of
https://github.com/friendica/friendica
synced 2025-04-28 19:44:23 +02:00
Merge pull request #14267 from mexon/mat/permanent-redirect
Update feed URL after permanent redirect
This commit is contained in:
commit
491a5cab53
5 changed files with 63 additions and 2 deletions
|
@ -90,10 +90,20 @@ interface ICanHandleHttpResponses
|
|||
public function getUrl(): string;
|
||||
|
||||
/**
|
||||
* If the request was redirected to another URL, gets the final URL requested
|
||||
* @return string
|
||||
*/
|
||||
public function getRedirectUrl(): string;
|
||||
|
||||
/**
|
||||
* If the request was redirected to another URL, indicates if the redirect is permanent.
|
||||
* If the request was not redirected, returns false.
|
||||
* If the request was redirected multiple times, returns true only if all of the redirects were permanent.
|
||||
*
|
||||
* @return bool True if the redirect is permanent
|
||||
*/
|
||||
public function redirectIsPermanent(): bool;
|
||||
|
||||
/**
|
||||
* Getter for body
|
||||
*
|
||||
|
|
|
@ -81,6 +81,11 @@ class CurlResult implements ICanHandleHttpResponses
|
|||
*/
|
||||
private $isRedirectUrl;
|
||||
|
||||
/**
|
||||
* @var boolean true if the URL has a permanent redirect
|
||||
*/
|
||||
private $redirectIsPermanent;
|
||||
|
||||
/**
|
||||
* @var boolean true if the curl request timed out
|
||||
*/
|
||||
|
@ -197,7 +202,7 @@ class CurlResult implements ICanHandleHttpResponses
|
|||
$this->redirectUrl = $this->info['url'];
|
||||
}
|
||||
|
||||
if ($this->returnCode == 301 || $this->returnCode == 302 || $this->returnCode == 303 || $this->returnCode == 307) {
|
||||
if ($this->returnCode == 301 || $this->returnCode == 302 || $this->returnCode == 303 || $this->returnCode == 307 || $this->returnCode == 308) {
|
||||
$redirect_parts = parse_url($this->info['redirect_url'] ?? '');
|
||||
if (empty($redirect_parts)) {
|
||||
$redirect_parts = [];
|
||||
|
@ -224,10 +229,11 @@ class CurlResult implements ICanHandleHttpResponses
|
|||
}
|
||||
|
||||
$this->redirectUrl = (string)Uri::fromParts((array)$redirect_parts);
|
||||
|
||||
$this->isRedirectUrl = true;
|
||||
$this->redirectIsPermanent = $this->returnCode == 301 || $this->returnCode == 308;
|
||||
} else {
|
||||
$this->isRedirectUrl = false;
|
||||
$this->redirectIsPermanent = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -340,6 +346,12 @@ class CurlResult implements ICanHandleHttpResponses
|
|||
return $this->isRedirectUrl;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function redirectIsPermanent(): bool
|
||||
{
|
||||
return $this->redirectIsPermanent;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getErrorNumber(): int
|
||||
{
|
||||
|
|
|
@ -52,6 +52,8 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon
|
|||
private $redirectUrl = '';
|
||||
/** @var bool */
|
||||
private $isRedirectUrl = false;
|
||||
/** @var bool */
|
||||
private $redirectIsPermanent = false;
|
||||
|
||||
public function __construct(ResponseInterface $response, string $url, $errorNumber = 0, $error = '')
|
||||
{
|
||||
|
@ -91,6 +93,13 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon
|
|||
if (count($headersRedirect) > 0) {
|
||||
$this->redirectUrl = end($headersRedirect);
|
||||
$this->isRedirectUrl = true;
|
||||
|
||||
$this->redirectIsPermanent = true;
|
||||
foreach (($response->getHeader(RedirectMiddleware::STATUS_HISTORY_HEADER) ?? []) as $history) {
|
||||
if (preg_match('/30(2|3|4|7)/', $history)) {
|
||||
$this->redirectIsPermanent = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -145,6 +154,12 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon
|
|||
return $this->isRedirectUrl;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function redirectIsPermanent(): bool
|
||||
{
|
||||
return $this->redirectIsPermanent;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getErrorNumber(): int
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue