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-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
---|
20 | * @license http://framework.zend.com/license/new-bsd New BSD License
|
---|
21 | */
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * @see Zend_Http_Client_Adapter_Proxy
|
---|
25 | */
|
---|
26 | require_once 'Zend/Http/Client/Adapter/Proxy.php';
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Extends the proxy HTTP adapter to handle streams instead of discrete body
|
---|
30 | * strings.
|
---|
31 | *
|
---|
32 | * @category Zend
|
---|
33 | * @package Zend_Gdata
|
---|
34 | * @subpackage Gdata
|
---|
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
|
---|
37 | */
|
---|
38 | class Zend_Gdata_HttpAdapterStreamingProxy extends Zend_Http_Client_Adapter_Proxy
|
---|
39 | {
|
---|
40 | /**
|
---|
41 | * The amount read from a stream source at a time.
|
---|
42 | *
|
---|
43 | * @var integer
|
---|
44 | */
|
---|
45 | const CHUNK_SIZE = 1024;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Send request to the proxy server with streaming support
|
---|
49 | *
|
---|
50 | * @param string $method
|
---|
51 | * @param Zend_Uri_Http $uri
|
---|
52 | * @param string $http_ver
|
---|
53 | * @param array $headers
|
---|
54 | * @param string $body
|
---|
55 | * @return string Request as string
|
---|
56 | */
|
---|
57 | public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
|
---|
58 | {
|
---|
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!');
|
---|
63 | }
|
---|
64 |
|
---|
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');
|
---|
70 | }
|
---|
71 |
|
---|
72 | $host = $this->config['proxy_host'];
|
---|
73 | $port = $this->config['proxy_port'];
|
---|
74 |
|
---|
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 ' .
|
---|
79 | 'server');
|
---|
80 | }
|
---|
81 |
|
---|
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']
|
---|
86 | );
|
---|
87 | }
|
---|
88 |
|
---|
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;
|
---|
93 | }
|
---|
94 |
|
---|
95 | // Save request method for later
|
---|
96 | $this->method = $method;
|
---|
97 |
|
---|
98 | // Build request headers
|
---|
99 | $request = "{$method} {$uri->__toString()} HTTP/{$http_ver}\r\n";
|
---|
100 |
|
---|
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";
|
---|
105 | }
|
---|
106 |
|
---|
107 | $request .= "\r\n";
|
---|
108 |
|
---|
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');
|
---|
114 | }
|
---|
115 |
|
---|
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');
|
---|
122 | }
|
---|
123 | }
|
---|
124 | return 'Large upload, request is not cached.';
|
---|
125 | }
|
---|
126 | }
|
---|