Skip to content

Commit efe6a58

Browse files
committed
Make sure we do not rewind a non-seekable stream
1 parent 016359f commit efe6a58

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

spec/StreamFactory/DiactorosStreamFactorySpec.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,12 @@ function it_creates_a_stream_from_stream()
1919
$this->createStream(new Stream('php://memory'))
2020
->shouldHaveType('Psr\Http\Message\StreamInterface');
2121
}
22+
23+
function it_creates_a_stream_from_non_seekable_resource()
24+
{
25+
$url = 'https://raw.githubusercontent.com/php-http/multipart-stream-builder/master/tests/Resources/httplug.png';
26+
$resource = fopen($url, 'r');
27+
$this->createStream($resource)
28+
->shouldHaveType('Psr\Http\Message\StreamInterface');
29+
}
2230
}

spec/StreamFactory/GuzzleStreamFactorySpec.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,12 @@ public function it_creates_a_stream_from_stream()
1919
$this->createStream(new Stream(fopen('php://memory', 'rw')))
2020
->shouldHaveType('Psr\Http\Message\StreamInterface');
2121
}
22+
23+
function it_creates_a_stream_from_non_seekable_resource()
24+
{
25+
$url = 'https://raw.githubusercontent.com/php-http/multipart-stream-builder/master/tests/Resources/httplug.png';
26+
$resource = fopen($url, 'r');
27+
$this->createStream($resource)
28+
->shouldHaveType('Psr\Http\Message\StreamInterface');
29+
}
2230
}

spec/StreamFactory/SlimStreamFactorySpec.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@ function it_creates_a_stream_from_stream()
2323
$this->createStream(new Stream($resource))
2424
->shouldHaveType('Psr\Http\Message\StreamInterface');
2525
}
26+
27+
function it_creates_a_stream_from_non_seekable_resource()
28+
{
29+
$url = 'https://raw.githubusercontent.com/php-http/multipart-stream-builder/master/tests/Resources/httplug.png';
30+
$resource = fopen($url, 'r');
31+
$this->createStream($resource)
32+
->shouldHaveType('Psr\Http\Message\StreamInterface');
33+
}
2634
}

src/StreamFactory/DiactorosStreamFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public function createStream($body = null)
3434
}
3535
}
3636

37-
$body->rewind();
37+
if ($body->isSeekable()) {
38+
$body->rewind();
39+
}
3840

3941
return $body;
4042
}

src/StreamFactory/SlimStreamFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public function createStream($body = null)
3535
$stream->write((string) $body);
3636
}
3737

38-
$stream->rewind();
38+
if ($stream->isSeekable()) {
39+
$stream->rewind();
40+
}
3941

4042
return $stream;
4143
}

0 commit comments

Comments
 (0)