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