diff options
| author | Christian Weiske <cweiske@cweiske.de> | 2020-03-07 21:42:45 +0100 |
|---|---|---|
| committer | Christian Weiske <cweiske@cweiske.de> | 2020-03-07 21:42:45 +0100 |
| commit | f0427f03bde2846e544565571e10542ea7426c4f (patch) | |
| tree | d9089ef7384f6ee526d87d6a95e85e1802134c61 /src/phinde | |
| parent | b7bd33c14a0e1fa8a382a6c4345941e854b95bf2 (diff) | |
| download | phinde-f0427f03bde2846e544565571e10542ea7426c4f.tar.gz phinde-f0427f03bde2846e544565571e10542ea7426c4f.zip | |
Support multiple hub links for WebSub
Diffstat (limited to 'src/phinde')
| -rw-r--r-- | src/phinde/HubUrlExtractor.php | 42 | ||||
| -rw-r--r-- | src/phinde/Subscriptions.php | 2 |
2 files changed, 30 insertions, 14 deletions
diff --git a/src/phinde/HubUrlExtractor.php b/src/phinde/HubUrlExtractor.php index 81a612c..4f1baa3 100644 --- a/src/phinde/HubUrlExtractor.php +++ b/src/phinde/HubUrlExtractor.php @@ -1,6 +1,11 @@ <?php namespace phinde; +/** + * Perform WebSub discovery for "hub" and "self" URLs + * + * @link https://www.w3.org/TR/websub/#discovery + */ class HubUrlExtractor { /** @@ -16,7 +21,11 @@ class HubUrlExtractor * * @param string $url Topic URL * - * @return array Array of URLs with keys: hub, self + * @return array Array of URLs with keys: hub, self. + * - "self" value is the URL + * - "hub" value is an array of URLs + * Keys may be there but most not if the URL + * does not advertise them. */ public function getUrls($url) { @@ -29,7 +38,7 @@ class HubUrlExtractor if (intval($res->getStatus() / 100) >= 4 && $res->getStatus() != 405 //method not supported/allowed ) { - return null; + return []; } $url = $res->getEffectiveUrl(); @@ -66,6 +75,8 @@ class HubUrlExtractor return $this->absolutifyUrls($urls, $base); } + $urls = [];//do not mix header and content links + $body = $res->getBody(); $doc = $this->loadHtml($body, $res); @@ -111,15 +122,16 @@ class HubUrlExtractor if ($type == 'canonical') { $type = 'self'; } - if ($type == 'hub' || $type == 'self' - && !isset($urls[$type]) - ) { - $urls[$type] = $uri; + if ($type == 'self' && !isset($urls['self'])) { + $urls['self'] = $uri; + } else if ($type == 'hub') { + $urls['hub'][] = $uri; } } } - //FIXME: base href + //<base href=".."> extraction is not necessary; RFC 5988 says: + // Note that any base IRI from the message's content is not applied. return $this->absolutifyUrls($urls, $base); } @@ -138,10 +150,8 @@ class HubUrlExtractor $links = $http->parseLinks($res->getHeader('Link')); foreach ($links as $link) { if (isset($link['_uri']) && isset($link['rel'])) { - if (!isset($urls['hub']) - && array_search('hub', $link['rel']) !== false - ) { - $urls['hub'] = $link['_uri']; + if (array_search('hub', $link['rel']) !== false) { + $urls['hub'][] = $link['_uri']; } if (!isset($urls['self']) && array_search('self', $link['rel']) !== false @@ -221,7 +231,7 @@ class HubUrlExtractor /** * Make the list of urls absolute * - * @param array $urls Array of maybe relative URLs + * @param array $urls Array of maybe relative URLs, or array of URLs * @param object $base Base URL to resolve the relatives against * * @return array List of absolute URLs @@ -229,7 +239,13 @@ class HubUrlExtractor protected function absolutifyUrls($urls, \Net_URL2 $base) { foreach ($urls as $key => $url) { - $urls[$key] = (string) $base->resolve($url); + if (is_array($url)) { + foreach ($url as $singleKey => $singleUrl) { + $urls[$key][$singleKey] = (string) $base->resolve($singleUrl); + } + } else { + $urls[$key] = (string) $base->resolve($url); + } } return $urls; } diff --git a/src/phinde/Subscriptions.php b/src/phinde/Subscriptions.php index 403f5d4..4650a0c 100644 --- a/src/phinde/Subscriptions.php +++ b/src/phinde/Subscriptions.php @@ -262,7 +262,7 @@ class Subscriptions $urls = $hue->getUrls($url); //we violate the spec by not requiring a self URL $topicUrl = isset($urls['self']) ? $urls['self'] : $url; - $hubUrl = isset($urls['hub']) ? $urls['hub'] : null; + $hubUrl = isset($urls['hub'][0]) ? $urls['hub'][0] : null; return array($topicUrl, $hubUrl); } |
