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_Socket
|
---|
25 | */
|
---|
26 | require_once 'Zend/Http/Client/Adapter/Socket.php';
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Extends the default 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_HttpAdapterStreamingSocket extends Zend_Http_Client_Adapter_Socket
|
---|
39 | {
|
---|
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 remote 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(),
|
---|
59 | $body = '')
|
---|
60 | {
|
---|
61 | // Make sure we're properly connected
|
---|
62 | if (! $this->socket) {
|
---|
63 | require_once 'Zend/Http/Client/Adapter/Exception.php';
|
---|
64 | throw new Zend_Http_Client_Adapter_Exception(
|
---|
65 | 'Trying to write but we are not connected');
|
---|
66 | }
|
---|
67 |
|
---|
68 | $host = $uri->getHost();
|
---|
69 | $host = (strtolower($uri->getScheme()) == 'https' ? $this->config['ssltransport'] : 'tcp') . '://' . $host;
|
---|
70 | if ($this->connected_to[0] != $host || $this->connected_to[1] != $uri->getPort()) {
|
---|
71 | require_once 'Zend/Http/Client/Adapter/Exception.php';
|
---|
72 | throw new Zend_Http_Client_Adapter_Exception(
|
---|
73 | 'Trying to write but we are connected to the wrong host');
|
---|
74 | }
|
---|
75 |
|
---|
76 | // Save request method for later
|
---|
77 | $this->method = $method;
|
---|
78 |
|
---|
79 | // Build request headers
|
---|
80 | $path = $uri->getPath();
|
---|
81 | if ($uri->getQuery()) $path .= '?' . $uri->getQuery();
|
---|
82 | $request = "{$method} {$path} HTTP/{$http_ver}\r\n";
|
---|
83 | foreach ($headers as $k => $v) {
|
---|
84 | if (is_string($k)) $v = ucfirst($k) . ": $v";
|
---|
85 | $request .= "$v\r\n";
|
---|
86 | }
|
---|
87 |
|
---|
88 | // Send the headers over
|
---|
89 | $request .= "\r\n";
|
---|
90 | if (! @fwrite($this->socket, $request)) {
|
---|
91 | require_once 'Zend/Http/Client/Adapter/Exception.php';
|
---|
92 | throw new Zend_Http_Client_Adapter_Exception(
|
---|
93 | 'Error writing request to server');
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | //read from $body, write to socket
|
---|
98 | while ($body->hasData()) {
|
---|
99 | if (! @fwrite($this->socket, $body->read(self::CHUNK_SIZE))) {
|
---|
100 | require_once 'Zend/Http/Client/Adapter/Exception.php';
|
---|
101 | throw new Zend_Http_Client_Adapter_Exception(
|
---|
102 | 'Error writing request to server');
|
---|
103 | }
|
---|
104 | }
|
---|
105 | return 'Large upload, request is not cached.';
|
---|
106 | }
|
---|
107 | }
|
---|