HttpAdapterStreamingProxy.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Http_Client_Adapter_Proxy
  24. */
  25. require_once 'Zend/Http/Client/Adapter/Proxy.php';
  26. /**
  27. * Extends the proxy HTTP adapter to handle streams instead of discrete body
  28. * strings.
  29. *
  30. * @category Zend
  31. * @package Zend_Gdata
  32. * @subpackage Gdata
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Gdata_HttpAdapterStreamingProxy extends Zend_Http_Client_Adapter_Proxy
  37. {
  38. /**
  39. * The amount read from a stream source at a time.
  40. *
  41. * @var integer
  42. */
  43. const CHUNK_SIZE = 1024;
  44. /**
  45. * Send request to the proxy server with streaming support
  46. *
  47. * @param string $method
  48. * @param Zend_Uri_Http $uri
  49. * @param string $http_ver
  50. * @param array $headers
  51. * @param string $body
  52. * @return string Request as string
  53. */
  54. public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
  55. {
  56. // If no proxy is set, throw an error
  57. if (! $this->config['proxy_host']) {
  58. require_once 'Zend/Http/Client/Adapter/Exception.php';
  59. throw new Zend_Http_Client_Adapter_Exception('No proxy host set!');
  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. $host = $this->config['proxy_host'];
  68. $port = $this->config['proxy_port'];
  69. if ($this->connected_to[0] != $host || $this->connected_to[1] != $port) {
  70. require_once 'Zend/Http/Client/Adapter/Exception.php';
  71. throw new Zend_Http_Client_Adapter_Exception(
  72. 'Trying to write but we are connected to the wrong proxy ' .
  73. 'server');
  74. }
  75. // Add Proxy-Authorization header
  76. if ($this->config['proxy_user'] && ! isset($headers['proxy-authorization'])) {
  77. $headers['proxy-authorization'] = Zend_Http_Client::encodeAuthHeader(
  78. $this->config['proxy_user'], $this->config['proxy_pass'], $this->config['proxy_auth']
  79. );
  80. }
  81. // if we are proxying HTTPS, preform CONNECT handshake with the proxy
  82. if ($uri->getScheme() == 'https' && (! $this->negotiated)) {
  83. $this->connectHandshake($uri->getHost(), $uri->getPort(), $http_ver, $headers);
  84. $this->negotiated = true;
  85. }
  86. // Save request method for later
  87. $this->method = $method;
  88. // Build request headers
  89. $request = "{$method} {$uri->__toString()} HTTP/{$http_ver}\r\n";
  90. // Add all headers to the request string
  91. foreach ($headers as $k => $v) {
  92. if (is_string($k)) $v = "$k: $v";
  93. $request .= "$v\r\n";
  94. }
  95. $request .= "\r\n";
  96. // Send the request headers
  97. if (! @fwrite($this->socket, $request)) {
  98. require_once 'Zend/Http/Client/Adapter/Exception.php';
  99. throw new Zend_Http_Client_Adapter_Exception(
  100. 'Error writing request to proxy server');
  101. }
  102. // Read from $body, write to socket
  103. $chunk = $body->read(self::CHUNK_SIZE);
  104. while ($chunk !== false) {
  105. if (!@fwrite($this->socket, $chunk)) {
  106. require_once 'Zend/Http/Client/Adapter/Exception.php';
  107. throw new Zend_Http_Client_Adapter_Exception(
  108. 'Error writing request to server'
  109. );
  110. }
  111. $chunk = $body->read(self::CHUNK_SIZE);
  112. }
  113. $body->closeFileHandle();
  114. return 'Large upload, request is not cached.';
  115. }
  116. }