File indexing completed on 2024-05-12 06:02:40

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_Proxy
0026  */
0027 // require_once 'Zend/Http/Client/Adapter/Proxy.php';
0028 
0029 /**
0030  * Extends the proxy 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_HttpAdapterStreamingProxy extends Zend_Http_Client_Adapter_Proxy
0040 {
0041     /**
0042      * The amount read from a stream source at a time.
0043      *
0044      * @var integer
0045      */
0046     const CHUNK_SIZE = 1024;
0047 
0048     /**
0049      * Send request to the proxy server with streaming support
0050      *
0051      * @param string        $method
0052      * @param Zend_Uri_Http $uri
0053      * @param string        $http_ver
0054      * @param array         $headers
0055      * @param string        $body
0056      * @return string Request as string
0057      */
0058     public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
0059     {
0060         // If no proxy is set, throw an error
0061         if (! $this->config['proxy_host']) {
0062             // require_once 'Zend/Http/Client/Adapter/Exception.php';
0063             throw new Zend_Http_Client_Adapter_Exception('No proxy host set!');
0064         }
0065 
0066         // Make sure we're properly connected
0067         if (! $this->socket) {
0068             // require_once 'Zend/Http/Client/Adapter/Exception.php';
0069             throw new Zend_Http_Client_Adapter_Exception(
0070                 'Trying to write but we are not connected');
0071         }
0072 
0073         $host = $this->config['proxy_host'];
0074         $port = $this->config['proxy_port'];
0075 
0076         if ($this->connected_to[0] != $host || $this->connected_to[1] != $port) {
0077             // require_once 'Zend/Http/Client/Adapter/Exception.php';
0078             throw new Zend_Http_Client_Adapter_Exception(
0079                 'Trying to write but we are connected to the wrong proxy ' .
0080                 'server');
0081         }
0082 
0083         // Add Proxy-Authorization header
0084         if ($this->config['proxy_user'] && ! isset($headers['proxy-authorization'])) {
0085             $headers['proxy-authorization'] = Zend_Http_Client::encodeAuthHeader(
0086                 $this->config['proxy_user'], $this->config['proxy_pass'], $this->config['proxy_auth']
0087             );
0088         }
0089 
0090         // if we are proxying HTTPS, preform CONNECT handshake with the proxy
0091         if ($uri->getScheme() == 'https' && (! $this->negotiated)) {
0092             $this->connectHandshake($uri->getHost(), $uri->getPort(), $http_ver, $headers);
0093             $this->negotiated = true;
0094         }
0095 
0096         // Save request method for later
0097         $this->method = $method;
0098 
0099         // Build request headers
0100         $request = "{$method} {$uri->__toString()} HTTP/{$http_ver}\r\n";
0101 
0102         // Add all headers to the request string
0103         foreach ($headers as $k => $v) {
0104             if (is_string($k)) $v = "$k: $v";
0105             $request .= "$v\r\n";
0106         }
0107 
0108         $request .= "\r\n";
0109 
0110         // Send the request headers
0111         if (! @fwrite($this->socket, $request)) {
0112             // require_once 'Zend/Http/Client/Adapter/Exception.php';
0113             throw new Zend_Http_Client_Adapter_Exception(
0114                 'Error writing request to proxy server');
0115         }
0116 
0117         // Read from $body, write to socket
0118         $chunk = $body->read(self::CHUNK_SIZE);
0119         while ($chunk !== false) {
0120             if (!@fwrite($this->socket, $chunk)) {
0121                 // require_once 'Zend/Http/Client/Adapter/Exception.php';
0122                 throw new Zend_Http_Client_Adapter_Exception(
0123                     'Error writing request to server'
0124                 );
0125             }
0126             $chunk = $body->read(self::CHUNK_SIZE);
0127         }
0128         $body->closeFileHandle();
0129 
0130         return 'Large upload, request is not cached.';
0131     }
0132 }