-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlcinoe.HTTP.Server.HttpSys.pas
More file actions
2217 lines (2043 loc) · 106 KB
/
Alcinoe.HTTP.Server.HttpSys.pas
File metadata and controls
2217 lines (2043 loc) · 106 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
unit Alcinoe.HTTP.Server.HttpSys;
//
// Advantages of using http.sys
//
// * Kernel-mode HTTP stack → runs directly inside Windows networking core, giving you
// IIS-level performance, stability, and security without needing a heavyweight web server.
// * Port sharing → multiple apps can listen on the same port with different URL prefixes.
// * I/O Completion Ports → your thread only spends time building the response.
// Receiving requests and sending responses (even to very slow clients) is
// handled in the kernel, so your thread never blocks. Concretely, this means
// a single thread can handle thousands of simultaneous connections.
// * TLS/SSL handled in kernel → you don’t manage OpenSSL/Schannel yourself
// * Modern protocols → supports HTTP/1.1, HTTP/2, and HTTP/3 (QUIC) out of the box.
// * Automatic timeout handling → idle, header, and entity-body timeouts enforced by kernel.
// * Kernel-mode response caching → frequently requested responses can be cached directly
// in kernel space, bypassing user-mode, which reduces latency and CPU usage.
// * Kernel-managed W3C logging → http.sys writes W3C logs for you and handles
// file rotation and I/O off the request path, so your app avoids blocking.
// * Quality of Service (QoS) → bandwidth throttling and connection limits built in.
// * Widely tested → same engine IIS uses under the hood
//
//
// Enable HTTP on localhost:23456 with HTTP.sys (Windows 10+)
// Enable HTTPS on localhost:34567 with HTTP.sys (Windows 10+)
// -----------------------------------------------------------
//
// Run PowerShell (Admin)
//
// 1) Create a self-signed cert for localhost
// $cert = New-SelfSignedCertificate -DnsName "localhost" -FriendlyName "Dev Localhost" -CertStoreLocation "Cert:\LocalMachine\My" -NotAfter (Get-Date).AddYears(99)
//
// 2) (Optional but nice) Trust it to avoid browser warnings
// $store = New-Object System.Security.Cryptography.X509Certificates.X509Store("Root","LocalMachine")
// $store.Open("ReadWrite"); $store.Add($cert); $store.Close()
//
// 3) Grab the thumbprint (no spaces)
// $tp = ($cert.Thumbprint -replace ' ')
// $appid = [guid]::NewGuid().Guid
//
// 4) Bind the cert to port 34567 (IPv4 + IPv6)
// netsh http add sslcert ipport=0.0.0.0:34567 certhash=$tp appid="{$appid}" certstorename=MY
// netsh http add sslcert ipport=[::]:34567 certhash=$tp appid="{$appid}" certstorename=MY
//
// Open the Local Computer certificate console: certlm.msc
// You should see the cert in:
// - Personal → Certificates
// - Trusted Root Certification Authorities → Certificates
//
// 5) Allow your user to reserve the HTTPS URL
// netsh http add urlacl url="https://+:34567/" user="YourWindowsUser"
//
// 5) Allow your user to reserve the HTTP URL
// netsh http add urlacl url="https://+:23456/" user="YourWindowsUser"
//
//
// High-performance HTTP Server code example
// https://learn.microsoft.com/en-us/windows/win32/http/http-server-hp-code-example
//
interface
{$I Alcinoe.inc}
uses
System.Generics.Collections,
System.Classes,
Alcinoe.HTTP,
Alcinoe.StringUtils,
Alcinoe.StringList,
Alcinoe.Net,
Alcinoe.Url,
Alcinoe.HTTP.Server,
Alcinoe.Winapi.Http,
Alcinoe.WinApi.Windows;
type
{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
TALHttpSysServerRequestHeaders = Class(TALHttpRequestHeadersA)
private
FHeaders: PHTTP_REQUEST_HEADERS;
FKnownHeaders: array[0..Ord(HTTP_HEADER_ID.HttpHeaderRequestMaximum)-1] of AnsiString;
FCookies: TALStringsA;
FUnknownHeaders: TALStringsA;
function PropertyIndexToHeaderID(const APropertyIndex: Integer): HTTP_HEADER_ID;
protected
function GetCookies: TALStringsA; override;
function GetUnknownHeaders: TALStringsA; override;
function GetHeaderValueByPropertyIndex(const Index: Integer): AnsiString; override;
procedure SetHeaderValueByPropertyIndex(const Index: Integer; const Value: AnsiString); override;
Function GetRawHeaderText: AnsiString; override;
procedure SetRawHeaderText(const ARawHeaderText: AnsiString); override;
public
constructor Create(const AHeaders: PHTTP_REQUEST_HEADERS); virtual;
destructor Destroy; override;
procedure Clear; override;
End;
{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
TALHttpSysServerResponseHeaders = Class(TALHttpResponseHeadersA)
private
FKnownHeaders: array[0..Ord(HTTP_HEADER_ID.HttpHeaderResponseMaximum)-1] of AnsiString;
FCookies: TObjectList<TALHttpCookieA>;
FUnknownHeaders: TALStringsA;
function PropertyIndexToHeaderID(const APropertyIndex: Integer): HTTP_HEADER_ID;
protected
function GetCookies: TObjectList<TALHttpCookieA>; override;
function GetUnknownHeaders: TALStringsA; override;
function GetHeaderValueByPropertyIndex(const Index: Integer): AnsiString; override;
procedure SetHeaderValueByPropertyIndex(const Index: Integer; const Value: AnsiString); override;
procedure SetRawHeaderText(const ARawHeaderText: AnsiString); override;
Function GetRawHeaderText: AnsiString; override;
public
constructor Create; virtual;
destructor Destroy; override;
procedure Clear; override;
end;
{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
TALHttpSysServerRequest = class(TALHttpServerRequestA)
private
FHttpRequest: PHTTP_REQUEST;
FRawUrl: AnsiString;
FCookedUrl: TALCookedUrlA;
FVerb: AnsiString;
FHeaders: TALHttpSysServerRequestHeaders;
FBodyStream: TALStringStreamA;
FRemoteAddress: TALNetEndpoint;
FLocalAddress: TALNetEndpoint;
protected
function GetVersion: TALHttpVersion; override;
procedure SetVersion(const AValue: TALHttpVersion); override;
function GetVerb: AnsiString; override;
procedure SetVerb(const AValue: AnsiString); override;
function GetRawUrl: AnsiString; override;
procedure SetRawUrl(const AValue: AnsiString); override;
function GetCookedUrl: TALCookedUrlA; override;
function GetHeaders: TALHttpRequestHeadersA; override;
function GetBodyStream: TStream; override;
procedure SetBodyStream(const AValue: TStream); override;
function GetOwnsBodyStream: Boolean; override;
procedure SetOwnsBodyStream(const AValue: Boolean); override;
function GetBodyString: AnsiString; override;
procedure SetBodyString(const AValue: AnsiString); override;
function GetRemoteAddress: TALNetEndpoint; override;
procedure SetRemoteAddress(const AValue: TALNetEndpoint); override;
function GetLocalAddress: TALNetEndpoint; override;
procedure SetLocalAddress(const AValue: TALNetEndpoint); override;
function GetIsSecure: Boolean; override;
public
constructor Create(const AHttpRequest: PHTTP_REQUEST); virtual;
destructor Destroy; override;
function ExtractHeaders: TALHttpRequestHeadersA; override;
function ExtractBodyStream: TStream; override;
end;
{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
TALHttpSysServerResponse = class(TALHttpServerResponseA)
private
FVersion: TALHttpVersion;
FStatusCode: Integer;
FReason: AnsiString;
FHeaders: TALHttpSysServerResponseHeaders;
FBodyStream: TStream;
FOwnsBodyStream: Boolean;
FBodyFileHandle: THandle;
FBodyByteRangeStartingOffset: UInt64;
FBodyByteRangeLength: UInt64;
FCachePolicyType: TALHttpServerResponseA.TCachePolicyType;
FCacheSecondsToLive: Cardinal;
protected
function GetVersion: TALHttpVersion; override;
procedure SetVersion(const AValue: TALHttpVersion); override;
function GetStatusCode: Integer; override;
procedure SetStatusCode(const AValue: Integer); override;
function GetReason: AnsiString; override;
procedure SetReason(const AValue: AnsiString); override;
function GetHeaders: TALHttpResponseHeadersA; override;
function GetBodyStream: TStream; override;
procedure SetBodyStream(const AValue: TStream); override;
function GetOwnsBodyStream: Boolean; override;
procedure SetOwnsBodyStream(const AValue: Boolean); override;
function GetBodyString: AnsiString; override;
procedure SetBodyString(const AValue: AnsiString); override;
function GetBodyFileHandle: THandle; override;
procedure SetBodyFileHandle(const AValue: THandle); override;
function GetBodyByteRangeStartingOffset: UInt64; override;
procedure SetBodyByteRangeStartingOffset(const AValue: UInt64); override;
function GetBodyByteRangeLength: UInt64; override;
procedure SetBodyByteRangeLength(const AValue: UInt64); override;
function GetCachePolicyType: TALHttpServerResponseA.TCachePolicyType; override;
procedure SetCachePolicyType(const AValue: TALHttpServerResponseA.TCachePolicyType); override;
function GetCacheSecondsToLive: Cardinal; override;
procedure SetCacheSecondsToLive(const AValue: Cardinal); override;
public
constructor Create; virtual;
destructor Destroy; override;
function ExtractHeaders: TALHttpResponseHeadersA; override;
function ExtractBodyStream: TStream; override;
end;
{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
TALHttpSysServer = class(TALHttpServerA)
private
class var FHttpApiModuleHandle: HMODULE;
public
type
// ------------
// TLoggingInfo
TLoggingInfo = Class(TObject)
private
FEnabled: Boolean;
FLoggingFlags: Cardinal;
FSoftwareName: String;
FDirectoryName: String;
FFormat: HTTP_LOGGING_TYPE;
FFields: Cardinal;
FRolloverType: HTTP_LOGGING_ROLLOVER_TYPE;
FRolloverSize: Cardinal;
public
constructor Create; virtual;
property Enabled: Boolean read FEnabled write FEnabled;
property LoggingFlags: Cardinal read FLoggingFlags write FLoggingFlags;
property SoftwareName: String read FSoftwareName write FSoftwareName;
property DirectoryName: String read FDirectoryName write FDirectoryName;
property Format: HTTP_LOGGING_TYPE read FFormat write FFormat;
property Fields: Cardinal read FFields write FFields;
property RolloverType: HTTP_LOGGING_ROLLOVER_TYPE read FRolloverType write FRolloverType;
property RolloverSize: Cardinal read FRolloverSize write FRolloverSize;
End;
private
type
// -------------
// TWorkerThread
TWorkerThread = Class(TThread)
private
FOwner: TALHttpSysServer;
protected
procedure Execute; override;
public
constructor Create(const AOwner: TALHttpSysServer);
End;
private
FUrlPrefixes: TALStringListW;
FLoggingInfo: TLoggingInfo;
FWorkerThreads: TObjectList<TWorkerThread>;
FMinWorkerThreadCount: Integer;
FMaxWorkerThreadCount: Integer;
FWorkerThreadCount: Integer;
FBusyWorkerThreadCount: Integer;
FPeakWorkerThreadCount: Integer;
FPeakBusyWorkerThreadCount: Integer;
FRequestQueueHandle: HANDLE;
FIoCompletionPort: THandle;
FServerSessionId: HTTP_SERVER_SESSION_ID;
FUrlGroupId: HTTP_URL_GROUP_ID;
FIOCPPendingInitialReceiveCount: Integer;
FIOCPPendingContinuationCount: Integer;
FMinInitialReceiveCount: Integer;
FMaxInitialReceiveCount: Integer;
public
constructor Create; override;
destructor Destroy; override;
procedure Start; override;
procedure Stop; override;
/// <summary>
/// Minimum number of worker threads to start with.
/// If set to 0, defaults to (CPU core count * 4).
/// </summary>
property MinWorkerThreadCount: Integer read FMinWorkerThreadCount write FMinWorkerThreadCount;
/// <summary>
/// Maximum number of worker threads the pool can grow to.
/// Set to 0 (or any value <= MinWorkerThreadCount) to disable dynamic growth.
/// </summary>
property MaxWorkerThreadCount: Integer read FMaxWorkerThreadCount write FMaxWorkerThreadCount;
property WorkerThreadCount: Integer read FWorkerThreadCount;
property BusyWorkerThreadCount: Integer read FBusyWorkerThreadCount;
property PeakWorkerThreadCount: Integer read FPeakWorkerThreadCount;
property PeakBusyWorkerThreadCount: Integer read FPeakBusyWorkerThreadCount;
property IOCPPendingInitialReceiveCount: Integer read FIOCPPendingInitialReceiveCount;
property IOCPPendingContinuationCount: Integer read FIOCPPendingContinuationCount;
property UrlPrefixes: TALStringListW read FUrlPrefixes;
property LoggingInfo: TLoggingInfo read FLoggingInfo;
end;
function ALHttpRequestHeaderIdToNameA(const AHeaderID: HTTP_HEADER_ID): AnsiString;
function ALHttpResponseHeaderIdToNameA(const AHeaderID: HTTP_HEADER_ID): AnsiString;
function ALHttpVerbToStringA(const AVERB: HTTP_VERB): AnsiString;
implementation
uses
System.SysUtils,
System.Math,
Winapi.Windows,
Alcinoe.Common;
{*********************************************************************************}
function ALHttpRequestHeaderIdToNameA(const AHeaderID: HTTP_HEADER_ID): AnsiString;
begin
case AHeaderID of
HTTP_HEADER_ID.HttpHeaderAccept: Result := 'Accept';
HTTP_HEADER_ID.HttpHeaderAcceptCharset: Result := 'Accept-Charset';
HTTP_HEADER_ID.HttpHeaderAcceptEncoding: Result := 'Accept-Encoding';
HTTP_HEADER_ID.HttpHeaderAcceptLanguage: Result := 'Accept-Language';
HTTP_HEADER_ID.HttpHeaderAllow: Result := 'Allow';
HTTP_HEADER_ID.HttpHeaderAuthorization: Result := 'Authorization';
HTTP_HEADER_ID.HttpHeaderCacheControl: Result := 'Cache-Control';
HTTP_HEADER_ID.HttpHeaderConnection: Result := 'Connection';
HTTP_HEADER_ID.HttpHeaderContentEncoding: Result := 'Content-Encoding';
HTTP_HEADER_ID.HttpHeaderContentLanguage: Result := 'Content-Language';
HTTP_HEADER_ID.HttpHeaderContentLength: Result := 'Content-Length';
HTTP_HEADER_ID.HttpHeaderContentLocation: Result := 'Content-Location';
HTTP_HEADER_ID.HttpHeaderContentMD5: Result := 'Content-MD5';
HTTP_HEADER_ID.HttpHeaderContentRange: Result := 'Content-Range';
HTTP_HEADER_ID.HttpHeaderContentType: Result := 'Content-Type';
HTTP_HEADER_ID.HttpHeaderDate: Result := 'Date';
HTTP_HEADER_ID.HttpHeaderExpect: Result := 'Expect';
HTTP_HEADER_ID.HttpHeaderExpires: Result := 'Expires';
HTTP_HEADER_ID.HttpHeaderFrom: Result := 'From';
HTTP_HEADER_ID.HttpHeaderHost: Result := 'Host';
HTTP_HEADER_ID.HttpHeaderIfMatch: Result := 'If-Match';
HTTP_HEADER_ID.HttpHeaderIfModifiedSince: Result := 'If-Modified-Since';
HTTP_HEADER_ID.HttpHeaderIfNoneMatch: Result := 'If-None-Match';
HTTP_HEADER_ID.HttpHeaderIfRange: Result := 'If-Range';
HTTP_HEADER_ID.HttpHeaderIfUnmodifiedSince: Result := 'If-Unmodified-Since';
HTTP_HEADER_ID.HttpHeaderKeepAlive: Result := 'Keep-Alive';
HTTP_HEADER_ID.HttpHeaderLastModified: Result := 'Last-Modified';
HTTP_HEADER_ID.HttpHeaderMaxForwards: Result := 'Max-Forwards';
HTTP_HEADER_ID.HttpHeaderPragma: Result := 'Pragma';
HTTP_HEADER_ID.HttpHeaderProxyAuthorization: Result := 'Proxy-Authorization';
HTTP_HEADER_ID.HttpHeaderRange: Result := 'Range';
HTTP_HEADER_ID.HttpHeaderReferer: Result := 'Referer';
HTTP_HEADER_ID.HttpHeaderTE: Result := 'TE';
HTTP_HEADER_ID.HttpHeaderTrailer: Result := 'Trailer';
HTTP_HEADER_ID.HttpHeaderTranslate: Result := 'Translate';
HTTP_HEADER_ID.HttpHeaderTransferEncoding: Result := 'Transfer-Encoding';
HTTP_HEADER_ID.HttpHeaderUpgrade: Result := 'Upgrade';
HTTP_HEADER_ID.HttpHeaderUserAgent: Result := 'User-Agent';
HTTP_HEADER_ID.HttpHeaderVia: Result := 'Via';
HTTP_HEADER_ID.HttpHeaderWarning: Result := 'Warning';
HTTP_HEADER_ID.HttpHeaderCookie: Result := 'Cookie';
else
Raise Exception.Create('Error D689854E-728D-4069-840D-4003E5842719')
end;
end;
{**********************************************************************************}
function ALHttpResponseHeaderIdToNameA(const AHeaderID: HTTP_HEADER_ID): AnsiString;
begin
case AHeaderID of
HTTP_HEADER_ID.HttpHeaderAcceptRanges: Result := 'Accept-Ranges';
HTTP_HEADER_ID.HttpHeaderAge: Result := 'Age';
HTTP_HEADER_ID.HttpHeaderAllow: Result := 'Allow';
HTTP_HEADER_ID.HttpHeaderCacheControl: Result := 'Cache-Control';
HTTP_HEADER_ID.HttpHeaderConnection: Result := 'Connection';
HTTP_HEADER_ID.HttpHeaderContentEncoding: Result := 'Content-Encoding';
HTTP_HEADER_ID.HttpHeaderContentLanguage: Result := 'Content-Language';
HTTP_HEADER_ID.HttpHeaderContentLength: Result := 'Content-Length';
HTTP_HEADER_ID.HttpHeaderContentLocation: Result := 'Content-Location';
HTTP_HEADER_ID.HttpHeaderContentMD5: Result := 'Content-MD5';
HTTP_HEADER_ID.HttpHeaderContentRange: Result := 'Content-Range';
HTTP_HEADER_ID.HttpHeaderContentType: Result := 'Content-Type';
HTTP_HEADER_ID.HttpHeaderDate: Result := 'Date';
HTTP_HEADER_ID.HttpHeaderETag: Result := 'ETag';
HTTP_HEADER_ID.HttpHeaderExpires: Result := 'Expires';
HTTP_HEADER_ID.HttpHeaderKeepAlive: Result := 'Keep-Alive';
HTTP_HEADER_ID.HttpHeaderLastModified: Result := 'Last-Modified';
HTTP_HEADER_ID.HttpHeaderLocation: Result := 'Location';
HTTP_HEADER_ID.HttpHeaderPragma: Result := 'Pragma';
HTTP_HEADER_ID.HttpHeaderProxyAuthenticate: Result := 'Proxy-Authenticate';
HTTP_HEADER_ID.HttpHeaderRetryAfter: Result := 'Retry-After';
HTTP_HEADER_ID.HttpHeaderServer: Result := 'Server';
HTTP_HEADER_ID.HttpHeaderTrailer: Result := 'Trailer';
HTTP_HEADER_ID.HttpHeaderTransferEncoding: Result := 'Transfer-Encoding';
HTTP_HEADER_ID.HttpHeaderUpgrade: Result := 'Upgrade';
HTTP_HEADER_ID.HttpHeaderVary: Result := 'Vary';
HTTP_HEADER_ID.HttpHeaderVia: Result := 'Via';
HTTP_HEADER_ID.HttpHeaderWarning: Result := 'Warning';
HTTP_HEADER_ID.HttpHeaderWWWAuthenticate: Result := 'WWW-Authenticate';
HTTP_HEADER_ID.HttpHeaderSetCookie: Result := 'Set-Cookie';
else
Raise Exception.Create('Error 6E31EEDC-AD7E-4B1B-AE99-F5B3B0CC60C3')
end;
end;
{***************************************************************}
function ALHttpVerbToStringA(const AVERB: HTTP_VERB): AnsiString;
begin
case AVERB of
HTTP_VERB.HttpVerbUnparsed: Result := '';
HTTP_VERB.HttpVerbUnknown: Result := '';
HTTP_VERB.HttpVerbInvalid: Result := '';
HTTP_VERB.HttpVerbOPTIONS: Result := 'OPTIONS';
HTTP_VERB.HttpVerbGET: Result := 'GET';
HTTP_VERB.HttpVerbHEAD: Result := 'HEAD';
HTTP_VERB.HttpVerbPOST: Result := 'POST';
HTTP_VERB.HttpVerbPUT: Result := 'PUT';
HTTP_VERB.HttpVerbDELETE: Result := 'DELETE';
HTTP_VERB.HttpVerbTRACE: Result := 'TRACE';
HTTP_VERB.HttpVerbCONNECT: Result := 'CONNECT';
HTTP_VERB.HttpVerbTRACK: Result := 'TRACK';
HTTP_VERB.HttpVerbMOVE: Result := 'MOVE';
HTTP_VERB.HttpVerbCOPY: Result := 'COPY';
HTTP_VERB.HttpVerbPROPFIND: Result := 'PROPFIND';
HTTP_VERB.HttpVerbPROPPATCH: Result := 'PROPPATCH';
HTTP_VERB.HttpVerbMKCOL: Result := 'MKCOL';
HTTP_VERB.HttpVerbLOCK: Result := 'LOCK';
HTTP_VERB.HttpVerbUNLOCK: Result := 'UNLOCK';
HTTP_VERB.HttpVerbSEARCH: Result := 'SEARCH';
else
Raise Exception.Create('Error F6EADBFC-A8AB-4DFF-AC13-F1C524E5BED6')
end
end;
{***************************************************************************************}
constructor TALHttpSysServerRequestHeaders.Create(const AHeaders: PHTTP_REQUEST_HEADERS);
begin
inherited Create;
FHeaders := AHeaders;
//for var i := Low(FKnownHeaders) to High(FKnownHeaders) do
// FKnownHeaders[i] := '';
FCookies:= nil;
FUnknownHeaders := nil;
end;
{************************************************}
destructor TALHttpSysServerRequestHeaders.Destroy;
begin
ALFreeAndNil(FCookies);
ALFreeAndNil(FUnknownHeaders);
inherited;
end;
{*************************************************************************************************************}
function TALHttpSysServerRequestHeaders.PropertyIndexToHeaderID(const APropertyIndex: Integer): HTTP_HEADER_ID;
begin
case APropertyIndex of
0: Result := HTTP_HEADER_ID.HttpHeaderAccept; // {Accept: audio/*; q=0.2, audio/basic}
1: Result := HTTP_HEADER_ID.HttpHeaderAcceptCharset; // {Accept-Charset: iso-8859-5, unicode-1-1;q=0.8}
2: Result := HTTP_HEADER_ID.HttpHeaderAcceptEncoding; // {Accept-Encoding: gzip;q=1.0, identity; q=0.5, *;q=0}
3: Result := HTTP_HEADER_ID.HttpHeaderAcceptLanguage; // {Accept-Language: da, en-gb;q=0.8, en;q=0.7}
4: Result := HTTP_HEADER_ID.HttpHeaderAllow; // {Allow: GET, HEAD, PUT}
5: Result := HTTP_HEADER_ID.HttpHeaderAuthorization; // {Authorization: BASIC d2VibWFzdGVyOnpycW1hNHY=}
6: Result := HTTP_HEADER_ID.HttpHeaderCacheControl; // {Cache-Control: no-cache}
7: Result := HTTP_HEADER_ID.HttpHeaderConnection; // {Connection: close}
8: Result := HTTP_HEADER_ID.HttpHeaderContentEncoding; // {Content-Encoding: gzip}
9: Result := HTTP_HEADER_ID.HttpHeaderContentLanguage; // {Content-Language: mi, en}
10: Result := HTTP_HEADER_ID.HttpHeaderContentLength; // {Content-Length: 3495}
11: Result := HTTP_HEADER_ID.HttpHeaderContentLocation; // {Content-Location: http://localhost/page.asp}
12: Result := HTTP_HEADER_ID.HttpHeaderContentMD5; // {Content-MD5: [md5-digest]}
13: Result := HTTP_HEADER_ID.HttpHeaderContentRange; // {Content-Range: bytes 2543-4532/7898}
14: Result := HTTP_HEADER_ID.HttpHeaderContentType; // {Content-Type: text/html; charset=ISO-8859-4}
15: Result := HTTP_HEADER_ID.HttpHeaderDate; // {Date: Tue, 15 Nov 1994 08:12:31 GMT}
16: Result := HTTP_HEADER_ID.HttpHeaderExpect; // {Expect: 100-continue}
17: Result := HTTP_HEADER_ID.HttpHeaderExpires; // {Expires: Thu, 01 Dec 1994 16:00:00 GMT}
18: Result := HTTP_HEADER_ID.HttpHeaderFrom; // {From: webmaster@w3.org}
19: Result := HTTP_HEADER_ID.HttpHeaderHost; // {Host: www.w3.org}
20: Result := HTTP_HEADER_ID.HttpHeaderIfMatch; // {If-Match: entity_tag001}
21: Result := HTTP_HEADER_ID.HttpHeaderIfModifiedSince; // {If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT}
22: Result := HTTP_HEADER_ID.HttpHeaderIfNoneMatch; // {If-None-Match: entity_tag001}
23: Result := HTTP_HEADER_ID.HttpHeaderIfRange; // {If-Range: entity_tag001}
24: Result := HTTP_HEADER_ID.HttpHeaderIfUnmodifiedSince; // {If-Unmodified-Since: Sat, 29 Oct 1994 19:43:31 GMT}
25: Result := HTTP_HEADER_ID.HttpHeaderKeepAlive; // {Keep-Alive: timeout=5, max=1000}
26: Result := HTTP_HEADER_ID.HttpHeaderLastModified; // {Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT}
27: Result := HTTP_HEADER_ID.HttpHeaderMaxForwards; // {Max-Forwards: 3}
28: Result := HTTP_HEADER_ID.HttpHeaderPragma; // {Pragma: no-cache}
29: Result := HTTP_HEADER_ID.HttpHeaderProxyAuthorization; // {Proxy-Authorization: [credentials]}
30: Result := HTTP_HEADER_ID.HttpHeaderRange; // {Range: bytes=100-599}
31: Result := HTTP_HEADER_ID.HttpHeaderReferer; // {Referer: http://www.w3.org/hypertext/DataSources/Overview.html}
32: Result := HTTP_HEADER_ID.HttpHeaderTE; // {TE: trailers, deflate;q=0.5}
33: Result := HTTP_HEADER_ID.HttpHeaderTrailer; // {Trailer: Date}
34: Result := HTTP_HEADER_ID.HttpHeaderTranslate; // {Translate: f}
35: Result := HTTP_HEADER_ID.HttpHeaderTransferEncoding; // {Transfer-Encoding: chunked}
36: Result := HTTP_HEADER_ID.HttpHeaderUpgrade; // {Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11}
37: Result := HTTP_HEADER_ID.HttpHeaderUserAgent; // {User-Agent: CERN-LineMode/2.15 libwww/2.17b3}
38: Result := HTTP_HEADER_ID.HttpHeaderVia; // {Via: 1.0 ricky, 1.1 mertz, 1.0 lucy}
39: Result := HTTP_HEADER_ID.HttpHeaderWarning; // {Warning: 112 Disconnected Operation}
else
Raise Exception.Create('Error D689854E-728D-4069-840D-4003E5842719')
end;
end;
{**************************************************************}
function TALHttpSysServerRequestHeaders.GetCookies: TALStringsA;
begin
if FCookies = nil then begin
FCookies := TALNVStringListA.Create;
FCookies.LineBreak := '; ';
FCookies.TrailingLineBreak := False;
var LRawCookiesStr: AnsiString;
SetString(
LRawCookiesStr,
FHeaders^.KnownHeaders[Ord(HTTP_HEADER_ID.HttpHeaderCookie)].pRawValue,
FHeaders^.KnownHeaders[Ord(HTTP_HEADER_ID.HttpHeaderCookie)].RawValueLength);
ALExtractHeaderFields(
[';'], // const ASeparators: TSysCharSet;,
[' ', #9], // const AWhiteSpace: TSysCharSet;
[], // const AQuoteChars: TSysCharSet;
PAnsiChar(LRawCookiesStr), // const AContent: PAnsiChar;
FCookies); // const AStrings: TALStringsA;
end;
Result := FCookies;
end;
{*********************************************************************}
function TALHttpSysServerRequestHeaders.GetUnknownHeaders: TALStringsA;
begin
if FUnknownHeaders = nil then begin
FUnknownHeaders := TALNVStringListA.Create;
FUnknownHeaders.NameValueSeparator := ':';
FUnknownHeaders.TrailingLineBreak := False;
for var I := 0 to FHeaders^.UnknownHeaderCount - 1 do begin
var LUnknownHeader := PHTTP_UNKNOWN_HEADER(NativeUInt(FHeaders^.pUnknownHeaders) + (NativeUInt(I) * SizeOf(HTTP_UNKNOWN_HEADER)));
var LName: AnsiString;
SetString(
LName,
LUnknownHeader.pName,
LUnknownHeader.NameLength);
var LValue: AnsiString;
SetString(
LValue,
LUnknownHeader.pRawValue,
LUnknownHeader.RawValueLength);
FUnknownHeaders.AddNameValue(LName,LValue);
end;
end;
Result := FUnknownHeaders;
end;
{******************************************************************************************************}
function TALHttpSysServerRequestHeaders.GetHeaderValueByPropertyIndex(const Index: Integer): AnsiString;
begin
var LHttpHeaderID := Ord(PropertyIndexToHeaderID(Index));
if FKnownHeaders[LHttpHeaderID] <> '' then Result := FKnownHeaders[LHttpHeaderID]
else begin
SetString(
Result,
FHeaders^.KnownHeaders[LHttpHeaderID].pRawValue,
FHeaders^.KnownHeaders[LHttpHeaderID].RawValueLength);
FKnownHeaders[LHttpHeaderID] := Result;
end;
end;
{********************************************************************************************************************}
procedure TALHttpSysServerRequestHeaders.SetHeaderValueByPropertyIndex(const Index: Integer; const Value: AnsiString);
begin
raise Exception.Create('Cannot modify request headers: http.sys exposes a read-only HTTP_REQUEST');
end;
{*******************************************************************}
Function TALHttpSysServerRequestHeaders.GetRawHeaderText: AnsiString;
begin
var SB := TALStringBuilderA.Create(2048);
try
// 1) Known headers
var LValue: AnsiString;
for var LHeaderId := 0 to ord(HTTP_HEADER_ID.HttpHeaderRequestMaximum) - 1 do begin
if FHeaders^.KnownHeaders[LHeaderId].RawValueLength > 0 then begin
SetString(
LValue,
FHeaders^.KnownHeaders[LHeaderId].pRawValue,
FHeaders^.KnownHeaders[LHeaderId].RawValueLength);
SB.Append(ALHttpRequestHeaderIdToNameA(HTTP_HEADER_ID(LHeaderId)));
SB.Append(': ');
SB.AppendLine(LValue);
end;
end;
// 2) Unknown headers
for var I := 0 to UnknownHeaders.Count - 1 do begin
SB.Append(UnknownHeaders.Names[I]);
SB.Append(': ');
SB.AppendLine(UnknownHeaders.ValueFromIndex[I]);
end;
// 3) Produce the final string
Result := SB.ToString(true{AUpdateCapacity});
finally
ALFreeAndNil(SB);
end;
end;
{******************************************************************************************}
procedure TALHttpSysServerRequestHeaders.SetRawHeaderText(const ARawHeaderText: AnsiString);
begin
raise Exception.Create('Cannot modify request headers: http.sys exposes a read-only HTTP_REQUEST');
end;
{*********************************************}
procedure TALHttpSysServerRequestHeaders.Clear;
begin
raise Exception.Create('Cannot modify request headers: http.sys exposes a read-only HTTP_REQUEST');
end;
{*************************************************}
constructor TALHttpSysServerResponseHeaders.Create;
begin
inherited Create;
//for var i := Low(FKnownHeaders) to High(FKnownHeaders) do
// FKnownHeaders[i] := '';
FCookies := nil;
FUnknownHeaders := nil;
end;
{*************************************************}
destructor TALHttpSysServerResponseHeaders.Destroy;
begin
ALFreeAndNil(FCookies);
ALFreeAndNil(FUnknownHeaders);
inherited;
end;
{**************************************************************************************************************}
function TALHttpSysServerResponseHeaders.PropertyIndexToHeaderID(const APropertyIndex: Integer): HTTP_HEADER_ID;
begin
case APropertyIndex of
0: Result := HTTP_HEADER_ID.HttpHeaderAcceptRanges; // {Accept-Ranges: bytes}
1: Result := HTTP_HEADER_ID.HttpHeaderAge; // {Age: 2147483648(2^31)}
2: Result := HTTP_HEADER_ID.HttpHeaderAllow; // {Allow: GET, HEAD, PUT}
3: Result := HTTP_HEADER_ID.HttpHeaderCacheControl; // {Cache-Control: no-cache}
4: Result := HTTP_HEADER_ID.HttpHeaderConnection; // {Connection: close}
5: Result := HTTP_HEADER_ID.HttpHeaderContentEncoding; // {Content-Encoding: gzip}
6: Result := HTTP_HEADER_ID.HttpHeaderContentLanguage; // {Content-Language: mi, en}
7: Result := HTTP_HEADER_ID.HttpHeaderContentLength; // {Content-Length: 3495}
8: Result := HTTP_HEADER_ID.HttpHeaderContentLocation; // {Content-Location: http://localhost/page.asp}
9: Result := HTTP_HEADER_ID.HttpHeaderContentMD5; // {Content-MD5: [md5-digest]}
10: Result := HTTP_HEADER_ID.HttpHeaderContentRange; // {Content-Range: bytes 2543-4532/7898}
11: Result := HTTP_HEADER_ID.HttpHeaderContentType; // {Content-Type: text/html; charset=ISO-8859-4}
12: Result := HTTP_HEADER_ID.HttpHeaderDate; // {Date: Tue, 15 Nov 1994 08:12:31 GMT}
13: Result := HTTP_HEADER_ID.HttpHeaderETag; // {ETag: W/"xyzzy"}
14: Result := HTTP_HEADER_ID.HttpHeaderExpires; // {Expires: Thu, 01 Dec 1994 16:00:00 GMT}
15: Result := HTTP_HEADER_ID.HttpHeaderKeepAlive; // {Keep-Alive: timeout=5, max=1000}
16: Result := HTTP_HEADER_ID.HttpHeaderLastModified; // {Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT}
17: Result := HTTP_HEADER_ID.HttpHeaderLocation; // {Location: http://www.w3.org/pub/WWW/People.html}
18: Result := HTTP_HEADER_ID.HttpHeaderPragma; // {Pragma: no-cache}
19: Result := HTTP_HEADER_ID.HttpHeaderProxyAuthenticate; // {Proxy-Authenticate: [challenge]}
20: Result := HTTP_HEADER_ID.HttpHeaderRetryAfter; // {Retry-After: Fri, 31 Dec 1999 23:59:59 GMT}
21: Result := HTTP_HEADER_ID.HttpHeaderServer; // {Server: CERN/3.0 libwww/2.17}
22: Result := HTTP_HEADER_ID.HttpHeaderTrailer; // {Trailer: Date}
23: Result := HTTP_HEADER_ID.HttpHeaderTransferEncoding; // {Transfer-Encoding: chunked}
24: Result := HTTP_HEADER_ID.HttpHeaderUpgrade; // {Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11}
25: Result := HTTP_HEADER_ID.HttpHeaderVary; // {Vary: Date}
26: Result := HTTP_HEADER_ID.HttpHeaderVia; // {Via: 1.0 ricky, 1.1 mertz, 1.0 lucy}
27: Result := HTTP_HEADER_ID.HttpHeaderWarning; // {Warning: 112 Disconnected Operation}
28: Result := HTTP_HEADER_ID.HttpHeaderWWWAuthenticate; // {WWW-Authenticate: [challenge]}
else
Raise Exception.Create('Error 1BDF78DD-E23D-4120-9CBF-ED52052BA4B6')
end;
end;
{*******************************************************************************}
function TALHttpSysServerResponseHeaders.GetCookies: TObjectList<TALHttpCookieA>;
begin
if FCookies = nil then
FCookies := TObjectList<TALHttpCookieA>.Create(True{AOwnsObjects});
Result := FCookies;
end;
{**********************************************************************}
function TALHttpSysServerResponseHeaders.GetUnknownHeaders: TALStringsA;
begin
if FUnknownHeaders = nil then begin
FUnknownHeaders := TALNVStringListA.Create;
FUnknownHeaders.NameValueSeparator := ':';
FUnknownHeaders.TrailingLineBreak := False;
end;
Result := FUnknownHeaders;
end;
{*******************************************************************************************************}
function TALHttpSysServerResponseHeaders.GetHeaderValueByPropertyIndex(const Index: Integer): AnsiString;
begin
Result := FKnownHeaders[Ord(PropertyIndexToHeaderID(Index))];
end;
{*********************************************************************************************************************}
procedure TALHttpSysServerResponseHeaders.SetHeaderValueByPropertyIndex(const Index: Integer; const Value: AnsiString);
begin
FKnownHeaders[Ord(PropertyIndexToHeaderID(Index))] := Value;
end;
{*******************************************************************************************}
procedure TALHttpSysServerResponseHeaders.SetRawHeaderText(const ARawHeaderText: AnsiString);
begin
raise ENotImplemented.Create('TALHttpSysServerResponseHeaders.SetRawHeaderText is not yet implemented');
end;
{********************************************************************}
Function TALHttpSysServerResponseHeaders.GetRawHeaderText: AnsiString;
begin
raise ENotImplemented.Create('TALHttpSysServerResponseHeaders.GetRawHeaderText is not yet implemented');
end;
{**********************************************}
procedure TALHttpSysServerResponseHeaders.Clear;
begin
if FCookies <> nil then FCookies.Clear;
if FUnknownHeaders <> nil then FUnknownHeaders.Clear;
for var i := Low(FKnownHeaders) to High(FKnownHeaders) do
FKnownHeaders[i] := '';
end;
{****************************************************************************}
constructor TALHttpSysServerRequest.Create(const AHttpRequest: PHTTP_REQUEST);
begin
inherited Create;
FHttpRequest := AHttpRequest;
FRawUrl := '';
FCookedUrl := nil;
FVerb := '';
FHeaders := TALHttpSysServerRequestHeaders.Create(@FHttpRequest^.Headers);
FBodyStream := TALStringStreamA.Create('');
FRemoteAddress := nil;
FLocalAddress := nil;
end;
{*****************************************}
destructor TALHttpSysServerRequest.Destroy;
begin
ALFreeAndNil(FCookedUrl);
ALFreeAndNil(FHeaders);
ALFreeAndNil(FBodyStream);
ALFreeAndNil(FRemoteAddress);
ALFreeAndNil(FLocalAddress);
inherited;
end;
{**********************************************************}
function TALHttpSysServerRequest.GetVersion: TALHttpVersion;
begin
case FHttpRequest^.Version.MajorVersion of
0: begin
if FHttpRequest^.Version.MinorVersion = 9 then Result := TALHttpVersion.v0_9
else Result := TALHttpVersion.Unspecified;
end;
//--
1: begin
if FHttpRequest^.Version.MinorVersion = 0 then Result := TALHttpVersion.v1_0
else Result := TALHttpVersion.v1_1;
end;
//--
2: Result := TALHttpVersion.v2;
//--
3: Result := TALHttpVersion.v3;
//--
else Result := TALHttpVersion.Unspecified;
end;
end;
{*************************************************************************}
procedure TALHttpSysServerRequest.SetVersion(const AValue: TALHttpVersion);
begin
raise Exception.Create('Cannot modify request: http.sys exposes a read-only HTTP_REQUEST');
end;
{***************************************************}
function TALHttpSysServerRequest.GetVerb: AnsiString;
begin
if FVerb <> '' then Result := FVerb
else begin
Result := ALHttpVerbToStringA(FHttpRequest^.Verb);
if Result = '' then
SetString(
Result,
FHttpRequest^.pUnknownVerb,
FHttpRequest^.UnknownVerbLength);
FVerb := Result;
end;
end;
{******************************************************************}
procedure TALHttpSysServerRequest.SetVerb(const AValue: AnsiString);
begin
raise Exception.Create('Cannot modify request: http.sys exposes a read-only HTTP_REQUEST');
end;
{*****************************************************}
function TALHttpSysServerRequest.GetRawUrl: AnsiString;
begin
if FRawUrl <> '' then Result := FRawUrl
else begin
SetString(
Result,
FHttpRequest^.pRawUrl,
FHttpRequest^.RawUrlLength);
FRawUrl := Result;
end;
end;
{********************************************************************}
procedure TALHttpSysServerRequest.SetRawUrl(const AValue: AnsiString);
begin
raise Exception.Create('Cannot modify request: http.sys exposes a read-only HTTP_REQUEST');
end;
{***********************************************************}
function TALHttpSysServerRequest.GetCookedUrl: TALCookedUrlA;
begin
If FCookedUrl = nil then begin
if IsSecure then FCookedUrl := TALCookedUrlA.Create('https://' + Headers.Host + RawUrl)
else FCookedUrl := TALCookedUrlA.Create('http://' + Headers.Host + RawUrl);
end;
Result := FCookedUrl;
end;
{******************************************************************}
function TALHttpSysServerRequest.GetHeaders: TALHttpRequestHeadersA;
begin
Result := FHeaders;
end;
{**********************************************************************}
function TALHttpSysServerRequest.ExtractHeaders: TALHttpRequestHeadersA;
begin
raise Exception.Create('Cannot modify request: http.sys exposes a read-only HTTP_REQUEST');
end;
{******************************************************}
function TALHttpSysServerRequest.GetBodyStream: TStream;
begin
Result := FBodyStream;
end;
{**********************************************************}
function TALHttpSysServerRequest.ExtractBodyStream: TStream;
begin
raise Exception.Create('Cannot modify request: http.sys exposes a read-only HTTP_REQUEST');
end;
{*********************************************************************}
procedure TALHttpSysServerRequest.SetBodyStream(const AValue: TStream);
begin
raise Exception.Create('Cannot modify request: http.sys exposes a read-only HTTP_REQUEST');
end;
{**********************************************************}
function TALHttpSysServerRequest.GetOwnsBodyStream: Boolean;
begin
Result := True;
end;
{*************************************************************************}
procedure TALHttpSysServerRequest.SetOwnsBodyStream(const AValue: Boolean);
begin
raise Exception.Create('Cannot modify request: http.sys exposes a read-only HTTP_REQUEST');
end;
{*********************************************************}
function TALHttpSysServerRequest.GetBodyString: AnsiString;
begin
Result := FBodyStream.DataString;
end;
{************************************************************************}
procedure TALHttpSysServerRequest.SetBodyString(const AValue: AnsiString);
begin
raise Exception.Create('Cannot modify request: http.sys exposes a read-only HTTP_REQUEST');
end;
{****************************************************************}
function TALHttpSysServerRequest.GetRemoteAddress: TALNetEndpoint;
begin
if FRemoteAddress = nil then
FRemoteAddress := TALNetEndpoint.Create(FHttpRequest^.Address.pRemoteAddress);
Result := FRemoteAddress;
end;
{*******************************************************************************}
procedure TALHttpSysServerRequest.SetRemoteAddress(const AValue: TALNetEndpoint);
begin
raise Exception.Create('Cannot modify request: http.sys exposes a read-only HTTP_REQUEST');
end;
{***************************************************************}
function TALHttpSysServerRequest.GetLocalAddress: TALNetEndpoint;
begin
if FLocalAddress = nil then
FLocalAddress := TALNetEndpoint.Create(FHttpRequest^.Address.pLocalAddress);
Result := FLocalAddress;
end;
{******************************************************************************}
procedure TALHttpSysServerRequest.SetLocalAddress(const AValue: TALNetEndpoint);
begin
raise Exception.Create('Cannot modify request: http.sys exposes a read-only HTTP_REQUEST');
end;
{****************************************************}
function TALHttpSysServerRequest.GetIsSecure: Boolean;
begin
// If this request has SSL info, it means the connection is HTTPS
Result := Assigned(FHttpRequest^.pSslInfo);
end;
{******************************************}
constructor TALHttpSysServerResponse.Create;
begin
inherited;
FVersion := TALHttpVersion.v1_1;
FStatusCode := 200;
FReason := 'OK';
FHeaders := nil;
FBodyStream := nil;
FOwnsBodyStream := True;
FBodyFileHandle := INVALID_HANDLE_VALUE;
FBodyByteRangeStartingOffset := 0;
FBodyByteRangeLength := HTTP_BYTE_RANGE_TO_EOF;
FCachePolicyType := TALHttpServerResponseA.TCachePolicyType.Nocache;
FCacheSecondsToLive := 0;
end;
{******************************************}
destructor TALHttpSysServerResponse.Destroy;
begin
if FBodyFileHandle <> INVALID_HANDLE_VALUE then
CloseHandle(FBodyFileHandle); // best-effort; do not raise on failure
ALFreeAndNil(FHeaders);
if OwnsBodyStream then ALFreeAndNil(FBodyStream);
inherited;
end;
{***********************************************************}
function TALHttpSysServerResponse.GetVersion: TALHttpVersion;
begin
Result := FVersion;
end;
{**************************************************************************}
procedure TALHttpSysServerResponse.SetVersion(const AValue: TALHttpVersion);
begin
FVersion := AValue;
end;
{*******************************************************}
function TALHttpSysServerResponse.GetStatusCode: Integer;
begin
Result := FStatusCode;
end;
{**********************************************************************}
procedure TALHttpSysServerResponse.SetStatusCode(const AValue: Integer);
begin
If AValue <> FStatusCode then begin
FStatusCode := AValue;
Reason := ALGetHttpReasonPhraseA(FStatusCode);
end;
end;
{******************************************************}
function TALHttpSysServerResponse.GetReason: AnsiString;
begin
Result := FReason;
end;
{*********************************************************************}
procedure TALHttpSysServerResponse.SetReason(const AValue: AnsiString);
begin
FReason := AValue;
end;
{********************************************************************}
function TALHttpSysServerResponse.GetHeaders: TALHttpResponseHeadersA;
begin
if FHeaders = nil then
FHeaders := TALHttpSysServerResponseHeaders.Create;
Result := FHeaders;
end;
{************************************************************************}
function TALHttpSysServerResponse.ExtractHeaders: TALHttpResponseHeadersA;
begin
Result := FHeaders;
FHeaders := nil;
end;
{*******************************************************}
function TALHttpSysServerResponse.GetBodyStream: TStream;
begin
if FBodyStream = nil then
FBodyStream := TALStringStreamA.Create('');
Result := FBodyStream;
end;
{***********************************************************}
function TALHttpSysServerResponse.ExtractBodyStream: TStream;
begin
Result := FBodyStream;
FBodyStream := nil;
FOwnsBodyStream := True;
end;
{**********************************************************************}
procedure TALHttpSysServerResponse.SetBodyStream(const AValue: TStream);
begin
if AValue <> FBodyStream then begin
if OwnsBodyStream then ALFreeAndNil(FBodyStream);