diff options
Diffstat (limited to 'inc/mailgun/php-http/message/src/Encoding')
10 files changed, 544 insertions, 0 deletions
diff --git a/inc/mailgun/php-http/message/src/Encoding/ChunkStream.php b/inc/mailgun/php-http/message/src/Encoding/ChunkStream.php new file mode 100644 index 0000000..74c2fbd --- /dev/null +++ b/inc/mailgun/php-http/message/src/Encoding/ChunkStream.php @@ -0,0 +1,39 @@ +<?php + +namespace Http\Message\Encoding; + +/** + * Transform a regular stream into a chunked one. + * + * @author Joel Wurtz <joel.wurtz@gmail.com> + */ +class ChunkStream extends FilteredStream +{ + /** + * {@inheritdoc} + */ + protected function readFilter() + { + return 'chunk'; + } + + /** + * {@inheritdoc} + */ + protected function writeFilter() + { + return 'dechunk'; + } + + /** + * {@inheritdoc} + */ + protected function fill() + { + parent::fill(); + + if ($this->stream->eof()) { + $this->buffer .= "0\r\n\r\n"; + } + } +} diff --git a/inc/mailgun/php-http/message/src/Encoding/CompressStream.php b/inc/mailgun/php-http/message/src/Encoding/CompressStream.php new file mode 100644 index 0000000..d1013dc --- /dev/null +++ b/inc/mailgun/php-http/message/src/Encoding/CompressStream.php @@ -0,0 +1,42 @@ +<?php + +namespace Http\Message\Encoding; + +use Psr\Http\Message\StreamInterface; + +/** + * Stream compress (RFC 1950). + * + * @author Joel Wurtz <joel.wurtz@gmail.com> + */ +class CompressStream extends FilteredStream +{ + /** + * @param StreamInterface $stream + * @param int $level + */ + public function __construct(StreamInterface $stream, $level = -1) + { + if (!extension_loaded('zlib')) { + throw new \RuntimeException('The zlib extension must be enabled to use this stream'); + } + + parent::__construct($stream, ['window' => 15, 'level' => $level], ['window' => 15]); + } + + /** + * {@inheritdoc} + */ + protected function readFilter() + { + return 'zlib.deflate'; + } + + /** + * {@inheritdoc} + */ + protected function writeFilter() + { + return 'zlib.inflate'; + } +} diff --git a/inc/mailgun/php-http/message/src/Encoding/DechunkStream.php b/inc/mailgun/php-http/message/src/Encoding/DechunkStream.php new file mode 100644 index 0000000..4cade83 --- /dev/null +++ b/inc/mailgun/php-http/message/src/Encoding/DechunkStream.php @@ -0,0 +1,29 @@ +<?php + +namespace Http\Message\Encoding; + +/** + * Decorate a stream which is chunked. + * + * Allow to decode a chunked stream + * + * @author Joel Wurtz <joel.wurtz@gmail.com> + */ +class DechunkStream extends FilteredStream +{ + /** + * {@inheritdoc} + */ + protected function readFilter() + { + return 'dechunk'; + } + + /** + * {@inheritdoc} + */ + protected function writeFilter() + { + return 'chunk'; + } +} diff --git a/inc/mailgun/php-http/message/src/Encoding/DecompressStream.php b/inc/mailgun/php-http/message/src/Encoding/DecompressStream.php new file mode 100644 index 0000000..4e3a723 --- /dev/null +++ b/inc/mailgun/php-http/message/src/Encoding/DecompressStream.php @@ -0,0 +1,42 @@ +<?php + +namespace Http\Message\Encoding; + +use Psr\Http\Message\StreamInterface; + +/** + * Stream decompress (RFC 1950). + * + * @author Joel Wurtz <joel.wurtz@gmail.com> + */ +class DecompressStream extends FilteredStream +{ + /** + * @param StreamInterface $stream + * @param int $level + */ + public function __construct(StreamInterface $stream, $level = -1) + { + if (!extension_loaded('zlib')) { + throw new \RuntimeException('The zlib extension must be enabled to use this stream'); + } + + parent::__construct($stream, ['window' => 15], ['window' => 15, 'level' => $level]); + } + + /** + * {@inheritdoc} + */ + protected function readFilter() + { + return 'zlib.inflate'; + } + + /** + * {@inheritdoc} + */ + protected function writeFilter() + { + return 'zlib.deflate'; + } +} diff --git a/inc/mailgun/php-http/message/src/Encoding/DeflateStream.php b/inc/mailgun/php-http/message/src/Encoding/DeflateStream.php new file mode 100644 index 0000000..1d7344b --- /dev/null +++ b/inc/mailgun/php-http/message/src/Encoding/DeflateStream.php @@ -0,0 +1,38 @@ +<?php + +namespace Http\Message\Encoding; + +use Psr\Http\Message\StreamInterface; + +/** + * Stream deflate (RFC 1951). + * + * @author Joel Wurtz <joel.wurtz@gmail.com> + */ +class DeflateStream extends FilteredStream +{ + /** + * @param StreamInterface $stream + * @param int $level + */ + public function __construct(StreamInterface $stream, $level = -1) + { + parent::__construct($stream, ['window' => -15, 'level' => $level], ['window' => -15]); + } + + /** + * {@inheritdoc} + */ + protected function readFilter() + { + return 'zlib.deflate'; + } + + /** + * {@inheritdoc} + */ + protected function writeFilter() + { + return 'zlib.inflate'; + } +} diff --git a/inc/mailgun/php-http/message/src/Encoding/Filter/Chunk.php b/inc/mailgun/php-http/message/src/Encoding/Filter/Chunk.php new file mode 100644 index 0000000..0f8f53b --- /dev/null +++ b/inc/mailgun/php-http/message/src/Encoding/Filter/Chunk.php @@ -0,0 +1,30 @@ +<?php + +namespace Http\Message\Encoding\Filter; + +/** + * Userland implementation of the chunk stream filter. + * + * @author Joel Wurtz <joel.wurtz@gmail.com> + */ +class Chunk extends \php_user_filter +{ + /** + * {@inheritdoc} + */ + public function filter($in, $out, &$consumed, $closing) + { + while ($bucket = stream_bucket_make_writeable($in)) { + $lenbucket = stream_bucket_new($this->stream, dechex($bucket->datalen)."\r\n"); + stream_bucket_append($out, $lenbucket); + + $consumed += $bucket->datalen; + stream_bucket_append($out, $bucket); + + $lenbucket = stream_bucket_new($this->stream, "\r\n"); + stream_bucket_append($out, $lenbucket); + } + + return PSFS_PASS_ON; + } +} diff --git a/inc/mailgun/php-http/message/src/Encoding/FilteredStream.php b/inc/mailgun/php-http/message/src/Encoding/FilteredStream.php new file mode 100644 index 0000000..a32554b --- /dev/null +++ b/inc/mailgun/php-http/message/src/Encoding/FilteredStream.php @@ -0,0 +1,198 @@ +<?php + +namespace Http\Message\Encoding; + +use Clue\StreamFilter as Filter; +use Http\Message\Decorator\StreamDecorator; +use Psr\Http\Message\StreamInterface; + +/** + * A filtered stream has a filter for filtering output and a filter for filtering input made to a underlying stream. + * + * @author Joel Wurtz <joel.wurtz@gmail.com> + */ +abstract class FilteredStream implements StreamInterface +{ + const BUFFER_SIZE = 8192; + + use StreamDecorator; + + /** + * @var callable + */ + protected $readFilterCallback; + + /** + * @var resource + * + * @deprecated since version 1.5, will be removed in 2.0 + */ + protected $readFilter; + + /** + * @var callable + * + * @deprecated since version 1.5, will be removed in 2.0 + */ + protected $writeFilterCallback; + + /** + * @var resource + * + * @deprecated since version 1.5, will be removed in 2.0 + */ + protected $writeFilter; + + /** + * Internal buffer. + * + * @var string + */ + protected $buffer = ''; + + /** + * @param StreamInterface $stream + * @param mixed|null $readFilterOptions + * @param mixed|null $writeFilterOptions deprecated since 1.5, will be removed in 2.0 + */ + public function __construct(StreamInterface $stream, $readFilterOptions = null, $writeFilterOptions = null) + { + $this->readFilterCallback = Filter\fun($this->readFilter(), $readFilterOptions); + $this->writeFilterCallback = Filter\fun($this->writeFilter(), $writeFilterOptions); + + if (null !== $writeFilterOptions) { + @trigger_error('The $writeFilterOptions argument is deprecated since version 1.5 and will be removed in 2.0.', E_USER_DEPRECATED); + } + + $this->stream = $stream; + } + + /** + * {@inheritdoc} + */ + public function read($length) + { + if (strlen($this->buffer) >= $length) { + $read = substr($this->buffer, 0, $length); + $this->buffer = substr($this->buffer, $length); + + return $read; + } + + if ($this->stream->eof()) { + $buffer = $this->buffer; + $this->buffer = ''; + + return $buffer; + } + + $read = $this->buffer; + $this->buffer = ''; + $this->fill(); + + return $read.$this->read($length - strlen($read)); + } + + /** + * {@inheritdoc} + */ + public function eof() + { + return $this->stream->eof() && $this->buffer === ''; + } + + /** + * Buffer is filled by reading underlying stream. + * + * Callback is reading once more even if the stream is ended. + * This allow to get last data in the PHP buffer otherwise this + * bug is present : https://bugs.php.net/bug.php?id=48725 + */ + protected function fill() + { + $readFilterCallback = $this->readFilterCallback; + $this->buffer .= $readFilterCallback($this->stream->read(self::BUFFER_SIZE)); + + if ($this->stream->eof()) { + $this->buffer .= $readFilterCallback(); + } + } + + /** + * {@inheritdoc} + */ + public function getContents() + { + $buffer = ''; + + while (!$this->eof()) { + $buf = $this->read(self::BUFFER_SIZE); + // Using a loose equality here to match on '' and false. + if ($buf == null) { + break; + } + + $buffer .= $buf; + } + + return $buffer; + } + + /** + * {@inheritdoc} + */ + public function getSize() + { + return; + } + + /** + * {@inheritdoc} + */ + public function __toString() + { + return $this->getContents(); + } + + /** + * Returns the read filter name. + * + * @return string + * + * @deprecated since version 1.5, will be removed in 2.0 + */ + public function getReadFilter() + { + @trigger_error('The '.__CLASS__.'::'.__METHOD__.' method is deprecated since version 1.5 and will be removed in 2.0.', E_USER_DEPRECATED); + + return $this->readFilter(); + } + + /** + * Returns the write filter name. + * + * @return string + */ + abstract protected function readFilter(); + + /** + * Returns the write filter name. + * + * @return string + * + * @deprecated since version 1.5, will be removed in 2.0 + */ + public function getWriteFilter() + { + @trigger_error('The '.__CLASS__.'::'.__METHOD__.' method is deprecated since version 1.5 and will be removed in 2.0.', E_USER_DEPRECATED); + + return $this->writeFilter(); + } + + /** + * Returns the write filter name. + * + * @return string + */ + abstract protected function writeFilter(); +} diff --git a/inc/mailgun/php-http/message/src/Encoding/GzipDecodeStream.php b/inc/mailgun/php-http/message/src/Encoding/GzipDecodeStream.php new file mode 100644 index 0000000..4f958ed --- /dev/null +++ b/inc/mailgun/php-http/message/src/Encoding/GzipDecodeStream.php @@ -0,0 +1,42 @@ +<?php + +namespace Http\Message\Encoding; + +use Psr\Http\Message\StreamInterface; + +/** + * Stream for decoding from gzip format (RFC 1952). + * + * @author Joel Wurtz <joel.wurtz@gmail.com> + */ +class GzipDecodeStream extends FilteredStream +{ + /** + * @param StreamInterface $stream + * @param int $level + */ + public function __construct(StreamInterface $stream, $level = -1) + { + if (!extension_loaded('zlib')) { + throw new \RuntimeException('The zlib extension must be enabled to use this stream'); + } + + parent::__construct($stream, ['window' => 31], ['window' => 31, 'level' => $level]); + } + + /** + * {@inheritdoc} + */ + protected function readFilter() + { + return 'zlib.inflate'; + } + + /** + * {@inheritdoc} + */ + protected function writeFilter() + { + return 'zlib.deflate'; + } +} diff --git a/inc/mailgun/php-http/message/src/Encoding/GzipEncodeStream.php b/inc/mailgun/php-http/message/src/Encoding/GzipEncodeStream.php new file mode 100644 index 0000000..1066eec --- /dev/null +++ b/inc/mailgun/php-http/message/src/Encoding/GzipEncodeStream.php @@ -0,0 +1,42 @@ +<?php + +namespace Http\Message\Encoding; + +use Psr\Http\Message\StreamInterface; + +/** + * Stream for encoding to gzip format (RFC 1952). + * + * @author Joel Wurtz <joel.wurtz@gmail.com> + */ +class GzipEncodeStream extends FilteredStream +{ + /** + * @param StreamInterface $stream + * @param int $level + */ + public function __construct(StreamInterface $stream, $level = -1) + { + if (!extension_loaded('zlib')) { + throw new \RuntimeException('The zlib extension must be enabled to use this stream'); + } + + parent::__construct($stream, ['window' => 31, 'level' => $level], ['window' => 31]); + } + + /** + * {@inheritdoc} + */ + protected function readFilter() + { + return 'zlib.deflate'; + } + + /** + * {@inheritdoc} + */ + protected function writeFilter() + { + return 'zlib.inflate'; + } +} diff --git a/inc/mailgun/php-http/message/src/Encoding/InflateStream.php b/inc/mailgun/php-http/message/src/Encoding/InflateStream.php new file mode 100644 index 0000000..7070230 --- /dev/null +++ b/inc/mailgun/php-http/message/src/Encoding/InflateStream.php @@ -0,0 +1,42 @@ +<?php + +namespace Http\Message\Encoding; + +use Psr\Http\Message\StreamInterface; + +/** + * Stream inflate (RFC 1951). + * + * @author Joel Wurtz <joel.wurtz@gmail.com> + */ +class InflateStream extends FilteredStream +{ + /** + * @param StreamInterface $stream + * @param int $level + */ + public function __construct(StreamInterface $stream, $level = -1) + { + if (!extension_loaded('zlib')) { + throw new \RuntimeException('The zlib extension must be enabled to use this stream'); + } + + parent::__construct($stream, ['window' => -15], ['window' => -15, 'level' => $level]); + } + + /** + * {@inheritdoc} + */ + protected function readFilter() + { + return 'zlib.inflate'; + } + + /** + * {@inheritdoc} + */ + protected function writeFilter() + { + return 'zlib.deflate'; + } +} |
