<commit_before>%% This module behaves differently depending on a specific header.

-module(stream_handler_h).
-behavior(cowboy_stream).

-export([init/3]).
-export([data/4]).
-export([info/3]).
-export([terminate/3]).
-export([early_error/5]).

%% For switch_protocol.
-export([takeover/7]).

-record(state, {
	pid,
	test
}).

init(StreamID, Req, Opts) ->
	Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, Req))),
	Test = binary_to_atom(cowboy_req:header(<<"x-test-case">>, Req), latin1),
	State = #state{pid=Pid, test=Test},
	Pid ! {Pid, self(), init, StreamID, Req, Opts},
	{init_commands(StreamID, Req, State), State}.

init_commands(_, _, #state{test=crash_in_init}) ->
	error(crash);
init_commands(_, _, #state{test=crash_in_data}) ->
	[];
init_commands(_, _, #state{test=crash_in_info}) ->
	[];
init_commands(_, _, #state{test=crash_in_terminate}) ->
	[{response, 200, #{<<"content-length">> => <<"12">>}, <<"Hello world!">>}, stop];
init_commands(_, _, #state{test=crash_in_early_error}) ->
	error(crash);
init_commands(_, _, State=#state{test=shutdown_on_stream_stop}) ->
	Spawn = init_process(false, State),
	[{headers, 200, #{}}, {spawn, Spawn, 5000}, stop];
init_commands(_, _, State=#state{test=shutdown_on_socket_close}) ->
	Spawn = init_process(false, State),
	[{headers, 200, #{}}, {spawn, Spawn, 5000}];
init_commands(_, _, State=#state{test=shutdown_timeout_on_stream_stop}) ->
	Spawn = init_process(true, State),
	[{headers, 200, #{}}, {spawn, Spawn, 2000}, stop];
init_commands(_, _, State=#state{test=shutdown_timeout_on_socket_close}) ->
	Spawn = init_process(true, State),
	[{headers, 200, #{}}, {spawn, Spawn, 2000}];
init_commands(_, _, State=#state{test=terminate_on_switch_protocol}) ->
	[{switch_protocol, #{}, ?MODULE, State}];
init_commands(_, _, State=#state{test=terminate_on_stop}) ->
	[{response, 204, #{}, <<>>}];<commit_msg>Fix a unused warning<commit_after>%% This module behaves differently depending on a specific header.

-module(stream_handler_h).
-behavior(cowboy_stream).

-export([init/3]).
-export([data/4]).
-export([info/3]).
-export([terminate/3]).
-export([early_error/5]).

%% For switch_protocol.
-export([takeover/7]).

-record(state, {
	pid,
	test
}).

init(StreamID, Req, Opts) ->
	Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, Req))),
	Test = binary_to_atom(cowboy_req:header(<<"x-test-case">>, Req), latin1),
	State = #state{pid=Pid, test=Test},
	Pid ! {Pid, self(), init, StreamID, Req, Opts},
	{init_commands(StreamID, Req, State), State}.

init_commands(_, _, #state{test=crash_in_init}) ->
	error(crash);
init_commands(_, _, #state{test=crash_in_data}) ->
	[];
init_commands(_, _, #state{test=crash_in_info}) ->
	[];
init_commands(_, _, #state{test=crash_in_terminate}) ->
	[{response, 200, #{<<"content-length">> => <<"12">>}, <<"Hello world!">>}, stop];
init_commands(_, _, #state{test=crash_in_early_error}) ->
	error(crash);
init_commands(_, _, State=#state{test=shutdown_on_stream_stop}) ->
	Spawn = init_process(false, State),
	[{headers, 200, #{}}, {spawn, Spawn, 5000}, stop];
init_commands(_, _, State=#state{test=shutdown_on_socket_close}) ->
	Spawn = init_process(false, State),
	[{headers, 200, #{}}, {spawn, Spawn, 5000}];
init_commands(_, _, State=#state{test=shutdown_timeout_on_stream_stop}) ->
	Spawn = init_process(true, State),
	[{headers, 200, #{}}, {spawn, Spawn, 2000}, stop];
init_commands(_, _, State=#state{test=shutdown_timeout_on_socket_close}) ->
	Spawn = init_process(true, State),
	[{headers, 200, #{}}, {spawn, Spawn, 2000}];
init_commands(_, _, State=#state{test=terminate_on_switch_protocol}) ->
	[{switch_protocol, #{}, ?MODULE, State}];
init_commands(_, _, #state{test=terminate_on_stop}) ->
	[{response, 204, #{}, <<>>}];
{'fraction_non_alphanumeric': 0.2157488693801543, 'fraction_numerical': 0.022878425113061984, 'mean_word_length': 7.034188034188034, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 53, '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': '6521136', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1440, 'n_words': 237}