/** * @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 * @param string $dir * * @throws FileNotWritableException * @return CurlWrapper */ public static function prepareDownload(CurlWrapper $curl, $dir) { do { $fileName = md5($curl->getUrl()) . '.' . Strings::random() . '.tmp'; } while (is_file($dir . '/' . $fileName)); if (($fileHandle = @fopen($curl->file = $dir . '/' . $fileName, 'wb')) === FALSE) { throw new FileNotWritableException("File {$curl->file} is not writable."); } if (($headersHandle = @fopen($curl->file . '.headers', 'wb')) === FALSE) { throw new FileNotWritableException("File {$curl->file} is not writable."); } return $curl->setOptions(array('file' => $fileHandle, 'writeHeader' => $headersHandle, 'binaryTransfer' => TRUE)); }
/** * @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; }