public function testRewindResetsToStartOfStream() { $this->tmpnam = tempnam(sys_get_temp_dir(), 'phly'); file_put_contents($this->tmpnam, 'FOO BAR'); $resource = fopen($this->tmpnam, 'wb+'); $stream = new Stream($resource); $this->assertTrue($stream->seek(2)); $stream->rewind(); $this->assertEquals(0, $stream->tell()); }
/** * Split the stream into headers and body content. * * Returns an array containing two elements * * - The first is an array of headers * - The second is a StreamInterface containing the body content * * @param StreamInterface $stream * @return array * @throws UnexpectedValueException For invalid headers. */ protected static function splitStream(StreamInterface $stream) { $headers = []; $currentHeader = false; while ($line = self::getLine($stream)) { if (preg_match(';^(?P<name>[!#$%&\'*+.^_`\\|~0-9a-zA-Z-]+):(?P<value>.*)$;', $line, $matches)) { $currentHeader = $matches['name']; if (!isset($headers[$currentHeader])) { $headers[$currentHeader] = []; } $headers[$currentHeader][] = ltrim($matches['value']); continue; } if (!$currentHeader) { throw new UnexpectedValueException('Invalid header detected'); } if (!preg_match('#^[ \\t]#', $line)) { throw new UnexpectedValueException('Invalid header continuation'); } // Append continuation to last header value found $value = array_pop($headers[$currentHeader]); $headers[$currentHeader][] = $value . ltrim($line); } $body = new Stream('php://temp', 'wb+'); if (!$stream->eof()) { while ($data = $stream->read(4096)) { $body->write($data); } $body->rewind(); } return [$headers, $body]; }