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.
19 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
20 * @license http://framework.zend.com/license/new-bsd New BSD License
24 * @see Zend_Http_Client_Adapter_Proxy
26 require_once 'Zend/Http/Client/Adapter/Proxy.php';
29 * Extends the proxy HTTP adapter to handle streams instead of discrete body
35 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
36 * @license http://framework.zend.com/license/new-bsd New BSD License
38 class Zend_Gdata_HttpAdapterStreamingProxy
extends Zend_Http_Client_Adapter_Proxy
41 * The amount read from a stream source at a time.
45 const CHUNK_SIZE
= 1024;
48 * Send request to the proxy server with streaming support
50 * @param string $method
51 * @param Zend_Uri_Http $uri
52 * @param string $http_ver
53 * @param array $headers
55 * @return string Request as string
57 public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
59 // If no proxy is set, throw an error
60 if (! $this->config
['proxy_host']) {
61 require_once 'Zend/Http/Client/Adapter/Exception.php';
62 throw new Zend_Http_Client_Adapter_Exception('No proxy host set!');
65 // Make sure we're properly connected
66 if (! $this->socket
) {
67 require_once 'Zend/Http/Client/Adapter/Exception.php';
68 throw new Zend_Http_Client_Adapter_Exception(
69 'Trying to write but we are not connected');
72 $host = $this->config
['proxy_host'];
73 $port = $this->config
['proxy_port'];
75 if ($this->connected_to
[0] != $host ||
$this->connected_to
[1] != $port) {
76 require_once 'Zend/Http/Client/Adapter/Exception.php';
77 throw new Zend_Http_Client_Adapter_Exception(
78 'Trying to write but we are connected to the wrong proxy ' .
82 // Add Proxy-Authorization header
83 if ($this->config
['proxy_user'] && ! isset($headers['proxy-authorization'])) {
84 $headers['proxy-authorization'] = Zend_Http_Client
::encodeAuthHeader(
85 $this->config
['proxy_user'], $this->config
['proxy_pass'], $this->config
['proxy_auth']
89 // if we are proxying HTTPS, preform CONNECT handshake with the proxy
90 if ($uri->getScheme() == 'https' && (! $this->negotiated
)) {
91 $this->connectHandshake($uri->getHost(), $uri->getPort(), $http_ver, $headers);
92 $this->negotiated
= true;
95 // Save request method for later
96 $this->method
= $method;
98 // Build request headers
99 $request = "{$method} {$uri->__toString()} HTTP/{$http_ver}\r\n";
101 // Add all headers to the request string
102 foreach ($headers as $k => $v) {
103 if (is_string($k)) $v = "$k: $v";
104 $request .= "$v\r\n";
109 // Send the request headers
110 if (! @fwrite
($this->socket
, $request)) {
111 require_once 'Zend/Http/Client/Adapter/Exception.php';
112 throw new Zend_Http_Client_Adapter_Exception(
113 'Error writing request to proxy server');
116 //read from $body, write to socket
117 while ($body->hasData()) {
118 if (! @fwrite
($this->socket
, $body->read(self
::CHUNK_SIZE
))) {
119 require_once 'Zend/Http/Client/Adapter/Exception.php';
120 throw new Zend_Http_Client_Adapter_Exception(
121 'Error writing request to server');
124 return 'Large upload, request is not cached.';