aboutsummaryrefslogtreecommitdiff
path: root/src/phinde
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2020-03-07 22:26:59 +0100
committerChristian Weiske <cweiske@cweiske.de>2020-03-07 22:26:59 +0100
commiteca6e9af6dea38f5912c881a5dc05193e1b19848 (patch)
tree6db03321e333cc48022d76814728ed5f99269066 /src/phinde
parentf0427f03bde2846e544565571e10542ea7426c4f (diff)
downloadphinde-eca6e9af6dea38f5912c881a5dc05193e1b19848.tar.gz
phinde-eca6e9af6dea38f5912c881a5dc05193e1b19848.zip
Support subscriptions to redirect URLs
Resolves: https://github.com/cweiske/phinde/issues/37
Diffstat (limited to 'src/phinde')
-rw-r--r--src/phinde/HubUrlExtractor.php15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/phinde/HubUrlExtractor.php b/src/phinde/HubUrlExtractor.php
index 4f1baa3..da29650 100644
--- a/src/phinde/HubUrlExtractor.php
+++ b/src/phinde/HubUrlExtractor.php
@@ -19,7 +19,8 @@ class HubUrlExtractor
* Get the hub and self/canonical URL of a given topic URL.
* Uses link headers and parses HTML link rels.
*
- * @param string $url Topic URL
+ * @param string $url Topic URL
+ * @param int $redirects Number of redirects that were followed
*
* @return array Array of URLs with keys: hub, self.
* - "self" value is the URL
@@ -27,12 +28,13 @@ class HubUrlExtractor
* Keys may be there but most not if the URL
* does not advertise them.
*/
- public function getUrls($url)
+ public function getUrls($url, $redirects = 0)
{
//at first, try a HEAD request that does not transfer so much data
$req = $this->getRequest();
$req->setUrl($url);
$req->setMethod(\HTTP_Request2::METHOD_HEAD);
+ $req->setConfig('follow_redirects', false);
$res = $req->send();
if (intval($res->getStatus() / 100) >= 4
@@ -49,6 +51,15 @@ class HubUrlExtractor
return $this->absolutifyUrls($urls, $base);
}
+ if ($res->isRedirect()) {
+ //we tried header links and that failed, now follow the redirect
+ if ($redirects > 5) {
+ return [];
+ }
+ $redirectUrl = (string) $base->resolve($res->getHeader('location'));
+ return $this->getUrls($redirectUrl, $redirects + 1);
+ }
+
list($type) = explode(';', $res->getHeader('Content-type'));
if ($type != 'text/html' && $type != 'text/xml'
&& $type != 'application/xhtml+xml'