Skip to content

Commit a89f53c

Browse files
authored
Merge pull request #71 from php-http/Nyholm-non-seekable
Make sure we do not rewind a non-seekable stream
2 parents 016359f + 4c29798 commit a89f53c

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

spec/StreamFactory/StreamFactoryBehavior.php

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

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)