<commit_before><?php
namespace Laravoole\Wrapper;

use swoole_websocket_server;
use swoole_http_request;
use swoole_http_response;

use Laravoole\Request;
use Laravoole\Response;

class SwooleWebSocketWrapper extends SwooleHttpWrapper implements ServerInterface
{
    public function __construct($host, $port)
    {
        $this->server = new swoole_websocket_server($host, $port);
    }

    protected $connections = [];
    protected $unfinished = [];
    protected $protocolCodecs = [
        'json' => SwooleWebSocketCodecJson::class,
    ];

    public static function getDefaults()
    {
        return array_merge(parent::getDefaults(), ['LARAVOOLE_WEBSOCKET_CLOSE_CALLBACK' => null]);
    }

    public function start()
    {
        if (!empty($this->settings)) {
            $this->server->set($this->settings);
        }

        $this->server->on('Start', [$this, 'onServerStart']);
        $this->server->on('Shutdown', [$this, 'onServerShutdown']);
        $this->server->on('WorkerStart', [$this, 'onWorkerStart']);
        $this->server->on('HandShake', [$this, 'onHandShake']);
        // $this->server->on('Open', [$this, 'onOpen']);
        $this->server->on('Message', [$this, 'onMessage']);
        $this->server->on('Close', [$this, 'onClose']);

        $this->server->start();
    }

    public function onHandShake(swoole_http_request $request, swoole_http_response $response)
    {
        $protocol = 'json';
        if (isset($request->header['sec-websocket-protocol'])) {
            $protocols = $request->header['sec-websocket-protocol'];
            $protocols = array_intersect(preg_split('~,\s*~', $protocols), array_keys($this->protocolCodecs));
            if (!empty($protocols)) {
                $protocol = $protocols[0];
                $response->header('Sec-WebSocket-Protocol', $protocol);
            }
        }

        $secKey = $request->header['sec-websocket-key'];
        $secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));

        foreach ([
            "Upgrade" => "websocket",
            "Connection" => "Upgrade",
            "Sec-WebSocket-Version" => "13",
            "Sec-WebSocket-Accept" => $secAccept,
        ] as $k => $v) {
            $response->header($k, $v);
        }
        $response->status(101);

        $laravooleRequest = new Request($request->fd);
        foreach ($request as $k => $v) {
            $laravooleRequest->$k = $v;
        }
        $this->connections[$request->fd] = ['request' => $laravooleRequest, 'protocol' => $this->protocolCodecs[$protocol]];
        $this->unfinished[$request->fd] = '';
        return true;

    }

    public function onMessage(swoole_websocket_server $server, $frame)
    {
        if (!isset($this->unfinished[$frame->fd])) {
            return false;
        }
        if (isset($this->connections[$frame->fd]['request']->laravooleInfo->nextMessageRoute)) {
            $request = $this->connections[$frame->fd]['request'];
            $route = $request->laravooleInfo->nextMessageRoute;
            $data['method'] = $route['method'];
            $data['params'] = $route['params'];
            $data['params']['_laravoole_raw'] = $frame->data;
            $data['params']['_laravoole_previous'] = $route['previous'];

            $data['echo'] = $request->echo;
            if ($frame->finish) {
                unset($request->laravooleInfo->nextMessageRoute);
            }
            return $this->dispatch($server, $frame->fd, $data);

        } else {
            $this->unfinished[$frame->fd] .= $frame->data;
        }

        if (!$frame->finish) {
            return;
        }
        $data = $this->connections[$frame->fd]['protocol']::decode($this->unfinished[$frame->fd]);

        $this->unfinished[$frame->fd] = '';

        return $this->dispatch($server, $frame->fd, $data);

    }

    protected function dispatch($server, $fd, $data)
    {
        $request = $this->connections[$fd]['request'];

        $request->method = $request->server['request_uri'] = $data['method'];
        $request->get = (array) ($data['params']);
        $request->echo = isset($data['echo']) ? $data['echo'] : null;
        $request = $this->ucHeaders($request);

        $response = new Response($this, $request);

        $illuminateRequest = $this->dealWithRequest($request);

        if (isset($request->laravooleInfo)) {
            $illuminateRequest->setLaravooleInfo($request->laravooleInfo);
        } else {
            $illuminateRequest->setLaravooleInfo((object) [
                'fd' => $fd,
                'server' => $server,
            ]);
        }

        $this->onRequest($request, $response, $illuminateRequest);

        $request->laravooleInfo = $illuminateRequest->getLaravooleInfo();
    }

    public function endResponse($response, $content)
    {
        $data = $this->connections[$response->request->fd]['protocol']::encode(
            $response->http_status,
            $response->request->method,
            $content,
            $response->request->echo
        );
        $this->server->push($response->request->fd, $data);
<commit_msg>also handle http request<commit_after><?php
namespace Laravoole\Wrapper;

use swoole_websocket_server;
use swoole_http_request;
use swoole_http_response;

use Laravoole\Request;
use Laravoole\Response;

class SwooleWebSocketWrapper extends SwooleHttpWrapper implements ServerInterface
{
    public function __construct($host, $port)
    {
        $this->server = new swoole_websocket_server($host, $port);
    }

    protected $connections = [];
    protected $unfinished = [];
    protected $protocolCodecs = [
        'json' => SwooleWebSocketCodecJson::class,
    ];

    public static function getDefaults()
    {
        return array_merge(parent::getDefaults(), ['LARAVOOLE_WEBSOCKET_CLOSE_CALLBACK' => null]);
    }

    public function start()
    {
        if (!empty($this->settings)) {
            $this->server->set($this->settings);
        }

        $this->server->on('Start', [$this, 'onServerStart']);
        $this->server->on('Shutdown', [$this, 'onServerShutdown']);
        $this->server->on('WorkerStart', [$this, 'onWorkerStart']);
        $this->server->on('HandShake', [$this, 'onHandShake']);
        $this->server->on('Message', [$this, 'onMessage']);
        $this->server->on('Request', [$this, 'onRequest']);
        $this->server->on('Close', [$this, 'onClose']);

        $this->server->start();
    }

    public function onHandShake(swoole_http_request $request, swoole_http_response $response)
    {
        $protocol = 'json';
        if (isset($request->header['sec-websocket-protocol'])) {
            $protocols = $request->header['sec-websocket-protocol'];
            $protocols = array_intersect(preg_split('~,\s*~', $protocols), array_keys($this->protocolCodecs));
            if (!empty($protocols)) {
                $protocol = $protocols[0];
                $response->header('Sec-WebSocket-Protocol', $protocol);
            }
        }

        $secKey = $request->header['sec-websocket-key'];
        $secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));

        foreach ([
            "Upgrade" => "websocket",
            "Connection" => "Upgrade",
            "Sec-WebSocket-Version" => "13",
            "Sec-WebSocket-Accept" => $secAccept,
        ] as $k => $v) {
            $response->header($k, $v);
        }
        $response->status(101);

        $laravooleRequest = new Request($request->fd);
        foreach ($request as $k => $v) {
            $laravooleRequest->$k = $v;
        }
        $this->connections[$request->fd] = ['request' => $laravooleRequest, 'protocol' => $this->protocolCodecs[$protocol]];
        $this->unfinished[$request->fd] = '';
        return true;

    }

    public function onMessage(swoole_websocket_server $server, $frame)
    {
        if (!isset($this->unfinished[$frame->fd])) {
            return false;
        }
        if (isset($this->connections[$frame->fd]['request']->laravooleInfo->nextMessageRoute)) {
            $request = $this->connections[$frame->fd]['request'];
            $route = $request->laravooleInfo->nextMessageRoute;
            $data['method'] = $route['method'];
            $data['params'] = $route['params'];
            $data['params']['_laravoole_raw'] = $frame->data;
            $data['params']['_laravoole_previous'] = $route['previous'];

            $data['echo'] = $request->echo;
            if ($frame->finish) {
                unset($request->laravooleInfo->nextMessageRoute);
            }
            return $this->dispatch($server, $frame->fd, $data);

        } else {
            $this->unfinished[$frame->fd] .= $frame->data;
        }

        if (!$frame->finish) {
            return;
        }
        $data = $this->connections[$frame->fd]['protocol']::decode($this->unfinished[$frame->fd]);

        $this->unfinished[$frame->fd] = '';

        return $this->dispatch($server, $frame->fd, $data);

    }

    protected function dispatch($server, $fd, $data)
    {
        $request = $this->connections[$fd]['request'];

        $request->method = $request->server['request_uri'] = $data['method'];
        $request->get = (array) ($data['params']);
        $request->echo = isset($data['echo']) ? $data['echo'] : null;
        $request = $this->ucHeaders($request);

        $response = new Response($this, $request);

        $illuminateRequest = $this->dealWithRequest($request);

        if (isset($request->laravooleInfo)) {
            $illuminateRequest->setLaravooleInfo($request->laravooleInfo);
        } else {
            $illuminateRequest->setLaravooleInfo((object) [
                'fd' => $fd,
                'server' => $server,
            ]);
        }

        $this->onRequest($request, $response, $illuminateRequest);

        $request->laravooleInfo = $illuminateRequest->getLaravooleInfo();
    }

    public function endResponse($response, $content)
    {
        if(isset($response->request)) {
            // This is a websocket request
            $data = $this->connections[$response->request->fd]['protocol']::encode(
                $response->http_status,
                $response->request->method,
                $content,
                $response->request->echo
            );
            $this->server->push($response->request->fd, $data);
        } else {
            // This is a http request
            parent::endResponse($response, $content);
        }

{'fraction_non_alphanumeric': 0.17744688922610016, 'fraction_numerical': 0.004931714719271623, 'mean_word_length': 2.5196929238985315, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 208, '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': '2477972', 'n_tokens_mistral': 3157, 'n_tokens_neox': 3044, 'n_words': 473}