unit uMainExample_TPS_HTTPFileServerAddon;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,
  IdCustomTCPServer, IdCustomHTTPServer, IdHTTPServer, IdContext,
  uMain_TPS_HTTPFileServerAddon, Vcl.ExtCtrls, Vcl.StdCtrls;

type
  TfExampleHTTPFileServer = class(TForm)
    IdHTTPServer: TIdHTTPServer;
    bToggleStartStopServer: TButton;
    lTimeOfWorking: TLabel;
    tWorkingTime: TTimer;
    PS_HTTPFileServerAddon1: TPS_HTTPFileServerAddon;
    procedure IdHTTPServerCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo;
      AResponseInfo: TIdHTTPResponseInfo);
    procedure FormCreate(Sender: TObject);
    procedure bToggleStartStopServerClick(Sender: TObject);
    procedure tWorkingTimeTimer(Sender: TObject);
  private
    { Private declarations }

    FStartDateTime:TDateTime;

  public
    { Public declarations }
  end;

var
  fExampleHTTPFileServer: TfExampleHTTPFileServer;

implementation

{$R *.dfm}

procedure TfExampleHTTPFileServer.bToggleStartStopServerClick(Sender: TObject);
begin


if IdHTTPServer.Active then
    begin

    Self.Caption:=Self.Name+' Server Stopped';
    bToggleStartStopServer.Caption:='Start Server';

    idHTTPServer.Active:=false;
    //FIsListening:=false;
    idHTTPServer.StopListening;

    idHTTPServer.Bindings.Clear;

    tWorkingTime.Enabled:=false;

    end else

    begin

    Self.Caption:=Self.Name+' Server Started';
    bToggleStartStopServer.Caption:='Stop Server';

    //idHTTPServer.StartListening;
    idHTTPServer.Active:=true;
    //FIsListening:=True;

    FStartDateTime:=Now();
    tWorkingTime.Enabled:=True;

    end;




end;

procedure TfExampleHTTPFileServer.FormCreate(Sender: TObject);
begin


  idHTTPServer.DefaultPort := 40000;
  idHTTPServer.Active := false;

// Activating Server Here
bToggleStartStopServer.Click;


//IdHTTPServer.Bindings.Add.Port := 80;
//IdHTTPServer.Bindings.Add.Port := 443;
IdHTTPServer.Bindings.Add.Port := 40000;


 PS_HTTPFileServerAddon1.HTTPServerToRequest:=
 //'http:// << no need here
 'localhost:'+idHTTPServer.DefaultPort.ToString();


end;

procedure TfExampleHTTPFileServer.IdHTTPServerCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo;
  AResponseInfo: TIdHTTPResponseInfo);

var ChunksInfo:string;

begin



with PS_HTTPFileServerAddon1.HTTPCommandGet_TPS_HTTPFileServerAddon do begin

   if ARequestInfo.URI='/testConnection' then

    begin

       AResponseInfo.ContentText := 'ok';
       AResponseInfo.ResponseNo := 200;
       AResponseInfo.WriteContent;

    end else


  if ARequestInfo.URI=('/CreateDirOnServer') then
  CreateDirOnServer(AContext,ARequestInfo,AResponseInfo) else

  if ARequestInfo.URI=('/DeleteDirOnServer') then
  DeleteDirOnServer(AContext,ARequestInfo,AResponseInfo) else

  if ARequestInfo.URI=('/SendOneWholeFile') then
  ReceiveOneWholeFile(AContext,ARequestInfo,AResponseInfo) else

  if ARequestInfo.URI=('/JustGatherFileFromChunks') then
  JustGatherFileFromChunks(AContext,ARequestInfo,AResponseInfo) else

  if ARequestInfo.URI=('/CheckChunksIntegrity') then
  CheckChunksIntegrity(AContext,ARequestInfo,AResponseInfo) else

  if ARequestInfo.URI=('/SendChunksInfoFile') then
  ReceiveChunksInfoFile(AContext,ARequestInfo,AResponseInfo,ChunksInfo);



end;



end;

procedure TfExampleHTTPFileServer.tWorkingTimeTimer(Sender: TObject);

var WorkingTime:TDateTime;
begin
WorkingTime:=Now-FStartDateTime;
lTimeOfWorking.Caption:=FormatDateTime('hh:mm:ss',WorkingTime);

end;

end.
