web/lib/Zend/Gdata/HttpAdapterStreamingSocket.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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   Zend
       
    17  * @package    Zend_Gdata
       
    18  * @subpackage Gdata
       
    19  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    21  * @version    $Id: HttpAdapterStreamingSocket.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    22  */
       
    23 
       
    24 /**
       
    25  * @see Zend_Http_Client_Adapter_Socket
       
    26  */
       
    27 require_once 'Zend/Http/Client/Adapter/Socket.php';
       
    28 
       
    29 /**
       
    30  * Extends the default HTTP adapter to handle streams instead of discrete body
       
    31  * strings.
       
    32  *
       
    33  * @category   Zend
       
    34  * @package    Zend_Gdata
       
    35  * @subpackage Gdata
       
    36  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    38  */
       
    39 class Zend_Gdata_HttpAdapterStreamingSocket extends Zend_Http_Client_Adapter_Socket
       
    40 {
       
    41 
       
    42     /**
       
    43      * The amount read from a stream source at a time.
       
    44      *
       
    45      * @var integer
       
    46      */
       
    47     const CHUNK_SIZE = 1024;
       
    48 
       
    49     /**
       
    50      * Send request to the remote server with streaming support.
       
    51      *
       
    52      * @param string        $method
       
    53      * @param Zend_Uri_Http $uri
       
    54      * @param string        $http_ver
       
    55      * @param array         $headers
       
    56      * @param string        $body
       
    57      * @return string Request as string
       
    58      */
       
    59     public function write($method, $uri, $http_ver = '1.1', $headers = array(),
       
    60         $body = '')
       
    61     {
       
    62         // Make sure we're properly connected
       
    63         if (! $this->socket) {
       
    64             require_once 'Zend/Http/Client/Adapter/Exception.php';
       
    65             throw new Zend_Http_Client_Adapter_Exception(
       
    66                 'Trying to write but we are not connected');
       
    67         }
       
    68 
       
    69         $host = $uri->getHost();
       
    70         $host = (strtolower($uri->getScheme()) == 'https' ? $this->config['ssltransport'] : 'tcp') . '://' . $host;
       
    71         if ($this->connected_to[0] != $host || $this->connected_to[1] != $uri->getPort()) {
       
    72             require_once 'Zend/Http/Client/Adapter/Exception.php';
       
    73             throw new Zend_Http_Client_Adapter_Exception(
       
    74                 'Trying to write but we are connected to the wrong host');
       
    75         }
       
    76 
       
    77         // Save request method for later
       
    78         $this->method = $method;
       
    79 
       
    80         // Build request headers
       
    81         $path = $uri->getPath();
       
    82         if ($uri->getQuery()) $path .= '?' . $uri->getQuery();
       
    83         $request = "{$method} {$path} HTTP/{$http_ver}\r\n";
       
    84         foreach ($headers as $k => $v) {
       
    85             if (is_string($k)) $v = ucfirst($k) . ": $v";
       
    86             $request .= "$v\r\n";
       
    87         }
       
    88 
       
    89         // Send the headers over
       
    90         $request .= "\r\n";
       
    91         if (! @fwrite($this->socket, $request)) {
       
    92             require_once 'Zend/Http/Client/Adapter/Exception.php';
       
    93             throw new Zend_Http_Client_Adapter_Exception(
       
    94                 'Error writing request to server');
       
    95         }
       
    96 
       
    97 
       
    98         //read from $body, write to socket
       
    99         $chunk = $body->read(self::CHUNK_SIZE);
       
   100         while ($chunk !== FALSE) {
       
   101             if (! @fwrite($this->socket, $chunk)) {
       
   102                 require_once 'Zend/Http/Client/Adapter/Exception.php';
       
   103                 throw new Zend_Http_Client_Adapter_Exception(
       
   104                     'Error writing request to server');
       
   105             }
       
   106             $chunk = $body->read(self::CHUNK_SIZE);
       
   107         }
       
   108         $body->closeFileHandle();
       
   109         return 'Large upload, request is not cached.';
       
   110     }
       
   111 }