StreamWrapper.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpClient\Response;
  11. use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
  12. use Symfony\Contracts\HttpClient\HttpClientInterface;
  13. use Symfony\Contracts\HttpClient\ResponseInterface;
  14. /**
  15. * Allows turning ResponseInterface instances to PHP streams.
  16. *
  17. * @author Nicolas Grekas <p@tchwork.com>
  18. */
  19. class StreamWrapper
  20. {
  21. /** @var resource|null */
  22. public $context;
  23. private $client;
  24. private $response;
  25. /** @var resource|string|null */
  26. private $content;
  27. /** @var resource|null */
  28. private $handle;
  29. private bool $blocking = true;
  30. private ?float $timeout = null;
  31. private bool $eof = false;
  32. private ?int $offset = 0;
  33. /**
  34. * Creates a PHP stream resource from a ResponseInterface.
  35. *
  36. * @return resource
  37. */
  38. public static function createResource(ResponseInterface $response, HttpClientInterface $client = null)
  39. {
  40. if ($response instanceof StreamableInterface) {
  41. $stack = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 2);
  42. if ($response !== ($stack[1]['object'] ?? null)) {
  43. return $response->toStream(false);
  44. }
  45. }
  46. if (null === $client && !method_exists($response, 'stream')) {
  47. throw new \InvalidArgumentException(sprintf('Providing a client to "%s()" is required when the response doesn\'t have any "stream()" method.', __CLASS__));
  48. }
  49. static $registered = false;
  50. if (!$registered = $registered || stream_wrapper_register(strtr(__CLASS__, '\\', '-'), __CLASS__)) {
  51. throw new \RuntimeException(error_get_last()['message'] ?? 'Registering the "symfony" stream wrapper failed.');
  52. }
  53. $context = [
  54. 'client' => $client ?? $response,
  55. 'response' => $response,
  56. ];
  57. return fopen(strtr(__CLASS__, '\\', '-').'://'.$response->getInfo('url'), 'r', false, stream_context_create(['symfony' => $context]));
  58. }
  59. public function getResponse(): ResponseInterface
  60. {
  61. return $this->response;
  62. }
  63. /**
  64. * @param resource|callable|null $handle The resource handle that should be monitored when
  65. * stream_select() is used on the created stream
  66. * @param resource|null $content The seekable resource where the response body is buffered
  67. */
  68. public function bindHandles(&$handle, &$content): void
  69. {
  70. $this->handle = &$handle;
  71. $this->content = &$content;
  72. $this->offset = null;
  73. }
  74. public function stream_open(string $path, string $mode, int $options): bool
  75. {
  76. if ('r' !== $mode) {
  77. if ($options & \STREAM_REPORT_ERRORS) {
  78. trigger_error(sprintf('Invalid mode "%s": only "r" is supported.', $mode), \E_USER_WARNING);
  79. }
  80. return false;
  81. }
  82. $context = stream_context_get_options($this->context)['symfony'] ?? null;
  83. $this->client = $context['client'] ?? null;
  84. $this->response = $context['response'] ?? null;
  85. $this->context = null;
  86. if (null !== $this->client && null !== $this->response) {
  87. return true;
  88. }
  89. if ($options & \STREAM_REPORT_ERRORS) {
  90. trigger_error('Missing options "client" or "response" in "symfony" stream context.', \E_USER_WARNING);
  91. }
  92. return false;
  93. }
  94. public function stream_read(int $count)
  95. {
  96. if (\is_resource($this->content)) {
  97. // Empty the internal activity list
  98. foreach ($this->client->stream([$this->response], 0) as $chunk) {
  99. try {
  100. if (!$chunk->isTimeout() && $chunk->isFirst()) {
  101. $this->response->getStatusCode(); // ignore 3/4/5xx
  102. }
  103. } catch (ExceptionInterface $e) {
  104. trigger_error($e->getMessage(), \E_USER_WARNING);
  105. return false;
  106. }
  107. }
  108. if (0 !== fseek($this->content, $this->offset ?? 0)) {
  109. return false;
  110. }
  111. if ('' !== $data = fread($this->content, $count)) {
  112. fseek($this->content, 0, \SEEK_END);
  113. $this->offset += \strlen($data);
  114. return $data;
  115. }
  116. }
  117. if (\is_string($this->content)) {
  118. if (\strlen($this->content) <= $count) {
  119. $data = $this->content;
  120. $this->content = null;
  121. } else {
  122. $data = substr($this->content, 0, $count);
  123. $this->content = substr($this->content, $count);
  124. }
  125. $this->offset += \strlen($data);
  126. return $data;
  127. }
  128. foreach ($this->client->stream([$this->response], $this->blocking ? $this->timeout : 0) as $chunk) {
  129. try {
  130. $this->eof = true;
  131. $this->eof = !$chunk->isTimeout();
  132. if (!$this->eof && !$this->blocking) {
  133. return '';
  134. }
  135. $this->eof = $chunk->isLast();
  136. if ($chunk->isFirst()) {
  137. $this->response->getStatusCode(); // ignore 3/4/5xx
  138. }
  139. if ('' !== $data = $chunk->getContent()) {
  140. if (\strlen($data) > $count) {
  141. if (null === $this->content) {
  142. $this->content = substr($data, $count);
  143. }
  144. $data = substr($data, 0, $count);
  145. }
  146. $this->offset += \strlen($data);
  147. return $data;
  148. }
  149. } catch (ExceptionInterface $e) {
  150. trigger_error($e->getMessage(), \E_USER_WARNING);
  151. return false;
  152. }
  153. }
  154. return '';
  155. }
  156. public function stream_set_option(int $option, int $arg1, ?int $arg2): bool
  157. {
  158. if (\STREAM_OPTION_BLOCKING === $option) {
  159. $this->blocking = (bool) $arg1;
  160. } elseif (\STREAM_OPTION_READ_TIMEOUT === $option) {
  161. $this->timeout = $arg1 + $arg2 / 1e6;
  162. } else {
  163. return false;
  164. }
  165. return true;
  166. }
  167. public function stream_tell(): int
  168. {
  169. return $this->offset ?? 0;
  170. }
  171. public function stream_eof(): bool
  172. {
  173. return $this->eof && !\is_string($this->content);
  174. }
  175. public function stream_seek(int $offset, int $whence = \SEEK_SET): bool
  176. {
  177. if (null === $this->content && null === $this->offset) {
  178. $this->response->getStatusCode();
  179. $this->offset = 0;
  180. }
  181. if (!\is_resource($this->content) || 0 !== fseek($this->content, 0, \SEEK_END)) {
  182. return false;
  183. }
  184. $size = ftell($this->content);
  185. if (\SEEK_CUR === $whence) {
  186. $offset += $this->offset ?? 0;
  187. }
  188. if (\SEEK_END === $whence || $size < $offset) {
  189. foreach ($this->client->stream([$this->response]) as $chunk) {
  190. try {
  191. if ($chunk->isFirst()) {
  192. $this->response->getStatusCode(); // ignore 3/4/5xx
  193. }
  194. // Chunks are buffered in $this->content already
  195. $size += \strlen($chunk->getContent());
  196. if (\SEEK_END !== $whence && $offset <= $size) {
  197. break;
  198. }
  199. } catch (ExceptionInterface $e) {
  200. trigger_error($e->getMessage(), \E_USER_WARNING);
  201. return false;
  202. }
  203. }
  204. if (\SEEK_END === $whence) {
  205. $offset += $size;
  206. }
  207. }
  208. if (0 <= $offset && $offset <= $size) {
  209. $this->eof = false;
  210. $this->offset = $offset;
  211. return true;
  212. }
  213. return false;
  214. }
  215. public function stream_cast(int $castAs)
  216. {
  217. if (\STREAM_CAST_FOR_SELECT === $castAs) {
  218. $this->response->getHeaders(false);
  219. return (\is_callable($this->handle) ? ($this->handle)() : $this->handle) ?? false;
  220. }
  221. return false;
  222. }
  223. public function stream_stat(): array
  224. {
  225. try {
  226. $headers = $this->response->getHeaders(false);
  227. } catch (ExceptionInterface $e) {
  228. trigger_error($e->getMessage(), \E_USER_WARNING);
  229. $headers = [];
  230. }
  231. return [
  232. 'dev' => 0,
  233. 'ino' => 0,
  234. 'mode' => 33060,
  235. 'nlink' => 0,
  236. 'uid' => 0,
  237. 'gid' => 0,
  238. 'rdev' => 0,
  239. 'size' => (int) ($headers['content-length'][0] ?? -1),
  240. 'atime' => 0,
  241. 'mtime' => strtotime($headers['last-modified'][0] ?? '') ?: 0,
  242. 'ctime' => 0,
  243. 'blksize' => 0,
  244. 'blocks' => 0,
  245. ];
  246. }
  247. private function __construct()
  248. {
  249. }
  250. }