phplibrary/core/connection/httpconnectionresponse.php
2016-07-14 16:29:26 +03:00

82 lines
2 KiB
PHP

<?php
/**
* Обрабатывает HTTP ответ
*/
class HttpConnectionResponse
{
private $offset;
private $param = array ();
private $code;
public function __construct($response)
{
$this->offset = 0;
$this->response = $response;
$this->parseMessage();
}
/**
* Обработка HTTP ответа
*/
private function parseMessage()
{
$http = explode(" ", $this->getLine());
$this->version = $http[0];
$this->code = $http[1];
$line = $this->getLine();
while ($offset = strpos($line, ":")) {
$this->param[substr($line, 0, $offset)] = trim(substr($line, $offset + 1));
$line = $this->getLine();
}
if (isset($this->param['Transfer-Encoding']) && $this->param['Transfer-Encoding'] == 'chunked') {
//$this->data = substr($this->response, $this->offset);
$line = hexdec($this->getLine());
$chunk = array();
while ($line > 0) {
$chunk [] = substr($this->response, $this->offset, $line);
$this->offset += $line;
$line = hexdec($this->getLine());
}
$this->data = implode("", $chunk);
} else {
$this->data = substr($this->response, $this->offset);
}
}
/**
* Обработка строки HTTP ответа
*/
private function getLine()
{
$begin = $this->offset;
$offset = strpos($this->response, "\r\n", $this->offset);
$result = substr($this->response, $begin, $offset - $begin);
$this->offset = $offset + 2;
return $result;
}
/**
* Значение параметра HTTP ответа
*/
public function getParameter($name)
{
return $this->param[$name];
}
public function getData()
{
return $this->data;
}
/**
* Состояние
*/
public function getCode()
{
return $this->code;
}
}