From efe6a581187eb9e598343ac3310685cc2759b4d0 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 16 Jan 2017 12:23:43 +0100 Subject: [PATCH 1/2] Make sure we do not rewind a non-seekable stream --- spec/StreamFactory/DiactorosStreamFactorySpec.php | 8 ++++++++ spec/StreamFactory/GuzzleStreamFactorySpec.php | 8 ++++++++ spec/StreamFactory/SlimStreamFactorySpec.php | 8 ++++++++ src/StreamFactory/DiactorosStreamFactory.php | 4 +++- src/StreamFactory/SlimStreamFactory.php | 4 +++- 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/spec/StreamFactory/DiactorosStreamFactorySpec.php b/spec/StreamFactory/DiactorosStreamFactorySpec.php index 372ef1b..c42428e 100644 --- a/spec/StreamFactory/DiactorosStreamFactorySpec.php +++ b/spec/StreamFactory/DiactorosStreamFactorySpec.php @@ -19,4 +19,12 @@ function it_creates_a_stream_from_stream() $this->createStream(new Stream('php://memory')) ->shouldHaveType('Psr\Http\Message\StreamInterface'); } + + function it_creates_a_stream_from_non_seekable_resource() + { + $url = 'https://raw.githubusercontent.com/php-http/multipart-stream-builder/master/tests/Resources/httplug.png'; + $resource = fopen($url, 'r'); + $this->createStream($resource) + ->shouldHaveType('Psr\Http\Message\StreamInterface'); + } } diff --git a/spec/StreamFactory/GuzzleStreamFactorySpec.php b/spec/StreamFactory/GuzzleStreamFactorySpec.php index 799a01d..cebffb5 100644 --- a/spec/StreamFactory/GuzzleStreamFactorySpec.php +++ b/spec/StreamFactory/GuzzleStreamFactorySpec.php @@ -19,4 +19,12 @@ public function it_creates_a_stream_from_stream() $this->createStream(new Stream(fopen('php://memory', 'rw'))) ->shouldHaveType('Psr\Http\Message\StreamInterface'); } + + function it_creates_a_stream_from_non_seekable_resource() + { + $url = 'https://raw.githubusercontent.com/php-http/multipart-stream-builder/master/tests/Resources/httplug.png'; + $resource = fopen($url, 'r'); + $this->createStream($resource) + ->shouldHaveType('Psr\Http\Message\StreamInterface'); + } } diff --git a/spec/StreamFactory/SlimStreamFactorySpec.php b/spec/StreamFactory/SlimStreamFactorySpec.php index c3bfef3..a3e8da0 100644 --- a/spec/StreamFactory/SlimStreamFactorySpec.php +++ b/spec/StreamFactory/SlimStreamFactorySpec.php @@ -23,4 +23,12 @@ function it_creates_a_stream_from_stream() $this->createStream(new Stream($resource)) ->shouldHaveType('Psr\Http\Message\StreamInterface'); } + + function it_creates_a_stream_from_non_seekable_resource() + { + $url = 'https://raw.githubusercontent.com/php-http/multipart-stream-builder/master/tests/Resources/httplug.png'; + $resource = fopen($url, 'r'); + $this->createStream($resource) + ->shouldHaveType('Psr\Http\Message\StreamInterface'); + } } diff --git a/src/StreamFactory/DiactorosStreamFactory.php b/src/StreamFactory/DiactorosStreamFactory.php index d3bed9d..a73abb0 100644 --- a/src/StreamFactory/DiactorosStreamFactory.php +++ b/src/StreamFactory/DiactorosStreamFactory.php @@ -34,7 +34,9 @@ public function createStream($body = null) } } - $body->rewind(); + if ($body->isSeekable()) { + $body->rewind(); + } return $body; } diff --git a/src/StreamFactory/SlimStreamFactory.php b/src/StreamFactory/SlimStreamFactory.php index 32b9ac7..ba0bb17 100644 --- a/src/StreamFactory/SlimStreamFactory.php +++ b/src/StreamFactory/SlimStreamFactory.php @@ -35,7 +35,9 @@ public function createStream($body = null) $stream->write((string) $body); } - $stream->rewind(); + if ($stream->isSeekable()) { + $stream->rewind(); + } return $stream; } From 4c297982853ef7d4d8c592db9c91231d93f125cd Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 16 Jan 2017 12:44:20 +0100 Subject: [PATCH 2/2] Put the test in the trait instead --- spec/StreamFactory/DiactorosStreamFactorySpec.php | 8 -------- spec/StreamFactory/GuzzleStreamFactorySpec.php | 8 -------- spec/StreamFactory/SlimStreamFactorySpec.php | 8 -------- spec/StreamFactory/StreamFactoryBehavior.php | 8 ++++++++ 4 files changed, 8 insertions(+), 24 deletions(-) diff --git a/spec/StreamFactory/DiactorosStreamFactorySpec.php b/spec/StreamFactory/DiactorosStreamFactorySpec.php index c42428e..372ef1b 100644 --- a/spec/StreamFactory/DiactorosStreamFactorySpec.php +++ b/spec/StreamFactory/DiactorosStreamFactorySpec.php @@ -19,12 +19,4 @@ function it_creates_a_stream_from_stream() $this->createStream(new Stream('php://memory')) ->shouldHaveType('Psr\Http\Message\StreamInterface'); } - - function it_creates_a_stream_from_non_seekable_resource() - { - $url = 'https://raw.githubusercontent.com/php-http/multipart-stream-builder/master/tests/Resources/httplug.png'; - $resource = fopen($url, 'r'); - $this->createStream($resource) - ->shouldHaveType('Psr\Http\Message\StreamInterface'); - } } diff --git a/spec/StreamFactory/GuzzleStreamFactorySpec.php b/spec/StreamFactory/GuzzleStreamFactorySpec.php index cebffb5..799a01d 100644 --- a/spec/StreamFactory/GuzzleStreamFactorySpec.php +++ b/spec/StreamFactory/GuzzleStreamFactorySpec.php @@ -19,12 +19,4 @@ public function it_creates_a_stream_from_stream() $this->createStream(new Stream(fopen('php://memory', 'rw'))) ->shouldHaveType('Psr\Http\Message\StreamInterface'); } - - function it_creates_a_stream_from_non_seekable_resource() - { - $url = 'https://raw.githubusercontent.com/php-http/multipart-stream-builder/master/tests/Resources/httplug.png'; - $resource = fopen($url, 'r'); - $this->createStream($resource) - ->shouldHaveType('Psr\Http\Message\StreamInterface'); - } } diff --git a/spec/StreamFactory/SlimStreamFactorySpec.php b/spec/StreamFactory/SlimStreamFactorySpec.php index a3e8da0..c3bfef3 100644 --- a/spec/StreamFactory/SlimStreamFactorySpec.php +++ b/spec/StreamFactory/SlimStreamFactorySpec.php @@ -23,12 +23,4 @@ function it_creates_a_stream_from_stream() $this->createStream(new Stream($resource)) ->shouldHaveType('Psr\Http\Message\StreamInterface'); } - - function it_creates_a_stream_from_non_seekable_resource() - { - $url = 'https://raw.githubusercontent.com/php-http/multipart-stream-builder/master/tests/Resources/httplug.png'; - $resource = fopen($url, 'r'); - $this->createStream($resource) - ->shouldHaveType('Psr\Http\Message\StreamInterface'); - } } diff --git a/spec/StreamFactory/StreamFactoryBehavior.php b/spec/StreamFactory/StreamFactoryBehavior.php index 88ab4f4..afc67ef 100644 --- a/spec/StreamFactory/StreamFactoryBehavior.php +++ b/spec/StreamFactory/StreamFactoryBehavior.php @@ -24,4 +24,12 @@ function it_creates_a_stream_from_null() { $this->createStream(null)->shouldHaveType('Psr\Http\Message\StreamInterface'); } + + function it_creates_a_stream_from_non_seekable_resource() + { + $url = 'https://raw.githubusercontent.com/php-http/multipart-stream-builder/master/tests/Resources/httplug.png'; + $resource = fopen($url, 'r'); + $this->createStream($resource) + ->shouldHaveType('Psr\Http\Message\StreamInterface'); + } }