File indexing completed on 2024-12-22 05:36:46

0001 <?php
0002 
0003 /**
0004  * Zend Framework
0005  *
0006  * LICENSE
0007  *
0008  * This source file is subject to the new BSD license that is bundled
0009  * with this package in the file LICENSE.txt.
0010  * It is also available through the world-wide-web at this URL:
0011  * http://framework.zend.com/license/new-bsd
0012  * If you did not receive a copy of the license and are unable to
0013  * obtain it through the world-wide-web, please send an email
0014  * to license@zend.com so we can send you a copy immediately.
0015  *
0016  * @category   Zend
0017  * @package    Zend_Gdata
0018  * @subpackage Gdata
0019  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0020  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0021  * @version    $Id$
0022  */
0023 
0024 /**
0025  * @see Zend_Http_Client_Adapter_Socket
0026  */
0027 // require_once 'Zend/Http/Client/Adapter/Socket.php';
0028 
0029 /**
0030  * Extends the default HTTP adapter to handle streams instead of discrete body
0031  * strings.
0032  *
0033  * @category   Zend
0034  * @package    Zend_Gdata
0035  * @subpackage Gdata
0036  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0037  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0038  */
0039 class Zend_Gdata_HttpAdapterStreamingSocket extends Zend_Http_Client_Adapter_Socket
0040 {
0041 
0042     /**
0043      * The amount read from a stream source at a time.
0044      *
0045      * @var integer
0046      */
0047     const CHUNK_SIZE = 1024;
0048 
0049     /**
0050      * Send request to the remote server with streaming support.
0051      *
0052      * @param string        $method
0053      * @param Zend_Uri_Http $uri
0054      * @param string        $http_ver
0055      * @param array         $headers
0056      * @param string        $body
0057      * @return string Request as string
0058      */
0059     public function write($method, $uri, $http_ver = '1.1', $headers = array(),
0060         $body = '')
0061     {
0062         // Make sure we're properly connected
0063         if (! $this->socket) {
0064             // require_once 'Zend/Http/Client/Adapter/Exception.php';
0065             throw new Zend_Http_Client_Adapter_Exception(
0066                 'Trying to write but we are not connected');
0067         }
0068 
0069         $host = $uri->getHost();
0070         $host = (strtolower($uri->getScheme()) == 'https' ? $this->config['ssltransport'] : 'tcp') . '://' . $host;
0071         if ($this->connected_to[0] != $host || $this->connected_to[1] != $uri->getPort()) {
0072             // require_once 'Zend/Http/Client/Adapter/Exception.php';
0073             throw new Zend_Http_Client_Adapter_Exception(
0074                 'Trying to write but we are connected to the wrong host');
0075         }
0076 
0077         // Save request method for later
0078         $this->method = $method;
0079 
0080         // Build request headers
0081         $path = $uri->getPath();
0082         if ($uri->getQuery()) $path .= '?' . $uri->getQuery();
0083         $request = "{$method} {$path} HTTP/{$http_ver}\r\n";
0084         foreach ($headers as $k => $v) {
0085             if (is_string($k)) $v = ucfirst($k) . ": $v";
0086             $request .= "$v\r\n";
0087         }
0088 
0089         // Send the headers over
0090         $request .= "\r\n";
0091         if (! @fwrite($this->socket, $request)) {
0092             // require_once 'Zend/Http/Client/Adapter/Exception.php';
0093             throw new Zend_Http_Client_Adapter_Exception(
0094                 'Error writing request to server');
0095         }
0096 
0097 
0098         //read from $body, write to socket
0099         $chunk = $body->read(self::CHUNK_SIZE);
0100         while ($chunk !== FALSE) {
0101             if (! @fwrite($this->socket, $chunk)) {
0102                 // require_once 'Zend/Http/Client/Adapter/Exception.php';
0103                 throw new Zend_Http_Client_Adapter_Exception(
0104                     'Error writing request to server');
0105             }
0106             $chunk = $body->read(self::CHUNK_SIZE);
0107         }
0108         $body->closeFileHandle();
0109         return 'Large upload, request is not cached.';
0110     }
0111 }