Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 protected function doValidation(Response $response)
 {
     $document = new Document($response->getBody(), false);
     $urls = $document->getDependencies($response->getUri());
     $invalidUrls = array();
     foreach ($urls as $url) {
         if (!filter_var(trim((string) $url), FILTER_VALIDATE_URL)) {
             $invalidUrls[] = (string) $url;
         }
     }
     $this->assert(count($invalidUrls) === 0, 'Invalid urls found (' . implode(', ', $invalidUrls) . ').');
 }
Esempio n. 2
0
 public function testGetCssFiles()
 {
     $document = new Document(file_get_contents(__DIR__ . '/fixtures/referencedUrls.html'));
     $urls = $document->getCssFiles(new Uri('http://www.example.com/test/'));
     foreach ($urls as $url) {
         $currentUrls[] = (string) $url;
     }
     $expectedUrls = array('http://fonts.googleapis.com/css?family=Dancing+Script');
     sort($expectedUrls);
     sort($currentUrls);
     $this->assertEquals($currentUrls, $expectedUrls);
 }
Esempio n. 3
0
 public function validate(Response $response)
 {
     $request = $response->getRequest();
     if ('https' !== $request->getUri()->getScheme()) {
         return;
     }
     $htmlDocument = new Document($response->getBody());
     $resources = $htmlDocument->getDependencies($response->getRequest()->getUri(), false);
     foreach ($resources as $resource) {
         if ($resource->getScheme() && 'https' !== $resource->getScheme()) {
             throw new ValidationFailedException('At least one dependency was found on a secure url, that was transfered insecure (' . (string) $resource . ')');
         }
     }
 }
Esempio n. 4
0
 protected function doValidation(Response $response)
 {
     $document = new Document($response->getBody());
     $images = $document->getImages($response->getUri());
     $foreignImages = array();
     /* @var $currentUri Uri */
     $currentUri = $response->getUri();
     foreach ($images as $image) {
         /* @var $image Uri */
         if ($currentUri->getHost($this->depth) !== $image->getHost($this->depth)) {
             $foreignImages[] = (string) $image;
         }
     }
     $this->assert(count($foreignImages) === 0, 'Images from a foreign domain where found (' . implode(', ', $foreignImages) . ')');
 }
Esempio n. 5
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $client = new Client();
     $url = $input->getArgument("url");
     $startResult = $client->get($url);
     $results[$url] = $startResult;
     if ($input->getOption('children')) {
         $document = new Document((string) $startResult->getBody());
         $dependendUrls = $document->getDependencies(new Uri($url), false);
         foreach ($dependendUrls as $dependendUrl) {
             $results[(string) $dependendUrl] = $client->get($dependendUrl);
         }
     }
     foreach ($results as $url => $result) {
         $this->printHeaders($result, $url, $output);
     }
 }
Esempio n. 6
0
 public function next()
 {
     if (count($this->responseCache) == 0) {
         $urls = $this->pageContainer->pop($this->parallelReqeusts);
         if (empty($urls)) {
             return false;
         }
         $requests = array();
         foreach ($urls as $url) {
             if (!$this->isFiltered($url)) {
                 $request = RequestFactory::getRequest($url, 'GET', 'php://memory', [], []);
                 $requests[] = $request;
             }
         }
         if (empty($requests)) {
             return $this->next();
         }
         try {
             $this->responseCache = $this->httpClient->sendRequests($requests);
         } catch (MultiHttpAdapterException $e) {
             $exceptions = $e->getExceptions();
             $errorMessages = "";
             foreach ($exceptions as $exception) {
                 // @fixme this must be part of the http client
                 $message = $exception->getMessage();
                 if (strpos($message, "An error occurred when fetching the URI") === 0) {
                     $url = substr($message, "41", strpos($message, '"', 41) - 41);
                     if (strpos($url, '/') === 0) {
                         $this->pageContainer->push(new Uri($this->startUri->getScheme() . '://' . $this->startUri->getHost() . $url));
                     }
                 } else {
                     $errorMessages .= $exception->getMessage() . "\n";
                 }
             }
             if ($errorMessages != "") {
                 throw new \RuntimeException($errorMessages);
             }
         }
     }
     if (empty($this->responseCache)) {
         return $this->next();
     }
     $response = array_pop($this->responseCache);
     if ($response->hasHeader('Content-Type')) {
         $contentTypeElements = explode(';', $response->getHeader('Content-Type')[0]);
         $contentType = array_shift($contentTypeElements);
         if ($contentType === "text/html") {
             $document = new Document((string) $response->getBody(), true);
             $elements = $document->getUnorderedDependencies($response->getUri());
             foreach ($elements as $element) {
                 $urlString = $this->createCleanUriString($element);
                 if (!array_key_exists($urlString, $this->comingFrom)) {
                     $this->comingFrom[$urlString] = $response->getUri();
                 }
                 $this->pageContainer->push($element);
             }
         }
     }
     return $response;
 }
Esempio n. 7
0
 protected function getFilesToCount(Document $document, Response $response)
 {
     return $document->getJsFiles($response->getUri());
 }