<commit_before>%% @author <NAME> <<EMAIL>>
%% @copyright 2012 <NAME>, 2011, <NAME> <<EMAIL>>
%% @doc WebSocket draft-ietf-hybi-thewebsocketprotocol-17 (Chrome 16)

%% Major parts of the protocol handling are copied from Cowboy.
%% https://github.com/extend/cowboy/blob/master/src/cowboy_http_websocket.erl
%% Cowboy is Copyright (c) 2011, <NAME> <<EMAIL>>
%% And released under the MIT license.

%% Copyright 2012 <NAME>
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%% 
%%     http://www.apache.org/licenses/LICENSE-2.0
%% 
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.

-module(z_websocket_hybi17).
-author("<NAME> <<EMAIL>>").

-export([
    start/2,
    
    handle_data/3,
    receive_loop/3,
    send_loop/2
]).

-include_lib("zotonic.hrl").

% Send a ping every 30 seconds.
-define(PING_TIMEOUT, 30000).


start(ReqData, Context) ->
    Socket = webmachine_request:socket(ReqData),
    WsKey = z_string:trim(z_context:get_req_header("sec-websocket-key", Context)),
    Accept = base64:encode(crypto:sha(WsKey++"258EAFA5-E914-47DA-95CA-C5AB0DC85B11")),

    %% Send the handshake
    Data = ["HTTP/1.1 101 Switching Protocols", 13, 10,
            "Upgrade: websocket", 13, 10,
            "Connection: Upgrade", 13, 10,
            "Sec-WebSocket-Accept: ", Accept, 13, 10,
            "Sec-WebSocket-Protocol: zotonic", 13, 10,<commit_msg>Removed websocket response header to conform to the latest proposed websocket protocol standard (see http://tools.ietf.org/html/rfc6455#section-4.2.2, December 2011) (cherry picked from commit ce01f70e20404f54ae36f0d971f6f9e75269bebf)<commit_after>%% @author <NAME> <<EMAIL>>
%% @copyright 2012 <NAME>, 2011, <NAME> <<EMAIL>>
%% @doc WebSocket draft-ietf-hybi-thewebsocketprotocol-17 (Chrome 16)

%% Major parts of the protocol handling are copied from Cowboy.
%% https://github.com/extend/cowboy/blob/master/src/cowboy_http_websocket.erl
%% Cowboy is Copyright (c) 2011, <NAME> <<EMAIL>>
%% And released under the MIT license.

%% Copyright 2012 <NAME>
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%% 
%%     http://www.apache.org/licenses/LICENSE-2.0
%% 
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.

-module(z_websocket_hybi17).
-author("<NAME> <<EMAIL>>").

-export([
    start/2,
    
    handle_data/3,
    receive_loop/3,
    send_loop/2
]).

-include_lib("zotonic.hrl").

% Send a ping every 30 seconds.
-define(PING_TIMEOUT, 30000).


start(ReqData, Context) ->
    Socket = webmachine_request:socket(ReqData),
    WsKey = z_string:trim(z_context:get_req_header("sec-websocket-key", Context)),
    Accept = base64:encode(crypto:sha(WsKey++"258EAFA5-E914-47DA-95CA-C5AB0DC85B11")),

    %% Send the handshake
    Data = ["HTTP/1.1 101 Switching Protocols", 13, 10,
            "Upgrade: websocket", 13, 10,
            "Connection: Upgrade", 13, 10,
            "Sec-WebSocket-Accept: ", Accept, 13, 10,
{'fraction_non_alphanumeric': 0.13152173913043477, 'fraction_numerical': 0.052445652173913046, 'mean_word_length': 4.021828103683492, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '5036763', 'n_tokens_mistral': 1318, 'n_tokens_neox': 1167, 'n_words': 360}