<commit_before>(*
  Copyright 2016, MARS-Curiosity - REST Library

  Home: https://github.com/andrea-magni/MARS
*)
program MARSTemplateServerConsoleApplication;
{$APPTYPE CONSOLE}

{$I MARS.inc}

uses
{$ifdef DelphiXE3_UP}
  System.SysUtils,
  System.Types,
  IPPeerServer,
  IPPeerAPI,
  IdHTTPWebBrokerBridge,
  IdSchedulerOfThreadPool,
  Web.WebReq,
  Web.WebBroker,
{$else}
  SysUtils, StrUtils,
  Types,
  IdHTTPWebBrokerBridge,
  IdSchedulerOfThreadPool,
  WebReq,
  WebBroker,
{$endif}
  ServerConst in 'ServerConst.pas',
  Server.WebModule in 'Server.WebModule.pas' {ServerWebModule: TWebModule},
  Server.Ignition in 'Server.Ignition.pas';

{$R *.res}

procedure StartServer(const AServer: TIdHTTPWebBrokerBridge);
begin
  if not (AServer.Active) then
  begin
    AServer.DefaultPort := TServerEngine.Default.Port;
    Writeln(Format(sStartingServer, [AServer.DefaultPort]));
    AServer.Active := True;
  end
  else
    Writeln(sServerRunning);
  Write(cArrow);
end;

procedure StopServer(const AServer: TIdHTTPWebBrokerBridge);
begin
  if AServer.Active  then
  begin
    Writeln(sStoppingServer);
    AServer.Active := False;
    Writeln(sServerStopped);
  end
  else
    Writeln(sServerNotRunning);
  Write(cArrow);
end;

procedure SetPort(const AServer: TIdHTTPWebBrokerBridge; const APort: string);
var
  LPort: Integer;
  LWasActive: Boolean;
begin
  LPort := StrToIntDef(APort, -1);
  if LPort = -1 then
  begin
    Writeln('Port should be an integer number. Try again.');
    Exit;
  end;

  LWasActive := AServer.Active;
  if LWasActive  then
    StopServer(AServer);
  TServerEngine.Default.Port := LPort;
  if LWasActive then
    StartServer(AServer);
  Writeln(Format(sPortSet, [IntToStr(TServerEngine.Default.Port)]));
  Write(cArrow);
end;

procedure  WriteCommands;
begin
  Writeln(sCommands);
  Write(cArrow);
end;

procedure  WriteStatus(const AServer: TIdHTTPWebBrokerBridge);
begin
  Writeln(sIndyVersion + AServer.SessionList.Version);
  Writeln(sActive + BoolToStr(AServer.Active, True));
  Writeln(sPort + IntToStr(TServerEngine.Default.Port));
  Write(cArrow);
end;

procedure RunServer();
var
  LServer: TIdHTTPWebBrokerBridge;
  LScheduler: TIdSchedulerOfThreadPool;
  LResponse: string;
begin
  WriteCommands;
  LServer := TIdHTTPWebBrokerBridge.Create(nil);
  try
    LServer.DefaultPort := TServerEngine.Default.Port;

    LScheduler := TIdSchedulerOfThreadPool.Create(LServer);
    try
      LScheduler.PoolSize := TServerEngine.Default.ThreadPoolSize;
      LServer.Scheduler := LScheduler;
      LServer.MaxConnections := LScheduler.PoolSize;
    except
      LServer.Scheduler.Free;
      LServer.Scheduler := nil;
      raise;
    end;



    while True do
    begin
      Readln(LResponse);
      LResponse := LowerCase(LResponse);
      if sametext(LResponse, cCommandStart) then
        StartServer(LServer)
      else if sametext(LResponse, cCommandStatus) then
        WriteStatus(LServer)
      else if sametext(LResponse, cCommandStop) then
        StopServer(LServer)
{$ifdef DelphiXE3_UP}
      else if LResponse.StartsWith(cCommandSetPort, True) then
        SetPort(LServer, LResponse.Split([' '])[2])
{$else}
      else if AnsiStartsText(cCommandSetPort, LResponse) then
        SetPort(LServer, Copy(LResponse, Length(cCommandSetPort)+1, MAXINT))
{$endif}

      else if sametext(LResponse, cCommandHelp) then
        WriteCommands
      else if sametext(LResponse, cCommandExit) then
        if LServer.Active then
        begin
          StopServer(LServer);
          break
        end
        else
          break
      else
      begin
        Writeln(sInvalidCommand);
        Write(cArrow);
      end;
    end;
  finally
    LServer.Free;
  end;
end;

begin
  try
  if WebRequestHandler <> nil then
    WebRequestHandler.WebModuleClass := WebModuleClass;
    RunServer();
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end
end.
<commit_msg>* added OnParseAuthentication event handler for Console app deploy mode (caused "Authentication Bearer" scheme to be refused with an hard error from Indy)<commit_after>(*
  Copyright 2016, MARS-Curiosity - REST Library

  Home: https://github.com/andrea-magni/MARS
*)
program MARSTemplateServerConsoleApplication;
{$APPTYPE CONSOLE}

{$I MARS.inc}

uses
{$ifdef DelphiXE3_UP}
  System.SysUtils,
  System.Types,
  IPPeerServer,
  IPPeerAPI,
  IdHTTPWebBrokerBridge,
  IdSchedulerOfThreadPool,
  Web.WebReq,
  Web.WebBroker,
{$else}
  SysUtils, StrUtils,
  Types,
  IdHTTPWebBrokerBridge,
  IdSchedulerOfThreadPool,
  WebReq,
  WebBroker,
{$endif}
  IdContext,
  ServerConst in 'ServerConst.pas',
  Server.WebModule in 'Server.WebModule.pas' {ServerWebModule: TWebModule},
  Server.Ignition in 'Server.Ignition.pas';

{$R *.res}

type
  TDummyIndyServer = class
  public
    procedure ParseAuthenticationHandler(AContext: TIdContext;
      const AAuthType, AAuthData: String; var VUsername, VPassword: String;
      var VHandled: Boolean); virtual;
  end;


procedure StartServer(const AServer: TIdHTTPWebBrokerBridge);
begin
  if not (AServer.Active) then
  begin
    AServer.DefaultPort := TServerEngine.Default.Port;
    Writeln(Format(sStartingServer, [AServer.DefaultPort]));
    AServer.Active := True;
  end
  else
    Writeln(sServerRunning);
  Write(cArrow);
end;

procedure StopServer(const AServer: TIdHTTPWebBrokerBridge);
begin
  if AServer.Active  then
  begin
    Writeln(sStoppingServer);
    AServer.Active := False;
    Writeln(sServerStopped);
  end
  else
    Writeln(sServerNotRunning);
  Write(cArrow);
end;

procedure SetPort(const AServer: TIdHTTPWebBrokerBridge; const APort: string);
var
  LPort: Integer;
  LWasActive: Boolean;
begin
  LPort := StrToIntDef(APort, -1);
  if LPort = -1 then
  begin
    Writeln('Port should be an integer number. Try again.');
    Exit;
  end;

  LWasActive := AServer.Active;
  if LWasActive  then
    StopServer(AServer);
  TServerEngine.Default.Port := LPort;
  if LWasActive then
    StartServer(AServer);
  Writeln(Format(sPortSet, [IntToStr(TServerEngine.Default.Port)]));
  Write(cArrow);
end;

procedure  WriteCommands;
begin
  Writeln(sCommands);
  Write(cArrow);
end;

procedure  WriteStatus(const AServer: TIdHTTPWebBrokerBridge);
begin
  Writeln(sIndyVersion + AServer.SessionList.Version);
  Writeln(sActive + BoolToStr(AServer.Active, True));
  Writeln(sPort + IntToStr(TServerEngine.Default.Port));
  Write(cArrow);
end;

procedure RunServer();
var
  LServer: TIdHTTPWebBrokerBridge;
  LDummyIndy: TDummyIndyServer;
  LScheduler: TIdSchedulerOfThreadPool;
  LResponse: string;
begin
  WriteCommands;
  LDummyIndy := TDummyIndyServer.Create;
  try
    LServer := TIdHTTPWebBrokerBridge.Create(nil);
    try
      LServer.DefaultPort := TServerEngine.Default.Port;

      LScheduler := TIdSchedulerOfThreadPool.Create(LServer);
      try
        LScheduler.PoolSize := TServerEngine.Default.ThreadPoolSize;
        LServer.Scheduler := LScheduler;
        LServer.MaxConnections := LScheduler.PoolSize;
        LServer.OnParseAuthentication := LDummyIndy.ParseAuthenticationHandler;
      except
        LServer.Scheduler.Free;
        LServer.Scheduler := nil;
        raise;
      end;



      while True do
      begin
        Readln(LResponse);
        LResponse := LowerCase(LResponse);
        if sametext(LResponse, cCommandStart) then
          StartServer(LServer)
        else if sametext(LResponse, cCommandStatus) then
          WriteStatus(LServer)
        else if sametext(LResponse, cCommandStop) then
          StopServer(LServer)
  {$ifdef DelphiXE3_UP}
        else if LResponse.StartsWith(cCommandSetPort, True) then
          SetPort(LServer, LResponse.Split([' '])[2])
  {$else}
        else if AnsiStartsText(cCommandSetPort, LResponse) then
          SetPort(LServer, Copy(LResponse, Length(cCommandSetPort)+1, MAXINT))
  {$endif}

        else if sametext(LResponse, cCommandHelp) then
          WriteCommands
        else if sametext(LResponse, cCommandExit) then
          if LServer.Active then
          begin
            StopServer(LServer);
            break
          end
          else
            break
        else
        begin
          Writeln(sInvalidCommand);
          Write(cArrow);
        end;
      end;
    finally
      LServer.Free;
    end;
  finally
    LDummyIndy.Free;
  end;
end;

{ TDummyIndyServer }

procedure TDummyIndyServer.ParseAuthenticationHandler(AContext: TIdContext;
  const AAuthType, AAuthData: String; var VUsername, VPassword: String;
  var VHandled: Boolean);
begin
  // Allow JWT Bearer authentication's scheme
  if SameText(AAuthType, 'Bearer') then
    VHandled := True;
end;

begin
  try
  if WebRequestHandler <> nil then
    WebRequestHandler.WebModuleClass := WebModuleClass;
    RunServer();
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end
end.

{'fraction_non_alphanumeric': 0.09409906352250931, 'fraction_numerical': 0.002256572266726842, 'mean_word_length': 3.5737874097007225, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '675536', 'n_tokens_mistral': 2957, 'n_tokens_neox': 2890, 'n_words': 635}