/** * {@inheritdoc} */ public function discard() : Awaitable { return new Coroutine(function () { $len = 0; try { while (null !== ($chunk = (yield $this->stream->read()))) { $len += \strlen($chunk); } return $len; } finally { $this->stream->close(); } }); }
/** * Read next chunk from temp file. * * Fall back to reading from buffered source stream if contents of the temp file have been read. */ protected function readNextChunk() : \Generator { $chunk = (yield $this->stream->readBuffer($this->bufferSize)); if ($chunk === null) { $chunk = (yield $this->source->readBuffer($this->bufferSize)); if ($chunk === null) { $this->body->computeSize(); $this->source->close(); } else { $this->body->incrementOffset((yield $this->stream->write($chunk))); } $chunk = (yield $this->stream->readBuffer($this->bufferSize)); } return $chunk; }
protected function streamReader(ReadableStream $stream) : \Generator { $contents = ''; try { $channel = $stream->channel(8192, $this->length); while (null !== ($chunk = (yield $channel->receive()))) { $contents .= $chunk; } } finally { if ($this->closeSource) { $stream->close(); } } return $contents; }
/** * Create the input stream being used to read decoded body data from the remote peer. */ protected function createInputStream() : EntityStream { if ($this->chunked) { $stream = new ChunkDecodedStream($this->stream); } elseif ($this->length > 0) { $stream = new LimitStream($this->stream, $this->length); } elseif ($this->closeSupported) { $stream = $this->stream; } else { if ($this->cascadeClose) { $this->stream->close(); } return new EntityStream(new ReadableMemoryStream(), true, $this->expectContinue); } return new EntityStream($stream, $this->cascadeClose, $this->expectContinue); }
/** * Create the input stream being used to read decoded body data from the remote peer. * * @return ReadableStream */ protected function createInputStream() : \Generator { if ($this->expectContinue) { (yield $this->expectContinue->write("HTTP/1.1 100 Continue\r\n")); } if ($this->chunked) { $stream = new ChunkDecodedStream($this->stream, $this->cascadeClose); } elseif ($this->length > 0) { $stream = new LimitStream($this->stream, $this->length, $this->cascadeClose); } else { if ($this->cascadeClose) { $this->stream->close(); } return new ReadableMemoryStream(); } switch ($this->compression) { case self::COMPRESSION_GZIP: return new ReadableInflateStream($stream, \ZLIB_ENCODING_GZIP); case self::COMPRESSION_DEFLATE: return new ReadableInflateStream($stream, \ZLIB_ENCODING_DEFLATE); } return $stream; }
/** * Stream a binary WebSocket message. * * @param ReadableStream $stream * @param int $priority * @return int Number of transmitted bytes. * * @throws \InvalidArgumentException When the text is not UTF-8 encoded. */ public function sendBinary(ReadableStream $stream, int $priority = 0) : Awaitable { return $this->writer->execute(function () use($stream) { $type = Frame::BINARY; $len = 0; try { $chunk = (yield $stream->readBuffer(4092)); while (null !== ($next = (yield $stream->readBuffer(4092)))) { $len += (yield $this->writeFrame(new Frame($type, $chunk, false))); $chunk = $next; $type = Frame::CONTINUATION; } if ($chunk !== null) { $len += (yield $this->writeFrame(new Frame($type, $chunk))); } return $len; } finally { $stream->close(); } }, $priority); }
/** * {@inheritdoc} */ public function sendBinary(ReadableStream $stream, int $priority = 0) : Awaitable { return $this->writer->execute(function () use($stream) { $type = Frame::BINARY; $reserved = Frame::RESERVED1; $context = $this->deflate->getCompressionContext(); $len = 0; try { $chunk = (yield $stream->readBuffer(4092)); while (null !== ($next = (yield $stream->readBuffer(4092)))) { $chunk = \deflate_add($context, $chunk, \ZLIB_SYNC_FLUSH); $len += (yield $this->writeFrame(new Frame($type, $chunk, false, $reserved))); $chunk = $next; $type = Frame::CONTINUATION; $reserved = 0; } if ($chunk !== null) { $chunk = \substr(\deflate_add($context, $chunk, $this->deflate->getCompressionFlushMode()), 0, -4); $len += (yield $this->writeFrame(new Frame($type, $chunk, true, $reserved))); } return $len; } finally { $stream->close(); } }, $priority); }