Browse Source

add option to ignore rel-alternate as2

main v1.15.0
Aaron Parecki 2 months ago
parent
commit
c530011b2c
6 changed files with 29 additions and 6 deletions
  1. +4
    -0
      controllers/Parse.php
  2. +6
    -1
      lib/XRay/Formats/HTML.php
  3. +16
    -2
      tests/ParseTest.php
  4. +1
    -1
      tests/data/source.example.com/rel-alternate-as2
  5. +1
    -1
      tests/data/source.example.com/rel-alternate-priority
  6. +1
    -1
      tests/data/source.example.com/rel-alternate-priority.json

+ 4
- 0
controllers/Parse.php View File

@ -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');

+ 6
- 1
lib/XRay/Formats/HTML.php View File

@ -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);

+ 16
- 2
tests/ParseTest.php View File

@ -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()

+ 1
- 1
tests/data/source.example.com/rel-alternate-as2 View File

@ -13,7 +13,7 @@ Connection: keep-alive
</head>
<body>
<div class="h-entry">
<p class="p-content">This should not be the content from XRay</p>
<p class="p-content">This is the content in the HTML instead of the AS2 JSON</p>
</div>
</body>
</html>

+ 1
- 1
tests/data/source.example.com/rel-alternate-priority View File

@ -13,7 +13,7 @@ Connection: keep-alive
</head>
<body>
<div class="h-entry">
<p class="p-content">This should not be the content from XRay</p>
<p class="p-content">This is the content in the HTML page</p>
</div>
</body>
</html>

+ 1
- 1
tests/data/source.example.com/rel-alternate-priority.json View File

@ -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"
]
}
}

Loading…
Cancel
Save