|
29 | 29 |
|
30 | 30 | use DOMDocument;
|
31 | 31 | use DOMXPath;
|
| 32 | +use GuzzleHttp\Exception\ConnectException; |
| 33 | +use GuzzleHttp\Exception\RequestException; |
32 | 34 | use Kitodo\Dlf\Validation\AbstractDlfValidator;
|
| 35 | +use Psr\Log\LoggerAwareTrait; |
33 | 36 | use Slub\Dfgviewer\Common\ValidationHelper;
|
| 37 | +use TYPO3\CMS\Core\Http\RequestFactory; |
| 38 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
34 | 39 |
|
35 | 40 | /**
|
36 | 41 | * The validator checks the document URLs for their existence.
|
|
42 | 47 | */
|
43 | 48 | class DomDocumentUrlExistenceValidator extends AbstractDlfValidator
|
44 | 49 | {
|
| 50 | + use LoggerAwareTrait; |
45 | 51 |
|
46 | 52 | /**
|
47 | 53 | * Excluded host names separated by comma.
|
48 | 54 | * @var array
|
49 | 55 | */
|
50 | 56 | private array $excludeHosts;
|
51 | 57 |
|
52 |
| - public function __construct(array $configuration = []) |
| 58 | + public function __construct(array $configuration=[]) |
53 | 59 | {
|
54 | 60 | parent::__construct(DOMDocument::class);
|
55 | 61 | $this->excludeHosts = [];
|
@@ -121,14 +127,16 @@ protected function getFileUrlAndRemoveFileGroups(DOMDocument $document): array
|
121 | 127 |
|
122 | 128 | private function urlExists($url): bool
|
123 | 129 | {
|
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()); |
127 | 138 | }
|
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; |
132 | 140 | }
|
133 | 141 |
|
134 | 142 | private function isExcluded($url): bool
|
|
0 commit comments