<commit_before>-module(boss_mochicow_handler).
-behaviour(cowboy_websocket_handler).

-export([init/3, loop/1, terminate/2]).
-export([websocket_init/3, websocket_handle/3,
	 websocket_info/3, websocket_terminate/3]).

init({_Any, http}, Req, _Opts) ->
    case cowboy_req:header(<<"upgrade">>, Req) of
	{undefined, _Req2} -> {upgrade, protocol, mochicow_upgrade};
	{<<"websocket">>, _Req2} -> {upgrade, protocol, cowboy_websocket};
	{<<"Websocket">>, _Req2} -> {upgrade, protocol, cowboy_websocket};
	{<<"WebSocket">>, _Req2} -> {upgrade, protocol, cowboy_websocket}
    end.

-record(state, {websocket_id, session_id, service_url}).

loop(Req) ->
    boss_web_controller:handle_request(Req, mochiweb_request_bridge, mochiweb_response_bridge).

terminate(_Req, _State) ->
    ok.

websocket_init(_Any, Req, _Opts) ->
    SessionKey = boss_env:get_env(session_key, "_boss_session"),
    {ServiceUrl, _Req1} = cowboy_req:path(Req),
    {SessionId, _Req2}  = cowboy_req:cookie(list_to_binary(SessionKey), Req),
    WebsocketId = self(),    
    State= #state{websocket_id=WebsocketId, 
		  session_id=SessionId,
		  service_url=ServiceUrl},
    boss_websocket_router:join(ServiceUrl, WebsocketId, Req, SessionId),
    {ok, Req, State, hibernate}.

websocket_handle({text, Msg}, Req, State) ->
    #state{websocket_id=WebsocketId, 
	   session_id=SessionId, 
	   service_url=ServiceUrl } = State,
    boss_websocket_router:incoming(ServiceUrl, WebsocketId, Req, SessionId, Msg),
    {ok, Req, State, hibernate};

websocket_handle(_Any, Req, State) ->
    {ok, Req, State}.

websocket_info({text, Msg}, Req, State) ->
    {reply, {text, Msg}, Req, State};

websocket_info(_Info, Req, State) ->
    {ok, Req, State, hibernate}.

websocket_terminate(_Reason, Req, State) ->
    #state{websocket_id=WebsocketId, 
	   session_id=SessionId, 
	   service_url=ServiceUrl } = State,
    boss_websocket_router:close(ServiceUrl, WebsocketId, Req, SessionId),
    ok.<commit_msg>add websocket global timeout configuration<commit_after>-module(boss_mochicow_handler).
-behaviour(cowboy_websocket_handler).

-export([init/3, loop/1, terminate/2]).
-export([websocket_init/3, websocket_handle/3,
	 websocket_info/3, websocket_terminate/3]).

init({_Any, http}, Req, _Opts) ->
    case cowboy_req:header(<<"upgrade">>, Req) of
	{undefined, _Req2} -> {upgrade, protocol, mochicow_upgrade};
	{<<"websocket">>, _Req2} -> {upgrade, protocol, cowboy_websocket};
	{<<"Websocket">>, _Req2} -> {upgrade, protocol, cowboy_websocket};
	{<<"WebSocket">>, _Req2} -> {upgrade, protocol, cowboy_websocket}
    end.

-record(state, {websocket_id, session_id, service_url}).

loop(Req) ->
    boss_web_controller:handle_request(Req, mochiweb_request_bridge, mochiweb_response_bridge).

terminate(_Req, _State) ->
    ok.

websocket_init(_Any, Req, _Opts) ->
    SessionKey = boss_env:get_env(session_key, "_boss_session"),
    {ServiceUrl, _Req1} = cowboy_req:path(Req),
    {SessionId, _Req2}  = cowboy_req:cookie(list_to_binary(SessionKey), Req),
    WebsocketId = self(),    
    State= #state{websocket_id=WebsocketId, 
		  session_id=SessionId,
		  service_url=ServiceUrl},
    boss_websocket_router:join(ServiceUrl, WebsocketId, Req, SessionId),
    case boss_env:get_env(websocket_timeout, undefined) of
        undefined ->
            {ok, Req, State, hibernate};
        Timeout ->
            {ok, Req, State, Timeout, hibernate}
    end.

websocket_handle({text, Msg}, Req, State) ->
    #state{websocket_id=WebsocketId, 
	   session_id=SessionId, 
	   service_url=ServiceUrl } = State,
    boss_websocket_router:incoming(ServiceUrl, WebsocketId, Req, SessionId, Msg),
    {ok, Req, State, hibernate};

websocket_handle(_Any, Req, State) ->
    {ok, Req, State}.

websocket_info({text, Msg}, Req, State) ->
    {reply, {text, Msg}, Req, State};

websocket_info(_Info, Req, State) ->
    {ok, Req, State, hibernate}.

websocket_terminate(_Reason, Req, State) ->
    #state{websocket_id=WebsocketId, 
	   session_id=SessionId, 
	   service_url=ServiceUrl } = State,
    boss_websocket_router:close(ServiceUrl, WebsocketId, Req, SessionId),
    ok.
{'fraction_non_alphanumeric': 0.14677341096555072, 'fraction_numerical': 0.006307617661329452, 'mean_word_length': 4.932374100719424, 'pattern_counts': {'":': 0, '<': 19, '<?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': '2118236', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1533, 'n_words': 269}