Skip to content

Commit

Permalink
add option to ignore rel-alternate as2
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronpk committed Aug 30, 2024
1 parent db4ca59 commit c530011
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
4 changes: 4 additions & 0 deletions controllers/Parse.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public function parse(Request $request, Response $response) {
$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');

Expand Down
7 changes: 6 additions & 1 deletion lib/XRay/Formats/HTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,17 @@ public static function parse($http, $http_response, $opts=[]) {
}
}

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);
Expand Down
18 changes: 16 additions & 2 deletions tests/ParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ public function testRelAlternatePrioritizesJSON()

$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()
Expand All @@ -1181,7 +1181,21 @@ public function testRelAlternatePrioritizesMf2OverAS2()

$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()
Expand Down
2 changes: 1 addition & 1 deletion tests/data/source.example.com/rel-alternate-as2
Original file line number Diff line number Diff line change
Expand Up @@ -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>
2 changes: 1 addition & 1 deletion tests/data/source.example.com/rel-alternate-priority
Original file line number Diff line number Diff line change
Expand Up @@ -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>
2 changes: 1 addition & 1 deletion tests/data/source.example.com/rel-alternate-priority.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
}
Expand Down

0 comments on commit c530011

Please sign in to comment.