HttpAdapterStreamingSocket.php 3.5 KB

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