<commit_before>%%%----------------------------------------------------------------------
%%% File    : yaws_server.erl
%%% Author  : <NAME> <<EMAIL>>
%%% Purpose : 
%%% Created : 16 Jan 2002 by <NAME> <<EMAIL>>
%%%----------------------------------------------------------------------

-module(yaws_server).
-author('<EMAIL>').

-compile(export_all).
%%-export([Function/Arity, ...]).

-behaviour(gen_server).

-include_lib("yaws/include/yaws.hrl").
-include_lib("yaws/include/yaws_api.hrl").
-include("yaws_debug.hrl").

-include_lib("kernel/include/file.hrl").

%% External exports
-export([start_link/1]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2]).
-import(filename, [join/1]).
-import(lists, [member/2, foreach/2, map/2, 
		flatten/1, flatmap/2, reverse/1]).

-import(yaws_api, [ehtml_expand/1]).

-record(gs, {gconf,         
	     group,         %% list of #sconf{} s
	     ssl,           %% ssl | nossl
	     l,             %% listen socket
	     mnum = 0,      
	     sessions = 0,  %% number of HTTP sessions
	     reqs = 0}).    %% number of HTTP requests


-record(state, {gc,         %% Global conf #gc{} record
		pairs,      %% [{GservPid, ScList}] 
		mnum = 0,   %% dyn compiled erl module  number
		embedded    %% true if in embedded mode, false otherwise
	       }).


start_link(A) ->
    gen_server:start_link({local, yaws_server}, yaws_server, A, []).

status() ->
    gen_server:call(?MODULE, status).



stats() -> 
    {S, Time} = status(),
    Diff = calendar:time_difference(Time, calendar:local_time()),
    G = fun(L) -> reverse(lists:keysort(2, L)) end,
    R= flatmap(
	 fun({_Pid, SCS}) ->
		 map(
		   fun(SC) ->

			   E = SC#sconf.ets,
			   L = ets:match(E, {{urlc_total, '$1'}, '$2'}),
			   {SC#sconf.servername,  
			    flatten(yaws:fmt_ip(SC#sconf.listen)),
			    G(map(fun(P) -> list_to_tuple(P) end, L))}
		   end, SCS)
	 end, S#state.pairs),
    {Diff, R}.

l2a(L) when list(L) -> list_to_atom(L);
l2a(A) when atom(A) -> A.



%%----------------------------------------------------------------------
%% Func: init/1
%% Returns: {ok, State}          |
%%          {ok, State, Timeout} |
%%          ignore               |
%%          {stop, Reason}
%%----------------------------------------------------------------------

init({Debug, Trace, TraceOut, Conf, RunMod, Embedded}) ->
    process_flag(trap_exit, true),
    put(start_time, calendar:local_time()),  %% for uptime
    case Embedded of 
	false ->
	    Config = yaws_config:load(Conf, Trace, TraceOut, Debug),
	    ?Debug("Config= ~p~n", [Config]),
	    case Config of
		{ok, Gconf, Sconfs} ->
		    erase(logdir),
		    ?Debug("GC = ~p~n", [?format_record(Gconf, gconf)]),
		    ?Debug("SCS: ~p~n", [Sconfs]),
		    yaws_log:setdir(Gconf, Sconfs),
		    case Gconf#gconf.trace of
			{true, What} ->
			    yaws_log:open_trace(What),
			    yaws_api:set_tty_trace(?gc_has_tty_trace(Gconf));
			_ ->
			    ok
		    end,
		    init2(Gconf, Sconfs, RunMod, Embedded, true);
		{error, E} ->
		    case erase(logdir) of
			undefined ->
			    error_logger:error_msg("Yaws: Bad conf: ~p~n",[E]),
			    init:stop(),
			    {stop, E};
			Dir ->
			    yaws_log:setdir(#gconf{logdir = Dir}, []),
			    error_logger:error_msg("Yaws: bad conf: ~s~n",[E]),
			    init:stop(),
			    {stop, E}
		    end
	    end;
	true ->
	    {ok, #state{gc = undefined,
			pairs = [],
			mnum = 0}}
    end.


init2(GC, Sconfs, RunMod, Embedded, FirstTime) ->
    put(gc, GC),
    foreach(
      fun(D) ->
	      yaws_debug:format("Add path ~p~n", [D]),
	      code:add_pathz(D)
      end, GC#gconf.ebin_dir),
    yaws_debug:format("Running with id=~p~n"
		      "Running with debug checks turned on (slower server) ~n"
		      "Logging to directory ~p~n",
		      [GC#gconf.id, GC#gconf.logdir]),
<commit_msg>small embedded fix by <NAME><commit_after>%%%----------------------------------------------------------------------
%%% File    : yaws_server.erl
%%% Author  : <NAME> <<EMAIL>>
%%% Purpose : 
%%% Created : 16 Jan 2002 by <NAME> <<EMAIL>>
%%%----------------------------------------------------------------------

-module(yaws_server).
-author('<EMAIL>').

-compile(export_all).
%%-export([Function/Arity, ...]).

-behaviour(gen_server).

-include_lib("yaws/include/yaws.hrl").
-include_lib("yaws/include/yaws_api.hrl").
-include("yaws_debug.hrl").

-include_lib("kernel/include/file.hrl").

%% External exports
-export([start_link/1]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2]).
-import(filename, [join/1]).
-import(lists, [member/2, foreach/2, map/2, 
		flatten/1, flatmap/2, reverse/1]).

-import(yaws_api, [ehtml_expand/1]).

-record(gs, {gconf,         
	     group,         %% list of #sconf{} s
	     ssl,           %% ssl | nossl
	     l,             %% listen socket
	     mnum = 0,      
	     sessions = 0,  %% number of HTTP sessions
	     reqs = 0}).    %% number of HTTP requests


-record(state, {gc,         %% Global conf #gc{} record
		pairs,      %% [{GservPid, ScList}] 
		mnum = 0,   %% dyn compiled erl module  number
		embedded    %% true if in embedded mode, false otherwise
	       }).


start_link(A) ->
    gen_server:start_link({local, yaws_server}, yaws_server, A, []).

status() ->
    gen_server:call(?MODULE, status).



stats() -> 
    {S, Time} = status(),
    Diff = calendar:time_difference(Time, calendar:local_time()),
    G = fun(L) -> reverse(lists:keysort(2, L)) end,
    R= flatmap(
	 fun({_Pid, SCS}) ->
		 map(
		   fun(SC) ->

			   E = SC#sconf.ets,
			   L = ets:match(E, {{urlc_total, '$1'}, '$2'}),
			   {SC#sconf.servername,  
			    flatten(yaws:fmt_ip(SC#sconf.listen)),
			    G(map(fun(P) -> list_to_tuple(P) end, L))}
		   end, SCS)
	 end, S#state.pairs),
    {Diff, R}.

l2a(L) when list(L) -> list_to_atom(L);
l2a(A) when atom(A) -> A.



%%----------------------------------------------------------------------
%% Func: init/1
%% Returns: {ok, State}          |
%%          {ok, State, Timeout} |
%%          ignore               |
%%          {stop, Reason}
%%----------------------------------------------------------------------

init({Debug, Trace, TraceOut, Conf, RunMod, Embedded}) ->
    process_flag(trap_exit, true),
    put(start_time, calendar:local_time()),  %% for uptime
    case Embedded of 
	false ->
	    Config = yaws_config:load(Conf, Trace, TraceOut, Debug),
	    ?Debug("Config= ~p~n", [Config]),
	    case Config of
		{ok, Gconf, Sconfs} ->
		    erase(logdir),
		    ?Debug("GC = ~p~n", [?format_record(Gconf, gconf)]),
		    ?Debug("SCS: ~p~n", [Sconfs]),
		    yaws_log:setdir(Gconf, Sconfs),
		    case Gconf#gconf.trace of
			{true, What} ->
			    yaws_log:open_trace(What),
			    yaws_api:set_tty_trace(?gc_has_tty_trace(Gconf));
			_ ->
			    ok
		    end,
		    init2(Gconf, Sconfs, RunMod, Embedded, true);
		{error, E} ->
		    case erase(logdir) of
			undefined ->
			    error_logger:error_msg("Yaws: Bad conf: ~p~n",[E]),
			    init:stop(),
			    {stop, E};
			Dir ->
			    yaws_log:setdir(#gconf{logdir = Dir}, []),
			    error_logger:error_msg("Yaws: bad conf: ~s~n",[E]),
			    init:stop(),
			    {stop, E}
		    end
	    end;
	true ->
	    {ok, #state{gc = undefined,
			embedded = Embedded,
			pairs = [],
			mnum = 0}}
    end.


init2(GC, Sconfs, RunMod, Embedded, FirstTime) ->
    put(gc, GC),
    foreach(
      fun(D) ->
	      yaws_debug:format("Add path ~p~n", [D]),
	      code:add_pathz(D)
      end, GC#gconf.ebin_dir),
    yaws_debug:format("Running with id=~p~n"
		      "Running with debug checks turned on (slower server) ~n"
		      "Logging to directory ~p~n",
		      [GC#gconf.id, GC#gconf.logdir]),

{'fraction_non_alphanumeric': 0.24265942310179794, 'fraction_numerical': 0.008537058595265812, 'mean_word_length': 2.0476941269215607, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 58, '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': '5589069', 'n_tokens_mistral': 3045, 'n_tokens_neox': 2727, 'n_words': 566}