Finish implementing endpoint discovery for Webmentions
This commit is contained in:
1 parent
e5f7595321
commit
b63ada6dea
5 files changed
+312
-20
No files matched your search
+40
-14
@@ -4,34 +4,60 @@ namespace Lewisdale\Webmentions;
|
||||
|
||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||
use Symfony\Component\DomCrawler\Crawler;
|
||||
use League\Uri\Uri;
|
||||
use League\Uri\UriInfo;
|
||||
use League\Uri\UriResolver;
|
||||
|
||||
class EndpointParser {
|
||||
public static function parse(ResponseInterface $response) : string | null
|
||||
{
|
||||
$endpoint = self::parseHeaders($response) ?? self::parseBody($response);
|
||||
return self::absoluteURL($response, $endpoint);
|
||||
}
|
||||
|
||||
private static function parseHeaders(ResponseInterface $response) : string | null {
|
||||
$headers = $response->getHeaders();
|
||||
$endpoint = null;
|
||||
|
||||
if (isset($headers["link"])) {
|
||||
$link = $headers["link"][0];
|
||||
if (preg_match('/rel=("?)webmention("?)/', $link)) {
|
||||
$matches = [];
|
||||
preg_match('/\<(..*?)\>/', $link, $matches);
|
||||
foreach($headers["link"] as $link) {
|
||||
if (preg_match('/rel=("?)webmention("?)/', $link)) {
|
||||
$matches = [];
|
||||
preg_match('/\<(..*?)\>/', $link, $matches);
|
||||
|
||||
if (count($matches) > 1) {
|
||||
$endpoint = $matches[1];
|
||||
if (count($matches) > 1) {
|
||||
return $matches[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static function absoluteURL(ResponseInterface $response, ?string $endpoint) : string | null {
|
||||
if (!$endpoint) {
|
||||
$doc = new Crawler($response->getContent());
|
||||
$webmention = $doc->filter('rel="webmention"')->first();
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($endpoint && !str_contains($endpoint, "https://")) {
|
||||
$res = parse_url($response->getInfo('url'));
|
||||
$endpoint = $res["scheme"] . "://" . $res["host"] . $endpoint;
|
||||
$url = $response->getInfo("redirect_url") ?? $response->getInfo('url');
|
||||
$uri = Uri::createFromString($url);
|
||||
$endpoint = Uri::createFromString($endpoint);
|
||||
|
||||
if (!UriInfo::isAbsolute($endpoint)) {
|
||||
return (string) UriResolver::resolve($endpoint, $uri);
|
||||
}
|
||||
return $endpoint;
|
||||
|
||||
return (string) $endpoint;
|
||||
}
|
||||
|
||||
private static function parseBody(ResponseInterface $response) : string | null {
|
||||
$doc = new Crawler($response->getContent());
|
||||
$webmentions = $doc->filter('[rel="webmention"][href]');
|
||||
|
||||
if ($webmentions->count()) {
|
||||
return $webmentions->first()->attr('href');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+2
-3
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Lewisdale\Webmentions;
|
||||
|
||||
use DOMDocument;
|
||||
use Symfony\Component\DomCrawler\Crawler;
|
||||
use Symfony\Component\HttpClient\HttpClient;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
@@ -22,14 +21,14 @@ class Webmention {
|
||||
// $this->sendWebmention($source, $target);
|
||||
// }
|
||||
|
||||
$this->sendWebmention($source, "https://webmention.rocks/test/1");
|
||||
$this->sendWebmention($source, "https://webmention.rocks/test/23/page");
|
||||
}
|
||||
|
||||
private function getUrls(string $url) : array {
|
||||
$page = file_get_contents($url);
|
||||
$doc = new Crawler($page);
|
||||
|
||||
$urls = [];
|
||||
$urls = [];
|
||||
foreach ($doc->filter('.h-entry a') as $anchor) {
|
||||
$target_url = $anchor->attributes->getNamedItem('href')->textContent;
|
||||
|
||||
|
||||
Reference in new issue
Block a user