/** * @return false|KCurlHeaderResponse */ public function getHeader($sourceUrl, $noBody = false) { curl_setopt($this->ch, CURLOPT_HEADER, true); curl_setopt($this->ch, CURLOPT_BINARYTRANSFER, true); curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, 'KCurlWrapper::read_header'); curl_setopt($this->ch, CURLOPT_WRITEFUNCTION, 'KCurlWrapper::read_body'); $this->setSourceUrlAndprotocol($sourceUrl); if ($this->protocol == self::HTTP_PROTOCOL_FTP) { $noBody = true; } if ($noBody) { curl_setopt($this->ch, CURLOPT_NOBODY, true); } else { curl_setopt($this->ch, CURLOPT_RANGE, '0-0'); } self::$headers = ""; self::$lastHeader = false; curl_exec($this->ch); //Added to support multiple curl executions using the same curl. Wince this is the same curl re-used we need to reset the range option before continuing forward if (!$noBody) { curl_setopt($this->ch, CURLOPT_RANGE, '0-'); } if (!self::$headers) { return false; } self::$headers = explode("\r\n", self::$headers); $curlHeaderResponse = new KCurlHeaderResponse(); if ($this->protocol == self::HTTP_PROTOCOL_HTTP) { $header = reset(self::$headers); // this line is true if the protocol is HTTP (or HTTPS); $matches = null; if (preg_match('/HTTP\\/?[\\d.]{0,3} ([\\d]{3}) ([^\\n\\r]+)/', $header, $matches)) { $curlHeaderResponse->code = $matches[1]; $curlHeaderResponse->codeName = $matches[2]; } foreach (self::$headers as $header) { if (!strstr($header, ':')) { if (preg_match('/HTTP\\/?[\\d.]{0,3} ([\\d]{3}) (.+)/', $header, $matches)) { $curlHeaderResponse->code = $matches[1]; $curlHeaderResponse->codeName = $matches[2]; } continue; } list($name, $value) = explode(':', $header, 2); $curlHeaderResponse->headers[trim(strtolower($name))] = trim($value); } if (!$noBody) { $matches = null; if (isset($curlHeaderResponse->headers['content-range']) && preg_match('/0-0\\/([\\d]+)$/', $curlHeaderResponse->headers['content-range'], $matches)) { $curlHeaderResponse->headers['content-length'] = $matches[1]; } else { return $this->getHeader($sourceUrl, true); } } } else { // for now - assume FTP foreach (self::$headers as $header) { $headerParts = explode(':', $header, 2); if (count($headerParts) < 2) { continue; } list($name, $value) = $headerParts; $curlHeaderResponse->headers[trim(strtolower($name))] = trim($value); } // if this is a good ftp url - there will be a content-length header $length = @$curlHeaderResponse->headers["content-length"]; if ($length > 0) { // this is equivalent to a good HTTP request $curlHeaderResponse->code = KCurlHeaderResponse::HTTP_STATUS_OK; $curlHeaderResponse->codeName = "OK"; } else { if (isset($curlHeaderResponse->headers["curl"])) { // example: curl: (10) the username and/or the password are incorrect // in this case set the error code to unknown error and use the whole string as the description $curlHeaderResponse->code = -1; // unknown error $curlHeaderResponse->codeName = "curl: " . $curlHeaderResponse->headers["curl"]; } else { // example: curl: (10) the username and/or the password are incorrect // in this case set the error code to unknown error and use the whole string as the description $curlHeaderResponse->code = -1; // unknown error $curlHeaderResponse->codeName = "Unknown FTP error"; } } } curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, 'KCurlWrapper::read_header_do_nothing'); curl_setopt($this->ch, CURLOPT_WRITEFUNCTION, 'KCurlWrapper::read_body_do_nothing'); return $curlHeaderResponse; }