Microsoft_Http
[ class tree: Microsoft_Http ] [ index: Microsoft_Http ] [ all elements ]

Source for file Stream.php

Documentation is available at Stream.php

  1. <?php
  2.  
  3. /**
  4.  * Zend Framework
  5.  *
  6.  * LICENSE
  7.  *
  8.  * This source file is subject to the new BSD license that is bundled
  9.  * with this package in the file LICENSE.txt.
  10.  * It is also available through the world-wide-web at this URL:
  11.  * http://framework.zend.com/license/new-bsd
  12.  * If you did not receive a copy of the license and are unable to
  13.  * obtain it through the world-wide-web, please send an email
  14.  * to license@zend.com so we can send you a copy immediately.
  15.  *
  16.  * @category   Microsoft
  17.  * @package    Microsoft_Http
  18.  * @subpackage Response
  19.  * @version    $Id: Response.php 17131 2009-07-26 10:03:39Z shahar $
  20.  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  21.  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  22.  */
  23.  
  24. /**
  25.  * @see Microsoft_AutoLoader
  26.  */
  27. require_once dirname(__FILE__'/../../AutoLoader.php';
  28.  
  29. /**
  30.  * Microsoft_Http_Response represents an HTTP 1.0 / 1.1 response message. It
  31.  * includes easy access to all the response's different elemts, as well as some
  32.  * convenience methods for parsing and validating HTTP responses.
  33.  *
  34.  * @package    Microsoft_Http
  35.  * @subpackage Response
  36.  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  37.  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  38.  */
  39. {
  40.     /**
  41.      * Response as stream
  42.      *
  43.      * @var resource 
  44.      */
  45.     protected $stream;
  46.  
  47.     /**
  48.      * The name of the file containing the stream
  49.      *
  50.      * Will be empty if stream is not file-based.
  51.      *
  52.      * @var string 
  53.      */
  54.     protected $stream_name;
  55.  
  56.     /**
  57.      * Should we clean up the stream file when this response is closed?
  58.      *
  59.      * @var boolean 
  60.      */
  61.     protected $_cleanup;
  62.  
  63.     /**
  64.      * Get the response as stream
  65.      *
  66.      * @return resourse 
  67.      */
  68.     public function getStream()
  69.     {
  70.         return $this->stream;
  71.     }
  72.  
  73.     /**
  74.      * Set the response stream
  75.      *
  76.      * @param resourse $stream 
  77.      * @return Microsoft_Http_Response_Stream 
  78.      */
  79.     public function setStream($stream)
  80.     {
  81.         $this->stream = $stream;
  82.         return $this;
  83.     }
  84.  
  85.     /**
  86.      * Get the cleanup trigger
  87.      *
  88.      * @return boolean 
  89.      */
  90.     public function getCleanup({
  91.         return $this->_cleanup;
  92.     }
  93.  
  94.     /**
  95.      * Set the cleanup trigger
  96.      *
  97.      * @param $cleanup Set cleanup trigger
  98.      */
  99.     public function setCleanup($cleanup true{
  100.         $this->_cleanup = $cleanup;
  101.     }
  102.  
  103.     /**
  104.      * Get file name associated with the stream
  105.      *
  106.      * @return string 
  107.      */
  108.     public function getStreamName({
  109.         return $this->stream_name;
  110.     }
  111.  
  112.     /**
  113.      * Set file name associated with the stream
  114.      *
  115.      * @param string $stream_name Name to set
  116.      * @return Microsoft_Http_Response_Stream 
  117.      */
  118.     public function setStreamName($stream_name{
  119.         $this->stream_name = $stream_name;
  120.         return $this;
  121.     }
  122.  
  123.  
  124.     /**
  125.      * HTTP response constructor
  126.      *
  127.      * In most cases, you would use Microsoft_Http_Response::fromString to parse an HTTP
  128.      * response string and create a new Microsoft_Http_Response object.
  129.      *
  130.      * NOTE: The constructor no longer accepts nulls or empty values for the code and
  131.      * headers and will throw an exception if the passed values do not form a valid HTTP
  132.      * responses.
  133.      *
  134.      * If no message is passed, the message will be guessed according to the response code.
  135.      *
  136.      * @param int $code Response code (200, 404, ...)
  137.      * @param array $headers Headers array
  138.      * @param string $body Response body
  139.      * @param string $version HTTP version
  140.      * @param string $message Response code as text
  141.      * @throws Microsoft_Http_Exception
  142.      */
  143.     public function __construct($code$headers$body null$version '1.1'$message null)
  144.     {
  145.  
  146.         if(is_resource($body)) {
  147.             $this->setStream($body);
  148.             $body '';
  149.         }
  150.         parent::__construct($code$headers$body$version$message);
  151.     }
  152.  
  153.     /**
  154.      * Create a new Microsoft_Http_Response_Stream object from a string
  155.      *
  156.      * @param string $response_str 
  157.      * @param resource $stream 
  158.      * @return Microsoft_Http_Response_Stream 
  159.      */
  160.     public static function fromStream($response_str$stream)
  161.     {
  162.         $code    self::extractCode($response_str);
  163.         $headers self::extractHeaders($response_str);
  164.         $version self::extractVersion($response_str);
  165.         $message self::extractMessage($response_str);
  166.  
  167.         return new self($code$headers$stream$version$message);
  168.     }
  169.  
  170.     /**
  171.      * Get the response body as string
  172.      *
  173.      * This method returns the body of the HTTP response (the content), as it
  174.      * should be in it's readable version - that is, after decoding it (if it
  175.      * was decoded), deflating it (if it was gzip compressed), etc.
  176.      *
  177.      * If you want to get the raw body (as transfered on wire) use
  178.      * $this->getRawBody() instead.
  179.      *
  180.      * @return string 
  181.      */
  182.     public function getBody()
  183.     {
  184.         if($this->stream != null{
  185.             $this->readStream();
  186.         }
  187.         return parent::getBody();
  188.     }
  189.  
  190.     /**
  191.      * Get the raw response body (as transfered "on wire") as string
  192.      *
  193.      * If the body is encoded (with Transfer-Encoding, not content-encoding -
  194.      * IE "chunked" body), gzip compressed, etc. it will not be decoded.
  195.      *
  196.      * @return string 
  197.      */
  198.     public function getRawBody()
  199.     {
  200.         if($this->stream{
  201.             $this->readStream();
  202.         }
  203.         return $this->body;
  204.     }
  205.  
  206.     /**
  207.      * Read stream content and return it as string
  208.      *
  209.      * Function reads the remainder of the body from the stream and closes the stream.
  210.      *
  211.      * @return string 
  212.      */
  213.     protected function readStream()
  214.     {
  215.         if(!is_resource($this->stream)) {
  216.             return '';
  217.         }
  218.  
  219.         if(isset($headers['content-length'])) {
  220.             $this->body = stream_get_contents($this->stream$headers['content-length']);
  221.         else {
  222.             $this->body = stream_get_contents($this->stream);
  223.         }
  224.         fclose($this->stream);
  225.         $this->stream = null;
  226.     }
  227.  
  228.     public function __destruct()
  229.     {
  230.         if(is_resource($this->stream)) {
  231.             fclose($this->stream);
  232.             $this->stream = null;
  233.         }
  234.         if($this->_cleanup{
  235.             @unlink($this->stream_name);
  236.         }
  237.     }
  238.  
  239. }

Documentation generated on Wed, 18 May 2011 12:06:54 +0200 by phpDocumentor 1.4.3