[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Gdata / HttpAdapterStreamingProxy.php
blob0435bdb2bb8951d1f68864edef19c25c7ddb8756
1 <?php
3 /**
4 * Zend Framework
6 * LICENSE
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.
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 * @version $Id$
24 /**
25 * @see Zend_Http_Client_Adapter_Proxy
27 require_once 'Zend/Http/Client/Adapter/Proxy.php';
29 /**
30 * Extends the proxy HTTP adapter to handle streams instead of discrete body
31 * strings.
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
39 class Zend_Gdata_HttpAdapterStreamingProxy extends Zend_Http_Client_Adapter_Proxy
41 /**
42 * The amount read from a stream source at a time.
44 * @var integer
46 const CHUNK_SIZE = 1024;
48 /**
49 * Send request to the proxy server with streaming support
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
58 public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
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!');
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');
73 $host = $this->config['proxy_host'];
74 $port = $this->config['proxy_port'];
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');
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']
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;
96 // Save request method for later
97 $this->method = $method;
99 // Build request headers
100 $request = "{$method} {$uri->__toString()} HTTP/{$http_ver}\r\n";
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";
108 $request .= "\r\n";
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');
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');
125 return 'Large upload, request is not cached.';