Skip to content

Commit ce88893

Browse files
Alex ShuVitaliy Vatulya
authored andcommitted
Added adapter-specific exception. Added second attempt to obtain PSL ignoring SSL peer. Protect PHP cache to be rewrited with an empty array in case of error.
Throwing an HTTP adapter's exception if content couldn't be fetched Added handling of exceptions during getting PSL Prevent writing an empty data into a cache file (by Alex Shu)
1 parent 896e7e7 commit ce88893

File tree

9 files changed

+1973
-177
lines changed

9 files changed

+1973
-177
lines changed

data/public-suffix-list.php

Lines changed: 1892 additions & 164 deletions
Large diffs are not rendered by default.

src/Pdp/HttpAdapter/CurlHttpAdapter.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Pdp\HttpAdapter;
1313

14+
15+
use Pdp\HttpAdapter\Exception;
16+
1417
/**
1518
* cURL http adapter.
1619
*
@@ -31,8 +34,20 @@ public function getContent($url)
3134
{
3235
$ch = curl_init();
3336
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
37+
curl_setopt($ch, CURLOPT_FAILONERROR, true);
38+
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
3439
curl_setopt($ch, CURLOPT_URL, $url);
40+
3541
$content = curl_exec($ch);
42+
43+
if ($errNo = curl_errno($ch)) {
44+
throw new Exception\CurlHttpAdapterException("CURL error [{$errNo}]: " . curl_error($ch), $errNo);
45+
}
46+
47+
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
48+
if ($responseCode !== 200) {
49+
throw new Exception\CurlHttpAdapterException('Wrong HTTP response code: ' . $responseCode, $responseCode);
50+
}
3651
curl_close($ch);
3752

3853
return $content;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Pdp\HttpAdapter\Exception;
4+
5+
6+
class CurlHttpAdapterException extends HttpAdapterException
7+
{
8+
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Pdp\HttpAdapter\Exception;
4+
5+
6+
class HttpAdapterException extends \Exception
7+
{
8+
9+
}

src/Pdp/HttpAdapter/HttpAdapterInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ interface HttpAdapterInterface
3030
* @param string $url
3131
*
3232
* @return string Retrieved content
33+
*
34+
* @throws Exception\HttpAdapterException
3335
*/
3436
public function getContent($url);
3537
}

src/Pdp/PublicSuffixListManager.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Pdp;
1313

14+
use Pdp\HttpAdapter\Exception\HttpAdapterException;
1415
use Pdp\HttpAdapter\HttpAdapterInterface;
1516

1617
/**
@@ -69,13 +70,17 @@ public function refreshPublicSuffixList()
6970
$publicSuffixListArray = $this->parseListToArray(
7071
$this->cacheDir . '/' . self::PDP_PSL_TEXT_FILE
7172
);
72-
$this->writePhpCache($publicSuffixListArray);
73+
74+
// do not empty existing PHP cache file if source TXT is empty
75+
if (is_array($publicSuffixListArray) && !empty($publicSuffixListArray)) {
76+
$this->writePhpCache($publicSuffixListArray);
77+
}
7378
}
7479

7580
/**
7681
* Obtain Public Suffix List from its online source and write to cache dir.
7782
*
78-
* @return int Number of bytes that were written to the file
83+
* @return int|bool Number of bytes that were written to the file OR false in case of error
7984
*/
8085
public function fetchListFromSource()
8186
{
@@ -185,13 +190,20 @@ public function writePhpCache(array $publicSuffixList)
185190
*/
186191
public function getList()
187192
{
188-
if (!file_exists($this->cacheDir . '/' . self::PDP_PSL_PHP_FILE)) {
189-
$this->refreshPublicSuffixList();
193+
$filename = $this->cacheDir . '/' . self::PDP_PSL_PHP_FILE;
194+
195+
if (file_exists($filename)) {
196+
$data = include $filename;
197+
} else {
198+
try {
199+
$this->refreshPublicSuffixList();
200+
$data = include $filename;
201+
} catch (\Exception $e) {
202+
$data = array();
203+
}
190204
}
191205

192-
$this->list = new PublicSuffixList(
193-
include $this->cacheDir . '/' . self::PDP_PSL_PHP_FILE
194-
);
206+
$this->list = new PublicSuffixList((array) $data);
195207

196208
return $this->list;
197209
}
@@ -208,10 +220,16 @@ public function getList()
208220
*/
209221
protected function write($filename, $data)
210222
{
211-
$result = @file_put_contents($this->cacheDir . '/' . $filename, $data);
223+
$data = trim($data);
224+
$filename = $this->cacheDir . '/' . $filename;
225+
226+
if (empty($data)) {
227+
throw new \Exception("No data to write into '{$filename}'");
228+
}
212229

230+
$result = @file_put_contents($filename, $data . PHP_EOL);
213231
if ($result === false) {
214-
throw new \Exception("Cannot write '" . $this->cacheDir . '/' . "$filename'");
232+
throw new \Exception("Cannot write '{$filename}'");
215233
}
216234

217235
return $result;

tests/src/Pdp/CheckPublicSuffixTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ public function testPublicSuffixSpec()
6565
$this->checkPublicSuffix('test.ac', 'test.ac');
6666
// TLD with only 1 (wildcard) rule.
6767
$this->checkPublicSuffix('cy', null);
68-
$this->checkPublicSuffix('c.cy', null);
69-
$this->checkPublicSuffix('b.c.cy', 'b.c.cy');
70-
$this->checkPublicSuffix('a.b.c.cy', 'b.c.cy');
68+
$this->checkPublicSuffix('c.cy', 'c.cy');
69+
$this->checkPublicSuffix('b.c.cy', 'c.cy');
70+
$this->checkPublicSuffix('a.b.c.cy', 'c.cy');
7171
// More complex TLD.
7272
$this->checkPublicSuffix('jp', null);
7373
$this->checkPublicSuffix('test.jp', 'test.jp');

tests/src/Pdp/HttpAdapter/CurlHttpAdapterTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Pdp\HttpAdapter;
44

5+
6+
use Pdp\HttpAdapter\Exception;
7+
58
/**
69
* @group internet
710
*/
@@ -32,4 +35,16 @@ public function testGetContent()
3235
$this->assertNotNull($content);
3336
$this->assertContains('google', $content);
3437
}
38+
39+
public function testExceptionBadUrl()
40+
{
41+
$this->setExpectedException('Pdp\HttpAdapter\Exception\HttpAdapterException', '', CURLE_COULDNT_RESOLVE_HOST);
42+
$content = $this->adapter->getContent('https://aaaa.aaaa');
43+
}
44+
45+
public function testExceptionBadHttpsCertificate()
46+
{
47+
$this->setExpectedException('Pdp\HttpAdapter\Exception\HttpAdapterException', '', CURLE_SSL_PEER_CERTIFICATE);
48+
$content = $this->adapter->getContent('https://tv.eurosport.com/');
49+
}
3550
}

tests/src/Pdp/ParserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public function parseDataProvider()
189189
array('cea-law.co.il', 'co.il', 'cea-law.co.il', null, 'cea-law.co.il'),
190190
array('http://edition.cnn.com/WORLD/', 'com', 'cnn.com', 'edition', 'edition.cnn.com'),
191191
array('http://en.wikipedia.org/', 'org', 'wikipedia.org', 'en', 'en.wikipedia.org'),
192-
array('a.b.c.cy', 'c.cy', 'b.c.cy', 'a', 'a.b.c.cy'),
192+
array('a.b.c.cy', 'cy', 'c.cy', 'a.b', 'a.b.c.cy'),
193193
array('https://test.k12.ak.us', 'k12.ak.us', 'test.k12.ak.us', null, 'test.k12.ak.us'),
194194
array('www.scottwills.co.uk', 'co.uk', 'scottwills.co.uk', 'www', 'www.scottwills.co.uk'),
195195
array('b.ide.kyoto.jp', 'ide.kyoto.jp', 'b.ide.kyoto.jp', null, 'b.ide.kyoto.jp'),

0 commit comments

Comments
 (0)