From c530011b2cd7c37c929beaf9fdaf3890c9650f4a Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Fri, 30 Aug 2024 07:29:22 -0700 Subject: [PATCH] add option to ignore rel-alternate as2 --- controllers/Parse.php | 4 ++++ lib/XRay/Formats/HTML.php | 7 ++++++- tests/ParseTest.php | 18 ++++++++++++++++-- .../data/source.example.com/rel-alternate-as2 | 2 +- .../source.example.com/rel-alternate-priority | 2 +- .../rel-alternate-priority.json | 2 +- 6 files changed, 29 insertions(+), 6 deletions(-) diff --git a/controllers/Parse.php b/controllers/Parse.php index 867bb5e..237c2df 100644 --- a/controllers/Parse.php +++ b/controllers/Parse.php @@ -75,6 +75,10 @@ class Parse { $opts['allowIframeVideo'] = $request->get('allow-iframe-video') == 'true'; } + if($request->get('ignore-as2')) { + $opts['ignore-as2'] = $request->get('ignore-as2') == 'true'; + } + $url = $request->get('url'); $html = $request->get('html') ?: $request->get('body'); diff --git a/lib/XRay/Formats/HTML.php b/lib/XRay/Formats/HTML.php index 9219610..ff7e79d 100644 --- a/lib/XRay/Formats/HTML.php +++ b/lib/XRay/Formats/HTML.php @@ -114,12 +114,17 @@ class HTML extends Format { } } - if(count($alternates['as2'])) { + $ignoreAS2 = false; + if(isset($opts['ignore-as2']) && $opts['ignore-as2'] == true) + $ignoreAS2 = true; + + if(!$ignoreAS2 && count($alternates['as2'])) { $relurl = $alternates['as2'][0]; // Fetch and parse the ActivityStreams JSON link $jsonpage = $http->get($relurl, [ 'Accept' => 'application/activity+json,application/json' ]); + // Skip and fall back to parsing the HTML if anything about this request fails if(!$jsonpage['error'] && $jsonpage['body']) { $jsondata = json_decode($jsonpage['body'],true); diff --git a/tests/ParseTest.php b/tests/ParseTest.php index bcb274e..cbf6144 100644 --- a/tests/ParseTest.php +++ b/tests/ParseTest.php @@ -1167,7 +1167,7 @@ class ParseTest extends PHPUnit\Framework\TestCase $this->assertEquals('mf2+json', $data['source-format']); $this->assertEquals('http://source.example.com/rel-alternate-priority.json', $data['parsed-url']); - $this->assertEquals('This should be the content from XRay', $data['data']['content']['text']); + $this->assertEquals('This is the content in the MF2 JSON file', $data['data']['content']['text']); } public function testRelAlternatePrioritizesMf2OverAS2() @@ -1181,7 +1181,21 @@ class ParseTest extends PHPUnit\Framework\TestCase $this->assertEquals('mf2+json', $data['source-format']); $this->assertEquals('http://source.example.com/rel-alternate-priority.json', $data['parsed-url']); - $this->assertEquals('This should be the content from XRay', $data['data']['content']['text']); + $this->assertEquals('This is the content in the MF2 JSON file', $data['data']['content']['text']); + } + + public function testRelAlternateIgnoreAS2AlternateOption() + { + $url = 'http://source.example.com/rel-alternate-as2'; + $response = $this->parse(['url' => $url, 'ignore-as2' => true]); + + $body = $response->getContent(); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode($body, true); + + $this->assertEquals('mf2+html', $data['source-format']); + $this->assertArrayNotHasKey('parsed-url', $data); + $this->assertEquals('This is the content in the HTML instead of the AS2 JSON', $data['data']['content']['text']); } public function testRelAlternateFallsBackOnInvalidJSON() diff --git a/tests/data/source.example.com/rel-alternate-as2 b/tests/data/source.example.com/rel-alternate-as2 index a9b5bf1..1f8ebe6 100644 --- a/tests/data/source.example.com/rel-alternate-as2 +++ b/tests/data/source.example.com/rel-alternate-as2 @@ -13,7 +13,7 @@ Connection: keep-alive
-

This should not be the content from XRay

+

This is the content in the HTML instead of the AS2 JSON

diff --git a/tests/data/source.example.com/rel-alternate-priority b/tests/data/source.example.com/rel-alternate-priority index bcc882f..6fbbbe8 100644 --- a/tests/data/source.example.com/rel-alternate-priority +++ b/tests/data/source.example.com/rel-alternate-priority @@ -13,7 +13,7 @@ Connection: keep-alive
-

This should not be the content from XRay

+

This is the content in the HTML page

diff --git a/tests/data/source.example.com/rel-alternate-priority.json b/tests/data/source.example.com/rel-alternate-priority.json index a3839ba..27e9dca 100644 --- a/tests/data/source.example.com/rel-alternate-priority.json +++ b/tests/data/source.example.com/rel-alternate-priority.json @@ -12,7 +12,7 @@ Connection: keep-alive ], "properties": { "content": [ - "This should be the content from XRay" + "This is the content in the MF2 JSON file" ] } }