{"id":93,"date":"2013-12-13T08:10:19","date_gmt":"2013-12-13T08:10:19","guid":{"rendered":"http:\/\/tastaturkind.ch\/?p=93"},"modified":"2013-12-13T08:10:19","modified_gmt":"2013-12-13T08:10:19","slug":"delphi-webserver-with-indy","status":"publish","type":"post","link":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/2013\/12\/13\/delphi-webserver-with-indy\/","title":{"rendered":"Delphi Webserver with Indy"},"content":{"rendered":"<p><code>{<br \/>\n implements a very basic webserver with Systray icon and menu using Indy components<br \/>\n (C) 2010 Oliver Frick<br \/>\n}<\/p>\n<p>unit Unit1;<\/p>\n<p>interface<\/p>\n<p>uses<br \/>\n  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br \/>\n  Dialogs, ShellAPI, Menus, ExtCtrls, StdCtrls, FileCtrl, IdBaseComponent,<br \/>\n  IdComponent, IdTCPServer, IdCustomHTTPServer, IdHTTPServer,<br \/>\n  IdAntiFreezeBase, IdAntiFreeze, IdGlobal, StrUtils, IdThreadMgr,<br \/>\n  IdThreadMgrDefault;<\/p>\n<p>const<br \/>\n  WM_ICONTRAY = WM_USER + 1;  \/\/ User-defined message<\/p>\n<p>type<br \/>\n  TForm1 = class(TForm)<br \/>\n    PopupMenu1: TPopupMenu;<br \/>\n    mnuExit: TMenuItem;<br \/>\n    GroupBox1: TGroupBox;<br \/>\n    Label2: TLabel;<br \/>\n    Button2: TButton;<br \/>\n    Label1: TLabel;<br \/>\n    IdHTTPServer1: TIdHTTPServer;<br \/>\n    IdAntiFreeze1: TIdAntiFreeze;<br \/>\n    Memo1: TMemo;<br \/>\n    IdThreadMgrDefault1: TIdThreadMgrDefault;<br \/>\n    Button1: TButton;<br \/>\n    procedure FormCreate(Sender: TObject);<br \/>\n    procedure FormClose(Sender: TObject; var Action: TCloseAction);<br \/>\n    procedure mnuExitClick(Sender: TObject);<br \/>\n    procedure Button2Click(Sender: TObject);<br \/>\n    procedure IdHTTPServer1CommandGet(AThread: TIdPeerThread;<br \/>\n      ARequestInfo: TIdHTTPRequestInfo;<br \/>\n      AResponseInfo: TIdHTTPResponseInfo);<br \/>\n    procedure Button1Click(Sender: TObject);<br \/>\n    procedure Logfile(Text: String);<br \/>\n    procedure FormDestroy(Sender: TObject);<br \/>\n  private<br \/>\n    { Private declarations }<br \/>\n    function GetMIMEType(sFile: TFileName): String;<br \/>\n    procedure Icontray(var Msg: TMessage); message WM_ICONTRAY;<br \/>\n  public<br \/>\n    MIMEMap: TIdMIMETable;<br \/>\n    { Public declarations }<br \/>\n  end;<\/p>\n<p>var<br \/>\n  Form1: TForm1;<br \/>\n  NotifyIconData : TNotifyIconData;<br \/>\n  FDirectory: string;<br \/>\n  LogfileMaxLines: Word;<br \/>\nimplementation<\/p>\n<p>{$R *.dfm}<\/p>\n<p>procedure TForm1.FormCreate(Sender: TObject);<br \/>\nbegin<br \/>\n  with NotifyIconData do begin<br \/>\n    hIcon := Application.Icon.Handle;    \/\/ use application icon<br \/>\n    StrPCopy(szTip, 'SimpleHTTP is running');<br \/>\n    Wnd := Handle;<br \/>\n    uCallbackMessage := WM_ICONTRAY;<br \/>\n    uID := 1;<br \/>\n    uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;<br \/>\n    cbSize := sizeof(TNotifyIconData);<br \/>\n  end;<br \/>\n  Shell_NotifyIcon(NIM_ADD, @NotifyIconData);<\/p>\n<p>  MIMEMap := TIdMIMETable.Create(true);<br \/>\n  {get current dir and set Label}<br \/>\n  GetDir(0, FDirectory);<br \/>\n  Label2.Caption := FDirectory;<br \/>\n  LogfileMaxLines := 500;<br \/>\nend;<\/p>\n<p>procedure TForm1.Icontray(var Msg: TMessage);<br \/>\nvar<br \/>\n  CursorPos : TPoint;<br \/>\nbegin<br \/>\n  if Msg.lParam = WM_RBUTTONDOWN then begin<br \/>\n    GetCursorPos(CursorPos);<br \/>\n    SetForegroundWindow(Handle);        \/\/ suggested by Berend Radstaat<br \/>\n    PopupMenu1.Popup(CursorPos.x, CursorPos.y);<br \/>\n    PostMessage(Handle, WM_NULL, 0, 0); \/\/ suggested by Berend Radstaat<br \/>\n  end else<br \/>\n  if Msg.LParam = WM_LBUTTONDBLCLK then begin<br \/>\n    Show;<br \/>\n    \/\/ Shell_NotifyIcon(NIM_DELETE, @NotifyIconData);<br \/>\n  end else<br \/>\n    inherited;<br \/>\nend;<\/p>\n<p>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br \/>\nbegin<br \/>\n  Action := caNone;<br \/>\n  Hide;<br \/>\n  \/\/ Shell_NotifyIcon(NIM_ADD, @NotifyIconData);<br \/>\nend;<\/p>\n<p>procedure TForm1.mnuExitClick(Sender: TObject);<br \/>\nbegin<br \/>\n  Shell_NotifyIcon(NIM_DELETE, @NotifyIconData);<br \/>\n  Application.ProcessMessages;<br \/>\n  Application.Terminate;<br \/>\nend;<\/p>\n<p>procedure TForm1.Button2Click(Sender: TObject);<br \/>\nconst<br \/>\n  SELDIRHELP = 1000;<br \/>\nvar<br \/>\n  dir: String;<br \/>\nbegin<br \/>\n  if SelectDirectory(<br \/>\n       dir,<br \/>\n       [sdAllowCreate,<br \/>\n       sdPerformCreate,<br \/>\n       sdPrompt],<br \/>\n       SELDIRHELP<br \/>\n     ) then<br \/>\n    {show dir in Label}<br \/>\n    Label2.Caption := dir;<br \/>\nend;<\/p>\n<p>procedure TForm1.IdHTTPServer1CommandGet(AThread: TIdPeerThread;<br \/>\n  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);<br \/>\nvar<br \/>\nLFilename:  string;<br \/>\nLPathname:  string;<br \/>\nFileExt, CType: string;<br \/>\nbegin<br \/>\n  LFilename := ARequestInfo.Document;<br \/>\n  if LFilename = '\/' then begin<br \/>\n    LFilename := '\/index.html';<br \/>\n  end;<br \/>\n  LPathname := Label2.Caption + LFilename;<br \/>\n  if FileExists(LPathname) then begin<br \/>\n    AResponseInfo.ContentType := GetMIMEType(LPathname);<br \/>\n    AResponseInfo.ContentLength := FileSizeByName(LPathname);<br \/>\n    AResponseInfo.ContentStream := TFileStream.Create(LPathname, fmOpenRead + fmShareDenyWrite);<br \/>\n  end else begin<br \/>\n    AResponseInfo.ResponseNo := 404;<br \/>\n    AResponseInfo.ContentText := 'The requested URL ' + ARequestInfo.Document<br \/>\n    + ' was not found on this server.';<br \/>\n  end;<br \/>\n  Logfile( TimeToStr(Time) + ' ' + ARequestInfo.RawHTTPCommand );<br \/>\nend;<\/p>\n<p>procedure TForm1.Button1Click(Sender: TObject);<br \/>\nvar<br \/>\n  Status : string;<br \/>\nbegin<br \/>\n  If IdHTTPServer1.Active then begin<br \/>\n  IdHTTPServer1.Active := False;<br \/>\n  Button1.Caption := 'Start';<br \/>\n  Status := 'SimpleHTTP is stopped';<br \/>\n  end else begin<br \/>\n  IdHTTPServer1.Active := True;<br \/>\n  Button1.Caption := 'Stop';<br \/>\n  Status := 'SimpleHTTP is running';<br \/>\n  end;<br \/>\n  StrPCopy(NotifyIconData.szTip, Status);<br \/>\n  Shell_NotifyIcon(NIM_MODIFY, @NotifyIconData);<br \/>\nend;<\/p>\n<p>procedure TForm1.Logfile(Text: String);<br \/>\nbegin<br \/>\n  If Memo1.Lines.Count > LogFileMaxLines then Memo1.Clear;<br \/>\n  Memo1.Lines.Append( Text );<br \/>\nend;<\/p>\n<p>function TForm1.GetMIMEType(sFile: TFileName): String;<br \/>\nbegin<br \/>\n  result := MIMEMap.GetFileMIMEType(sFile);<br \/>\nend;<\/p>\n<p>procedure TForm1.FormDestroy(Sender: TObject);<br \/>\nbegin<br \/>\n  MIMEMap.Free;<br \/>\nend;<\/p>\n<p>end.<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>{ implements a very basic webserver with Systray icon and menu using Indy components (C) 2010 Oliver Frick } unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ShellAPI, Menus, ExtCtrls, StdCtrls, FileCtrl, IdBaseComponent, IdComponent, IdTCPServer, &hellip; <a href=\"https:\/\/oliver-frick.ch\/wordpress\/index.php\/2013\/12\/13\/delphi-webserver-with-indy\/\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-93","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":0,"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"wp:attachment":[{"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}