<commit_before><?php /** @noinspection PhpComposerExtensionStubsInspection */

namespace Amp\Http\Websocket;

final class Rfc7692Compression implements CompressionContext
{
    public const DEFAULT_WINDOW_SIZE = 15;

    private const RSV = 0b100;
    private const MINIMUM_LENGTH = 860;
    private const EMPTY_BLOCK = "\x0\x0\xff\xff";

    /**
     * Create a compression context from a header received from a websocket client request.
     *
     * @param string $headerIn Header from request.
     * @param string $headerOut Sec-Websocket-Extension response header.
     *
     * @return self|null
     */
    public static function fromClientHeader(string $headerIn, string &$headerOut): ?self
    {
        return self::fromHeader(true, $headerIn, $headerOut);
    }

    /**
     * Create a compression context from a header received from a websocket server response.
     *
     * @param string $header Header from response.
     *
     * @return self|null
     */
    public static function fromServerHeader(string $header): ?self
    {
        return self::fromHeader(false, $header);
    }

    /**
     * @return string Header value for Sec-Websocket-Extension header.
     */
    public static function createRequestHeader(): string
    {
        return 'permessage-deflate';
    }

    /**
     * @param bool   $isServer True if creating a server context, false if creating a client context.
     * @param string $headerIn Header from request.
     * @param string $headerOut Sec-Websocket-Extension response header.
     *
     * @return self|null
     */
    private static function fromHeader(bool $isServer, string $headerIn, string &$headerOut = null): ?self
    {
        $headerIn = \explode(';', \strtolower($headerIn));
        $headerIn = \array_map('trim', $headerIn);

        if (!\in_array('permessage-deflate', $headerIn, true)) {
            return null;
        }

        $serverWindowSize = self::DEFAULT_WINDOW_SIZE;
        $clientWindowSize = self::DEFAULT_WINDOW_SIZE;
        $serverContextTakeover = true;
        $clientContextTakeover = true;

        $headers = [];
        $headerOut = 'permessage-deflate';

        foreach ($headerIn as $param) {
            $parts = \explode('=', $param, 2);

            if (\in_array($parts[0], $headers, true)) {
                return null; // Repeat params in header.<commit_msg>Allow null for out param<commit_after><?php /** @noinspection PhpComposerExtensionStubsInspection */

namespace Amp\Http\Websocket;

final class Rfc7692Compression implements CompressionContext
{
    public const DEFAULT_WINDOW_SIZE = 15;

    private const RSV = 0b100;
    private const MINIMUM_LENGTH = 860;
    private const EMPTY_BLOCK = "\x0\x0\xff\xff";

    /**
     * Create a compression context from a header received from a websocket client request.
     *
     * @param string $headerIn Header from request.
     * @param string $headerOut Sec-Websocket-Extension response header.
     *
     * @return self|null
     */
    public static function fromClientHeader(string $headerIn, ?string &$headerOut): ?self
    {
        return self::fromHeader(true, $headerIn, $headerOut);
    }

    /**
     * Create a compression context from a header received from a websocket server response.
     *
     * @param string $header Header from response.
     *
     * @return self|null
     */
    public static function fromServerHeader(string $header): ?self
    {
        return self::fromHeader(false, $header);
    }

    /**
     * @return string Header value for Sec-Websocket-Extension header.
     */
    public static function createRequestHeader(): string
    {
        return 'permessage-deflate';
    }

    /**
     * @param bool   $isServer True if creating a server context, false if creating a client context.
     * @param string $headerIn Header from request.
     * @param string $headerOut Sec-Websocket-Extension response header.
     *
     * @return self|null
     */
    private static function fromHeader(bool $isServer, string $headerIn, ?string &$headerOut = null): ?self
    {
        $headerIn = \explode(';', \strtolower($headerIn));
        $headerIn = \array_map('trim', $headerIn);

        if (!\in_array('permessage-deflate', $headerIn, true)) {
            return null;
        }

        $serverWindowSize = self::DEFAULT_WINDOW_SIZE;
        $clientWindowSize = self::DEFAULT_WINDOW_SIZE;
        $serverContextTakeover = true;
        $clientContextTakeover = true;

        $headers = [];
        $headerOut = 'permessage-deflate';

        foreach ($headerIn as $param) {
            $parts = \explode('=', $param, 2);

            if (\in_array($parts[0], $headers, true)) {
                return null; // Repeat params in header.
{'fraction_non_alphanumeric': 0.10752688172043011, 'fraction_numerical': 0.007168458781362007, 'mean_word_length': 2.6576715497301464, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '211099', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1325, 'n_words': 383}