/**
  * Cleans HTTP response body with a content type "text/html" from unwanted elements.
  *
  * @param Response   $response      The HTTP response object
  * @param array|null $removeNoise   The list of unwanted html elements to remove.
  *                                  If array is empty, non of elements will be removed.
  *                                  If null given, the default elements will be removed: comment, script, style.
  *                                  Available elements names and their meaning:
  *                                   - head        head tag with html tag. Only body tag will be left
  *                                   - comment     comments except IE hacks
  *                                   - script      script tags
  *                                   - style       style elements
  *                                   - img         img tags and input tags with type of image
  *                                   - input_meta  input and meta tags, except input with type of image
  *                                   - all         all elements mentioned above
  * @param bool       $minify
  * @param bool       $removeEmptyLines
  */
 public function clean(Response $response, array $removeNoise = null, $minify = false, $removeEmptyLines = true)
 {
     if (strpos($response->getContentType(), 'text/html') !== 0) {
         return;
     }
     $html = $response->getBody();
     $this->cleanHtml($html, $removeNoise, $minify, $removeEmptyLines);
     if ($this->cleaner) {
         $this->cleaner->clean($html, $response->getEffectiveUrl(), $response->getStatusCode());
     }
     $response->setBody($html);
 }
Ejemplo n.º 2
0
 /**
  * @param resource $ch cURL handler
  *
  * @return Response
  */
 public function createResponse($ch)
 {
     if (!$this->request->getUrl()) {
         throw new \InvalidArgumentException('Request url is empty.');
     }
     $resourceId = (int) $ch;
     $info = curl_getinfo($ch);
     $reasonPhrase = ResponseUtils::getReasonPhrase($info['http_code']);
     $errorNo = isset($ch['result']) ? $ch['result'] : curl_errno($ch);
     $response = new Response();
     $response->setStatusCode($info['http_code'])->setReasonPhrase($reasonPhrase)->setRequestUrl($this->request->getUrl())->setEffectiveUrl($info['url'])->setContentType($info['content_type'])->setErrorMsg(curl_error($ch))->setErrorNumber($errorNo)->setBody(curl_multi_getcontent($ch));
     if (isset($this->headers[$resourceId])) {
         $headersSet = explode("\n\n", trim($this->headers[$resourceId]));
         $this->parseResponseHeaders($response, array_pop($headersSet));
         $response->setStatusCode($info['http_code'])->setReasonPhrase($reasonPhrase);
         // Set redirects
         foreach ($headersSet as $headers) {
             $redirect = new Redirect();
             $this->parseResponseHeaders($redirect, $headers);
             $response->addRedirect($redirect);
         }
     }
     // Set curl info
     foreach ($this->unusedInfoKeys as $key) {
         unset($info[$key]);
     }
     $response->setCurlInfo($info);
     return $response;
 }
Ejemplo n.º 3
0
 /**
  * @param Response $response
  * @return bool
  */
 private static function isConnectionError(Response $response)
 {
     return $response->getStatusCode() === 0 && Codes::isConnectionError($response->getErrorNumber());
 }