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-2010 Zend Technologies USA Inc. (http://www.zend.com)
20 * @license http://framework.zend.com/license/new-bsd New BSD License
25 * @see Zend_Http_Client_Adapter_Socket
27 require_once 'Zend/Http/Client/Adapter/Socket.php';
30 * Extends the default HTTP adapter to handle streams instead of discrete body
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_HttpAdapterStreamingSocket extends Zend_Http_Client_Adapter_Socket
43 * The amount read from a stream source at a time.
47 const CHUNK_SIZE = 1024;
50 * Send request to the remote server with streaming support.
52 * @param string $method
53 * @param Zend_Uri_Http $uri
54 * @param string $http_ver
55 * @param array $headers
57 * @return string Request as string
59 public function write($method, $uri, $http_ver = '1.1', $headers = array(),
62 // Make sure we're properly connected
63 if (! $this->socket) {
64 require_once 'Zend/Http/Client/Adapter/Exception.php';
65 throw new Zend_Http_Client_Adapter_Exception(
66 'Trying to write but we are not connected');
69 $host = $uri->getHost();
70 $host = (strtolower($uri->getScheme()) == 'https' ? $this->config['ssltransport'] : 'tcp') . '://' . $host;
71 if ($this->connected_to[0] != $host || $this->connected_to[1] != $uri->getPort()) {
72 require_once 'Zend/Http/Client/Adapter/Exception.php';
73 throw new Zend_Http_Client_Adapter_Exception(
74 'Trying to write but we are connected to the wrong host');
77 // Save request method for later
78 $this->method = $method;
80 // Build request headers
81 $path = $uri->getPath();
82 if ($uri->getQuery()) $path .= '?' . $uri->getQuery();
83 $request = "{$method} {$path} HTTP/{$http_ver}\r\n";
84 foreach ($headers as $k => $v) {
85 if (is_string($k)) $v = ucfirst($k) . ": $v";
89 // Send the headers over
91 if (! @fwrite($this->socket, $request)) {
92 require_once 'Zend/Http/Client/Adapter/Exception.php';
93 throw new Zend_Http_Client_Adapter_Exception(
94 'Error writing request to server');
98 //read from $body, write to socket
99 $chunk = $body->read(self::CHUNK_SIZE);
100 while ($chunk !== FALSE) {
101 if (! @fwrite($this->socket, $chunk)) {
102 require_once 'Zend/Http/Client/Adapter/Exception.php';
103 throw new Zend_Http_Client_Adapter_Exception(
104 'Error writing request to server');
106 $chunk = $body->read(self::CHUNK_SIZE);
108 $body->closeFileHandle();
109 return 'Large upload, request is not cached.';