HttpAdapterStreamingSocket.php 3.7 KB

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