<commit_before><?php
/**
 * @namespace
 */
namespace Stdlib;

/**
 * @category Stdlib
 * @package  Stdlib
 * @author   stealth35
 */
class WebSocket
{
    protected $url;
    protected $protocols;
    protected $socket;

    /**
     * @param string $url
     * @param mixed $protocols
     * @throws \ErrorException
     */
    public function __construct($url, $protocols = array())
    {
        $this->url = $url;

        if(is_array($protocols))
        {
            $protocols = implode(', ', $protocols);
        }
        
        $this->protocols = $protocols;

        $url = (object) parse_url($url);

        if(empty($url->port))
        {
            if($url->scheme === 'ws')
            {
                $url->port = 80;
            }

            if($url->scheme === 'wss')
            {
                $url->port = 443;
            }
        }

        if(empty($url->path))
        {
            $url->path = '/';
        }

        $this->socket = @stream_socket_client("$url->host:$url->port", $errno, $errstr);

        if(!$this->socket)
        {
            throw new \ErrorException($errstr, $errno, E_USER_WARNING);
        }

        stream_set_blocking($this->socket, 0);

        $headers = array(
            "GET $url->path HTTP/1.1",
            'Upgrade: WebSocket',
            'Connection: Upgrade',
            "Host: $url->host:$url->port",
            'Origin: http://' . $_SERVER['HTTP_HOST'],
            'Sec-WebSocket-Key1: ' . $this->generateToken(),
            'Sec-WebSocket-Key2: ' . $this->generateToken(),
        );

        if($this->protocols)
        {
            $headers[] = "Sec-WebSocket-Protocol: $this->protocols";
        }<commit_msg>Websocket suport secure option<commit_after><?php
/**
 * @namespace
 */
namespace Stdlib;

/**
 * @category Stdlib
 * @package  Stdlib
 * @author   stealth35
 */
class WebSocket
{
    protected $url;
    protected $protocols;
    protected $socket;

    /**
     * @param string $url
     * @param mixed $protocols
     * @throws \ErrorException
     */
    public function __construct($url, $protocols = array())
    {
        $this->url = $url;

        if(is_array($protocols))
        {
            $protocols = implode(', ', $protocols);
        }
        
        $this->protocols = $protocols;

        $url = (object) parse_url($url);

        if(empty($url->port))
        {
            if($url->scheme === 'ws')
            {
                $url->port = 80;
            }

            if($url->scheme === 'wss')
            {
                $url->port = 443;
            }
        }

        if(empty($url->path))
        {
            $url->path = '/';
        }

        $this->socket = @stream_socket_client("$url->host:$url->port", $errno, $errstr);

        if(!$this->socket)
        {
            throw new \ErrorException($errstr, $errno, E_USER_WARNING);
        }

        if($url->scheme === 'wss')
        {
            stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
        }

        stream_set_blocking($this->socket, 0);

        $headers = array(
            "GET $url->path HTTP/1.1",
            'Upgrade: WebSocket',
            'Connection: Upgrade',
            "Host: $url->host:$url->port",
            'Origin: http://' . $_SERVER['HTTP_HOST'],
            'Sec-WebSocket-Key1: ' . $this->generateToken(),
            'Sec-WebSocket-Key2: ' . $this->generateToken(),
        );

        if($this->protocols)
        {
            $headers[] = "Sec-WebSocket-Protocol: $this->protocols";
        }
{'fraction_non_alphanumeric': 0.16470588235294117, 'fraction_numerical': 0.0067226890756302525, 'mean_word_length': 1.5858073859522086, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '215389', 'n_tokens_mistral': 1124, 'n_tokens_neox': 1076, 'n_words': 182}