-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest01.pas
More file actions
1044 lines (909 loc) · 35 KB
/
test01.pas
File metadata and controls
1044 lines (909 loc) · 35 KB
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
test01 - Simple HTTP Server Component
Author: Gecko71
Copyright: 2025
LICENSE:
========
This code is provided for non-commercial use only. The code is provided "as is"
without warranty of any kind, either expressed or implied, including but not
limited to the implied warranties of merchantability and fitness for a particular
purpose.
You are free to:
- Use this code for personal, educational, or non-commercial purposes
- Modify, adapt, or build upon this code as needed
- Share the code with others under the same license terms
You may not:
- Use this code for commercial purposes without explicit permission
- Remove this license notice from any copies or derivatives
THE AUTHOR(S) SHALL NOT BE LIABLE FOR ANY DAMAGES ARISING FROM THE USE
OF THIS SOFTWARE.
By using this code, you acknowledge that you have read and understood
this license and agree to its terms.
}
unit test01;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, WinSock;
type
TTestResult = record
TestName: string;
Status: string;
Details: string;
end;
TfrmHTTPTester = class(TForm)
btnRunTests: TButton;
mmResults: TMemo;
pnlControls: TPanel;
lblServerAddress: TLabel;
edtServerAddress: TEdit;
lblPort: TLabel;
edtPort: TEdit;
btnClear: TButton;
procedure btnRunTestsClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnClearClick(Sender: TObject);
private
FTestResults: array of TTestResult;
procedure InitializeWinSock;
procedure CleanupWinSock;
function SendHTTPRequest(const Host: string; Port: Integer; const Request: string): string;
procedure AddTestResult(const TestName, Status, Details: string);
procedure RunBasicGETTests;
procedure RunGETParametersTest;
procedure RunGETHeadersTest;
procedure RunGETPathsTest;
procedure RunGETConnectionLimitTest;
procedure RunGETPerformanceTest;
procedure RunMalformedGETTest;
procedure DisplayResults;
procedure RunGETCaseSensitivityTest;
procedure RunGETEncodingEdgeCasesTest;
procedure RunGETRedirectTest;
procedure RunGETSecurityTraversalTest;
procedure RunGETSlowLorisTest;
procedure RunPOSTTest;
procedure TestUploadFile();
procedure TestContentTypeHandling;
public
end;
var
frmHTTPTester: TfrmHTTPTester;
implementation
{$R *.dfm}
uses IdHTTP, IdMultipartFormData;
procedure TfrmHTTPTester.FormCreate(Sender: TObject);
begin
edtServerAddress.Text := '127.0.0.1';
edtPort.Text := '3042';
end;
procedure TfrmHTTPTester.InitializeWinSock;
var
WSAData: TWSAData;
begin
if WSAStartup($202, WSAData) <> 0 then
raise Exception.Create('WSAStartup failed');
end;
procedure TfrmHTTPTester.CleanupWinSock;
begin
WSACleanup;
end;
function TfrmHTTPTester.SendHTTPRequest(const Host: string; Port: Integer; const Request: string): string;
var
ClientSocket: TSocket;
Addr: TSockAddrIn;
Buffer: array[0..4095] of Byte;
BytesReceived: Integer;
Response: AnsiString;
Timeout: Integer;
ModifiedRequest: string;
DefaultUserAgent: string;
begin
Result := '';
ClientSocket := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if ClientSocket = INVALID_SOCKET then
Exit;
try
Timeout := 5000;
setsockopt(ClientSocket, SOL_SOCKET, SO_RCVTIMEO, @Timeout, SizeOf(Timeout));
Addr.sin_family := AF_INET;
Addr.sin_port := htons(Port);
Addr.sin_addr.S_addr := inet_addr(PAnsiChar(AnsiString(Host)));
if connect(ClientSocket, Addr, SizeOf(Addr)) = SOCKET_ERROR then
Exit;
DefaultUserAgent := 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:115.0) Gecko/20100101 Firefox/115.0';
if Pos('User-Agent:', Request) = 0 then
begin
ModifiedRequest := StringReplace(Request, #13#10#13#10,
#13#10 + DefaultUserAgent + #13#10#13#10, []);
end
else
ModifiedRequest := Request;
if (ModifiedRequest <> '') and (send(ClientSocket, PAnsiChar(AnsiString(ModifiedRequest))^, Length(ModifiedRequest), 0) = SOCKET_ERROR) then
Exit;
BytesReceived := recv(ClientSocket, Buffer, SizeOf(Buffer), 0);
while BytesReceived > 0 do
begin
SetString(Response, PAnsiChar(@Buffer[0]), BytesReceived);
Result := Result + string(Response);
BytesReceived := recv(ClientSocket, Buffer, SizeOf(Buffer), 0);
end;
finally
closesocket(ClientSocket);
end;
end;
procedure TfrmHTTPTester.AddTestResult(const TestName, Status, Details: string);
var
Index: Integer;
begin
Index := Length(FTestResults);
SetLength(FTestResults, Index + 1);
FTestResults[Index].TestName := TestName;
FTestResults[Index].Status := Status;
FTestResults[Index].Details := Details;
end;
procedure TfrmHTTPTester.RunBasicGETTests;
var
Response: string;
Host: string;
Port: Integer;
UserAgent: string;
begin
Host := edtServerAddress.Text;
Port := StrToInt(edtPort.Text);
UserAgent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36';
Response := SendHTTPRequest(Host, Port,
'GET / HTTP/1.0' + #13#10 +
'User-Agent: ' + UserAgent + #13#10 +
#13#10);
if Pos('200 OK', Response) > 0 then
AddTestResult('Basic GET Request with User-Agent', 'PASS', 'Received valid 200 OK response with User-Agent: ' + UserAgent)
else if Response = '' then
AddTestResult('Basic GET Request with User-Agent', 'FAIL', 'No response received')
else
AddTestResult('Basic GET Request with User-Agent', 'FAIL', 'Unexpected response: ' + Copy(Response, 1, 100) + '...');
Response := SendHTTPRequest(Host, Port,
'GET /nonexistent HTTP/1.0' + #13#10 +
'User-Agent: ' + UserAgent + #13#10 +
#13#10);
if Pos('404 Not Found', Response) > 0 then
AddTestResult('GET 404 Not Found Test with User-Agent', 'PASS', 'Received correct 404 response with User-Agent: ' + UserAgent)
else if Response = '' then
AddTestResult('GET 404 Not Found Test with User-Agent', 'FAIL', 'No response received')
else
AddTestResult('GET 404 Not Found Test with User-Agent', 'FAIL', 'Unexpected response: ' + Copy(Response, 1, 100) + '...');
Response := SendHTTPRequest(Host, Port,
'GET / HTTP/1.1' + #13#10 +
'Host: ' + Host + #13#10 +
'User-Agent: ' + UserAgent + #13#10 +
#13#10);
if Pos('200 OK', Response) > 0 then
AddTestResult('GET HTTP/1.1 Test with User-Agent', 'PASS', 'Server supports HTTP/1.1 with User-Agent: ' + UserAgent)
else if Response = '' then
AddTestResult('GET HTTP/1.1 Test with User-Agent', 'FAIL', 'No response received')
else
AddTestResult('GET HTTP/1.1 Test with User-Agent', 'FAIL', 'Unexpected response: ' + Copy(Response, 1, 100) + '...');
UserAgent := 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
Response := SendHTTPRequest(Host, Port,
'GET / HTTP/1.0' + #13#10 +
'User-Agent: ' + UserAgent + #13#10 +
#13#10);
if Pos('403 Forbidden', Response) > 0 then
AddTestResult('Suspicious User-Agent Test', 'PASS', 'Server correctly blocked suspicious User-Agent: ' + UserAgent + ' ' + Response )
else if Response = '' then
AddTestResult('Suspicious User-Agent Test', 'FAIL', 'No response received ' + Response)
else
AddTestResult('Suspicious User-Agent Test', 'FAIL', 'Unexpected response: ' + Copy(Response, 1, 100) + '...');
end;
procedure TfrmHTTPTester.RunGETParametersTest;
var
Response: string;
Host: string;
Port: Integer;
begin
Host := edtServerAddress.Text;
Port := StrToInt(edtPort.Text);
Response := SendHTTPRequest(Host, Port, 'GET /?param1=value1¶m2=value2 HTTP/1.0' + #13#10#13#10);
if Pos('200 OK', Response) > 0 then
AddTestResult('GET with Parameters Test', 'PASS', 'Server correctly handled parameters')
else if Response = '' then
AddTestResult('GET with Parameters Test', 'FAIL', 'No response received')
else
AddTestResult('GET with Parameters Test', 'FAIL', 'Unexpected response: ' + Copy(Response, 1, 100) + '...');
Response := SendHTTPRequest(Host, Port, 'GET /?param=za _g l _ja HTTP/1.0' + #13#10#13#10);
if Response <> '' then
AddTestResult('GET with Unicode Parameters Test', 'PASS', 'Server responded to Unicode parameters')
else
AddTestResult('GET with Unicode Parameters Test', 'FAIL', 'No response received');
end;
procedure TfrmHTTPTester.RunGETHeadersTest;
var
Response: string;
Host: string;
Port: Integer;
begin
Host := edtServerAddress.Text;
Port := StrToInt(edtPort.Text);
Response := SendHTTPRequest(Host, Port,
'GET / HTTP/1.0' + #13#10 +
'User-Agent: HTTPTester/1.0' + #13#10#13#10);
if Pos('200 OK', Response) > 0 then
AddTestResult('GET with User-Agent Header', 'PASS', 'Server accepted User-Agent header')
else if Response = '' then
AddTestResult('GET with User-Agent Header', 'FAIL', 'No response received')
else
AddTestResult('GET with User-Agent Header', 'FAIL', 'Unexpected response');
Response := SendHTTPRequest(Host, Port,
'GET / HTTP/1.0' + #13#10 +
'User-Agent: HTTPTester/1.0' + #13#10 +
'Accept: text/html' + #13#10 +
'Accept-Language: pl-PL' + #13#10#13#10);
if Pos('200 OK', Response) > 0 then
AddTestResult('GET with Multiple Headers', 'PASS', 'Server accepted multiple headers')
else if Response = '' then
AddTestResult('GET with Multiple Headers', 'FAIL', 'No response received')
else
AddTestResult('GET with Multiple Headers', 'FAIL', 'Unexpected response');
end;
procedure TfrmHTTPTester.RunGETPathsTest;
var
Response: string;
Host: string;
Port: Integer;
begin
Host := edtServerAddress.Text;
Port := StrToInt(edtPort.Text);
Response := SendHTTPRequest(Host, Port, 'GET /path/to/resource HTTP/1.0' + #13#10#13#10);
if Response <> '' then
AddTestResult('GET with Nested Path', 'PASS', 'Server handled nested path')
else
AddTestResult('GET with Nested Path', 'FAIL', 'No response received');
Response := SendHTTPRequest(Host, Port, 'GET /path%20with%20spaces HTTP/1.0' + #13#10#13#10);
if Response <> '' then
AddTestResult('GET with URL Encoded Path', 'PASS', 'Server handled URL encoded path')
else
AddTestResult('GET with URL Encoded Path', 'FAIL', 'No response received');
Response := SendHTTPRequest(Host, Port, 'GET /resource#fragment HTTP/1.0' + #13#10#13#10);
if Response <> '' then
AddTestResult('GET with URL Fragment', 'PASS', 'Server handled URL fragment')
else
AddTestResult('GET with URL Fragment', 'FAIL', 'No response received');
end;
procedure TfrmHTTPTester.RunGETConnectionLimitTest;
var
Host: string;
Port: Integer;
Sockets: array of TSocket;
Addr: TSockAddrIn;
i, SuccessCount: Integer;
begin
Host := edtServerAddress.Text;
Port := StrToInt(edtPort.Text);
SetLength(Sockets, 10);
Addr.sin_family := AF_INET;
Addr.sin_port := htons(Port);
Addr.sin_addr.S_addr := inet_addr(PAnsiChar(AnsiString(Host)));
SuccessCount := 0;
for i := 0 to High(Sockets) do
begin
Sockets[i] := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if Sockets[i] = INVALID_SOCKET then Break;
if connect(Sockets[i], Addr, SizeOf(Addr)) = 0 then
begin
if send(Sockets[i], PAnsiChar(AnsiString('GET / HTTP/1.0'#13#10#13#10))^, 18, 0) <> SOCKET_ERROR then
Inc(SuccessCount)
else
Break;
end
else
Break;
end;
for i := 0 to High(Sockets) do
if Sockets[i] <> INVALID_SOCKET then
closesocket(Sockets[i]);
if SuccessCount = Length(Sockets) then
AddTestResult('GET Connection Limit Test', 'PASS', Format('All %d GET connections succeeded', [SuccessCount]))
else
AddTestResult('GET Connection Limit Test', 'WARNING', Format('Server rejected connections after %d successful GET requests', [SuccessCount]));
end;
procedure TfrmHTTPTester.RunGETPerformanceTest;
var
Host: string;
Port, i: Integer;
StartTime: Cardinal;
RequestsCount: Integer;
Response: string;
TotalTime: Cardinal;
SuccessCount: Integer;
begin
Host := edtServerAddress.Text;
Port := StrToInt(edtPort.Text);
RequestsCount := 20;
SuccessCount := 0;
TotalTime := 0;
for i := 1 to RequestsCount do
begin
StartTime := GetTickCount;
Response := SendHTTPRequest(Host, Port, 'GET / HTTP/1.0' + #13#10#13#10);
TotalTime := TotalTime + (GetTickCount - StartTime);
if Response <> '' then
Inc(SuccessCount)
else
begin
AddTestResult('GET Performance Test', 'FAIL', 'No response on request ' + IntToStr(i));
Exit;
end;
end;
AddTestResult('GET Performance Test', 'PASS',
Format('%d GET requests completed in %d ms (avg %d ms per request)',
[SuccessCount, TotalTime, Integer(TotalTime) div Integer(RequestsCount) ]));
StartTime := GetTickCount;
for i := 1 to 5 do
begin
Response := SendHTTPRequest(Host, Port,
'GET /very/long/path/with/many/segments/to/test/server/path/handling/capabilities HTTP/1.0' + #13#10#13#10);
if Response = '' then
begin
AddTestResult('GET Performance - Long Paths', 'FAIL', 'Failed at request ' + IntToStr(i));
Exit;
end;
end;
AddTestResult('GET Performance - Long Paths', 'PASS',
Format('5 long path GET requests completed in %d ms', [GetTickCount - StartTime]));
end;
procedure TfrmHTTPTester.RunMalformedGETTest;
var
Host: string;
Port: Integer;
Response: string;
begin
Host := edtServerAddress.Text;
Port := StrToInt(edtPort.Text);
Response := SendHTTPRequest(Host, Port, 'GET/HTTP/1.0' + #13#10#13#10);
if Response <> '' then
AddTestResult('Malformed GET Test (No Space)', 'PASS', 'Server responded to malformed GET request')
else
AddTestResult('Malformed GET Test (No Space)', 'WARNING', 'Server closed connection');
Response := SendHTTPRequest(Host, Port, 'GET' + #13#10#13#10);
if Response <> '' then
AddTestResult('Incomplete GET Test', 'PASS', 'Server responded to incomplete GET request')
else
AddTestResult('Incomplete GET Test', 'WARNING', 'Server closed connection');
Response := SendHTTPRequest(Host, Port, 'GET ' + StringOfChar('a', 2000) + ' HTTP/1.0' + #13#10#13#10);
if Response <> '' then
AddTestResult('GET with Very Long URL', 'PASS', 'Server handled very long URL')
else
AddTestResult('GET with Very Long URL', 'WARNING', 'Server closed connection');
Response := SendHTTPRequest(Host, Port, '');
if Response = '' then
AddTestResult('Empty Request Test', 'PASS', 'Server closed connection (expected)')
else
AddTestResult('Empty Request Test', 'WARNING', 'Server responded: ' + Copy(Response, 1, 100) + '...');
end;
procedure TfrmHTTPTester.DisplayResults;
var
i, PassCount, FailCount, WarningCount: Integer;
begin
mmResults.Lines.Clear;
PassCount := 0;
FailCount := 0;
WarningCount := 0;
mmResults.Lines.Add('======= TEST RESULTS =======');
mmResults.Lines.Add('Server: ' + edtServerAddress.Text + ':' + edtPort.Text);
mmResults.Lines.Add('Date: ' + FormatDateTime('yyyy-mm-dd hh:nn:ss', Now));
mmResults.Lines.Add('============================');
for i := 0 to High(FTestResults) do
begin
mmResults.Lines.Add(Format('Test: %s', [FTestResults[i].TestName]));
mmResults.Lines.Add(Format('Status: %s', [FTestResults[i].Status]));
mmResults.Lines.Add(Format('Details: %s', [FTestResults[i].Details]));
mmResults.Lines.Add('----------------------------');
if FTestResults[i].Status = 'PASS' then
Inc(PassCount)
else if FTestResults[i].Status = 'WARNING' then
Inc(WarningCount)
else
Inc(FailCount);
end;
mmResults.Lines.Add(Format('SUMMARY: %d passed, %d warnings, %d failed', [PassCount, WarningCount, FailCount]));
mmResults.Lines.Add('============================');
end;
procedure TfrmHTTPTester.btnRunTestsClick(Sender: TObject);
begin
Screen.Cursor := crHourGlass;
try
InitializeWinSock;
try
SetLength(FTestResults, 0);
mmResults.Lines.Clear;
mmResults.Lines.Add('Starting HTTP GET tests...');
Application.ProcessMessages;
RunBasicGETTests;
RunGETParametersTest;
RunGETHeadersTest;
RunGETPathsTest;
RunGETConnectionLimitTest;
RunGETPerformanceTest;
RunMalformedGETTest;
RunGETCaseSensitivityTest;
RunGETEncodingEdgeCasesTest;
RunGETRedirectTest;
RunGETSecurityTraversalTest;
RunGETSlowLorisTest;
RunPOSTTest;
TestUploadFile;
TestContentTypeHandling;
DisplayResults;
finally
CleanupWinSock;
end;
finally
Screen.Cursor := crDefault;
end;
end;
procedure TfrmHTTPTester.btnClearClick(Sender: TObject);
begin
mmResults.Lines.Clear;
end;
procedure TfrmHTTPTester.RunGETCaseSensitivityTest;
var
Host: string;
Port: Integer;
Response: string;
begin
Host := edtServerAddress.Text;
Port := StrToInt(edtPort.Text);
Response := SendHTTPRequest(Host, Port, 'GET /TestCase HTTP/1.0' + #13#10#13#10);
if Pos('200 OK', Response) > 0 then
AddTestResult('Case Sensitivity Test', 'PASS', 'Server treats path as case-sensitive')
else
AddTestResult('Case Sensitivity Test', 'INFO', 'Server might treat paths as case-insensitive or not found');
end;
procedure TfrmHTTPTester.TestContentTypeHandling;
var
Host: string;
Port: Integer;
ClientSocket: TSocket;
Addr: TSockAddrIn;
Request: string;
FormData, MultipartData: string;
Boundary: string;
Buffer: array[0..4095] of Byte;
BytesReceived: Integer;
TempResponse: AnsiString;
TotalResponse: string;
Timeout: Integer;
begin
Host := edtServerAddress.Text;
Port := StrToInt(edtPort.Text);
ClientSocket := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if ClientSocket = INVALID_SOCKET then
begin
AddTestResult('Form URL Encoded Test', 'FAIL', 'Could not create socket');
Exit;
end;
try
Timeout := 5000;
setsockopt(ClientSocket, SOL_SOCKET, SO_RCVTIMEO, @Timeout, SizeOf(Timeout));
Addr.sin_family := AF_INET;
Addr.sin_port := htons(Port);
Addr.sin_addr.S_addr := inet_addr(PAnsiChar(AnsiString(Host)));
if connect(ClientSocket, Addr, SizeOf(Addr)) = SOCKET_ERROR then
begin
AddTestResult('Form URL Encoded Test', 'FAIL', 'Could not connect to server');
Exit;
end;
FormData := 'param1=value1¶m2=value+with+spaces¶m3=special%21%40%23%24';
Request :=
'POST /form-test HTTP/1.1' + #13#10 +
'Host: ' + Host + #13#10 +
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:115.0) Gecko/20100101 Firefox/115.0' + #13#10 +
'Content-Type: application/x-www-form-urlencoded' + #13#10 +
'Content-Length: ' + IntToStr(Length(FormData)) + #13#10 +
'Connection: close' + #13#10 +
#13#10 +
FormData;
if send(ClientSocket, PAnsiChar(AnsiString(Request))^, Length(Request), 0) = SOCKET_ERROR then
begin
AddTestResult('Form URL Encoded Test', 'FAIL', 'Failed to send request');
Exit;
end;
TotalResponse := '';
repeat
BytesReceived := recv(ClientSocket, Buffer, SizeOf(Buffer), 0);
if BytesReceived > 0 then
begin
SetString(TempResponse, PAnsiChar(@Buffer[0]), BytesReceived);
TotalResponse := TotalResponse + string(TempResponse);
end;
until BytesReceived <= 0;
if (Pos('200 OK', TotalResponse) > 0) and
((Pos('param1', TotalResponse) > 0) or (Pos('value1', TotalResponse) > 0)) then
AddTestResult('Form URL Encoded Test', 'PASS', 'Server correctly parsed form-urlencoded data')
else if TotalResponse = '' then
AddTestResult('Form URL Encoded Test', 'FAIL', 'No response received')
else
AddTestResult('Form URL Encoded Test', 'WARNING', 'Response received but parameters may not be parsed');
finally
closesocket(ClientSocket);
end;
ClientSocket := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if ClientSocket = INVALID_SOCKET then
begin
AddTestResult('Multipart Form Data Test', 'FAIL', 'Could not create socket');
Exit;
end;
try
Timeout := 10000;
setsockopt(ClientSocket, SOL_SOCKET, SO_RCVTIMEO, @Timeout, SizeOf(Timeout));
if connect(ClientSocket, Addr, SizeOf(Addr)) = SOCKET_ERROR then
begin
AddTestResult('Multipart Form Data Test', 'FAIL', 'Could not connect to server');
Exit;
end;
Boundary := 'boundary' + FormatDateTime('hhnnsszzz', Now);
MultipartData :=
'--' + Boundary + #13#10 +
'Content-Disposition: form-data; name="text_field"' + #13#10 +
#13#10 +
'This is a text field' + #13#10 +
'--' + Boundary + #13#10 +
'Content-Disposition: form-data; name="file1"; filename="test.txt"' + #13#10 +
'Content-Type: text/plain' + #13#10 +
#13#10 +
'This is the content of a test file.' + #13#10 +
'--' + Boundary + '--' + #13#10;
Request :=
'POST /multipart-test HTTP/1.1' + #13#10 +
'Host: ' + Host + #13#10 +
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:115.0) Gecko/20100101 Firefox/115.0' + #13#10 +
'Content-Type: multipart/form-data; boundary=' + Boundary + #13#10 +
'Content-Length: ' + IntToStr(Length(MultipartData)) + #13#10 +
'Connection: close' + #13#10 +
#13#10 +
MultipartData;
if send(ClientSocket, PAnsiChar(AnsiString(Request))^, Length(Request), 0) = SOCKET_ERROR then
begin
AddTestResult('Multipart Form Data Test', 'FAIL', 'Failed to send request');
Exit;
end;
TotalResponse := '';
repeat
BytesReceived := recv(ClientSocket, Buffer, SizeOf(Buffer), 0);
if BytesReceived > 0 then
begin
SetString(TempResponse, PAnsiChar(@Buffer[0]), BytesReceived);
TotalResponse := TotalResponse + string(TempResponse);
end;
until BytesReceived <= 0;
if (Pos('200 OK', TotalResponse) > 0) and
((Pos('text_field', TotalResponse) > 0) or (Pos('file1', TotalResponse) > 0)) then
AddTestResult('Multipart Form Data Test', 'PASS', 'Server correctly parsed multipart/form-data')
else if TotalResponse = '' then
AddTestResult('Multipart Form Data Test', 'FAIL', 'No response received')
else
AddTestResult('Multipart Form Data Test', 'WARNING', 'Response received but multipart data may not be parsed');
finally
closesocket(ClientSocket);
end;
end;
procedure TfrmHTTPTester.RunPOSTTest;
var
Host: string;
Port: Integer;
PostData: string;
ClientSocket: TSocket;
Addr: TSockAddrIn;
Request: string;
Buffer: array[0..4095] of Byte;
BytesReceived: Integer;
TempResponse: AnsiString;
TotalResponse: string;
Timeout: Integer;
begin
Host := edtServerAddress.Text;
Port := StrToInt(edtPort.Text);
ClientSocket := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if ClientSocket = INVALID_SOCKET then
begin
AddTestResult('Basic POST Request', 'FAIL', 'Could not create socket');
Exit;
end;
try
Timeout := 5000;
setsockopt(ClientSocket, SOL_SOCKET, SO_RCVTIMEO, @Timeout, SizeOf(Timeout));
Addr.sin_family := AF_INET;
Addr.sin_port := htons(Port);
Addr.sin_addr.S_addr := inet_addr(PAnsiChar(AnsiString(Host)));
if connect(ClientSocket, Addr, SizeOf(Addr)) = SOCKET_ERROR then
begin
AddTestResult('Basic POST Request', 'FAIL', 'Could not connect to server');
Exit;
end;
PostData := 'test=value&another=test';
Request :=
'POST /post-test HTTP/1.1' + #13#10 +
'Host: ' + Host + #13#10 +
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:115.0) Gecko/20100101 Firefox/115.0' + #13#10 +
'Content-Type: application/x-www-form-urlencoded' + #13#10 +
'Content-Length: ' + IntToStr(Length(PostData)) + #13#10 +
'Connection: close' + #13#10 +
#13#10 +
PostData;
if send(ClientSocket, PAnsiChar(AnsiString(Request))^, Length(Request), 0) = SOCKET_ERROR then
begin
AddTestResult('Basic POST Request', 'FAIL', 'Failed to send request');
Exit;
end;
TotalResponse := '';
repeat
BytesReceived := recv(ClientSocket, Buffer, SizeOf(Buffer), 0);
if BytesReceived > 0 then
begin
SetString(TempResponse, PAnsiChar(@Buffer[0]), BytesReceived);
TotalResponse := TotalResponse + string(TempResponse);
end;
until BytesReceived <= 0;
if Pos('200 OK', TotalResponse) > 0 then
AddTestResult('Basic POST Request', 'PASS', 'Received valid 200 OK response')
else if TotalResponse = '' then
AddTestResult('Basic POST Request', 'FAIL', 'No response received')
else
AddTestResult('Basic POST Request', 'FAIL', 'Unexpected response: ' + Copy(TotalResponse, 1, 100) + '...');
finally
closesocket(ClientSocket);
end;
ClientSocket := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if ClientSocket <> INVALID_SOCKET then
begin
try
Timeout := 5000;
setsockopt(ClientSocket, SOL_SOCKET, SO_RCVTIMEO, @Timeout, SizeOf(Timeout));
if connect(ClientSocket, Addr, SizeOf(Addr)) = 0 then
begin
Request :=
'POST /post-test HTTP/1.1' + #13#10 +
'Host: ' + Host + #13#10 +
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:115.0) Gecko/20100101 Firefox/115.0' + #13#10 +
'Content-Type: application/x-www-form-urlencoded' + #13#10 +
'Content-Length: 0' + #13#10 +
'Connection: close' + #13#10 +
#13#10;
if send(ClientSocket, PAnsiChar(AnsiString(Request))^, Length(Request), 0) <> SOCKET_ERROR then
begin
TotalResponse := '';
repeat
BytesReceived := recv(ClientSocket, Buffer, SizeOf(Buffer), 0);
if BytesReceived > 0 then
begin
SetString(TempResponse, PAnsiChar(@Buffer[0]), BytesReceived);
TotalResponse := TotalResponse + string(TempResponse);
end;
until BytesReceived <= 0;
if TotalResponse <> '' then
AddTestResult('POST with Empty Body', 'PASS', 'Server handled empty POST body')
else
AddTestResult('POST with Empty Body', 'FAIL', 'No response received');
end
else
AddTestResult('POST with Empty Body', 'FAIL', 'Failed to send request');
end
else
AddTestResult('POST with Empty Body', 'FAIL', 'Could not connect to server');
finally
closesocket(ClientSocket);
end;
end
else
AddTestResult('POST with Empty Body', 'FAIL', 'Could not create socket');
ClientSocket := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if ClientSocket <> INVALID_SOCKET then
begin
try
Timeout := 10000;
setsockopt(ClientSocket, SOL_SOCKET, SO_RCVTIMEO, @Timeout, SizeOf(Timeout));
if connect(ClientSocket, Addr, SizeOf(Addr)) = 0 then
begin
PostData := StringOfChar('a', 5000);
Request :=
'POST /post-test HTTP/1.1' + #13#10 +
'Host: ' + Host + #13#10 +
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:115.0) Gecko/20100101 Firefox/115.0' + #13#10 +
'Content-Type: text/plain' + #13#10 +
'Content-Length: ' + IntToStr(Length(PostData)) + #13#10 +
'Connection: close' + #13#10 +
#13#10;
if send(ClientSocket, PAnsiChar(AnsiString(Request))^, Length(Request), 0) <> SOCKET_ERROR then
begin
if send(ClientSocket, PAnsiChar(AnsiString(PostData))^, Length(PostData), 0) <> SOCKET_ERROR then
begin
TotalResponse := '';
repeat
BytesReceived := recv(ClientSocket, Buffer, SizeOf(Buffer), 0);
if BytesReceived > 0 then
begin
SetString(TempResponse, PAnsiChar(@Buffer[0]), BytesReceived);
TotalResponse := TotalResponse + string(TempResponse);
end;
until BytesReceived <= 0;
if Pos('200 OK', TotalResponse) > 0 then
AddTestResult('POST with Large Body', 'PASS', 'Server handled large POST body')
else if TotalResponse = '' then
AddTestResult('POST with Large Body', 'FAIL', 'No response received')
else
AddTestResult('POST with Large Body', 'FAIL', 'Unexpected response: ' + Copy(TotalResponse, 1, 100) + '...');
end
else
AddTestResult('POST with Large Body', 'FAIL', 'Failed to send POST data');
end
else
AddTestResult('POST with Large Body', 'FAIL', 'Failed to send request headers');
end
else
AddTestResult('POST with Large Body', 'FAIL', 'Could not connect to server');
finally
closesocket(ClientSocket);
end;
end
else
AddTestResult('POST with Large Body', 'FAIL', 'Could not create socket');
ClientSocket := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if ClientSocket <> INVALID_SOCKET then
begin
try
Timeout := 5000;
setsockopt(ClientSocket, SOL_SOCKET, SO_RCVTIMEO, @Timeout, SizeOf(Timeout));
if connect(ClientSocket, Addr, SizeOf(Addr)) = 0 then
begin
PostData := '{"key":"value","number":123}';
Request :=
'POST /post-test HTTP/1.1' + #13#10 +
'Host: ' + Host + #13#10 +
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:115.0) Gecko/20100101 Firefox/115.0' + #13#10 +
'Content-Type: application/json' + #13#10 +
'Content-Length: ' + IntToStr(Length(PostData)) + #13#10 +
'Connection: close' + #13#10 +
#13#10 +
PostData;
if send(ClientSocket, PAnsiChar(AnsiString(Request))^, Length(Request), 0) <> SOCKET_ERROR then
begin
TotalResponse := '';
repeat
BytesReceived := recv(ClientSocket, Buffer, SizeOf(Buffer), 0);
if BytesReceived > 0 then
begin
SetString(TempResponse, PAnsiChar(@Buffer[0]), BytesReceived);
TotalResponse := TotalResponse + string(TempResponse);
end;
until BytesReceived <= 0;
if Pos('200 OK', TotalResponse) > 0 then
AddTestResult('POST with JSON', 'PASS', 'Server accepted JSON payload')
else if TotalResponse = '' then
AddTestResult('POST with JSON', 'FAIL', 'No response received')
else
AddTestResult('POST with JSON', 'FAIL', 'Unexpected response: ' + Copy(TotalResponse, 1, 100) + '...');
end
else
AddTestResult('POST with JSON', 'FAIL', 'Failed to send request');
end
else
AddTestResult('POST with JSON', 'FAIL', 'Could not connect to server');
finally
closesocket(ClientSocket);
end;
end
else
AddTestResult('POST with JSON', 'FAIL', 'Could not create socket');
end;
procedure TfrmHTTPTester.RunGETEncodingEdgeCasesTest;
var
Host: string;
Port: Integer;
Response: string;
begin
Host := edtServerAddress.Text;
Port := StrToInt(edtPort.Text);
Response := SendHTTPRequest(Host, Port, 'GET /%ZZ HTTP/1.0' + #13#10#13#10);
if (Response <> '') then
AddTestResult('URL Encoding Edge Case', 'PASS', 'Server responded to invalid encoded URL')
else
AddTestResult('URL Encoding Edge Case', 'WARNING', 'Server closed connection or ignored malformed encoding');
end;
procedure TfrmHTTPTester.RunGETRedirectTest;
var
Host: string;
Port: Integer;
Response: string;
begin
Host := edtServerAddress.Text;
Port := StrToInt(edtPort.Text);
Response := SendHTTPRequest(Host, Port, 'GET /redirect HTTP/1.0' + #13#10#13#10);
if Pos('301', Response) > 0 then
AddTestResult('Redirect Test', 'PASS', 'Received 301 Moved Permanently')
else if Pos('302', Response) > 0 then
AddTestResult('Redirect Test', 'PASS', 'Received 302 Found')
else
AddTestResult('Redirect Test', 'INFO', 'No redirect response or endpoint missing');
end;
procedure TfrmHTTPTester.RunGETSecurityTraversalTest;
var
Host: string;
Port: Integer;
Response: string;
begin
Host := edtServerAddress.Text;
Port := StrToInt(edtPort.Text);
Response := SendHTTPRequest(Host, Port, 'GET /../secret HTTP/1.0' + #13#10#13#10);
if Pos('404', Response) > 0 then
AddTestResult('Path Traversal Test', 'PASS', 'Server blocked path traversal')
else if Pos('200 OK', Response) > 0 then
AddTestResult('Path Traversal Test', 'FAIL', 'Potential directory traversal vulnerability')
else
AddTestResult('Path Traversal Test', 'INFO', 'Response: ' + Copy(Response, 1, 100));
end;
procedure TfrmHTTPTester.RunGETSlowLorisTest;
var
Host: string;
Port: Integer;
ClientSocket: TSocket;
Addr: TSockAddrIn;
SentLen: Integer;
begin
Host := edtServerAddress.Text;
Port := StrToInt(edtPort.Text);
ClientSocket := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if ClientSocket = INVALID_SOCKET then
begin
AddTestResult('Slow Loris Test', 'FAIL', 'Could not create socket');
Exit;
end;
try
Addr.sin_family := AF_INET;
Addr.sin_port := htons(Port);
Addr.sin_addr.S_addr := inet_addr(PAnsiChar(AnsiString(Host)));
if connect(ClientSocket, Addr, SizeOf(Addr)) = SOCKET_ERROR then
begin
AddTestResult('Slow Loris Test', 'FAIL', 'Could not connect to server');
Exit;
end;
SentLen := send(ClientSocket, PAnsiChar(AnsiString('GET /slow HTTP/1.1'#13#10))^, 21, 0);
Sleep(2000);
SentLen := send(ClientSocket, PAnsiChar(AnsiString('Host: localhost'#13#10))^, 18, 0);
Sleep(2000);
SentLen := send(ClientSocket, PAnsiChar(AnsiString(#13#10))^, 2, 0);
if SentLen > 0 then