/** * Reads the response-header. * * @param int &$error_code Error-code by reference if an error occured. * @param string &$error_string Error-string by reference * * @return string The response-header or NULL if an error occured */ protected function readResponseHeader(&$error_code, &$error_string) { PHPCrawlerBenchmark::reset("server_response_time"); PHPCrawlerBenchmark::start("server_response_time"); $status = socket_get_status($this->socket); $source_read = ""; $header = ""; $server_responded = false; while ($status["eof"] == false) { socket_set_timeout($this->socket, $this->socketReadTimeout); // Read line from socket $line_read = fgets($this->socket, 1024); // Server responded if ($server_responded == false) { $server_responded = true; $this->server_response_time = PHPCrawlerBenchmark::stop("server_response_time"); // Determinate socket prefill size $status = socket_get_status($this->socket); $this->socket_prefill_size = $status["unread_bytes"]; // Start data-transfer-time bechmark PHPCrawlerBenchmark::reset("data_transfer_time"); PHPCrawlerBenchmark::start("data_transfer_time"); } $source_read .= $line_read; $this->global_traffic_count += strlen($line_read); $status = socket_get_status($this->socket); // Socket timed out if ($status["timed_out"] == true) { $error_code = PHPCrawlerRequestErrors::ERROR_SOCKET_TIMEOUT; $error_string = "Socket-stream timed out (timeout set to " . $this->socketReadTimeout . " sec)."; return $header; } // No "HTTP" at beginnig of response if (strtolower(substr($source_read, 0, 4)) != "http") { $error_code = PHPCrawlerRequestErrors::ERROR_NO_HTTP_HEADER; $error_string = "HTTP-protocol error."; return $header; } // Header found and read (2 newlines) -> stop if (substr($source_read, -4, 4) == "\r\n\r\n" || substr($source_read, -2, 2) == "\n\n") { $header = substr($source_read, 0, strlen($source_read) - 2); break; } } // Stop data-transfer-time bechmark PHPCrawlerBenchmark::stop("data_transfer_time"); // Header was found if ($header != "") { // Search for links (redirects) in the header $this->LinkFinder->processHTTPHeader($header); $this->header_bytes_received = strlen($header); return $header; } // No header found if ($header == "") { $this->server_response_time = null; $error_code = PHPCrawlerRequestErrors::ERROR_NO_HTTP_HEADER; $error_string = "Host doesn't respond with a HTTP-header."; return null; } }
/** * Reads the response-header. * * @param int &$error_code Error-code by reference if an error occured. * @param string &$error_string Error-string by reference * @return string The response-header or NULL if an error occured */ protected function readResponseHeader(&$error_code, &$error_string) { PHPCrawlerBenchmark::start("server_response_time"); PHPCrawlerBenchmark::start("data_transfer_time", true); $status = socket_get_status($this->socket); $source_read = ""; $header = ""; $server_responded = false; while ($status["eof"] == false) { socket_set_timeout($this->socket, $this->socketReadTimeout); // Read from socket $line_read = fgets($this->socket, 1024); // Das @ ist da um die blöde "SSL fatal protocol error"-Warnung zu unterdrücken, // die keinen Sinn macht if ($server_responded == false) { $server_responded = true; PHPCrawlerBenchmark::stop("server_response_time"); PHPCrawlerBenchmark::start("retreiving_header"); } $source_read .= $line_read; $this->global_traffic_count += strlen($line_read); $status = socket_get_status($this->socket); // Socket timed out if ($status["timed_out"] == true) { $error_code = PHPCrawlerRequestErrors::ERROR_SOCKET_TIMEOUT; $error_string = "Socket-stream timed out (timeout set to " . $this->socketReadTimeout . " sec)."; return $header; } // No "HTTP" at beginnig of response if (strtolower(substr($source_read, 0, 4)) != "http") { $error_code = PHPCrawlerRequestErrors::ERROR_NO_HTTP_HEADER; $error_string = "HTTP-protocol error."; return $header; } if (substr($source_read, -4, 4) == "\r\n\r\n") { $header = substr($source_read, 0, strlen($source_read) - 2); // Search for links (redirects) in the header $this->LinkFinder->processHTTPHeader($header); PHPCrawlerBenchmark::stop("retreiving_header"); PHPCrawlerBenchmark::stop("data_transfer_time"); return $header; } } // No header found if ($header == "") { $error_code = PHPCrawlerRequestErrors::ERROR_NO_HTTP_HEADER; $error_string = "Host doesn't respond with a HTTP-header."; return null; } }