-
Notifications
You must be signed in to change notification settings - Fork 1
/
unit1.pas
684 lines (499 loc) · 19.1 KB
/
unit1.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
unit Unit1;
{$IFDEF FPC}
//{$mode objfpc}{$H+}
{$mode delphi} {$H+}
{$ENDIF}
interface
uses
{$IFNDEF UNIX} //Wenn nicht Unix / Linux, dann ist es Windows und wir binden Windows ein!
Windows,
{$ENDIF}
Messages, SysUtils, Variants, Classes, Forms, Controls, Graphics, Dialogs, ComCtrls, Menus,
StdCtrls, ExtCtrls, FileCtrl,
//{$IFDEF FPC}
// fileutil, //für FindAllFiles
// {$ENDIF}
//Indy 10 Stuff:
IdContext, IdComponent, IdBaseComponent, IdCustomTCPServer, IdThreadSafe,
IdTCPConnection, IdYarn, IdTCPServer, IdGlobal, IdURI,
//My Class for the ContentType Finder:
MyContentTypeFinder,
//My Class for the Systray:
MySystray,
//My Singleton Class to Save all the Settings and make it available for everyone
ServerSettingsSingleton,
//My TCP Server Stuff. Putting all TCP related stuff here
MyTCPServerUnit;
//Crt;
//ggf: ScktComp, FileCtrl, Menus;
type
{$IFDEF FPC}
//See: https://wiki.freepascal.org/Asynchronous_Calls
TLogMsgData = record // record can hold much more then a simple string :-)
Text: string;
end;
PLogMsgData = ^TLogMsgData;
{$ENDIF}
{ TForm1 }
TForm1 = class(TForm)
StartButton: TButton;
StopButton: TButton;
ErrorPagePathSelectButton: TButton;
WorkDirectorySelectButton: TButton;
PortEdit: TEdit;
WorkingDirectoryEdit: TEdit;
ErrorPagePathEdit: TEdit;
MaxConnectionsEdit: TEdit;
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
GroupBox3: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
MainMenu1: TMainMenu;
Datei1: TMenuItem;
Memo1: TMemo;
Programm: TMenuItem;
Beenden2: TMenuItem;
N1: TMenuItem;
OpenDialog1: TOpenDialog;
PopupMenu1: TPopupMenu;
StandardErrorPageToSendRadioButton: TRadioButton;
OwnErorPageToSendRadioButton: TRadioButton;
SendErrorOnCleanPathRadioButton: TRadioButton;
SendFtpViewRadioButton: TRadioButton;
StatusBar1: TStatusBar;
Systray: TMenuItem;
Beenden: TMenuItem;
Info1: TMenuItem;
Hilfe1: TMenuItem;
Timer1: TTimer;
Update1: TMenuItem;
Info2: TMenuItem;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
procedure Beenden2Click(Sender: TObject);
procedure BeendenClick(Sender: TObject);
procedure StartButtonClick(Sender: TObject);
procedure StopButtonClick(Sender: TObject);
procedure ErrorPagePathSelectButtonClick(Sender: TObject);
procedure WorkDirectorySelectButtonClick(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure Hilfe1Click(Sender: TObject);
procedure Info2Click(Sender: TObject);
procedure ProgrammClick(Sender: TObject);
procedure StandardErrorPageToSendRadioButtonClick(Sender: TObject);
procedure OwnErorPageToSendRadioButtonClick(Sender: TObject);
procedure SystrayClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
//////My callback functions
//procedure IdTCPServerDisconnect(AContext: TIdContext);
//procedure IdTCPServerExecute(AContext: TIdContext);
//procedure IdTCPServerStatus(ASender: TObject; const AStatus: TIdStatus;
// const AStatusText: string);
procedure DisplayOnMemo(str : String);
procedure Update1Click(Sender: TObject);
// procedure SendErrorPage(RequestedFile:String; AContext: TIdContext);
// procedure sendeVerzeichnis (Path: string; AContext: TIdContext);
// procedure ListFileDir(Path: string; FileList: TStrings);
// function pfadUrlEncoding (Path: string):String;
// function pfadUrlDecoding (Path: string):String;
function SaveServerSettingsToSingleton: Boolean;
procedure DeactivateTheVisualControlls;
procedure TryActivateServer;
procedure ResetServerStateVisuals;
// procedure CreateIdTCPServerAndCallbackFunctions;
private
{$IFDEF FPC}
procedure WriteToMemo(const AMsg: string);
{$ENDIF}
public
{$IFDEF FPC}
procedure WriteAsyncQueue(Data: PtrInt);
{$ENDIF}
end;
var
Form1: TForm1;
// IdTCPServer : TIdTCPServer;
Version : String = '1.71';
Path_delimiter_OS : String;
MySystrayClass : SystrayClass;
MyTCPServer: TMyTCPServer;
implementation
//uses shellapi nur für das SysTray, darum nur für Delphi einbinden, da nur dort das Systray implementiert ist!
uses
{$IFNDEF FPC}
shellapi,
{$ENDIF}
Unit2;
{$IFDEF FPC}
{$R *.lfm}
{$ELSE}
{$R *.dfm}
{$ENDIF}
{ TForm1 }
procedure TForm1.BeendenClick(Sender: TObject);
begin
close;
end;
function TForm1.SaveServerSettingsToSingleton: Boolean;
begin
Result := True;
TServerSettingsSingleton.Instance.WorkingDirectoryPath := WorkingDirectoryEdit.Text;
if not DirectoryExists(TServerSettingsSingleton.Instance.WorkingDirectoryPath) then
Begin
showmessage('Bitte ein gültiges Arbeitsverzeichnis eintragen!');
Result := False;
exit;
end;
try
TServerSettingsSingleton.Instance.Port := StrToInt(PortEdit.text);
except
on e: Exception do
Begin
showmessage('Bitte einen gültigen Port eintragen! ' +#13+#10+#13+#10+
'Die Fehlermeldung lautet: ' +#13+#10+
e.Message );
Result := False;
end;
end;
try
TServerSettingsSingleton.Instance.MaxConnections := StrToInt(MaxConnectionsEdit.text);
except
on e: Exception do
Begin
showmessage('Bitte eine gültige Anzahl an maximalen Verbindungen eintragen! ' +#13+#10+#13+#10+
'Die Fehlermeldung lautet: ' +#13+#10+
e.Message );
Result := False;
end;
end;
TServerSettingsSingleton.Instance.SendIndividualErrorPage := OwnErorPageToSendRadioButton.checked;
if TServerSettingsSingleton.Instance.SendIndividualErrorPage then
TServerSettingsSingleton.Instance.IndividualErrorPagePath := ErrorPagePathEdit.text;
TServerSettingsSingleton.Instance.SendFtpLikeViewOnEmptyDir := SendFtpViewRadioButton.checked;
TServerSettingsSingleton.Instance.ProgramVersion := Version;
TServerSettingsSingleton.Instance.Path_delimiter_OS := Path_delimiter_OS;
end;
function ValidateDirectory(const Dir: string): Boolean;
begin
Result := True;
if Dir = '' then
begin
showmessage('Bitte wählen Sie das Arbeitsverzeichnis aus, in dem sich die Dateien befinden, die der Webserver verwenden soll');
Result := False;
end
else
if not DirectoryExists(Dir) then
begin
Showmessage('Das Arbeitsverzeichnis existiert garnicht ...'+#13+#10+'Bitte geben Sie ein korrektes Arbeitsverzeichnis an!');
Result := False;
end;
end;
function ValidateErrorDirectory: Boolean;
begin
Result := True;
//Checken ob eigene definierte 404 (html) Seite verwendet wird:
if TServerSettingsSingleton.Instance.SendIndividualErrorPage then
begin
if (TServerSettingsSingleton.Instance.IndividualErrorPagePath = '') then
begin
showmessage('Bitte geben Sie auch eine Fehlerseite an oder wählen Sie "Standard Error 404 Seite senden" aus.');
Result := False;
end;
if not fileexists(TServerSettingsSingleton.Instance.IndividualErrorPagePath) then
begin
Showmessage('Ihre Fehlerseite existiert nicht, bitte geben Sie eine existierende Fehlerseite an!');
Result := False;
end;
end;
end;
procedure TForm1.DeactivateTheVisualControlls;
begin
//Arbeitsverzeichnis, Port etc. kann während des Starts nicht mehr geändert werden!
WorkingDirectoryEdit.Enabled := False;
WorkDirectorySelectButton.Enabled := False;
PortEdit.Enabled := False;
MaxConnectionsEdit.Enabled := False;
StartButton.Enabled := False;
StandardErrorPageToSendRadioButton.Enabled := False;
OwnErorPageToSendRadioButton.Enabled := False;
ErrorPagePathEdit.Enabled := False;
ErrorPagePathSelectButton.Enabled := False;
SendErrorOnCleanPathRadioButton.Enabled := False;
SendFtpViewRadioButton.Enabled := False;
end;
procedure EnsureTrailingPathDelimiter;
var path:String;
begin
path := TServerSettingsSingleton.Instance.WorkingDirectoryPath;
if not Path.EndsWith(Path_delimiter_OS) then
TServerSettingsSingleton.Instance.WorkingDirectoryPath := Path + Path_delimiter_OS;
end;
procedure TForm1.TryActivateServer;
begin
MyTCPServer.ClearBindings;
try
// Port hinzufügen:
MyTCPServer.SetServerPort;
except
showmessage('Bitte einen gültigen Port eintragen!');
exit;
end;
try
// MaxConnections hinzufügen:
MyTCPServer.SetMaxConnections;
except
showmessage('Bitte eine gültige Anzahl an maximalen Verbindungen eintragen!');
exit;
end;
DeactivateTheVisualControlls;
//Ein aktives binding auf IPv4 ist gerade nicht nötig. Kann man aber mal im Hinterkopf behalten:
//IdTCPServer.Bindings.Items[0].IPVersion := Id_IPv4 ;
try
MyTCPServer.StartServer; //Server ist nun aktiv!
except
on e: Exception do
Begin
showmessage('Fehler beim Starten des Servers! ' +#13+#10+
'Ist der Port schon in Benutzung? Ein höherer Port, der keine speziellen Rechte braucht und nicht bereits benutzt wird, könnte das Problem beheben!' +#13+#10+
'Probieren Sie mal Port 8080 oder Port 8083 aus.' +#13+#10 +#13+#10+
'Ansonsten lautet die Fehlermeldung: ' +#13+#10+
e.Message );
ResetServerStateVisuals;
end;
end;
end;
procedure TForm1.ResetServerStateVisuals;
begin
StopButton.Click; //Den "Stop" Button drücken und somit alle Visuellen Komponenten wieder zurücksetzen!
end;
/////// Start - Button \\\\\\\
procedure TForm1.StartButtonClick(Sender: TObject);
begin
if not SaveServerSettingsToSingleton then Exit;
StopButton.Enabled := True; //Stop-Button aktivierbar machen
if not ValidateDirectory(TServerSettingsSingleton.Instance.WorkingDirectoryPath) then Exit;
if not ValidateErrorDirectory then Exit;
EnsureTrailingPathDelimiter;
//Falls ein delemiter am Ende geschrieben wurde schreiben wir es zurück, damit es angezeigt wird: (ist nur visuel und daher eigentlich optional)
WorkingDirectoryEdit.Text := TServerSettingsSingleton.Instance.WorkingDirectoryPath;
TryActivateServer;
end;
procedure TForm1.StopButtonClick(Sender: TObject);
begin
//IdTCPServer.Active := False; //Server wird gestoppt
MyTCPServer.StopServer;
//Arbeitsverzeichnis, Port etc. wieder aktivieren, damit sie geändert werden können
WorkingDirectoryEdit.Enabled := True;
WorkDirectorySelectButton.Enabled := True;
PortEdit.Enabled := True;
MaxConnectionsEdit.Enabled := True;
StartButton.Enabled := True;
StopButton.Enabled := False;
StandardErrorPageToSendRadioButton.Enabled := True;
OwnErorPageToSendRadioButton.Enabled := True;
ErrorPagePathEdit.Enabled := True;
ErrorPagePathSelectButton.Enabled := True;
SendErrorOnCleanPathRadioButton.Enabled := True;
SendFtpViewRadioButton.Enabled := True;
end;
procedure TForm1.ErrorPagePathSelectButtonClick(Sender: TObject);
begin
OpenDialog1.Filename:='';
OpenDialog1.Filter:='HTML-Dateien (*.html & *.htm)|*.htm;*.html;*.HTML;*.HTM | Alle Dateien Anzeigen | *.*';
if OpenDialog1.Execute then
begin
ErrorPagePathEdit.text:=(OpenDialog1.Filename);
end;
end;
procedure TForm1.WorkDirectorySelectButtonClick(Sender: TObject);
var Pfad:String;
begin
//uses FileCtrl;
//SelectDirectory('Ordner auswählen', '' ,Pfad);
Dialogs.SelectDirectory('Ordner auswählen', '' ,Pfad);
WorkingDirectoryEdit.Text:=pfad;
end;
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
MySystrayClass.Free;
end;
{
procedure TForm1.CreateIdTCPServerAndCallbackFunctions;
begin
//Let's create idTCPServer
IdTCPServer := TIdTCPServer.Create(self);
IdTCPServer.Active := False;
//adding my callback functions:
IdTCPServer.OnDisconnect := IdTCPServerDisconnect;
IdTCPServer.OnExecute := IdTCPServerExecute;
IdTCPServer.OnStatus := IdTCPServerStatus;
end;
}
procedure TForm1.FormCreate(Sender: TObject);
var pfad:String;
begin
Form1.position:=poMainFormCenter; //poDesktopCenter;
//CreateIdTCPServerAndCallbackFunctions;
//Let's create idTCPServer and adding my callback functions:
MyTCPServer := TMyTCPServer.Create(Self);
Path_delimiter_OS := '\';
{$IFDEF FPC}
//PathDelim is known in fpc, so we don't need to define it.
Path_delimiter_OS := PathDelim;
{$ELSE} //Otherwise (It's not fpc, so it's Delphi)
//In Delphi we just define it:
{$IFDEF MSWINDOWS}
//In windows we use the standard separator:
Path_delimiter_OS := '\';
{$ELSE} //not windows, so it's linux, unix, mac... //{$IFDEF LINUX} //{$IFDEF UNIX} //{$IFDEF MACOS}
Path_delimiter_OS := '/';
{$ENDIF}
{$ENDIF}
//Ansonsten setzt fpc den passenden PathDelim / DirectorySeparator für uns für das betriebssystem -> PathDelim, DirectorySeparator: directory separator for each platform ('/', '\', ...)
//Höhe und Größe der Form Einstellen. Unterschiede zwischen Delphi und Lazarus sowie bei Linux und Windows
{$IFDEF FPC} //Wenn FPC
{$IFDEF UNIX} //Wenn Linux / Unix / ...
Form1.ClientHeight:=600;
{$ELSE} //Ansonsten Windows:
Form1.ClientHeight:=725;//485;
{$ENDIF}
{$ELSE} //Ansonsten Delphi
Form1.ClientHeight:=436;
{$ENDIF}
pfad := ExtractFilePath(Application.ExeName); // der Pfad
WorkingDirectoryEdit.Text:=pfad; //den aktuellen pfad wo sich der webserver befindet (gestartet wurde) als standard start verzeichnis verwenden
StatusBar1.Panels[1].text := FormatDateTime('dddd, d. mmmm yyyy - hh:nn', Now) + ' Uhr';
Form1.caption:= 'Simple Webserver '+Version+' von Sönke Schmidt';
StopButton.Enabled := False;
//Die Sachen für "Eigene definierte 404 (html) Seite verwenden:" deaktivieren:
ErrorPagePathEdit.enabled := False;
ErrorPagePathSelectButton.enabled := False;
//Systray initialisieren:
MySystrayClass := SystrayClass.create();
{$IFDEF FPC}
MySystrayClass.CreateSystrayStuffForFPC;
{$ENDIF}
end;
procedure TForm1.Hilfe1Click(Sender: TObject);
begin
showmessage('Gerade keine Lust eine Hilfe zu schreiben :P'+#10+#13+'Vielleicht kommt noch eine Hilfe, aber ich denke es müsste alles verständlich sein.');
end;
procedure TForm1.Info2Click(Sender: TObject);
begin
form2.label3.Caption:=Version;
Form2.position:=poMainFormCenter; //poDesktopCenter;
form2.showmodal;
end;
procedure TForm1.StandardErrorPageToSendRadioButtonClick(Sender: TObject);
begin
ErrorPagePathEdit.enabled := False;
ErrorPagePathSelectButton.enabled := False;
end;
procedure TForm1.OwnErorPageToSendRadioButtonClick(Sender: TObject);
begin
ErrorPagePathEdit.enabled := True;
ErrorPagePathSelectButton.enabled := True;
end;
{
////////////////////////////////////////////////////////////////////////////////
Ab hier beginnt das SysTray!!!
Überwiegend ausgelagert in die Klasse "MySystray"
////////////////////////////////////////////////////////////////////////////////
}
{$IFNDEF FPC}
class procedure TForm1.WMTASKBAREVENT(var message: TMessage);
var point : TPoint;
begin
case message.LParamLo of
WM_LBUTTONDBLCLK : begin
form1.show;
MySystrayClass.TaskBarRemoveIcon;
end;
WM_RBUTTONDOWN : begin
GetCursorPos(point);
popupmenu1.popup(point.x,point.y);
end;
end;
end;
{$ENDIF}
procedure TForm1.ProgrammClick(Sender: TObject);
begin
MySystrayClass.PopupMenuClickOnProgramm;
end;
procedure TForm1.Beenden2Click(Sender: TObject);
begin
MySystrayClass.PopupMenuClickOnBeenden;
end;
procedure TForm1.SystrayClick(Sender: TObject);
begin
MySystrayClass.SystrayClick;
end;
{
////////////////////////////////////////////////////////////////////////////////
Ende des SysTray!!!
////////////////////////////////////////////////////////////////////////////////
}
procedure TForm1.Timer1Timer(Sender: TObject);
begin
StatusBar1.Panels[1].text := FormatDateTime('dddd, d. mmmm yyyy - hh:nn:ss', Now) + ' Uhr';
end;
{$IFDEF FPC}
//See: https://wiki.freepascal.org/Asynchronous_Calls
// https://forum.lazarus.freepascal.org/index.php?topic=48711.0
// https://github.com/blikblum/multilog/blob/master/memochannel.pas
procedure TForm1.WriteToMemo(const AMsg: string);
var
LogMsgToSend: PLogMsgData;
begin
New(LogMsgToSend);
LogMsgToSend^.Text:= AMsg;
//Application.QueueAsyncCall(@WriteAsyncQueue, PtrInt(LogMsgToSend)); // put log msg into queue that will be processed from the main thread after all other messages
Application.QueueAsyncCall(WriteAsyncQueue, PtrInt(LogMsgToSend)); // put log msg into queue that will be processed from the main thread after all other messages
end;
procedure TForm1.WriteAsyncQueue(Data: PtrInt);
var // called from main thread after all other messages have been processed to allow thread safe TMemo access
ReceivedLogMsg: TLogMsgData;
begin
ReceivedLogMsg := PLogMsgData(Data)^;
try
if (Memo1 <> nil) and (not Application.Terminated) then
begin
//...
Memo1.Append(ReceivedLogMsg.Text) // <<< fully thread safe
end;
finally
Dispose(PLogMsgData(Data));
end;
end;
{$ENDIF}
// DisplayOnMemo
procedure TForm1.DisplayOnMemo(str : String);
begin
{$IFDEF FPC}
WriteToMemo(str);
{$ELSE}
TThread.Queue(nil, procedure
begin
Memo1.Lines.Add( str );
end
);
{$ENDIF}
end;
procedure TForm1.Update1Click(Sender: TObject);
begin
showmessage('Ich arbeite unregelmäßig an diesem Projekt. Bitte checken Sie auf meiner Homepage oder der Github Seite nach einer neuen Version...'+ #10+#13+
'Infos dazu finden Sie im Menüreiter unter Info und dann im Aufploppenden Menü auf Info ganz unten klicken.'+ #10+#13+
'Falls sie Bugs oder ähnliches gefunden haben, dürfen Sie mich gerne anschreiben, damit ich die Software verbessern kann.'+#13+#10+
'Um mich anzuschreiben, können sie unter Info->Info meine Homepage Adresse sowie die Github Webseite finden.');
end;
///////////////////////////////////////
end.