protected function processChunk(string $chunk) : string { if ($chunk === '') { return \inflate_add($this->context, $chunk, \ZLIB_FINISH); } return \inflate_add($this->context, $chunk, \ZLIB_SYNC_FLUSH); }
/** * {@inheritdoc} * * @throws \Icicle\Http\Exception\MessageException If compressed message body exceeds the max length or if decoding * the compressed stream fails. */ protected function send(string $data, float $timeout = 0, bool $end = false) : \Generator { if (false === ($data = inflate_add($this->resource, $data, $end ? \ZLIB_FINISH : \ZLIB_SYNC_FLUSH))) { throw new FailureException('Failed adding date to inflate stream.'); } return yield from parent::send($data, $timeout, $end); }
/** * {@inheritdoc} * @throws \RuntimeException if zlib initialization error occurs * @throws \InvalidArgumentException if an error occurs while decoding (most likely invalid input) */ public function filter($value) { $context = inflate_init(ZLIB_ENCODING_DEFLATE); if (!$context) { // @codeCoverageIgnoreStart throw new \RuntimeException('Could not create inflate context'); // @codeCoverageIgnoreEnd } $output = @inflate_add($context, $value); if ($output === false) { throw new \InvalidArgumentException('Input does not appear to be a zlib stream'); } return $output; }
function inflateStream($mode, $flushSize) { $buffer = ""; $inflated = null; $resource = inflate_init($mode); while (true) { $dataToInflate = (yield $inflated); if (isset($dataToInflate)) { $buffer .= $dataToInflate; if (strlen($buffer) >= $flushSize) { $inflated = inflate_add($resource, $buffer); $buffer = ""; } else { $inflated = null; } } else { $inflated = inflate_add($resource, $buffer, ZLIB_FINISH); } } }
/** * {@inheritdoc} */ public function close() { set_error_handler($this->handleError()); try { $chunk = inflate_add($this->context, $this->buffer, ZLIB_FINISH); } finally { $this->buffer = ''; restore_error_handler(); } if ($this->stream !== NULL) { $this->stream->write($chunk); parent::close(); } }
<?php $resource = inflate_init(ZLIB_ENCODING_GZIP); $uncompressed = implode(range("a", "z")); $compressed = gzencode($uncompressed); $inflated = ""; for ($i = 0; $i < strlen($compressed); $i++) { $inflated .= inflate_add($resource, $compressed[$i]); } $inflated .= inflate_add($resource, "", ZLIB_FINISH); assert($inflated === $uncompressed); // Now reuse the existing resource after finishing the previous operations ... $inflated = ""; for ($i = 0; $i < strlen($compressed); $i++) { $inflated .= inflate_add($resource, $compressed[$i]); } $inflated .= inflate_add($resource, "", ZLIB_FINISH); assert($inflated === $uncompressed); ?> ===DONE===
<?php $dict = range("a", "z"); $r = deflate_init(ZLIB_ENCODING_DEFLATE, ["dictionary" => $dict]); $a = deflate_add($r, "abdcde", ZLIB_FINISH); var_dump($a); $r = deflate_init(ZLIB_ENCODING_DEFLATE, ["dictionary" => implode("", $dict) . ""]); $dictStr_a = deflate_add($r, "abdcde", ZLIB_FINISH); var_dump($dictStr_a === $a); $r = inflate_init(ZLIB_ENCODING_DEFLATE, ["dictionary" => $dict]); var_dump(inflate_add($r, $a, ZLIB_FINISH)); $r = inflate_init(ZLIB_ENCODING_DEFLATE, ["dictionary" => ["8"] + range("a", "z")]); var_dump(inflate_add($r, $a, ZLIB_FINISH));
<?php $badResource = fopen("php://memory", "r+"); var_dump(inflate_add($badResource, "test")); $resource = inflate_init(ZLIB_ENCODING_DEFLATE); $badFlushType = 6789; var_dump(inflate_add($resource, "test", $badFlushType));
public function __invoke(Request $req, Response $res) { $headers = $req->getAllHeaders(); unset($headers["accept-encoding"]); $connection = $headers["connection"]; unset($headers["connection"]); foreach ($connection as $value) { foreach (explode(",", strtolower($value)) as $type) { $type = trim($type); if ($type == "upgrade") { $headers["connection"][0] = "upgrade"; } else { unset($headers[$type]); } } } if ($this->headers) { if (is_callable($this->headers)) { $headers = ($this->headers)($headers); } else { $headers = $this->headers + $headers; } } $promise = $this->client->request((new \Amp\Artax\Request())->setMethod($req->getMethod())->setUri($this->target . $req->getUri())->setAllHeaders($headers)->setBody((yield $req->getBody()))); // no async sending possible :-( [because of redirects] $promise->watch(function ($update) use($req, $res, &$hasBody, &$status, &$zlib) { list($type, $data) = $update; if ($type == Notify::RESPONSE_HEADERS) { $headers = array_change_key_case($data["headers"], CASE_LOWER); foreach ($data["headers"] as $header => $values) { foreach ($values as $value) { $res->addHeader($header, $value); } } $res->setStatus($status = $data["status"]); $res->setReason($data["reason"]); if (isset($headers["content-encoding"]) && strcasecmp(trim(current($headers["content-encoding"])), 'gzip') === 0) { $zlib = inflate_init(ZLIB_ENCODING_GZIP); } $hasBody = true; } if ($type == Notify::RESPONSE_BODY_DATA) { if ($zlib) { $data = inflate_add($zlib, $data); } $res->stream($data); } if ($type == Notify::RESPONSE) { if (!$hasBody) { foreach ($data->getAllHeaders() as $header => $values) { foreach ($values as $value) { $res->addHeader($header, $value); } } $res->setStatus($status = $data->getStatus()); $res->setReason($data->getReason()); } if ($status == 101) { $req->setLocalVar("aerys.reverse.socket", $update["export_socket"]()); } $res->end($zlib ? inflate_add("", ZLIB_FINISH) : null); } }); (yield $promise); }