/** * @param CurlWrapper $curl * * @return Response|NULL */ protected function buildRedirectResponse(CurlWrapper $curl) { if ($curl->info['redirect_count'] === 0) { return NULL; } $previous = $last = NULL; /** @var Response $last */ $parts = Strings::split($curl->responseHeaders, '~(HTTP/\\d\\.\\d\\s\\d+\\s.*)~m', PREG_SPLIT_NO_EMPTY); while ($rawHeaders = array_shift($parts)) { if ($http = Strings::match($rawHeaders, CurlWrapper::VERSION_AND_STATUS)) { if ($http['code'] < 200) { continue; } $rawHeaders .= array_shift($parts); } if (!$parts) { $curl->responseHeaders = $rawHeaders; return $last; } if ($headers = CurlWrapper::parseHeaders($rawHeaders)) { $previous = new Response($curl, $headers); if ($last !== NULL) { $previous->setPrevious($last); } } $last = $previous; } return $last; }
/** * @param CurlWrapper $curl * * @throws CurlException * @throws InvalidStateException * @return array */ public static function stripHeaders(CurlWrapper $curl) { $headersFile = $curl->file . '.headers'; @fclose($curl->options['file']); // internationally @ @fclose($curl->options['writeHeader']); // internationally @ if (($headersHandle = @fopen($headersFile, "rb")) === FALSE) { // internationally @ throw new InvalidStateException("File '{$headersFile}' not readable."); } $curl->responseHeaders = fread($headersHandle, filesize($headersFile)); if (!($headers = CurlWrapper::parseHeaders($curl->responseHeaders))) { throw new CurlException("Failed parsing of response headers"); } if (!@fclose($headersHandle) || !@unlink($headersFile)) { throw new InvalidStateException("File '{$headersFile}' can't be deleted."); } return $headers; }
/** * @param CurlWrapper $curl * * @throws CurlException * @return array */ public static function stripHeaders(CurlWrapper $curl) { $curl->responseHeaders = Strings::substring($curl->response, 0, $headerSize = $curl->info['header_size']); if (!($headers = CurlWrapper::parseHeaders($curl->responseHeaders))) { throw new CurlException("Failed parsing of response headers"); } $curl->response = Strings::substring($curl->response, $headerSize); return $headers; }