mirror of
https://github.com/friendica/friendica
synced 2024-12-22 22:40:16 +00: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
|
@ -2445,6 +2445,19 @@ class Contact
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the poll URL of a contact. This is the right function to call if there is a redirect.
|
||||||
|
*
|
||||||
|
* @param integer $id contact id
|
||||||
|
* @param string $url The new URL to use for polling
|
||||||
|
*
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public static function updatePollUrl(int $id, string $url)
|
||||||
|
{
|
||||||
|
self::update(['poll', $url], ['id' => $id]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function for "updateFromProbe". Updates personal and public contact
|
* Helper function for "updateFromProbe". Updates personal and public contact
|
||||||
*
|
*
|
||||||
|
|
|
@ -90,10 +90,20 @@ interface ICanHandleHttpResponses
|
||||||
public function getUrl(): string;
|
public function getUrl(): string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* If the request was redirected to another URL, gets the final URL requested
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getRedirectUrl(): 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
|
* Getter for body
|
||||||
*
|
*
|
||||||
|
|
|
@ -81,6 +81,11 @@ class CurlResult implements ICanHandleHttpResponses
|
||||||
*/
|
*/
|
||||||
private $isRedirectUrl;
|
private $isRedirectUrl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var boolean true if the URL has a permanent redirect
|
||||||
|
*/
|
||||||
|
private $redirectIsPermanent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var boolean true if the curl request timed out
|
* @var boolean true if the curl request timed out
|
||||||
*/
|
*/
|
||||||
|
@ -197,7 +202,7 @@ class CurlResult implements ICanHandleHttpResponses
|
||||||
$this->redirectUrl = $this->info['url'];
|
$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'] ?? '');
|
$redirect_parts = parse_url($this->info['redirect_url'] ?? '');
|
||||||
if (empty($redirect_parts)) {
|
if (empty($redirect_parts)) {
|
||||||
$redirect_parts = [];
|
$redirect_parts = [];
|
||||||
|
@ -224,10 +229,11 @@ class CurlResult implements ICanHandleHttpResponses
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->redirectUrl = (string)Uri::fromParts((array)$redirect_parts);
|
$this->redirectUrl = (string)Uri::fromParts((array)$redirect_parts);
|
||||||
|
|
||||||
$this->isRedirectUrl = true;
|
$this->isRedirectUrl = true;
|
||||||
|
$this->redirectIsPermanent = $this->returnCode == 301 || $this->returnCode == 308;
|
||||||
} else {
|
} else {
|
||||||
$this->isRedirectUrl = false;
|
$this->isRedirectUrl = false;
|
||||||
|
$this->redirectIsPermanent = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -340,6 +346,12 @@ class CurlResult implements ICanHandleHttpResponses
|
||||||
return $this->isRedirectUrl;
|
return $this->isRedirectUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc} */
|
||||||
|
public function redirectIsPermanent(): bool
|
||||||
|
{
|
||||||
|
return $this->redirectIsPermanent;
|
||||||
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public function getErrorNumber(): int
|
public function getErrorNumber(): int
|
||||||
{
|
{
|
||||||
|
|
|
@ -52,6 +52,8 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon
|
||||||
private $redirectUrl = '';
|
private $redirectUrl = '';
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
private $isRedirectUrl = false;
|
private $isRedirectUrl = false;
|
||||||
|
/** @var bool */
|
||||||
|
private $redirectIsPermanent = false;
|
||||||
|
|
||||||
public function __construct(ResponseInterface $response, string $url, $errorNumber = 0, $error = '')
|
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) {
|
if (count($headersRedirect) > 0) {
|
||||||
$this->redirectUrl = end($headersRedirect);
|
$this->redirectUrl = end($headersRedirect);
|
||||||
$this->isRedirectUrl = true;
|
$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;
|
return $this->isRedirectUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc} */
|
||||||
|
public function redirectIsPermanent(): bool
|
||||||
|
{
|
||||||
|
return $this->redirectIsPermanent;
|
||||||
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public function getErrorNumber(): int
|
public function getErrorNumber(): int
|
||||||
{
|
{
|
||||||
|
|
|
@ -167,12 +167,23 @@ class OnePoll
|
||||||
$cookiejar = tempnam(System::getTempPath(), 'cookiejar-onepoll-');
|
$cookiejar = tempnam(System::getTempPath(), 'cookiejar-onepoll-');
|
||||||
$curlResult = DI::httpClient()->get($contact['poll'], HttpClientAccept::FEED_XML, [HttpClientOptions::COOKIEJAR => $cookiejar, HttpClientOptions::REQUEST => HttpClientRequest::FEEDFETCHER]);
|
$curlResult = DI::httpClient()->get($contact['poll'], HttpClientAccept::FEED_XML, [HttpClientOptions::COOKIEJAR => $cookiejar, HttpClientOptions::REQUEST => HttpClientRequest::FEEDFETCHER]);
|
||||||
unlink($cookiejar);
|
unlink($cookiejar);
|
||||||
|
Logger::debug('Polled feed', ['url' => $contact['poll'], 'http-code' => $curlResult->getReturnCode(), 'redirect-url' => $curlResult->getRedirectUrl()]);
|
||||||
|
|
||||||
if ($curlResult->isTimeout()) {
|
if ($curlResult->isTimeout()) {
|
||||||
Logger::notice('Polling timed out', ['id' => $contact['id'], 'url' => $contact['poll']]);
|
Logger::notice('Polling timed out', ['id' => $contact['id'], 'url' => $contact['poll']]);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($curlResult->redirectIsPermanent()) {
|
||||||
|
Logger::notice('Poll address permanently changed', [
|
||||||
|
'id' => $contact['id'],
|
||||||
|
'uid' => $contact['uid'],
|
||||||
|
'old' => $contact['poll'],
|
||||||
|
'new' => $curlResult->getRedirectUrl(),
|
||||||
|
]);
|
||||||
|
$success = Contact::updatePollUrl($contact['id'], $curlResult->getRedirectUrl());
|
||||||
|
}
|
||||||
|
|
||||||
$xml = $curlResult->getBodyString();
|
$xml = $curlResult->getBodyString();
|
||||||
if (empty($xml)) {
|
if (empty($xml)) {
|
||||||
Logger::notice('Empty content', ['id' => $contact['id'], 'url' => $contact['poll']]);
|
Logger::notice('Empty content', ['id' => $contact['id'], 'url' => $contact['poll']]);
|
||||||
|
|
Loading…
Reference in a new issue