• Модуль: dav
  • Путь к файлу: ~/bitrix/modules/dav/classes/general/exchangeclient.php
  • Класс: CDavExchangeClient
  • Вызов: CDavExchangeClient::GetResponse
private function GetResponse()
{
	if (!$this->connected)
	{
		return null;
	}

	$arHeaders = array();
	$body = "";

	while ($line = fgets($this->fp, 4096))
	{
		if ($line === "rn")
		{
			break;
		}

		$arHeaders[] = trim($line);
	}

	if (count($arHeaders) <= 0)
	{
		return null;
	}

	$bChunked = $bConnectionClosed = false;
	$contentLength = null;
	foreach ($arHeaders as $value)
	{
		if (!$bChunked && preg_match("#Transfer-Encoding:s*chunked#i", $value))
		{
			$bChunked = true;
		}
		if (!$bConnectionClosed && preg_match('#Connection:s*close#i', $value))
		{
			$bConnectionClosed = true;
		}
		if (is_null($contentLength))
		{
			if (preg_match('#Content-Length:s*([0-9]*)#i', $value, $arMatches))
			{
				$contentLength = intval($arMatches[1]);
			}
			if (preg_match('#HTTP/1.1s+204#i', $value))
			{
				$contentLength = 0;
			}
		}
	}

	if ($bChunked)
	{
		do
		{
			$line = fgets($this->fp, 4096);
			$line = mb_strtolower($line);

			$chunkSize = "";
			$i = 0;
			while ($i < mb_strlen($line))
			{
				$c = mb_substr($line, $i, 1);
				if (in_array($c, array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f")))
					$chunkSize .= $c;
				else
					break;
				$i++;
			}

			$chunkSize = hexdec($chunkSize);

			if ($chunkSize > 0)
			{
				$lb = $chunkSize;
				$body1 = '';
				while ($lb > 0)
				{
					$d = fread($this->fp, $lb);
					if ($d === false)
						break;

					$body1 .= $d;
					$lb = $chunkSize - ((function_exists('mb_strlen')? mb_strlen($body1, 'latin1') : mb_strlen($body1)));
				}

				$body .= $body1;
			}

			fgets($this->fp, 4096);
		}
		while ($chunkSize);
	}
	elseif ($contentLength === 0)
	{
	}
	elseif ($contentLength > 0)
	{
		$lb = $contentLength;
		while ($lb > 0)
		{
			$d = fread($this->fp, $lb);
			if ($d === false)
				break;

			$body .= $d;
			$lb = $contentLength - ((function_exists('mb_strlen')? mb_strlen($body, 'latin1') : mb_strlen($body)));
		}
	}
	else
	{
		socket_set_timeout($this->fp, 0);

		while (!feof($this->fp))
		{
			$d = fread($this->fp, 4096);
			if ($d === false)
				break;

			$body .= $d;
			if (mb_substr($body, -9) == "rnrn0rnrn")
			{
				$body = mb_substr($body, 0, -9);
				break;
			}
		}

		socket_set_timeout($this->fp, $this->socketTimeout);
	}

	if ($bConnectionClosed)
		$this->Disconnect();

	$responce = new CDavExchangeClientResponce($arHeaders, $body);

	$httpVersion = $responce->GetStatus('version');
	if (is_null($httpVersion) || ($httpVersion !== 'HTTP/1.1' && $httpVersion !== 'HTTP/1.0'))
		return null;

	return $responce;
}