2
0

HttpAdapterStreamingProxy.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage Gdata
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Http_Client_Adapter_Proxy
  23. */
  24. require_once 'Zend/Http/Client/Adapter/Proxy.php';
  25. /**
  26. * Extends the proxy HTTP adapter to handle streams instead of discrete body
  27. * strings.
  28. *
  29. * @category Zend
  30. * @package Zend_Gdata
  31. * @subpackage Gdata
  32. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Gdata_HttpAdapterStreamingProxy extends Zend_Http_Client_Adapter_Proxy
  36. {
  37. /**
  38. * The amount read from a stream source at a time.
  39. *
  40. * @var integer
  41. */
  42. const CHUNK_SIZE = 1024;
  43. /**
  44. * Send request to the proxy server with streaming support
  45. *
  46. * @param string $method
  47. * @param Zend_Uri_Http $uri
  48. * @param string $http_ver
  49. * @param array $headers
  50. * @param string $body
  51. * @return string Request as string
  52. */
  53. public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
  54. {
  55. // If no proxy is set, throw an error
  56. if (! $this->config['proxy_host']) {
  57. require_once 'Zend/Http/Client/Adapter/Exception.php';
  58. throw new Zend_Http_Client_Adapter_Exception('No proxy host set!');
  59. }
  60. // Make sure we're properly connected
  61. if (! $this->socket) {
  62. require_once 'Zend/Http/Client/Adapter/Exception.php';
  63. throw new Zend_Http_Client_Adapter_Exception(
  64. 'Trying to write but we are not connected');
  65. }
  66. $host = $this->config['proxy_host'];
  67. $port = $this->config['proxy_port'];
  68. if ($this->connected_to[0] != $host || $this->connected_to[1] != $port) {
  69. require_once 'Zend/Http/Client/Adapter/Exception.php';
  70. throw new Zend_Http_Client_Adapter_Exception(
  71. 'Trying to write but we are connected to the wrong proxy ' .
  72. 'server');
  73. }
  74. // Add Proxy-Authorization header
  75. if ($this->config['proxy_user'] && ! isset($headers['proxy-authorization'])) {
  76. $headers['proxy-authorization'] = Zend_Http_Client::encodeAuthHeader(
  77. $this->config['proxy_user'], $this->config['proxy_pass'], $this->config['proxy_auth']
  78. );
  79. }
  80. // if we are proxying HTTPS, preform CONNECT handshake with the proxy
  81. if ($uri->getScheme() == 'https' && (! $this->negotiated)) {
  82. $this->connectHandshake($uri->getHost(), $uri->getPort(), $http_ver, $headers);
  83. $this->negotiated = true;
  84. }
  85. // Save request method for later
  86. $this->method = $method;
  87. // Build request headers
  88. $request = "{$method} {$uri->__toString()} HTTP/{$http_ver}\r\n";
  89. // Add all headers to the request string
  90. foreach ($headers as $k => $v) {
  91. if (is_string($k)) $v = "$k: $v";
  92. $request .= "$v\r\n";
  93. }
  94. $request .= "\r\n";
  95. // Send the request headers
  96. if (! @fwrite($this->socket, $request)) {
  97. require_once 'Zend/Http/Client/Adapter/Exception.php';
  98. throw new Zend_Http_Client_Adapter_Exception(
  99. 'Error writing request to proxy server');
  100. }
  101. //read from $body, write to socket
  102. while ($body->hasData()) {
  103. if (! @fwrite($this->socket, $body->read(self::CHUNK_SIZE))) {
  104. require_once 'Zend/Http/Client/Adapter/Exception.php';
  105. throw new Zend_Http_Client_Adapter_Exception(
  106. 'Error writing request to server');
  107. }
  108. }
  109. return 'Large upload, request is not cached.';
  110. }
  111. }