Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 88e23d3

Browse files
Improve handling of feeds when discover encounters feel links without a protocol (#405)
Signed-off-by: Accalia <[email protected]> Signed-off-by: Accalia <[email protected]> Co-authored-by: alexdebril <[email protected]>
1 parent 8b40946 commit 88e23d3

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

src/FeedIo/Explorer.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,15 @@ protected function extractFeeds(string $html): array
4545
$feeds = [];
4646
foreach ($links as $link) {
4747
if ($this->isFeedLink($link)) {
48-
$feeds[] = $link->getAttribute('href');
48+
$href = $link->getAttribute('href');
49+
if (strpos($href, '//') === 0) {
50+
// Link href is protocol-less, Implies feed supports http
51+
// and https. Consumers will often assume that feed url
52+
// includes protocol, so we will assign a protocol before
53+
// returning
54+
$href = 'https:' . $href;
55+
}
56+
$feeds[] = $href;
4957
}
5058
}
5159

tests/FeedIo/ExplorerTest.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,24 @@ class ExplorerTest extends TestCase
2626
protected $object;
2727

2828
public function setUp(): void
29+
{
30+
31+
}
32+
33+
protected function createTestObject(string $htmlPath)
2934
{
3035
$this->object = new Explorer(
31-
$this->getClientMock(),
36+
$this->getClientMock($htmlPath),
3237
new NullLogger()
3338
);
3439
}
3540

3641
/**
3742
* @return \FeedIo\Adapter\ClientInterface
3843
*/
39-
protected function getClientMock()
44+
protected function getClientMock(string $htmlPath)
4045
{
41-
$html = file_get_contents(dirname(__FILE__)."/../samples/discovery.html");
46+
$html = file_get_contents(dirname(__FILE__) . $htmlPath);
4247
$client = $this->createMock('FeedIo\Adapter\ClientInterface');
4348
$response = $this->createMock('FeedIo\Adapter\ResponseInterface');
4449
$response->expects($this->any())->method('getBody')->will($this->returnValue($html));
@@ -52,8 +57,31 @@ protected function getClientMock()
5257
*/
5358
public function testDiscover()
5459
{
60+
$this->createTestObject("/../samples/discovery.html");
5561
$feeds = $this->object->discover('https://exmple.org/feed.atom');
5662

5763
$this->assertEquals(['http://example.org/feed.xml', 'http://example.org/comments.xml'], $feeds);
5864
}
65+
66+
/**
67+
* @covers \FeedIo\Reader::addParser
68+
*/
69+
public function testDiscoverHttps()
70+
{
71+
$this->createTestObject("/../samples/discovery-https.html");
72+
$feeds = $this->object->discover('https://exmple.org/feed.atom');
73+
74+
$this->assertEquals(['https://example.org/feed.xml', 'https://example.org/comments.xml'], $feeds);
75+
}
76+
77+
/**
78+
* @covers \FeedIo\Reader::addParser
79+
*/
80+
public function testDiscoverProtocolless()
81+
{
82+
$this->createTestObject("/../samples/discovery-mixed.html");
83+
$feeds = $this->object->discover('https://exmple.org/feed.atom');
84+
85+
$this->assertEquals(['https://example.org/feed.xml', 'https://example.org/comments.xml'], $feeds);
86+
}
5987
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<html>
2+
<head>
3+
4+
<link rel="shortcut icon" href="/favicon.ico">
5+
<link rel="search" type="application/opensearchdescription+xml" href="/improvedsearch.src" title="Add search">
6+
<link rel="alternate" type="application/atom+xml" href="//example.org/feed.xml" title="Main feed">
7+
<link rel="alternate" type="application/atom+xml" href="//example.org/comments.xml" title="Comments feed">
8+
9+
<link rel="canonical" href="//example.org/index.php">
10+
<link rel="shorturl" href="//example.org/index">
11+
<link rel="alternate" href="//example.org/index" hreflang="x-default">
12+
13+
<link rel="stylesheet" type="text/css" href="//example.org/styles/home.css" media="screen">
14+
</head>
15+
<body></body>
16+
</html>

tests/samples/discovery-https.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<html>
2+
<head>
3+
4+
<link rel="shortcut icon" href="/favicon.ico">
5+
<link rel="search" type="application/opensearchdescription+xml" href="/improvedsearch.src" title="Add search">
6+
<link rel="alternate" type="application/atom+xml" href="https://example.org/feed.xml" title="Main feed">
7+
<link rel="alternate" type="application/atom+xml" href="https://example.org/comments.xml" title="Comments feed">
8+
9+
<link rel="canonical" href="https://example.org/index.php">
10+
<link rel="shorturl" href="https://example.org/index">
11+
<link rel="alternate" href="https://example.org/index" hreflang="x-default">
12+
13+
<link rel="stylesheet" type="text/css" href="https://example.org/styles/home.css" media="screen">
14+
</head>
15+
<body></body>
16+
</html>

0 commit comments

Comments
 (0)