Skip to content

Commit 1befaf8

Browse files
committed
Use TYPO3 HTTP Client
1 parent 2acf6cb commit 1befaf8

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

Classes/Validation/DomDocumentUrlExistenceValidator.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@
2929

3030
use DOMDocument;
3131
use DOMXPath;
32+
use GuzzleHttp\Exception\ConnectException;
33+
use GuzzleHttp\Exception\RequestException;
3234
use Kitodo\Dlf\Validation\AbstractDlfValidator;
35+
use Psr\Log\LoggerAwareTrait;
3336
use Slub\Dfgviewer\Common\ValidationHelper;
37+
use TYPO3\CMS\Core\Http\RequestFactory;
38+
use TYPO3\CMS\Core\Utility\GeneralUtility;
3439

3540
/**
3641
* The validator checks the document URLs for their existence.
@@ -42,14 +47,15 @@
4247
*/
4348
class DomDocumentUrlExistenceValidator extends AbstractDlfValidator
4449
{
50+
use LoggerAwareTrait;
4551

4652
/**
4753
* Excluded host names separated by comma.
4854
* @var array
4955
*/
5056
private array $excludeHosts;
5157

52-
public function __construct(array $configuration = [])
58+
public function __construct(array $configuration=[])
5359
{
5460
parent::__construct(DOMDocument::class);
5561
$this->excludeHosts = [];
@@ -121,14 +127,16 @@ protected function getFileUrlAndRemoveFileGroups(DOMDocument $document): array
121127

122128
private function urlExists($url): bool
123129
{
124-
$headers = @get_headers($url);
125-
if ($headers === false || !is_array($headers) || count($headers) == 0) {
126-
return false;
130+
/** @var RequestFactory $requestFactory */
131+
$requestFactory = GeneralUtility::makeInstance(RequestFactory::class);
132+
try {
133+
$response = $requestFactory->request($url);
134+
$statusCode = $response->getStatusCode();
135+
return $statusCode >= 200 && $statusCode < 400;
136+
} catch (ConnectException|RequestException $e) {
137+
$this->logger->debug($e->getMessage());
127138
}
128-
129-
preg_match('/HTTP\/\d\.\d\s+(\d+)/', $headers[0], $matches);
130-
$statusCode = (int)$matches[1];
131-
return $statusCode >= 200 && $statusCode < 400;
139+
return false;
132140
}
133141

134142
private function isExcluded($url): bool

0 commit comments

Comments
 (0)