HTTP_Response Class Reference

List of all members.

Public Member Functions

 HTTP_Response (&$sock, &$listeners)
 process ($saveBody=true)
 _processHeader ($header)
 _parseCookie ($headervalue)
 _readChunked ()
 _notify ($event, $data=null)

Public Attributes

 $_sock
 $_protocol
 $_code
 $_headers
 $_cookies
 $_body = ''
 $_chunkLength = 0
 $_listeners = array()

Detailed Description

Response class to complement the Request class

Definition at line 900 of file Request.php.


Constructor & Destructor Documentation

HTTP_Response::HTTP_Response &$  sock,
&$  listeners
 

Constructor

Parameters:
object Net_Socket socket to read the response from
array listeners attached to request
Returns:
mixed PEAR Error on error, true otherwise

Definition at line 957 of file Request.php.

00958     {
00959         $this->_sock      =& $sock;
00960         $this->_listeners =& $listeners;
00961     }


Member Function Documentation

HTTP_Response::_notify event,
data = null
 

Notifies all registered listeners of an event.

Parameters:
string Event name
mixed Additional data private
See also:
HTTP_Request::_notify()

Definition at line 1136 of file Request.php.

References $id.

Referenced by process().

01137     {
01138         foreach (array_keys($this->_listeners) as $id) {
01139             $this->_listeners[$id]->update($this, $event, $data);
01140         }
01141     }

HTTP_Response::_parseCookie headervalue  ) 
 

Parse a Set-Cookie header to fill $_cookies array

private

Parameters:
string value of Set-Cookie header

Definition at line 1052 of file Request.php.

References $pos, and name.

Referenced by _processHeader().

01053     {
01054         $cookie = array(
01055             'expires' => null,
01056             'domain'  => null,
01057             'path'    => null,
01058             'secure'  => false
01059         );
01060 
01061         // Only a name=value pair
01062         if (!strpos($headervalue, ';')) {
01063             $pos = strpos($headervalue, '=');
01064             $cookie['name']  = trim(substr($headervalue, 0, $pos));
01065             $cookie['value'] = trim(substr($headervalue, $pos + 1));
01066 
01067         // Some optional parameters are supplied
01068         } else {
01069             $elements = explode(';', $headervalue);
01070             $pos = strpos($elements[0], '=');
01071             $cookie['name']  = trim(substr($elements[0], 0, $pos));
01072             $cookie['value'] = trim(substr($elements[0], $pos + 1));
01073 
01074             for ($i = 1; $i < count($elements); $i++) {
01075                 if (false === strpos($elements[$i], '=')) {
01076                     $elName  = trim($elements[$i]);
01077                     $elValue = null;
01078                 } else {
01079                     list ($elName, $elValue) = array_map('trim', explode('=', $elements[$i]));
01080                 }
01081                 $elName = strtolower($elName);
01082                 if ('secure' == $elName) {
01083                     $cookie['secure'] = true;
01084                 } elseif ('expires' == $elName) {
01085                     $cookie['expires'] = str_replace('"', '', $elValue);
01086                 } elseif ('path' == $elName || 'domain' == $elName) {
01087                     $cookie[$elName] = urldecode($elValue);
01088                 } else {
01089                     $cookie[$elName] = $elValue;
01090                 }
01091             }
01092         }
01093         $this->_cookies[] = $cookie;
01094     }

HTTP_Response::_processHeader header  ) 
 

Processes the response header

private

Parameters:
string HTTP header

Definition at line 1031 of file Request.php.

References _parseCookie().

Referenced by process().

01032     {
01033         list($headername, $headervalue) = explode(':', $header, 2);
01034         $headername_i = strtolower($headername);
01035         $headervalue  = ltrim($headervalue);
01036         
01037         if ('set-cookie' != $headername_i) {
01038             $this->_headers[$headername]   = $headervalue;
01039             $this->_headers[$headername_i] = $headervalue;
01040         } else {
01041             $this->_parseCookie($headervalue);
01042         }
01043     }

HTTP_Response::_readChunked  ) 
 

Read a part of response body encoded with chunked Transfer-Encoding

private

Returns:
string

Definition at line 1103 of file Request.php.

References $data, and $line.

Referenced by process().

01104     {
01105         // at start of the next chunk?
01106         if (0 == $this->_chunkLength) {
01107             $line = $this->_sock->readLine();
01108             if (preg_match('/^([0-9a-f]+)/i', $line, $matches)) {
01109                 $this->_chunkLength = hexdec($matches[1]); 
01110                 // Chunk with zero length indicates the end
01111                 if (0 == $this->_chunkLength) {
01112                     $this->_sock->readAll(); // make this an eof()
01113                     return '';
01114                 }
01115             } elseif ($this->_sock->eof()) {
01116                 return '';
01117             }
01118         }
01119         $data = $this->_sock->read($this->_chunkLength);
01120         $this->_chunkLength -= strlen($data);
01121         if (0 == $this->_chunkLength) {
01122             $this->_sock->readLine(); // Trailing CRLF
01123         }
01124         return $data;
01125     }

HTTP_Response::process saveBody = true  ) 
 

Processes a HTTP response

This extracts response code, headers, cookies and decodes body if it was encoded in some way

public

Parameters:
bool Whether to store response body in object property, set this to false if downloading a LARGE file and using a Listener. This is assumed to be true if body is gzip-encoded.
Exceptions:
PEAR_Error 
Returns:
mixed true on success, PEAR_Error in case of malformed response

Definition at line 977 of file Request.php.

References $data, $line, _notify(), _processHeader(), _readChunked(), and PEAR::raiseError().

00978     {
00979         do {
00980             $line = $this->_sock->readLine();
00981             if (sscanf($line, 'HTTP/%s %s', $http_version, $returncode) != 2) {
00982                 return PEAR::raiseError('Malformed response.');
00983             } else {
00984                 $this->_protocol = 'HTTP/' . $http_version;
00985                 $this->_code     = intval($returncode);
00986             }
00987             while ('' !== ($header = $this->_sock->readLine())) {
00988                 $this->_processHeader($header);
00989             }
00990         } while (100 == $this->_code);
00991 
00992         $this->_notify('gotHeaders', $this->_headers);
00993 
00994         // If response body is present, read it and decode
00995         $chunked = isset($this->_headers['transfer-encoding']) && ('chunked' == $this->_headers['transfer-encoding']);
00996         $gzipped = isset($this->_headers['content-encoding']) && ('gzip' == $this->_headers['content-encoding']);
00997         $hasBody = false;
00998         while (!$this->_sock->eof()) {
00999             if ($chunked) {
01000                 $data = $this->_readChunked();
01001             } else {
01002                 $data = $this->_sock->read(4096);
01003             }
01004             if ('' != $data) {
01005                 $hasBody = true;
01006                 if ($saveBody || $gzipped) {
01007                     $this->_body .= $data;
01008                 }
01009                 $this->_notify($gzipped? 'gzTick': 'tick', $data);
01010             }
01011         }
01012         if ($hasBody) {
01013             // Uncompress the body if needed
01014             if ($gzipped) {
01015                 $this->_body = gzinflate(substr($this->_body, 10));
01016                 $this->_notify('gotBody', $this->_body);
01017             } else {
01018                 $this->_notify('gotBody');
01019             }
01020         }
01021         return true;
01022     }


Member Data Documentation

HTTP_Response::$_body = ''
 

Definition at line 936 of file Request.php.

HTTP_Response::$_chunkLength = 0
 

Definition at line 942 of file Request.php.

HTTP_Response::$_code
 

Definition at line 918 of file Request.php.

HTTP_Response::$_cookies
 

Definition at line 930 of file Request.php.

HTTP_Response::$_headers
 

Definition at line 924 of file Request.php.

HTTP_Response::$_listeners = array()
 

Definition at line 948 of file Request.php.

HTTP_Response::$_protocol
 

Definition at line 912 of file Request.php.

HTTP_Response::$_sock
 

Definition at line 906 of file Request.php.


The documentation for this class was generated from the following file:
Generated on Fri Mar 17 14:54:32 2006 for CRE Loaded 6.2 Pro by  doxygen 1.4.4