]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - Zend/Gdata/HttpAdapterStreamingProxy.php
Release 6.5.0
[Github/sugarcrm.git] / Zend / Gdata / HttpAdapterStreamingProxy.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   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
22  */
23
24 /**
25  * @see Zend_Http_Client_Adapter_Proxy
26  */
27 require_once 'Zend/Http/Client/Adapter/Proxy.php';
28
29 /**
30  * Extends the proxy 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_HttpAdapterStreamingProxy extends Zend_Http_Client_Adapter_Proxy
40 {
41     /**
42      * The amount read from a stream source at a time.
43      *
44      * @var integer
45      */
46     const CHUNK_SIZE = 1024;
47
48     /**
49      * Send request to the proxy server with streaming support
50      *
51      * @param string        $method
52      * @param Zend_Uri_Http $uri
53      * @param string        $http_ver
54      * @param array         $headers
55      * @param string        $body
56      * @return string Request as string
57      */
58     public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
59     {
60         // If no proxy is set, throw an error
61         if (! $this->config['proxy_host']) {
62             require_once 'Zend/Http/Client/Adapter/Exception.php';
63             throw new Zend_Http_Client_Adapter_Exception('No proxy host set!');
64         }
65
66         // Make sure we're properly connected
67         if (! $this->socket) {
68             require_once 'Zend/Http/Client/Adapter/Exception.php';
69             throw new Zend_Http_Client_Adapter_Exception(
70                 'Trying to write but we are not connected');
71         }
72
73         $host = $this->config['proxy_host'];
74         $port = $this->config['proxy_port'];
75
76         if ($this->connected_to[0] != $host || $this->connected_to[1] != $port) {
77             require_once 'Zend/Http/Client/Adapter/Exception.php';
78             throw new Zend_Http_Client_Adapter_Exception(
79                 'Trying to write but we are connected to the wrong proxy ' .
80                 'server');
81         }
82
83         // Add Proxy-Authorization header
84         if ($this->config['proxy_user'] && ! isset($headers['proxy-authorization'])) {
85             $headers['proxy-authorization'] = Zend_Http_Client::encodeAuthHeader(
86                 $this->config['proxy_user'], $this->config['proxy_pass'], $this->config['proxy_auth']
87             );
88         }
89
90         // if we are proxying HTTPS, preform CONNECT handshake with the proxy
91         if ($uri->getScheme() == 'https' && (! $this->negotiated)) {
92             $this->connectHandshake($uri->getHost(), $uri->getPort(), $http_ver, $headers);
93             $this->negotiated = true;
94         }
95
96         // Save request method for later
97         $this->method = $method;
98
99         // Build request headers
100         $request = "{$method} {$uri->__toString()} HTTP/{$http_ver}\r\n";
101
102         // Add all headers to the request string
103         foreach ($headers as $k => $v) {
104             if (is_string($k)) $v = "$k: $v";
105             $request .= "$v\r\n";
106         }
107
108         $request .= "\r\n";
109
110         // Send the request headers
111         if (! @fwrite($this->socket, $request)) {
112             require_once 'Zend/Http/Client/Adapter/Exception.php';
113             throw new Zend_Http_Client_Adapter_Exception(
114                 'Error writing request to proxy server');
115         }
116
117         //read from $body, write to socket
118         while ($body->hasData()) {
119             if (! @fwrite($this->socket, $body->read(self::CHUNK_SIZE))) {
120                 require_once 'Zend/Http/Client/Adapter/Exception.php';
121                 throw new Zend_Http_Client_Adapter_Exception(
122                     'Error writing request to server');
123             }
124         }
125         return 'Large upload, request is not cached.';
126     }
127 }