Skip to content

Commit c4c9323

Browse files
committed
Fix "WordPress\ByteStream\ByteStreamException: Cannot seek to a negative offset"
This errors seems to occur in some scenarios when the RequestReadStream reaches the end of the data, without processing the FINISHED event within pull_until_event(). Originally, the remote_file_length was backfilled only while processing the FINISHED event; now it's ensured also when the file length is actually read.
1 parent 9b032ae commit c4c9323

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: CI
2+
on: [push]
3+
jobs:
4+
build:
5+
runs-on: macos-latest
6+
steps:
7+
- uses: actions/checkout@v2
8+
9+
- name: Set up PHP
10+
uses: shivammathur/setup-php@v2
11+
with:
12+
php-version: '8.0'
13+
tools: phpunit-polyfills
14+
extensions: zip, sqlite3, pdo, pdo_sqlite
15+
16+
- name: Install Composer dependencies (using composer-ci-matrix-tests.json)
17+
run: |
18+
php -r "echo OPENSSL_VERSION_TEXT;"
19+
rm composer.lock
20+
cp composer-ci-matrix-tests.json composer.json
21+
composer install --no-interaction --no-progress --optimize-autoloader
22+
23+
- name: Setup tmate session
24+
uses: mxschmitt/action-tmate@v3

components/HttpClient/ByteStream/RequestReadStream.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ private function pull_until_event( $options = array() ) {
183183
}
184184

185185
public function length(): ?int {
186+
/*
187+
* If the server did not provide a Content-Length header and we've reached
188+
* the end of the data without processing the FINISHED event, we need to
189+
* ensure the file length is backfilled to the number of downloaded bytes.
190+
*/
191+
if ( null === $this->remote_file_length && $this->internal_reached_end_of_data() ) {
192+
$this->remote_file_length = $this->bytes_already_forgotten + strlen( $this->buffer );
193+
}
186194
return $this->remote_file_length;
187195
}
188196

0 commit comments

Comments
 (0)