//Advanced Delphi Systems Code: ads_WebRequest
{{Copyright(c)2016 Advanced Delphi Systems

 Richard Maley
  Advanced Delphi Systems
  12613 Maidens Bower Drive
  Potomac, MD 20854 USA
  phone 301-840-1554
  dickmaley@advdelphisys.com

  The code herein can be used or modified by anyone.  Please retain references
  to Richard Maley at Advanced Delphi Systems.  If you make improvements to the
  code please send your improvements to dickmaley@advdelphisys.com so that the
  entire Delphi community can benefit.  All comments are welcome.
}
unit ads_WebRequest;

(*
UnitIndex Master Index Implementation Section Download Units
Description: ads_WebRequest.pas
This unit contains the following routines.

BitmapToGif   DeviceTypeToContentType   DeviceTypeToFileExtension   MakeHTMLPageNav   SetWebRequest   WebGetResponse   WebPostResponse   WebPostResponse_Default   WebPostResponse_HTML   WebPostResponse_MultiFile   WebPostResponse_PDF   WebPostResponse_SingleFile  WebPostResponse_TIF   WebPostResponse_XLS   WebResponseDeleteFile  

*)
interface
Uses
  Windows,
  SysUtils,
  Classes,
  httpapp
  ;


Var
  GlobalReportsPath : String;
  GlobalCachePath   : String;
  GlobalExePath     : String;
  GlobalExeName     : String;


Function  BitmapToGif(BitmapFile,GifFile:String;DeleteBitmap: Boolean): Boolean;
Function  MakeHTMLPageNav(PageNum,PageCount: Integer): String;
Function  DeviceTypeToContentType(DeviceType: String): String;
Function  DeviceTypeToFileExtension(DeviceType: String): String;
procedure SetWebRequest(Request: TWebRequest);
Procedure WebGetResponse(var Response : TWebResponse);
Procedure WebPostResponse(var Response:TWebResponse;OutFile,DeviceType:String);
Procedure WebPostResponse_Default(var Response:TWebResponse;OutFile,DeviceType:String);
Procedure WebPostResponse_HTML(var Response:TWebResponse;OutFile,DeviceType:String);
Procedure WebPostResponse_MultiFile(var Response:TWebResponse;OutFile:String);
Procedure WebPostResponse_PDF(var Response:TWebResponse;OutFile:String);
Procedure WebPostResponse_SingleFile(var Response:TWebResponse;OutFile,Caption:String);
Procedure WebPostResponse_TIF(var Response:TWebResponse;OutFile:String);
Procedure WebPostResponse_XLS(var Response:TWebResponse;OutFile:String);
Procedure WebResponseDeleteFile(OutFile:String);

Var
  WebRequest      : TStringList;
  WebRequestIsSet : Boolean;


implementation


uses
  ads_Exception,
  ads_File,
  FileCtrl,
  Forms,
  GifImage,
  Graphics
  ;
Var
  UnitName : String = 'ads_WebRequest';
  ProcName : String = 'Unknown';

//
Unit Description UnitIndex Master Index
Procedure SetWebRequest(Request: TWebRequest);
Var
  lst       : TStringList;
  sgTemp    : String;
  sgTemp2   : String;
  inCounter : Integer;
begin
  ProcName  := 'PopulateRequest'; Try
  //TimeTest_ads.Start(ProcName);
  lst       := TStringList.Create();
  Try
    sgTemp  := '';
    sgTemp2 := '';
    lst.Clear;
    //Request.Accept
    sgTemp := sgTemp+'Request.Accept='+Request.Accept+#13;

    //Request.Authorization
    sgTemp := sgTemp+'Request.Authorization='+Request.Authorization+#13;

    //Request.CacheControl
    sgTemp := sgTemp+'Request.CacheControl='+Request.CacheControl+#13;

    //Request.Connection
    sgTemp := sgTemp+'Request.Connection='+Request.Connection+#13;

    //Request.Content
    lst.Clear;
    lst.SetText(PChar(Request.content));
    For inCounter := 0 To lst.Count - 1 Do
    Begin
      lst[inCounter] := 'Request.Content['+IntToStr(inCounter)+']="'+lst[inCounter]+'"';
    End;
    lst.Add('Request.Content.Count='+IntToStr(lst.Count));
    sgTemp := sgTemp+lst.Text;
    lst.Clear;
    lst.SetText(PChar(Request.content));
    For inCounter := 0 To lst.Count - 1 Do
    Begin
      lst[inCounter] := 'Request.Content.Item.'+lst[inCounter];
    End;
    sgTemp := sgTemp+lst.Text;

    //Request.ContentEncoding
    sgTemp := sgTemp+'Request.ContentEncoding='+Request.ContentEncoding+#13;

    //Request.ContentFields
    lst.Clear;
    Request.ExtractContentFields(lst);
    sgTemp2 := lst.Text;
    For inCounter := 0 To lst.Count - 1 Do
    Begin
      lst[inCounter] := 'Request.ContentFields['+IntToStr(inCounter)+']="'+lst[inCounter]+'"';
    End;
    lst.Add('Request.ContentFields.Count='+IntToStr(lst.Count));
    sgTemp := sgTemp+lst.Text;
    lst.Clear;
    lst.SetText(Pchar(sgTemp2));
    For inCounter := 0 To lst.Count - 1 Do
    Begin
      lst[inCounter] := 'Request.ContentFields.Item.'+lst[inCounter];
    End;
    sgTemp := sgTemp+lst.Text;

    //Request.ContentLength
    sgTemp := sgTemp+'Request.ContentLength='+IntToStr(Request.ContentLength)+#13;

    //Request.ContentType
    sgTemp := sgTemp+'Request.ContentType='+Request.ContentType+#13;

    //Request.ContentVersion
    sgTemp := sgTemp+'Request.ContentVersion='+Request.ContentVersion+#13;

    //Request.Cookie
    lst.Clear;
    Request.ExtractCookieFields(lst);
    sgTemp2 := lst.Text;
    For inCounter := 0 To lst.Count - 1 Do
    Begin
      lst[inCounter] := 'Request.Cookie['+IntToStr(inCounter)+']="'+lst[inCounter]+'"';
    End;
    lst.Add('Request.Cookie.Count='+IntToStr(lst.Count));
    sgTemp := sgTemp+lst.Text;
    lst.Clear;
    lst.SetText(PChar(sgTemp2));
    For inCounter := 0 To lst.Count - 1 Do
    Begin
      lst[inCounter] := 'Request.Cookie.Item.'+lst[inCounter];
    End;
    sgTemp := sgTemp+lst.Text;

    //Request.CookieFields
    lst.Clear;
    Request.ExtractCookieFields(lst);
    sgTemp2 := lst.Text;
    For inCounter := 0 To lst.Count - 1 Do
    Begin
      lst[inCounter] := 'Request.CookieFields['+IntToStr(inCounter)+']="'+lst[inCounter]+'"';
    End;
    lst.Add('Request.CookieFields.Count='+IntToStr(lst.Count));
    sgTemp := sgTemp+lst.Text;
    lst.Clear;
    lst.SetText(PChar(sgTemp2));
    For inCounter := 0 To lst.Count - 1 Do
    Begin
      lst[inCounter] := 'Request.CookieFields.Item.'+lst[inCounter];
    End;
    sgTemp := sgTemp+lst.Text;

    //Request.Date
    sgTemp := sgTemp+'Request.Date='+FormatDateTime('mm/dd/yyyy hh:nn:ss',Request.Date)+#13;

    //Request.DerivedFrom
    sgTemp := sgTemp+'Request.DerivedFrom='+Request.DerivedFrom+#13;

    //Request.Expires
    sgTemp := sgTemp+'Request.Expires='+FormatDateTime('mm/dd/yyyy hh:nn:ss',Request.Expires)+#13;

    //Request.From
    sgTemp := sgTemp+'Request.From='+Request.From+#13;

    //Request.Host
    sgTemp := sgTemp+'Request.Host='+Request.Host+#13;

    //Request.IfModifiedSince
    sgTemp := sgTemp+'Request.IfModifiedSince='+FormatDateTime('mm/dd/yyyy hh:nn:ss',Request.IfModifiedSince)+#13;

    //Request.Method
    sgTemp := sgTemp+'Request.Method='+Request.Method+#13;

    //Request.MethodType
    sgTemp := sgTemp+'Request.MethodType='+'?'+#13;

    //Request.PathInfo
    sgTemp := sgTemp+'Request.PathInfo='+Request.PathInfo+#13;

    //Request.PathTranslated
    sgTemp := sgTemp+'Request.PathTranslated='+Request.PathTranslated+#13;

    //Request.ProtocolVersion
    sgTemp := sgTemp+'Request.ProtocolVersion='+Request.ProtocolVersion+#13;

    //Request.Query
    lst.Clear;
    Request.ExtractQueryFields(lst);
    sgTemp2 := lst.Text;
    For inCounter := 0 To lst.Count - 1 Do
    Begin
      lst[inCounter] := 'Request.Query['+IntToStr(inCounter)+']="'+lst[inCounter]+'"';
    End;
    lst.Add('Request.Query.Count='+IntToStr(lst.Count));
    sgTemp := sgTemp+lst.Text;
    lst.Clear;
    lst.SetText(PChar(sgTemp2));
    For inCounter := 0 To lst.Count - 1 Do
    Begin
      lst[inCounter] := 'Request.Query.Item.'+lst[inCounter];
    End;
    sgTemp := sgTemp+lst.Text;

    //Request.QueryFields
    lst.Clear;
    Request.ExtractQueryFields(lst);
    sgTemp2 := lst.Text;
    For inCounter := 0 To lst.Count - 1 Do
    Begin
      lst[inCounter] := 'Request.QueryFields['+IntToStr(inCounter)+']="'+lst[inCounter]+'"';
    End;
    lst.Add('Request.QueryFields.Count='+IntToStr(lst.Count));
    sgTemp := sgTemp+lst.Text;
    lst.Clear;
    lst.SetText(PChar(sgTemp2));
    For inCounter := 0 To lst.Count - 1 Do
    Begin
      lst[inCounter] := 'Request.QueryFields.Item.'+lst[inCounter];
    End;
    sgTemp := sgTemp+lst.Text;

    //Request.Referer
    sgTemp := sgTemp+'Request.Referer='+Request.Referer+#13;

    //Request.RemoteAddr
    sgTemp := sgTemp+'Request.RemoteAddr='+Request.RemoteAddr+#13;

    //Request.RemoteHost
    sgTemp := sgTemp+'Request.RemoteHost='+Request.RemoteHost+#13;

    //Request.ScriptName
    sgTemp := sgTemp+'Request.ScriptName='+Request.ScriptName+#13;

    //Request.ServerPort
    sgTemp := sgTemp+'Request.ServerPort='+IntToStr(Request.ServerPort)+#13;

    //Request.Title
    sgTemp := sgTemp+'Request.Title='+Request.Title+#13;

    //Request.URL
    sgTemp := sgTemp+'Request.URL='+Request.URL+#13;

    //Request.UserAgent
    sgTemp := sgTemp+'Request.UserAgent='+Request.UserAgent+#13;
    WebRequest.SetText(PChar(sgTemp));
    WebRequestIsSet := True;
  Finally
    lst.Free;
  End;
  //TimeTest_ads.Stop;
  Except On E : Exception Do RaiseError(UnitName,ProcName,E); End;
end;

//
Unit Description UnitIndex Master Index
Function DeviceTypeToFileExtension(DeviceType: String): String;
Var
  lstDTypes : TStringList;
  lstCTypes : TStringList;
  sgDType   : String;
  inIndex   : Integer;
Begin
  Result    := '';
  ProcName  := 'RBuilderDeviceTypeToFileExtension'; Try
  //TimeTest_ads.Start(ProcName);
  lstDTypes := TStringList.Create();
  lstCTypes := TStringList.Create();
  Try
    (*
    
    *)
    lstDTypes.Add('EXCELFILE');           lstCTypes.Add('.xls');
    lstDTypes.Add('GRAPHICFILE');         lstCTypes.Add('.jpg');
    lstDTypes.Add('GRAPHICFILE_BMP');     lstCTypes.Add('.bmp');
    lstDTypes.Add('GRAPHICFILE_GIF');     lstCTypes.Add('.gif');
    lstDTypes.Add('GRAPHICFILE_JPEG');    lstCTypes.Add('.jpeg');
    lstDTypes.Add('GRAPHICFILE_JPG');     lstCTypes.Add('.jpg');
    lstDTypes.Add('GRAPHICFILE_TIF');     lstCTypes.Add('.tif');
    lstDTypes.Add('HTMLONEPAGEFILE');     lstCTypes.Add('.htm');
    lstDTypes.Add('HTMLFILE');            lstCTypes.Add('.htm');
    lstDTypes.Add('HTMLLAYERFILE');       lstCTypes.Add('.htm');
    lstDTypes.Add('LOTUSFILE');           lstCTypes.Add('.wk1');
    lstDTypes.Add('PDFFILE');             lstCTypes.Add('.pdf');
    lstDTypes.Add('QUATTROFILE');         lstCTypes.Add('.wq1');
    lstDTypes.Add('REPORTTEXTFILE');      lstCTypes.Add('.txt');
    lstDTypes.Add('RTFFILE');             lstCTypes.Add('.rtf');
    lstDTypes.Add('TEXTFILEARCHIVEFILE'); lstCTypes.Add('.arc');
    sgDType   := UpperCase(DeviceType);
    inIndex   := lstDTypes.IndexOf(sgDType);
    If inIndex = -1 Then Exit;
    Result    := lstCTypes[inIndex];
  Finally
    lstDTypes.Free;
    lstCTypes.Free;
  End;
  //TimeTest_ads.Stop;
  Except On E : Exception Do RaiseError(UnitName,ProcName,E); End;
End;

//
Unit Description UnitIndex Master Index
Function DeviceTypeToContentType(DeviceType: String): String;
Var
  lstDTypes : TStringList;
  lstCTypes : TStringList;
  sgDType   : String;
  inIndex   : Integer;
Begin
  Result    := 'text/html';
  ProcName  := 'RBuilderDeviceTypeToContentType'; Try
  lstDTypes := TStringList.Create();
  lstCTypes := TStringList.Create();
  Try
    lstDTypes.Add('EXCELFILE');           lstCTypes.Add('application/x-msexcel');
    lstDTypes.Add('GRAPHICFILE');         lstCTypes.Add('image/jpeg');
    lstDTypes.Add('GRAPHICFILE_BMP');     lstCTypes.Add('image/x-ms-bmp');
    lstDTypes.Add('GRAPHICFILE_GIF');     lstCTypes.Add('image/gif');
    lstDTypes.Add('GRAPHICFILE_JPEG');    lstCTypes.Add('image/jpeg');
    lstDTypes.Add('GRAPHICFILE_JPG');     lstCTypes.Add('image/jpeg');
    lstDTypes.Add('GRAPHICFILE_TIF');     lstCTypes.Add('image/tiff');
    lstDTypes.Add('HTMLONEPAGEFILE');     lstCTypes.Add('text/html');
    lstDTypes.Add('HTMLFILE');            lstCTypes.Add('text/html');
    lstDTypes.Add('HTMLLAYERFILE');       lstCTypes.Add('text/html');
    lstDTypes.Add('LOTUSFILE');           lstCTypes.Add('application/x-wingz');
    lstDTypes.Add('PDFFILE');             lstCTypes.Add('application/pdf');
    lstDTypes.Add('QUATTROFILE');         lstCTypes.Add('text/plain');
    lstDTypes.Add('REPORTTEXTFILE');      lstCTypes.Add('text/plain');
    lstDTypes.Add('RTFFILE');             lstCTypes.Add('application/msword');
    lstDTypes.Add('TEXTFILEARCHIVEFILE'); lstCTypes.Add('text/plain');
    sgDType   := UpperCase(DeviceType);
    inIndex   := lstDTypes.IndexOf(sgDType);
    If inIndex = -1 Then Exit;
    Result    := lstCTypes[inIndex];
  Finally
    lstDTypes.Free;
    lstCTypes.Free;
  End;
  Except On E : Exception Do RaiseError(UnitName,ProcName,E); End;
End;

//
Unit Description UnitIndex Master Index
Procedure WebGetResponse(var Response : TWebResponse);
Var
  sgSrc        : String;
  sgExt        : String;
  lst          : TStringList;
  Content_Type : String;
begin
  ProcName     := 'WebImageRequest'; Try
  lst          := TStringList.Create();
  Try
    Content_Type         := 'image/jpeg';
    Response.ContentType := '';
    Response.Content     := '';
    lst.SetText(PChar(WebRequest.Text));
    If UpperCase(lst.Values['Request.Method'])='GET' Then
    Begin
      sgSrc := lst.Values['Request.QueryFields.Item.src'];
    End
    Else
    Begin
      sgSrc := lst.Values['Request.ContentFields.Item.src'];
    End;
    If sgSrc  = '' Then
    Begin
      If lst.Values['Request.PathInfo'] <> '' Then
      Begin
        sgSrc := lst.Values['Request.PathInfo'];
        sgSrc := StringReplace(sgSrc,'/','\',[rfReplaceAll]);
        sgSrc := ExtractFileName(sgSrc);
      End
      Else
      Begin
        Exit;
      End;
    End;
    sgSrc := StringReplace(sgSrc,'"','',[rfReplaceall]);
    sgSrc := ExtractFileName(sgSrc);

    If Not FileExists(GlobalCachePath+sgSrc) Then Exit;

    (*
    //In case the content is still being produced allow 30 seconds
    If Not FileExists(sgSrc) Then
    Begin
      For inWait := 1 To 30 Do
      Begin
        Sleep(1000);
        If FileExists(sgSrc) Then Break;
        If inWait >= 30 Then Exit;
      End;
    End;
    *)
    Content_Type := '';
    sgExt := ExtractFileExt(sgSrc);
    sgExt := UpperCase(sgExt);
    If sgExt = '.JPG'  Then Content_Type := 'image/jpeg';
    If sgExt = '.JPEG' Then Content_Type := 'image/jpeg';
    If sgExt = '.JPE'  Then Content_Type := 'image/jpeg';
    If sgExt = '.GIF'  Then Content_Type := 'image/gif';
    If sgExt = '.HTM'  Then Content_Type := 'text/html';
    If sgExt = '.HTML' Then Content_Type := 'text/html';
    If sgExt = '.XBM'  Then Content_Type := 'image/x-xbitmap';
    If sgExt = '.XPM'  Then Content_Type := 'image/x-xpixmap';
    If sgExt = '.PDF'  Then Content_Type := 'application/pdf';
    If sgExt = '.PNG'  Then Content_Type := 'image/x-png';
    If sgExt = '.IEF'  Then Content_Type := 'image/ief';
    If sgExt = '.TIF'  Then Content_Type := 'image/tiff';
    If sgExt = '.TIFF' Then Content_Type := 'image/tiff';
    If sgExt = '.RGB'  Then Content_Type := 'image/rgb';
    Response.ContentType := Content_Type;
    Response.Content     := FileToStr(GlobalCachePath+sgSrc);
    If FileExists(GlobalCachePath+sgSrc) Then DeleteFile(GlobalCachePath+sgSrc);
  Finally
    lst.Free;
  End;
  Except On E : Exception Do RaiseError(UnitName,ProcName,E); End;
end;

//
Unit Description UnitIndex Master Index
Procedure WebPostResponse(var Response:TWebResponse;OutFile,DeviceType:String);
Var
  sgExt        : String;
  lstExtensions: TStringList;
  inIndex      : Integer;
begin
  ProcName     := 'WebPostResponse'; Try
  sgExt        := UpperCase(ExtractFileExt(OutFile));
  lstExtensions:= TStringList.Create();
  Try
    With lstExtensions Do
    Begin
      Clear;
      Add('.ARC');
      Add('.BMP');
      Add('.GIF');
      Add('.HTM');
      Add('.HTML');
      Add('.IEF');
      Add('.JPE');
      Add('.JPEG');
      Add('.JPG');
      Add('.PDF');
      Add('.PNG');
      Add('.RGB');
      Add('.RTF');
      Add('.TIF');
      Add('.TIFF');
      Add('.TXT');
      Add('.WK1');
      Add('.WQ1');
      Add('.XBM');
      Add('.XLS');
      Add('.XPM');
    End;
    inIndex := lstExtensions.IndexOf(sgExt);
    Case inIndex Of
     0 {'.ARC' }: WebPostResponse_Default(Response,OutFile,DeviceType);
     1 {'.BMP' }: WebPostResponse_MultiFile(Response,OutFile);
     2 {'.GIF' }: WebPostResponse_MultiFile(Response,OutFile);
     3 {'.HTM' }: WebPostResponse_HTML   (Response,OutFile,DeviceType);
     4 {'.HTML'}: WebPostResponse_HTML   (Response,OutFile,DeviceType);
     5 {'.IEF' }: WebPostResponse_Default(Response,OutFile,DeviceType);
     6 {'.JPE' }: WebPostResponse_MultiFile(Response,OutFile);
     7 {'.JPEG'}: WebPostResponse_MultiFile(Response,OutFile);
     8 {'.JPG' }: WebPostResponse_MultiFile(Response,OutFile);
     9 {'.PDF' }: WebPostResponse_PDF    (Response,OutFile);
    10 {'.PNG' }: WebPostResponse_MultiFile(Response,OutFile);
    11 {'.RGB' }: WebPostResponse_MultiFile(Response,OutFile);
    12 {'.RTF' }: WebPostResponse_Default(Response,OutFile,DeviceType);
    13 {'.TIF' }: WebPostResponse_TIF    (Response,OutFile);
    14 {'.TIFF'}: WebPostResponse_TIF    (Response,OutFile);
    15 {'.TXT' }: WebPostResponse_Default(Response,OutFile,DeviceType);
    16 {'.WK1' }: WebPostResponse_Default(Response,OutFile,DeviceType);
    17 {'.WQ1' }: WebPostResponse_Default(Response,OutFile,DeviceType);
    18 {'.XBM' }: WebPostResponse_MultiFile(Response,OutFile);
    19 {'.XLS' }: WebPostResponse_XLS    (Response,OutFile);
    20 {'.XPM' }: WebPostResponse_Default(Response,OutFile,DeviceType);
    Else
      WebPostResponse_Default(Response,OutFile,DeviceType);
    End;
  Finally
    lstExtensions.Free;
  End;
  Except On E : Exception Do RaiseError(UnitName,ProcName,E); End;
end;

//
Unit Description UnitIndex Master Index
Procedure WebPostResponse_Default(var Response:TWebResponse;OutFile,DeviceType:String);
Var
  inCounter : Integer;
begin
  ProcName := 'WebPostResponse_Default'; Try
  For inCounter := 1 To 300 Do
  Begin
    Application.ProcessMessages;
    Sleep(1000);
    If FileExists(OutFile) Then
    Begin
      If GetFileSize(OutFile) > 0 Then
      Begin
        Try
          Response.Content     := FileToStr(OutFile);
          Response.ContentType := DeviceTypeToContentType(DeviceType);
          WebResponseDeleteFile(OutFile);
          Exit;
        Except
          Response.Content     := 'Error';
          Response.ContentType := 'text/html';
          Break;
        End;
      End;
    End;
  End;
  Except On E : Exception Do RaiseError(UnitName,ProcName,E); End;
end;

//
Unit Description UnitIndex Master Index
Procedure WebPostResponse_HTML(var Response:TWebResponse;OutFile,DeviceType:String);
Var
  inCounter : Integer;
begin
  ProcName := 'WebPostResponse_HTML'; Try
  For inCounter := 1 To 300 Do
  Begin
    Application.ProcessMessages;
    Sleep(1000);
    If FileExists(OutFile+'l') Then
    Begin
      If GetFileSize(OutFile+'l') > 0 Then
      Begin
        Try
          Response.Content     := FileToStr(OutFile+'l');
          Response.ContentType := DeviceTypeToContentType(DeviceType);
          WebResponseDeleteFile(OutFile);
          Exit;
        Except
          Response.Content     := 'Error';
          Response.ContentType := 'text/html';
          Break;
        End;
      End;
    End;
    If FileExists(OutFile) Then
    Begin
      If GetFileSize(OutFile) > 0 Then
      Begin
        Try
          Response.Content     := FileToStr(OutFile);
          Response.ContentType := DeviceTypeToContentType(DeviceType);
          WebResponseDeleteFile(OutFile);
          Exit;
        Except
          Response.Content     := 'Error';
          Response.ContentType := 'text/html';
          Break;
        End;
      End;
    End;
  End;
  Except On E : Exception Do RaiseError(UnitName,ProcName,E); End;
end;

//
Unit Description UnitIndex Master Index
Procedure WebPostResponse_PDF(var Response:TWebResponse;OutFile:String);
Var
  Caption   : String;
begin
  ProcName  := 'WebPostResponse_PDF'; Try
  Caption   := 'Adobe PDF';
  WebPostResponse_SingleFile(Response,OutFile,Caption);
  Except On E : Exception Do RaiseError(UnitName,ProcName,E); End;
end;

//
Unit Description UnitIndex Master Index
Procedure WebPostResponse_TIF(var Response:TWebResponse;OutFile:String);
Var
  Caption   : String;
begin
  ProcName  := 'WebPostResponse_TIF'; Try
  Caption   := 'TIF Image';
  WebPostResponse_SingleFile(Response,OutFile,Caption);
  Except On E : Exception Do RaiseError(UnitName,ProcName,E); End;
end;

//
Unit Description UnitIndex Master Index
Procedure WebPostResponse_XLS(var Response:TWebResponse;OutFile:String);
Var
  Caption   : String;
begin
  ProcName  := 'WebPostResponse_XLS'; Try
  Caption   := 'Microsoft Excel Spreadsheet';
  WebPostResponse_SingleFile(Response,OutFile,Caption);
  Except On E : Exception Do RaiseError(UnitName,ProcName,E); End;
end;

//
Unit Description UnitIndex Master Index
Procedure WebPostResponse_SingleFile(var Response:TWebResponse;OutFile,Caption:String);
Var
  inCounter : Integer;
begin
  ProcName  := 'WebPostResponse_SingleFile'; Try
  For inCounter := 1 To 1000 Do
  Begin
    Application.ProcessMessages;
    Sleep(300);
    If FileExists(OutFile) Then
    Begin
      If GetFileSize(OutFile) > 0 Then
      Begin
        Try
          Response.Content     :=
            ''+#13+
            ''+#13+
            ''     +#13+
            ''+#13+
            '' +#13+
            '' +#13+
            ''+#13+
            ''+#13+
            ''+#13+
            '
'+#13+ '

'+#13+ 'Retrieving Report in'+#13+ '
'+#13+ Caption+' Format'+#13+ '

'+#13+ '
'+#13+ ''+#13+ ''; Response.ContentType := 'text/html'; Exit; Except Response.Content := 'Error'; Response.ContentType := 'text/html'; Break; End; End; End; End; Except On E : Exception Do RaiseError(UnitName,ProcName,E); End; end; //
Unit Description UnitIndex Master Index
Procedure WebResponseDeleteFile(OutFile:String);
Var
  sgExt : String;
begin
  ProcName := 'WebResponseDeleteFile'; Try
  sgExt    := UpperCase(ExtractFileExt(OutFile));
  If FileExists(OutFile)     Then
    DeleteFile(OutFile)
    ;
  If sgExt = '.HTM'  Then If FileExists(OutFile+'l') Then WebResponseDeleteFile(OutFile+'l');
  If sgExt = '.HTML' Then If FileExists(Copy(OutFile,1,Length(OutFile)-1)) Then WebResponseDeleteFile(Copy(OutFile,1,Length(OutFile)-1));
  Except On E : Exception Do RaiseError(UnitName,ProcName,E); End;
end;

//
Unit Description UnitIndex Master Index
Procedure WebPostResponse_MultiFile(var Response:TWebResponse;OutFile:String);
Var
  inCounter : Integer;
  inPage    : Integer;
  FileFound : Boolean;
  FileDone  : Boolean;
  lst       : TStringList;
  FileMask  : String;
  inPos     : Integer;
  sgExt     : String;
  Page      : TStringList;
  ContentStr: String;
  OutFileWas: String;
  sgArgs    : String;
  sgExtWas  : String;
  sgGifFile : String;
begin
  ProcName  := 'WebPostResponse_MultiFile'; Try
  OutFileWas:= OutFile;
  sgExtWas  := UpperCase(ExtractFileExt(OutFile));
  If sgExtWas = '.GIF' Then
  Begin
    OutFile := Copy(OutFile,1,Length(OutFile)-3)+'bmp';
    If FileExists(GlobalExePath+'GifGen.Exe') Then
    Begin
      If Not FileExists(GlobalCachePath+'GifGen.Exe') Then
      Begin
        CopyFile(GlobalExePath+'GifGen.Exe',GlobalCachePath+'GifGen.Exe');
      End;
    End;
  End;
  FileFound := False;
  FileDone  := False;
  For inCounter := 1 To 300 Do
  Begin
    Application.ProcessMessages;
    Sleep(1000);
    If FileExists(OutFile) Then FileFound := True;
    If FileFound Then
    Begin
      If Not FileExists(OutFile) Then FileDone := True;
    End;
    If FileFound And FileDone Then
    Begin
      lst       := TStringList.Create();
      Try
        FileMask  := ExtractFileName(OutFile);
        sgExt     := ExtractFileExt(OutFile);
        inPos     := Pos('.',FileMask);
        If inPos <> 0 Then FileMask := Copy(FileMask,1,inPos-1)+'*'+sgExt;
        FilesInDirDetail(
          lst,              //FileList    : TStrings;
          GlobalCachePath,  //Directory   : String;
          FileMask,         //Mask        : String;
          False,            //Intersection: Boolean;
          False,            //IsReadOnly  : Boolean;
          False,            //IsHidden    : Boolean;
          False,            //IsSystem    : Boolean;
          False,            //IsVolumeID  : Boolean;
          False,            //IsDirectory : Boolean;
          False,            //IsArchive   : Boolean;
          True,             //IsNormal    : Boolean;
          False);           //InclDotFiles: Boolean): Boolean;
        If lst.Count > 0 Then
        Begin
          Page      := TStringList.Create();
          Try
            With Page Do
            Begin
              Clear;
              Add('');
              Add('');
              Add('');
              For inPage := 0 To lst.Count -1 Do
              Begin
                Add('');
                Add('');
                Add('');
                Add('');
                Add('');
                Add('');
              End;
              Add('
'); Add(MakeHTMLPageNav((inPage+1),lst.Count)); Add('
'); If sgExtWas = '.GIF' Then Begin sgGifFile := Copy(lst[inPage],1,Length(lst[inPage])-3)+'gif'; Add(''); sgArgs := ''; sgArgs := sgArgs + '"key1='+'mccammon'+'" '; sgArgs := sgArgs + '"key2='+'thornton'+'" '; sgArgs := sgArgs + '"GifFile='+GlobalCachePath+sgGifFile+'" '; sgArgs := sgArgs + '"BitmapFile='+GlobalCachePath+lst[inPage]+'" '; sgArgs := sgArgs + '"DeleteBMap='+'TRUE'+'" '; Try ExecuteExeParams( GlobalCachePath+'GifGen'+'.exe', //FileName : String; sgArgs , //ParamString : String; GlobalCachePath );//DefaultDir : String): Boolean; Except End; End Else Begin Add(''); End; Add('
'); Add(''); Add(''); ContentStr:= Text; End; Finally Page.Free; End; End Else Begin ContentStr:= 'Nothing Found'; End; Try Response.Content := ContentStr; Response.ContentType := 'text/html'; Exit; Except Response.Content := 'Error'; Response.ContentType := 'text/html'; Break; End; Finally lst.Free; End; End; End; Except On E : Exception Do RaiseError(UnitName,ProcName,E); End; end; //
Unit Description UnitIndex Master Index
Function MakeHTMLPageNav(PageNum,PageCount: Integer): String;
Var
  sgNav       : String;
  sgPageNumUp : String;
  sgPageNumDn : String;
  sgPageNum   : String;
  vAlign      : Integer;
  LastPage    : Boolean;
begin
  Result      := '';
  ProcName    := 'MakeHTMLPageNav'; Try
  vAlign      := 5;
  LastPage    := (PageNum=PageCount);
  sgPageNumUp := IntToStr(PageNum+1);
  sgPageNumDn := IntToStr(PageNum-1);
  sgPageNum   := IntToStr(PageNum);
  If PageNum = 1 Then
  Begin
    If Not LastPage Then
    Begin
      sgNav :=
        ''+
        ' '+
        ''+
        ''+
        'Next'+
        ''+
        ' '+
        ' '+
        ''+
        'Last'+
        ''+
        ' '+
        ' '+
        ''+
        'page '+sgPageNum+
        ''+
        '';
    End
    Else
    Begin
      sgNav := '';
    End;
  End
  Else
  Begin
    If LastPage Then
    Begin
      sgNav := '';
      sgNav :=
        ''+
        ''+
        ' '+
        ''+
        ''+
        'First'+
        ''+
        ' '+
        ' '+
        ''+
        'Back'+
        ''+
        ' '+
        ' '+
        ''+
        'page '+sgPageNum+
        ''+#13+
        '';
    End
    Else
    Begin
      sgNav :=
        ''+
        ' '+
        ''+
        ''+
        'First'+
        ''+
        ' '+
        ' '+
        ''+
        'Back'+
        ''+
        ' '+
        ' '+
        ''+
        'Next'+
        ''+
        ' '+
        ' '+
        ''+
        'Last'+
        ''+
        ' '+
        ' '+
        '' +
        'page '+sgPageNum+
        ''+
        '';
    End;
  End;
  sgNav  := StringReplace(sgNav,#13,'',[rfReplaceAll]);
  Result := sgNav;
  Except On E : Exception Do RaiseError(UnitName,ProcName,E); End;
end;

//
Unit Description UnitIndex Master Index
Function  BitmapToGif(BitmapFile,GifFile:String;DeleteBitmap: Boolean): Boolean;
Var
  Gif       : TGifImage;
  Bitmap    : TBitmap;
  sgBExt    : String;
  sgGExt    : String;
  sgGDir    : String;
begin
  Result    := False;
  ProcName  := 'BitmapToGif'; Try
  If Not FileExists(BitmapFile) Then Exit;
  BitmapFile:= Trim(BitmapFile);
  GifFile   := Trim(GifFile);
  If BitmapFile = '' Then Exit;
  If GifFile    = '' Then Exit;
  sgBExt    := UpperCase(ExtractFileExt(BitmapFile));
  sgGExt    := UpperCase(ExtractFileExt(GifFile));
  If sgBExt <> '.BMP' Then Exit;
  If sgGExt <> '.GIF' Then Exit;
  sgGDir    := ExtractFilePath(GifFile);
  If Not DirectoryExists(sgGDir) Then ForceDirectories(sgGDir);
  If FileExists(GifFile) Then DeleteFile(GifFile);
  Gif       := TGifImage.Create();
  Bitmap    := TBitmap.Create();
  Try
    Bitmap.LoadFromFile(BitmapFile);
    Gif.Assign(Bitmap);
    Gif.SaveToFile(GifFile);
  Finally
    Bitmap.Free;
    Gif.Free;
  End;
  If DeleteBitmap Then DeleteFile(BitmapFile);
  Except On E : Exception Do RaiseError(UnitName,ProcName,E); End;
end;

Initialization
  WebRequest        := TStringList.Create();
  WebRequestIsSet   := False;
  GlobalExePath     := ExtractFileDir(ParamStr(0));
  If Copy(GlobalExePath,Length(GlobalExePath),1) <> '\' Then
     GlobalExePath := GlobalExePath + '\';
  If DirectoryExists('Z:\') Then
  Begin
    GlobalCachePath := 'Z'+Copy(GlobalExePath,2,Length(GlobalExePath)-1);
    If Not DirectoryExists(GlobalCachePath) Then ForceDirectories(GlobalCachePath);
  End
  Else
  Begin
    GlobalCachePath := GlobalExePath;
  End;
  GlobalReportsPath := GlobalExePath + 'Exes\';
  GlobalExeName     := ExtractFileName(ParamStr(0));
  GlobalExeName     := Copy(GlobalExeName,1,Length(GlobalExeName)-4);
  GlobalExeName     := LowerCase(GlobalExeName);
Finalization
  WebRequest.Free;
end.
//