class-chunkeddecoderreadstream.php
6 days ago
class-chunkedencoderbytetransformer.php
6 days ago
class-requestreadstream.php
6 days ago
class-seekablerequestreadstream.php
6 days ago
class-seekablerequestreadstream.php
134 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\HttpClient\ByteStream; |
| 4 | |
| 5 | use WordPress\ByteStream\FileReadWriteStream; |
| 6 | use WordPress\ByteStream\ReadStream\BaseByteReadStream; |
| 7 | use WordPress\ByteStream\ReadStream\ByteReadStream; |
| 8 | use WordPress\HttpClient\Request; |
| 9 | |
| 10 | /** |
| 11 | * HTTP reader that can seek() within the stream. |
| 12 | * |
| 13 | * Downloaded bytes are stored in a temporary file. All the read operations are delegated to that file. |
| 14 | * |
| 15 | * – Seek()-ing forward is done by fetching all the bytes up to the target offset. |
| 16 | * – Seek()-ing backwards is done by seeking within the temporary file. |
| 17 | */ |
| 18 | class SeekableRequestReadStream implements ByteReadStream { |
| 19 | |
| 20 | |
| 21 | /** |
| 22 | * RequestReadStream |
| 23 | */ |
| 24 | private $remote; |
| 25 | /** |
| 26 | * FileReadWriteStream |
| 27 | */ |
| 28 | private $cache; |
| 29 | private $temp; |
| 30 | private $length_resolved = false; |
| 31 | |
| 32 | public function __construct( $request, array $options = array() ) { |
| 33 | if ( is_string( $request ) ) { |
| 34 | $request = new Request( $request ); |
| 35 | } |
| 36 | $this->remote = new RequestReadStream( $request, $options ); |
| 37 | $this->temp = $options['cache_path'] ?? tempnam( sys_get_temp_dir(), 'wp_http_cache_' ); |
| 38 | $this->cache = FileReadWriteStream::from_path( $this->temp, true ); |
| 39 | } |
| 40 | |
| 41 | private function pipe_until( int $offset ): void { |
| 42 | while ( null === $this->cache->length() || $this->cache->length() < $offset ) { |
| 43 | $pulled = $this->remote->pull( BaseByteReadStream::CHUNK_SIZE_BYTES ); |
| 44 | if ( 0 === $pulled ) { |
| 45 | break; |
| 46 | } |
| 47 | $this->cache->append_bytes( $this->remote->consume( $pulled ) ); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public function length(): ?int { |
| 52 | if ( ! $this->length_resolved && null === $this->remote->length() ) { |
| 53 | /** |
| 54 | * Wait for the remote headers before returning the length. |
| 55 | * |
| 56 | * This is an inconsistency between RequestReadStream::length(): |
| 57 | * |
| 58 | * * RequestReadStream returns null until the remote headers are known. |
| 59 | * * SeekableRequestReadStream proactively waits for the remote headers. |
| 60 | * |
| 61 | * That's because: |
| 62 | * |
| 63 | * * RequestReadStream class is a lower-level utility where we simply |
| 64 | * expose what's available at the moment. The developer is responsible |
| 65 | * for awaiting the response headers. |
| 66 | * * SeekableRequestReadStream is a higher-level tool meant for usage |
| 67 | * when knowing the length is vital, e.g. reading from a remote ZIP file. |
| 68 | */ |
| 69 | $this->remote->await_response(); |
| 70 | if ( null === $this->remote->length() ) { |
| 71 | // The server did not send the Content-Length header. |
| 72 | // We need to consume the entire stream to infer the length. |
| 73 | $position = $this->tell(); |
| 74 | $this->consume_all(); |
| 75 | $this->seek( $position ); |
| 76 | } |
| 77 | $this->length_resolved = true; |
| 78 | } |
| 79 | |
| 80 | return $this->remote->length(); |
| 81 | } |
| 82 | |
| 83 | public function tell(): int { |
| 84 | return $this->cache->tell(); |
| 85 | } |
| 86 | |
| 87 | public function seek( int $offset ): void { |
| 88 | $this->pipe_until( $offset ); |
| 89 | $this->cache->seek( $offset ); |
| 90 | } |
| 91 | |
| 92 | public function reached_end_of_data(): bool { |
| 93 | return $this->remote->reached_end_of_data() && $this->cache->reached_end_of_data(); |
| 94 | } |
| 95 | |
| 96 | public function pull( ?int $n, string $mode = self::PULL_NO_MORE_THAN ): int { |
| 97 | $this->pipe_until( $this->tell() + $n ); |
| 98 | |
| 99 | return $this->cache->pull( $n, $mode ); |
| 100 | } |
| 101 | |
| 102 | public function peek( int $n ): string { |
| 103 | $this->pipe_until( $this->tell() + $n ); |
| 104 | |
| 105 | return $this->cache->peek( $n ); |
| 106 | } |
| 107 | |
| 108 | public function consume( int $n ): string { |
| 109 | return $this->cache->consume( $n ); |
| 110 | } |
| 111 | |
| 112 | public function consume_all(): string { |
| 113 | while ( ! $this->remote->reached_end_of_data() ) { |
| 114 | $pulled = $this->remote->pull( BaseByteReadStream::CHUNK_SIZE_BYTES ); |
| 115 | if ( $pulled > 0 ) { |
| 116 | $this->cache->append_bytes( $this->remote->consume( $pulled ) ); |
| 117 | } |
| 118 | } |
| 119 | $this->cache->close_writing(); |
| 120 | |
| 121 | return $this->cache->consume_all(); |
| 122 | } |
| 123 | |
| 124 | public function await_response() { |
| 125 | return $this->remote->await_response(); |
| 126 | } |
| 127 | |
| 128 | public function close_reading(): void { |
| 129 | $this->remote->close_reading(); |
| 130 | $this->cache->close_reading(); |
| 131 | @unlink( $this->temp ); |
| 132 | } |
| 133 | } |
| 134 |