//Advanced Delphi Systems Code: ads_IntNet_NM
{{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_intnet_NM;
{Richard Maley
 Advanced Delphi Systems
 12613 Maidens Bower Drive
 Potomac, MD 20854
 301-840-1554
 maley@compuserve.com
 maley@advdelphisys.com
 maley@cpcug.org
 rmaley@fenix2.dol-esa.gov}

{
All of these utilities assume you have the NetMasters FastNet
internet components.  The FastNet components can be purchased
from NetMasters at http://www.netmastersllc.com.
}

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

NMHttp_GetURLToFile   NMHttp_IsUrl   NMHttp_OnConnect   NMHttp_OnConnectionFailed  NMHttp_OnDisconnect   NMHttp_OnFailure   NMHttp_OnHostResolved   NMHttp_OnPacketRecvd   NMHttp_OnPacketSent   NMHttp_OnStatus   NMHttp_OnSuccess   NMHttp_PostURLToFile   NMHttp_URLToFileDetail  

*)
interface
Uses SysUtils,  ExtCtrls, Buttons, Classes,NMHttp;

{!~
NMHTTP_GETURLTOFILE

This utility assumes you have the NetMasters FastNet
internet components.  The FastNet components can be purchased
from NetMasters at http://www.netmastersllc.com.

This utility copies a URL to file.

}
Function NMHttp_GetURLToFile(
  NMHttp       : TNMHttp;
  SourceURL    : String;
  DestFile     : String;
  Button_Stop  : TSpeedButton
  ): Boolean;

{!~
NMHTTP_ISURL

This utility assumes you have the NetMasters FastNet
internet components.  The FastNet components can be purchased
from NetMasters at http://www.netmastersllc.com.

This utility tests the existance of a URL.  If the URL exists
True is returned, otherwise False.

}
Function NMHttp_IsUrl(NMHttp: TNMHttp; URLString: String): Boolean;

Procedure NMHttp_OnConnect(MsgPanel : TPanel);
Procedure NMHttp_OnConnectionFailed(MsgPanel: TPanel);
Procedure NMHttp_OnDisconnect(MsgPanel : TPanel);
Procedure NMHttp_OnFailure(MsgPanel: TPanel;Cmd: CmdType);
Procedure NMHttp_OnHostResolved(MsgPanel : TPanel);
Procedure NMHttp_OnPacketRecvd(NMHttp: TNMHttp;MsgPanel: TPanel);
Procedure NMHttp_OnPacketSent(NMHttp: TNMHttp;MsgPanel: TPanel);
Procedure NMHttp_OnStatus(NMHttp: TNMHttp;MsgPanel: TPanel;Status: String);
Procedure NMHttp_OnSuccess(MsgPanel: TPanel;Cmd: CmdType);

{!~
NMHttp_PostURLToFile

This utility assumes you have the NetMasters FastNet
internet components.  The FastNet components can be purchased
from NetMasters at http://www.netmastersllc.com.

This utility copies a URL to file using http post.

}
Function NMHttp_PostURLToFile(
  NMHttp       : TNMHttp;
  SourceURL    : String;
  Parameters   : String;
  DestFile     : String;
  Button_Stop  : TSpeedButton
  ): Boolean;



implementation


//
Unit Description UnitIndex Master Index
Function NMHttp_URLToFileDetail(
  NMHttp       : TNMHttp;
  SourceURL    : String;
  Parameters   : String;
  DestFile     : String;
  Button_Stop  : TSpeedButton
  ): Boolean;
Var
  BodyFile_SL             : TStringList;
  BodyFile                : String;
begin
  Try
    Button_Stop.Enabled   := True;
    BodyFile              := DestFile;
    NMHttp.InputFileMode  := False;
    NMHttp.OutputFileMode := False;
    NMHttp.Header         := 'Header.Txt';
    NMHttp.Body           := BodyFile;
    NMHttp.ReportLevel    := 2;
    With NMHttp.HeaderInfo do
    Begin
      Cookie           := '';
      LocalMailAddress := '';
      LocalProgram     := '';
      Referer          := '';
      UserID           := '';
      Password         := '';
    End;

    If (Parameters = '') Then
    Begin
      NMHttp.Get(SourceURL);
    End
    Else
    Begin
      NMHttp.Post(SourceURL,Parameters);
    End;

    BodyFile_SL := TStringList.Create();
    Try
      BodyFile_SL.Clear;
      BodyFile_SL.Add(NMHttp.Body);
      BodyFile_SL.SaveToFile(BodyFile);
    Finally
      BodyFile_SL.Free;
    End;
    Result := True;
  Except
    Result := False;
  End;
  Button_Stop.Enabled := False;
end;

{!~
NMHTTP_GETURLTOFILE

This utility assumes you have the NetMasters FastNet
internet components.  The FastNet components can be purchased
from NetMasters at http://www.netmastersllc.com.

This utility copies a URL to file.

}
//
Unit Description UnitIndex Master Index
Function NMHttp_GetURLToFile(
  NMHttp       : TNMHttp;
  SourceURL    : String;
  DestFile     : String;
  Button_Stop  : TSpeedButton
  ): Boolean;
begin
  Result :=
    NMHttp_URLToFileDetail(
      NMHttp,
      SourceURL,
      '',
      DestFile,
      Button_Stop
      );
end;

{!~
NMHttp_PostURLToFile

This utility assumes you have the NetMasters FastNet
internet components.  The FastNet components can be purchased
from NetMasters at http://www.netmastersllc.com.

This utility copies a URL to file using http post.

}
//
Unit Description UnitIndex Master Index
Function NMHttp_PostURLToFile(
  NMHttp       : TNMHttp;
  SourceURL    : String;
  Parameters   : String;
  DestFile     : String;
  Button_Stop  : TSpeedButton
  ): Boolean;
begin
  Result :=
    NMHttp_URLToFileDetail(
      NMHttp,
      SourceURL,
      Parameters,
      DestFile,
      Button_Stop
      );
end;

//
Unit Description UnitIndex Master Index
Procedure NMHttp_OnConnect(MsgPanel : TPanel);
Begin
  If MsgPanel <> nil Then
  Begin
    MsgPanel.Caption := 'Connected';
    MsgPanel.Refresh;
  End;
End;

//
Unit Description UnitIndex Master Index
Procedure NMHttp_OnDisconnect(MsgPanel : TPanel);
Begin
  If MsgPanel <> nil Then
  Begin
    MsgPanel.Caption := 'Disconnected';
    MsgPanel.Refresh;
  End;
End;

//
Unit Description UnitIndex Master Index
Procedure NMHttp_OnFailure(MsgPanel: TPanel;Cmd: CmdType);
Begin
  If MsgPanel <> nil Then
  Begin
    Case Cmd Of
      CmdGET:     MsgPanel.Caption := 'Get Failed';
      CmdOPTIONS: MsgPanel.Caption := 'Options Failed';
      CmdHEAD:    MsgPanel.Caption := 'Head Failed';
      CmdPOST:    MsgPanel.Caption := 'Post Failed';
      CmdPUT:     MsgPanel.Caption := 'Put Failed';
      CmdPATCH:   MsgPanel.Caption := 'Patch Failed';
      CmdCOPY:    MsgPanel.Caption := 'Copy Failed';
      CmdMOVE:    MsgPanel.Caption := 'Move Failed';
      CmdDELETE:  MsgPanel.Caption := 'Delete Failed';
      CmdLINK:    MsgPanel.Caption := 'Link Failed';
      CmdUNLINK:  MsgPanel.Caption := 'UnLink Failed';
      CmdTRACE:   MsgPanel.Caption := 'Trace Failed';
      CmdWRAPPED: MsgPanel.Caption := 'Wrapped Failed';
    End;
    MsgPanel.Refresh;
  End;
End;

//
Unit Description UnitIndex Master Index
Procedure NMHttp_OnHostResolved(MsgPanel : TPanel);
Begin
  If MsgPanel <> nil Then
  Begin
    MsgPanel.Caption := 'Host Resolved';
    MsgPanel.Refresh;
  End;
End;

//
Unit Description UnitIndex Master Index
Procedure NMHttp_OnPacketRecvd(
  NMHttp   : TNMHttp;
  MsgPanel : TPanel);
Var
  Tot : Integer;
Begin
  Tot := NMHttp.BytesTotal;
  If Tot = 0 Then
  Begin
    MsgPanel.Caption :=
      IntToStr(NMHttp.BytesRecvd)+
      ' bytes retrieved';
  End
  Else
  Begin
    MsgPanel.Caption :=
      IntToStr(NMHttp.BytesRecvd)+
      ' of '+
      IntToStr(NMHttp.BytesTotal)+' retrieved';
  End;
  MsgPanel.Refresh;
End;

//
Unit Description UnitIndex Master Index
Procedure NMHttp_OnPacketSent(NMHttp: TNMHttp;MsgPanel: TPanel);
Begin
  If MsgPanel <> nil Then
  Begin
    MsgPanel.Caption :=
      IntToStr(NMHttp.BytesSent)+
      ' of '+
      IntToStr(NMHttp.BytesTotal)+
      ' sent';
    MsgPanel.Refresh;
  End;
End;

//
Unit Description UnitIndex Master Index
Procedure NMHttp_OnStatus(NMHttp: TNMHttp;MsgPanel: TPanel;Status: String);
Begin
  If MsgPanel <> nil Then
  Begin
    MsgPanel.Caption := Status;
    If NMHttp.ReplyNumber = 404 Then
      MsgPanel.Caption := 'Object Not Found';
    MsgPanel.Refresh;
  End;
End;

//
Unit Description UnitIndex Master Index
Procedure NMHttp_OnSuccess(MsgPanel: TPanel;Cmd: CmdType);
Begin
  If MsgPanel <> nil Then
  Begin
    Case Cmd Of
      CmdGET:     MsgPanel.Caption := 'Get Succeeded';
      CmdOPTIONS: MsgPanel.Caption := 'Options Succeeded';
      CmdHEAD:    MsgPanel.Caption := 'Head Succeeded';
      CmdPOST:    MsgPanel.Caption := 'Post Succeeded';
      CmdPUT:     MsgPanel.Caption := 'Put Succeeded';
      CmdPATCH:   MsgPanel.Caption := 'Patch Succeeded';
      CmdCOPY:    MsgPanel.Caption := 'Copy Succeeded';
      CmdMOVE:    MsgPanel.Caption := 'Move Succeeded';
      CmdDELETE:  MsgPanel.Caption := 'Delete Succeeded';
      CmdLINK:    MsgPanel.Caption := 'Link Succeeded';
      CmdUNLINK:  MsgPanel.Caption := 'UnLink Succeeded';
      CmdTRACE:   MsgPanel.Caption := 'Trace Succeeded';
      CmdWRAPPED: MsgPanel.Caption := 'Wrapped Succeeded';
    End;
    MsgPanel.Refresh;
  End;
End;

//
Unit Description UnitIndex Master Index
Procedure NMHttp_OnConnectionFailed(MsgPanel: TPanel);
Begin
  If MsgPanel <> nil Then
  Begin
    MsgPanel.Caption := 'Connection Failed';
    MsgPanel.Refresh;
  End;
End;

{!~
NMHTTP_ISURL

This utility assumes you have the NetMasters FastNet
internet components.  The FastNet components can be purchased
from NetMasters at http://www.netmastersllc.com.

This utility tests the existance of a URL.  If the URL exists
True is returned, otherwise False.

}
//
Unit Description UnitIndex Master Index
Function NMHttp_IsUrl(NMHttp: TNMHttp; URLString: String): Boolean;
Begin
  Try
    If FileExists(URLString) Then
    Begin
      Result := True;
      Exit;
    End;
  Except
  End;
  Try
    NMHttp.Head(URLString);
    Result := True;
  Except
    Result := False;
  End;
End;

End.
//