Browse Source

fix the bug where some mf2 JSON requests were not parsed correctly

pull/97/head v1.10.3
Aaron Parecki 4 years ago
parent
commit
9fb8a0c9a3
No known key found for this signature in database GPG Key ID: 276C2817346D6056
2 changed files with 33 additions and 7 deletions
  1. +9
    -7
      lib/XRay/Parser.php
  2. +24
    -0
      tests/ParseTest.php

+ 9
- 7
lib/XRay/Parser.php View File

@ -83,7 +83,7 @@ class Parser {
$body = $http_response['body']; $body = $http_response['body'];
// Check if an mf2 JSON object was passed in // Check if an mf2 JSON object was passed in
if(is_array($body) && isset($body['items']) && isset($body['rels']) && isset($body['rel-urls'])) {
if(is_array($body) && isset($body['items'])) {
$data = Formats\Mf2::parse($http_response, $this->http, $opts); $data = Formats\Mf2::parse($http_response, $this->http, $opts);
if($data == false) { if($data == false) {
$data = [ $data = [
@ -103,17 +103,19 @@ class Parser {
return $data; return $data;
} }
if(substr($body, 0, 5) == '<?xml') {
if(is_string($body) && substr($body, 0, 5) == '<?xml') {
return Formats\XML::parse($http_response); return Formats\XML::parse($http_response);
} }
// Some feeds don't start with <?xml
$begin = trim(substr($body, 0, 40));
if(substr($begin, 0, 4) == '<rss') {
return Formats\XML::parse($http_response);
if(is_string($body)) {
// Some feeds don't start with <?xml
$begin = trim(substr($body, 0, 40));
if(substr($begin, 0, 4) == '<rss') {
return Formats\XML::parse($http_response);
}
} }
if(substr($body, 0, 1) == '{') {
if(is_string($body) && substr($body, 0, 1) == '{') {
$parsed = json_decode($body, true); $parsed = json_decode($body, true);
if($parsed && isset($parsed['version']) && $parsed['version'] == 'https://jsonfeed.org/version/1') { if($parsed && isset($parsed['version']) && $parsed['version'] == 'https://jsonfeed.org/version/1') {
$http_response['body'] = $parsed; $http_response['body'] = $parsed;

+ 24
- 0
tests/ParseTest.php View File

@ -878,6 +878,30 @@ class ParseTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('http://one.example.com/test', $data['data']['url']); $this->assertEquals('http://one.example.com/test', $data['data']['url']);
} }
public function testInputIsJSON() {
$url = 'http://example.com/entry';
$mf2json = ['items' => [
[
'type' => ['h-entry'],
'properties' => [
'content' => [['html' => 'Hello World']]
]
]
]];
$response = $this->parse([
'body' => $mf2json,
'url' => $url,
]);
$body = $response->getContent();
$data = json_decode($body, true);
$this->assertEquals('mf2+json', $data['source-format']);
$this->assertEquals('Hello World', $data['data']['content']['text']);
}
public function testInputIsParsedMf2() { public function testInputIsParsedMf2() {
$html = '<div class="h-entry"><p class="p-content p-name">Hello World</p><img src="/photo.jpg"></p></div>'; $html = '<div class="h-entry"><p class="p-content p-name">Hello World</p><img src="/photo.jpg"></p></div>';
$mf2 = Mf2\parse($html, 'http://example.com/entry'); $mf2 = Mf2\parse($html, 'http://example.com/entry');

Loading…
Cancel
Save