diff --git a/composer.json b/composer.json index 0444d9f..89096e1 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ "indieweb/link-rel-parser": "0.1.*", "dg/twitter-php": "3.6.*", "p3k/timezone": "*", - "p3k/http": ">=0.1.7", + "p3k/http": ">=0.1.8", "cebe/markdown": "1.1.*", "p3k/picofeed": ">=0.1.38", "facebook/graph-sdk": "^5.5", diff --git a/composer.lock b/composer.lock index 4754eea..65f6038 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "38eef403bd3151bf73b7794372f4d8cd", + "content-hash": "fe5c8f1b6a8a559b0b76aa623ca3aef4", "packages": [ { "name": "cebe/markdown", @@ -382,16 +382,16 @@ }, { "name": "p3k/http", - "version": "0.1.7", + "version": "0.1.8", "source": { "type": "git", "url": "https://github.com/aaronpk/p3k-http.git", - "reference": "1826647c4902a18dea5ec532f21509ba4d51210b" + "reference": "a43977636d7a930080009eddda06994037c88fd7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/aaronpk/p3k-http/zipball/1826647c4902a18dea5ec532f21509ba4d51210b", - "reference": "1826647c4902a18dea5ec532f21509ba4d51210b", + "url": "https://api.github.com/repos/aaronpk/p3k-http/zipball/a43977636d7a930080009eddda06994037c88fd7", + "reference": "a43977636d7a930080009eddda06994037c88fd7", "shasum": "" }, "require": { @@ -416,7 +416,7 @@ ], "description": "A simple wrapper API around the PHP curl functions", "homepage": "https://github.com/aaronpk/p3k-http", - "time": "2018-03-04T15:21:58+00:00" + "time": "2019-06-15T20:49:26+00:00" }, { "name": "p3k/picofeed", diff --git a/lib/XRay/Feeds.php b/lib/XRay/Feeds.php index 1af19da..de3df46 100644 --- a/lib/XRay/Feeds.php +++ b/lib/XRay/Feeds.php @@ -103,6 +103,15 @@ class Feeds { } } + // Check if the feed URL was a temporary redirect + if($url != $result['url']) { + // p3k\http doesn't return the intermediate HTTP codes, so we have to fetch the input URL again without following redirects + $this->http->set_max_redirects(0); + $check = $this->http->get($url); + if($check['code'] == 302) + $result['url'] = $url; + } + $parsed = Formats\HTML::parse($this->http, $result, array_merge($opts, ['expect'=>'feed'])); if($parsed && isset($parsed['data']['type']) && $parsed['data']['type'] == 'feed') { $feeds[] = [ diff --git a/tests/FindFeedsTest.php b/tests/FindFeedsTest.php index 544da95..c4fb6af 100644 --- a/tests/FindFeedsTest.php +++ b/tests/FindFeedsTest.php @@ -138,6 +138,34 @@ class FindFeedsTest extends PHPUnit_Framework_TestCase { $this->assertEquals('atom', $feeds[0]->type); } + // input URL is a temporary redirect to another page. + // report the original input URL + public function testInputIsTemporaryRedirect() { + $url = 'http://feed.example.com/temporary-redirect'; + $response = $this->parse(['url' => $url]); + + $body = $response->getContent(); + $this->assertEquals(200, $response->getStatusCode()); + $feeds = json_decode($body)->feeds; + + $this->assertEquals(1, count($feeds)); + $this->assertEquals('http://feed.example.com/temporary-redirect', $feeds[0]->url); + $this->assertEquals('microformats', $feeds[0]->type); + } + + public function testInputIsPermanentRedirect() { + $url = 'http://feed.example.com/permanent-redirect'; + $response = $this->parse(['url' => $url]); + + $body = $response->getContent(); + $this->assertEquals(200, $response->getStatusCode()); + $feeds = json_decode($body)->feeds; + + $this->assertEquals(1, count($feeds)); + $this->assertEquals('http://feed.example.com/permanent-redirect-target', $feeds[0]->url); + $this->assertEquals('microformats', $feeds[0]->type); + } + // input URL is an RSS feed public function testInputIsRSS() { $url = 'http://feed.example.com/rss'; diff --git a/tests/data/feed.example.com/permanent-redirect b/tests/data/feed.example.com/permanent-redirect new file mode 100644 index 0000000..c85a58b --- /dev/null +++ b/tests/data/feed.example.com/permanent-redirect @@ -0,0 +1,15 @@ +HTTP/1.1 301 Moved Permanently +Server: Apache +Date: Wed, 09 Dec 2015 03:29:14 GMT +Content-Type: text/html; charset=utf-8 +Connection: keep-alive +Location: http://feed.example.com/permanent-redirect-target + + + + Moved + + + This page has moved + + diff --git a/tests/data/feed.example.com/permanent-redirect-target b/tests/data/feed.example.com/permanent-redirect-target new file mode 100644 index 0000000..efafa48 --- /dev/null +++ b/tests/data/feed.example.com/permanent-redirect-target @@ -0,0 +1,29 @@ +HTTP/1.1 200 OK +Server: Apache +Date: Wed, 09 Dec 2015 03:29:14 GMT +Content-Type: text/html; charset=utf-8 +Connection: keep-alive + + + + Test + + + + + + + diff --git a/tests/data/feed.example.com/temporary-redirect b/tests/data/feed.example.com/temporary-redirect new file mode 100644 index 0000000..cfceb49 --- /dev/null +++ b/tests/data/feed.example.com/temporary-redirect @@ -0,0 +1,15 @@ +HTTP/1.1 302 Found +Server: Apache +Date: Wed, 09 Dec 2015 03:29:14 GMT +Content-Type: text/html; charset=utf-8 +Connection: keep-alive +Location: http://feed.example.com/temporary-redirect-target + + + + Moved + + + This page has moved + + diff --git a/tests/data/feed.example.com/temporary-redirect-target b/tests/data/feed.example.com/temporary-redirect-target new file mode 100644 index 0000000..efafa48 --- /dev/null +++ b/tests/data/feed.example.com/temporary-redirect-target @@ -0,0 +1,29 @@ +HTTP/1.1 200 OK +Server: Apache +Date: Wed, 09 Dec 2015 03:29:14 GMT +Content-Type: text/html; charset=utf-8 +Connection: keep-alive + + + + Test + + + + + + +