<?
/**
* Class HttpResponse
*
* @abstract This class allow you to parse HTTP response
* It's generated by a HttpRequest->Send() call
* and it's a structure of 3 elements:
* code: es. HTTP/1.1 200 OK
* body: es. <html> ... </html>
* headers: associative array of response headers
* es. [content-type] = 'text/html'
* [content-length] = 2561
* [location] = 'page.php'
* ecc ...
*
* @author: Simone Cosci <hide@address.com>
* @version: 1.0
* @copyright: Copyright 2006, InterWorld.it
*
* */
class HttpResponse{
var $headers; // associative array of headers
var $code; // first header
var $body; // response body
var $_response; // All server response (not parsed)
function HttpResponse($response=''){
$this->_response = $response;
}
function Init(){
$start_body = $this->_getHeaders();
$this->_getBody($start_body);
}
function _getHeaders(){
$this->headers = array();
$lines = explode("\n",str_replace("\r",'',$this->_response));
foreach($lines as $k=>$line){
if($k==0){
$this->code = $line;
continue;
}
if(!empty($line)){
$elem = explode(': ',$line);
if(count($elem)>1){
$key = array_shift($elem);
if(isset($this->headers[strtolower($key)])) $this->headers[strtolower($key)] .= ';'.rtrim(ltrim(implode('',$elem)));
else $this->headers[strtolower($key)] = rtrim(ltrim(implode('',$elem)));
}elseif(count($elem)==1)
$this->headers[$elem[0]] = '';
}else return $k;
}
}
function _getBody($start_line){
$body = array();
$lines = explode("\n",str_replace("\r",'',$this->_response));
foreach($lines as $k=>$line){
if($k>=$start_line){
$body[] = $line;
}
}
$this->body = implode("\r\n",$body);
}
}
?>