/
dwsHTTPSysAPI.pas
1191 lines (1060 loc) · 42.5 KB
/
dwsHTTPSysAPI.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
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
{**********************************************************************}
{ }
{ "The contents of this file are subject to the Mozilla Public }
{ License Version 1.1 (the "License"); you may not use this }
{ file except in compliance with the License. You may obtain }
{ a copy of the License at http://www.mozilla.org/MPL/ }
{ }
{ Software distributed under the License is distributed on an }
{ "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express }
{ or implied. See the License for the specific language }
{ governing rights and limitations under the License. }
{ }
{ Copyright Creative IT. }
{ Current maintainer: Eric Grange }
{ }
{**********************************************************************}
{
HTTP.sys API definitions
This file is based on Synopse framework and is an attempt
at supporting HTTP.SYS 2.0
Synopse framework. Copyright (C) 2012 Arnaud Bouchez
Synopse Informatique - http://synopse.info
*** BEGIN LICENSE BLOCK *****
Version: MPL 1.1/GPL 2.0/LGPL 2.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL
}
unit dwsHTTPSysAPI;
interface
uses
Windows, SysUtils,
SynWinSock;
{$MINENUMSIZE 4}
{$A8}
type
ULONGLONG = Int64;
HTTP_OPAQUE_ID = ULONGLONG;
HTTP_URL_CONTEXT = HTTP_OPAQUE_ID;
HTTP_REQUEST_ID = HTTP_OPAQUE_ID;
HTTP_CONNECTION_ID = HTTP_OPAQUE_ID;
HTTP_RAW_CONNECTION_ID = HTTP_OPAQUE_ID;
HTTP_URL_GROUP_ID = HTTP_OPAQUE_ID;
HTTP_SERVER_SESSION_ID = HTTP_OPAQUE_ID;
// HTTP API version used
HTTPAPI_VERSION = packed record
MajorVersion : word;
MinorVersion : word;
end;
// the req* values identify Request Headers, and resp* Response Headers
THttpHeader = (
reqCacheControl,
reqConnection,
reqDate,
reqKeepAlive,
reqPragma,
reqTrailer,
reqTransferEncoding,
reqUpgrade,
reqVia,
reqWarning,
reqAllow,
reqContentLength,
reqContentType,
reqContentEncoding,
reqContentLanguage,
reqContentLocation,
reqContentMd5,
reqContentRange,
reqExpires,
reqLastModified,
reqAccept,
reqAcceptCharset,
reqAcceptEncoding,
reqAcceptLanguage,
reqAuthorization,
reqCookie,
reqExpect,
reqFrom,
reqHost,
reqIfMatch,
reqIfModifiedSince,
reqIfNoneMatch,
reqIfRange,
reqIfUnmodifiedSince,
reqMaxForwards,
reqProxyAuthorization,
reqReferer,
reqRange,
reqTe,
reqTranslate,
reqUserAgent,
respAcceptRanges = 20,
respAge,
respEtag,
respLocation,
respProxyAuthenticate,
respRetryAfter,
respServer,
respSetCookie,
respVary,
respWwwAuthenticate
);
THttpVerb = (
hvUnparsed,
hvUnknown,
hvInvalid,
hvOPTIONS,
hvGET,
hvHEAD,
hvPOST,
hvPUT,
hvDELETE,
hvTRACE,
hvCONNECT,
hvTRACK, // used by Microsoft Cluster Server for a non-logged trace
hvMOVE,
hvCOPY,
hvPROPFIND,
hvPROPPATCH,
hvMKCOL,
hvLOCK,
hvUNLOCK,
hvSEARCH,
hvMaximum
);
THttpChunkType = (
hctFromMemory,
hctFromFileHandle,
hctFromFragmentCache);
THttpServiceConfigID = (
hscIPListenList,
hscSSLCertInfo,
hscUrlAclInfo,
hscMax
);
THttpServiceConfigQueryType = (
hscQueryExact,
hscQueryNext,
hscQueryMax
);
// Pointers overlap and point into pFullUrl. nil if not present.
HTTP_COOKED_URL = record
FullUrlLength : word; // in bytes not including the #0
HostLength : word; // in bytes not including the #0
AbsPathLength : word; // in bytes not including the #0
QueryStringLength : word; // in bytes not including the #0
pFullUrl : PWideChar; // points to "http://hostname:port/abs/.../path?query"
pHost : PWideChar; // points to the first char in the hostname
pAbsPath : PWideChar; // Points to the 3rd '/' char
pQueryString : PWideChar; // Points to the 1st '?' char or #0
end;
HTTP_TRANSPORT_ADDRESS = record
pRemoteAddress : PSOCKADDR;
pLocalAddress : PSOCKADDR;
end;
HTTP_UNKNOWN_HEADER = record
NameLength : word; // in bytes not including the #0
RawValueLength : word; // in bytes not including the n#0
pName : PAnsiChar; // The header name (minus the ':' character)
pRawValue : PAnsiChar; // The header value
end;
PHTTP_UNKNOWN_HEADER = ^HTTP_UNKNOWN_HEADER;
HTTP_UNKNOWN_HEADER_ARRAY = array of HTTP_UNKNOWN_HEADER;
HTTP_KNOWN_HEADER = record
RawValueLength : word; // in bytes not including the #0
pRawValue : PAnsiChar;
end;
PHTTP_KNOWN_HEADER = ^HTTP_KNOWN_HEADER;
HTTP_RESPONSE_HEADERS = record
// number of entries in the unknown HTTP headers array
UnknownHeaderCount : word;
// array of unknown HTTP headers
pUnknownHeaders : PHTTP_UNKNOWN_HEADER;
// Reserved, must be 0
TrailerCount : word;
// Reserved, must be nil
pTrailers : pointer;
// Known headers
KnownHeaders : array[low(THttpHeader)..respWwwAuthenticate] of HTTP_KNOWN_HEADER;
end;
HTTP_REQUEST_HEADERS = record
// number of entries in the unknown HTTP headers array
UnknownHeaderCount : word;
// array of unknown HTTP headers
pUnknownHeaders : PHTTP_UNKNOWN_HEADER;
// Reserved, must be 0
TrailerCount : word;
// Reserved, must be nil
pTrailers : pointer;
// Known headers
KnownHeaders : array[low(THttpHeader)..reqUserAgent] of HTTP_KNOWN_HEADER;
end;
PHTTP_REQUEST_HEADERS = ^HTTP_REQUEST_HEADERS;
HTTP_BYTE_RANGE = record
StartingOffset : ULARGE_INTEGER;
Length : ULARGE_INTEGER;
end;
// we use 3 distinct HTTP_DATA_CHUNK_* records since variable records
// alignment is buggy/non compatible under Delphi XE3
HTTP_DATA_CHUNK_INMEMORY = record
DataChunkType : THttpChunkType; // always hctFromMemory
Reserved1 : ULONG;
pBuffer : pointer;
BufferLength : ULONG;
Reserved2 : ULONG;
Reserved3 : ULONG;
end;
PHTTP_DATA_CHUNK_INMEMORY = ^HTTP_DATA_CHUNK_INMEMORY;
HTTP_DATA_CHUNK_FILEHANDLE = record
DataChunkType : THttpChunkType; // always hctFromFileHandle
ByteRange : HTTP_BYTE_RANGE;
FileHandle : THandle;
end;
HTTP_DATA_CHUNK_FRAGMENTCACHE = record
DataChunkType : THttpChunkType; // always hctFromFragmentCache
FragmentNameLength : word; // in bytes not including the #0
pFragmentName : PWideChar;
end;
HTTP_SSL_CLIENT_CERT_INFO = record
CertFlags : ULONG;
CertEncodedSize : ULONG;
pCertEncoded : PUCHAR;
Token : THandle;
CertDeniedByMapper : boolean;
end;
PHTTP_SSL_CLIENT_CERT_INFO = ^HTTP_SSL_CLIENT_CERT_INFO;
HTTP_SSL_INFO = record
ServerCertKeySize : word;
ConnectionKeySize : word;
ServerCertIssuerSize : ULONG;
ServerCertSubjectSize : ULONG;
pServerCertIssuer : PAnsiChar;
pServerCertSubject : PAnsiChar;
pClientCertInfo : PHTTP_SSL_CLIENT_CERT_INFO;
SslClientCertNegotiated : ULONG;
end;
PHTTP_SSL_INFO = ^HTTP_SSL_INFO;
HTTP_SERVICE_CONFIG_URLACL_KEY = record
pUrlPrefix : PWideChar;
end;
HTTP_SERVICE_CONFIG_URLACL_PARAM = record
pStringSecurityDescriptor : PWideChar;
end;
HTTP_SERVICE_CONFIG_URLACL_SET = record
KeyDesc : HTTP_SERVICE_CONFIG_URLACL_KEY;
ParamDesc : HTTP_SERVICE_CONFIG_URLACL_PARAM;
end;
HTTP_SERVICE_CONFIG_URLACL_QUERY = record
QueryDesc : THttpServiceConfigQueryType;
KeyDesc : HTTP_SERVICE_CONFIG_URLACL_KEY;
dwToken : DWORD;
end;
HTTP_REQUEST_INFO_TYPE = (
HttpRequestInfoTypeAuth
);
HTTP_AUTH_STATUS = (
HttpAuthStatusSuccess,
HttpAuthStatusNotAuthenticated,
HttpAuthStatusFailure
);
HTTP_REQUEST_AUTH_TYPE = (
HttpRequestAuthTypeNone,
HttpRequestAuthTypeBasic,
HttpRequestAuthTypeDigest,
HttpRequestAuthTypeNTLM,
HttpRequestAuthTypeNegotiate,
HttpRequestAuthTypeKerberos
);
SECURITY_STATUS = ULONG;
HTTP_REQUEST_AUTH_INFO = record
AuthStatus : HTTP_AUTH_STATUS;
SecStatus : SECURITY_STATUS;
Flags : ULONG;
AuthType : HTTP_REQUEST_AUTH_TYPE;
AccessToken : THandle;
ContextAttributes : ULONG;
PackedContextLength : ULONG;
PackedContextType : ULONG;
PackedContext : PVOID;
MutualAuthDataLength : ULONG;
pMutualAuthData : PCHAR;
end;
PHTTP_REQUEST_AUTH_INFO = ^HTTP_REQUEST_AUTH_INFO;
HTTP_REQUEST_INFO = record
InfoType : HTTP_REQUEST_INFO_TYPE;
InfoLength : ULONG;
pInfo : PVOID;
end;
PHTTP_REQUEST_INFO = ^HTTP_REQUEST_INFO;
/// structure used to handle data associated with a specific request
HTTP_REQUEST_V2 = record
// either 0 (Only Header), either HTTP_RECEIVE_REQUEST_FLAG_COPY_BODY
Flags : cardinal;
// An identifier for the connection on which the request was received
ConnectionId : HTTP_CONNECTION_ID;
// A value used to identify the request when calling
// HttpReceiveRequestEntityBody, HttpSendHttpResponse, and/or
// HttpSendResponseEntityBody
RequestId : HTTP_REQUEST_ID;
// The context associated with the URL prefix
UrlContext : HTTP_URL_CONTEXT;
// The HTTP version number
Version : HTTPAPI_VERSION;
// An HTTP verb associated with this request
Verb : THttpVerb;
// The length of the verb string if the Verb field is hvUnknown
// (in bytes not including the last #0)
UnknownVerbLength : word;
// The length of the raw (uncooked) URL (in bytes not including the last #0)
RawUrlLength : word;
// Pointer to the verb string if the Verb field is hvUnknown
pUnknownVerb : PAnsiChar;
// Pointer to the raw (uncooked) URL
pRawUrl : PAnsiChar;
// The canonicalized Unicode URL
CookedUrl : HTTP_COOKED_URL;
// Local and remote transport addresses for the connection
Address : HTTP_TRANSPORT_ADDRESS;
// The request headers.
Headers : HTTP_REQUEST_HEADERS;
// The total number of bytes received from network for this request
BytesReceived : ULONGLONG;
EntityChunkCount : USHORT;
pEntityChunks : Pointer;
RawConnectionId : HTTP_RAW_CONNECTION_ID;
// SSL connection information
pSslInfo : PHTTP_SSL_INFO;
// V2 new fields
xxxPadding : DWORD;
RequestInfoCount : USHORT;
pRequestInfo : PHTTP_REQUEST_INFO;
end;
PHTTP_REQUEST_V2 = ^HTTP_REQUEST_V2;
HTTP_RESPONSE_INFO_TYPE = (
HttpResponseInfoTypeMultipleKnownHeaders,
HttpResponseInfoTypeAuthenticationProperty,
HttpResponseInfoTypeQosProperty,
HttpResponseInfoTypeChannelBind
);
HTTP_RESPONSE_INFO = record
Typ : HTTP_RESPONSE_INFO_TYPE;
Length : ULONG;
pInfo : Pointer;
end;
PHTTP_RESPONSE_INFO = ^HTTP_RESPONSE_INFO;
HTTP_RESPONSE_V2 = object
public
Flags : cardinal;
// The raw HTTP protocol version number
Version : HTTPAPI_VERSION;
// The HTTP status code (e.g., 200)
StatusCode : word;
// in bytes not including the '\0'
ReasonLength : word;
// The HTTP reason (e.g., "OK"). This MUST not contain non-ASCII characters
// (i.e., all chars must be in range 0x20-0x7E).
pReason : PAnsiChar;
// The response headers
Headers : HTTP_RESPONSE_HEADERS;
// number of elements in pEntityChunks[] array
EntityChunkCount : word;
// pEntityChunks points to an array of EntityChunkCount HTTP_DATA_CHUNK_*
pEntityChunks : pointer;
// V2 new fields
ResponseInfoCount : USHORT;
pResponseInfo : PHTTP_RESPONSE_INFO;
// will set both StatusCode and Reason
// - OutStatus is a temporary variable
// - if DataChunkForErrorContent is set, it will be used to add a content
// body in the response with the textual representation of the error code
procedure SetStatus(code : integer; var outStatus : RawByteString);
procedure SetErrorStatus(code : integer; var outStatus : RawByteString;
dataChunkForErrorContent : PHTTP_DATA_CHUNK_INMEMORY;
const errorMsg : String);
// will set the content of the reponse, and ContentType header
procedure SetContent(var DataChunk : HTTP_DATA_CHUNK_INMEMORY;
const Content : RawByteString; const ContentType : RawByteString = 'text/html');
/// will set all header values from lines
// - Content-Type/Content-Encoding/Location will be set in KnownHeaders[]
// - all other headers will be set in temp UnknownHeaders[0..MaxUnknownHeader]
procedure SetHeaders(P : PAnsiChar; const UnknownHeaders : HTTP_UNKNOWN_HEADER_ARRAY);
end;
PHTTP_RESPONSE_V2 = ^HTTP_RESPONSE_V2;
HTTP_PROPERTY_FLAGS = ULONG;
HTTP_ENABLED_STATE = (
HttpEnabledStateActive,
HttpEnabledStateInactive
);
PHTTP_ENABLED_STATE = ^HTTP_ENABLED_STATE;
HTTP_STATE_INFO = record
Flags : HTTP_PROPERTY_FLAGS;
State : HTTP_ENABLED_STATE;
end;
PHTTP_STATE_INFO = ^HTTP_STATE_INFO;
THTTP_503_RESPONSE_VERBOSITY = (
Http503ResponseVerbosityBasic,
Http503ResponseVerbosityLimited,
Http503ResponseVerbosityFull
);
PHTTP_503_RESPONSE_VERBOSITY = ^ THTTP_503_RESPONSE_VERBOSITY;
HTTP_QOS_SETTING_TYPE = (
HttpQosSettingTypeBandwidth,
HttpQosSettingTypeConnectionLimit,
HttpQosSettingTypeFlowRate // Windows Server 2008 R2 and Windows 7 only.
);
PHTTP_QOS_SETTING_TYPE = ^HTTP_QOS_SETTING_TYPE;
HTTP_QOS_SETTING_INFO = record
QosType : HTTP_QOS_SETTING_TYPE;
QosSetting : Pointer;
end;
PHTTP_QOS_SETTING_INFO = ^HTTP_QOS_SETTING_INFO;
HTTP_CONNECTION_LIMIT_INFO = record
Flags : HTTP_PROPERTY_FLAGS;
MaxConnections : ULONG;
end;
PHTTP_CONNECTION_LIMIT_INFO = ^HTTP_CONNECTION_LIMIT_INFO;
HTTP_BANDWIDTH_LIMIT_INFO = record
Flags : HTTP_PROPERTY_FLAGS;
MaxBandwidth : ULONG;
end;
PHTTP_BANDWIDTH_LIMIT_INFO = ^HTTP_BANDWIDTH_LIMIT_INFO;
const
HTTP_MIN_ALLOWED_BANDWIDTH_THROTTLING_RATE {:ULONG} = 1024;
HTTP_LIMIT_INFINITE {:ULONG} = ULONG(-1);
type
HTTP_SERVICE_CONFIG_TIMEOUT_KEY = (
IdleConnectionTimeout = 0,
HeaderWaitTimeout
);
PHTTP_SERVICE_CONFIG_TIMEOUT_KEY = ^HTTP_SERVICE_CONFIG_TIMEOUT_KEY;
HTTP_SERVICE_CONFIG_TIMEOUT_PARAM = USHORT;
PHTTP_SERVICE_CONFIG_TIMEOUT_PARAM = ^HTTP_SERVICE_CONFIG_TIMEOUT_PARAM;
HTTP_SERVICE_CONFIG_TIMEOUT_SET = record
KeyDesc : HTTP_SERVICE_CONFIG_TIMEOUT_KEY;
ParamDesc : HTTP_SERVICE_CONFIG_TIMEOUT_PARAM;
end;
PHTTP_SERVICE_CONFIG_TIMEOUT_SET = ^HTTP_SERVICE_CONFIG_TIMEOUT_SET;
HTTP_TIMEOUT_LIMIT_INFO = record
Flags : HTTP_PROPERTY_FLAGS;
EntityBody : USHORT;
DrainEntityBody : USHORT;
RequestQueue : USHORT;
IdleConnection : USHORT;
HeaderWait : USHORT;
MinSendRate : USHORT;
end;
PHTTP_TIMEOUT_LIMIT_INFO = ^HTTP_TIMEOUT_LIMIT_INFO;
HTTP_LISTEN_ENDPOINT_INFO = record
Flags : HTTP_PROPERTY_FLAGS;
EnableSharing : BOOLEAN;
end;
PHTTP_LISTEN_ENDPOINT_INFO = ^HTTP_LISTEN_ENDPOINT_INFO;
HTTP_SERVER_AUTHENTICATION_DIGEST_PARAMS = record
DomainNameLength : USHORT;
DomainName : PWideChar;
RealmLength : USHORT;
Realm : PWideChar;
end;
PHTTP_SERVER_AUTHENTICATION_DIGEST_PARAMS = ^HTTP_SERVER_AUTHENTICATION_DIGEST_PARAMS;
HTTP_SERVER_AUTHENTICATION_BASIC_PARAMS = record
RealmLength : USHORT;
Realm : PWideChar;
end;
PHTTP_SERVER_AUTHENTICATION_BASIC_PARAMS = ^HTTP_SERVER_AUTHENTICATION_BASIC_PARAMS;
const
HTTP_AUTH_ENABLE_BASIC = $00000001;
HTTP_AUTH_ENABLE_DIGEST = $00000002;
HTTP_AUTH_ENABLE_NTLM = $00000004;
HTTP_AUTH_ENABLE_NEGOTIATE = $00000008;
HTTP_AUTH_ENABLE_KERBEROS = $00000010;
HTTP_AUTH_ENABLE_ALL = $0000001F;
HTTP_AUTH_EX_FLAG_ENABLE_KERBEROS_CREDENTIAL_CACHING = $01;
HTTP_AUTH_EX_FLAG_CAPTURE_CREDENTIAL = $02;
type
HTTP_SERVER_AUTHENTICATION_INFO = record
Flags : HTTP_PROPERTY_FLAGS;
AuthSchemes : ULONG;
ReceiveMutualAuth : BYTEBOOL;
ReceiveContextHandle : BYTEBOOL;
DisableNTLMCredentialCaching : BYTEBOOL;
ExFlags : BYTE;
DigestParams : HTTP_SERVER_AUTHENTICATION_DIGEST_PARAMS;
BasicParams : HTTP_SERVER_AUTHENTICATION_BASIC_PARAMS;
end;
PHTTP_SERVER_AUTHENTICATION_INFO = ^HTTP_SERVER_AUTHENTICATION_INFO;
HTTP_SERVICE_BINDING_TYPE=(
HttpServiceBindingTypeNone = 0,
HttpServiceBindingTypeW,
HttpServiceBindingTypeA
);
HTTP_SERVICE_BINDING_BASE = record
BindingType : HTTP_SERVICE_BINDING_TYPE;
end;
PHTTP_SERVICE_BINDING_BASE = ^HTTP_SERVICE_BINDING_BASE;
HTTP_SERVICE_BINDING_A = record
Base : HTTP_SERVICE_BINDING_BASE;
Buffer : PAnsiChar;
BufferSize : ULONG;
end;
PHTTP_SERVICE_BINDING_A = HTTP_SERVICE_BINDING_A;
HTTP_SERVICE_BINDING_W = record
Base : HTTP_SERVICE_BINDING_BASE;
Buffer : PWCHAR;
BufferSize : ULONG;
end;
PHTTP_SERVICE_BINDING_W = ^HTTP_SERVICE_BINDING_W;
HTTP_AUTHENTICATION_HARDENING_LEVELS = (
HttpAuthenticationHardeningLegacy = 0,
HttpAuthenticationHardeningMedium,
HttpAuthenticationHardeningStrict
);
const
HTTP_CHANNEL_BIND_PROXY = $1;
HTTP_CHANNEL_BIND_PROXY_COHOSTING = $20;
HTTP_CHANNEL_BIND_NO_SERVICE_NAME_CHECK = $2;
HTTP_CHANNEL_BIND_DOTLESS_SERVICE = $4;
HTTP_CHANNEL_BIND_SECURE_CHANNEL_TOKEN = $8;
HTTP_CHANNEL_BIND_CLIENT_SERVICE = $10;
type
HTTP_CHANNEL_BIND_INFO = record
Hardening : HTTP_AUTHENTICATION_HARDENING_LEVELS;
Flags : ULONG;
ServiceNames : PHTTP_SERVICE_BINDING_BASE;
NumberOfServiceNames : ULONG;
end;
PHTTP_CHANNEL_BIND_INFO = ^HTTP_CHANNEL_BIND_INFO;
HTTP_REQUEST_CHANNEL_BIND_STATUS = record
ServiceName : PHTTP_SERVICE_BINDING_BASE;
ChannelToken : PUCHAR;
ChannelTokenSize : ULONG;
Flags : ULONG;
end;
PHTTP_REQUEST_CHANNEL_BIND_STATUS = ^HTTP_REQUEST_CHANNEL_BIND_STATUS;
const
// Logging option flags. When used in the logging configuration alters
// some default logging behaviour.
// HTTP_LOGGING_FLAG_LOCAL_TIME_ROLLOVER - This flag is used to change
// the log file rollover to happen by local time based. By default
// log file rollovers happen by GMT time.
HTTP_LOGGING_FLAG_LOCAL_TIME_ROLLOVER = 1;
// HTTP_LOGGING_FLAG_USE_UTF8_CONVERSION - When set the unicode fields
// will be converted to UTF8 multibytes when writting to the log
// files. When this flag is not present, the local code page
// conversion happens.
HTTP_LOGGING_FLAG_USE_UTF8_CONVERSION = 2;
// HTTP_LOGGING_FLAG_LOG_ERRORS_ONLY -
// HTTP_LOGGING_FLAG_LOG_SUCCESS_ONLY - These two flags are used to
// to do selective logging. If neither of them are present both
// types of requests will be logged. Only one these flags can be
// set at a time. They are mutually exclusive.
HTTP_LOGGING_FLAG_LOG_ERRORS_ONLY = 4;
HTTP_LOGGING_FLAG_LOG_SUCCESS_ONLY = 8;
//
// The known log fields recognized/supported by HTTPAPI. Following fields
// are used for W3C logging. Subset of them are also used for error
// logging.
//
HTTP_LOG_FIELD_DATE = $00000001;
HTTP_LOG_FIELD_TIME = $00000002;
HTTP_LOG_FIELD_CLIENT_IP = $00000004;
HTTP_LOG_FIELD_USER_NAME = $00000008;
HTTP_LOG_FIELD_SITE_NAME = $00000010;
HTTP_LOG_FIELD_COMPUTER_NAME = $00000020;
HTTP_LOG_FIELD_SERVER_IP = $00000040;
HTTP_LOG_FIELD_METHOD = $00000080;
HTTP_LOG_FIELD_URI_STEM = $00000100;
HTTP_LOG_FIELD_URI_QUERY = $00000200;
HTTP_LOG_FIELD_STATUS = $00000400;
HTTP_LOG_FIELD_WIN32_STATUS = $00000800;
HTTP_LOG_FIELD_BYTES_SENT = $00001000;
HTTP_LOG_FIELD_BYTES_RECV = $00002000;
HTTP_LOG_FIELD_TIME_TAKEN = $00004000;
HTTP_LOG_FIELD_SERVER_PORT = $00008000;
HTTP_LOG_FIELD_USER_AGENT = $00010000;
HTTP_LOG_FIELD_COOKIE = $00020000;
HTTP_LOG_FIELD_REFERER = $00040000;
HTTP_LOG_FIELD_VERSION = $00080000;
HTTP_LOG_FIELD_HOST = $00100000;
HTTP_LOG_FIELD_SUB_STATUS = $00200000;
HTTP_ALL_NON_ERROR_LOG_FIELDS = HTTP_LOG_FIELD_SUB_STATUS*2-1;
//
// Fields that are used only for error logging.
//
HTTP_LOG_FIELD_CLIENT_PORT = $00400000;
HTTP_LOG_FIELD_URI = $00800000;
HTTP_LOG_FIELD_SITE_ID = $01000000;
HTTP_LOG_FIELD_REASON = $02000000;
HTTP_LOG_FIELD_QUEUE_NAME = $04000000;
type
HTTP_LOGGING_TYPE = (
HttpLoggingTypeW3C,
HttpLoggingTypeIIS,
HttpLoggingTypeNCSA,
HttpLoggingTypeRaw
);
HTTP_LOGGING_ROLLOVER_TYPE = (
HttpLoggingRolloverSize,
HttpLoggingRolloverDaily,
HttpLoggingRolloverWeekly,
HttpLoggingRolloverMonthly,
HttpLoggingRolloverHourly
);
HTTP_LOGGING_INFO = record
Flags : HTTP_PROPERTY_FLAGS;
LoggingFlags : ULONG;
SoftwareName : PWideChar;
SoftwareNameLength : USHORT;
DirectoryNameLength : USHORT;
DirectoryName : PWideChar;
Format : HTTP_LOGGING_TYPE;
Fields : ULONG;
pExtFields : PVOID;
NumOfExtFields : USHORT;
MaxRecordSize : USHORT;
RolloverType : HTTP_LOGGING_ROLLOVER_TYPE;
RolloverSize : ULONG;
pSecurityDescriptor : PSECURITY_DESCRIPTOR;
end;
PHTTP_LOGGING_INFO = ^HTTP_LOGGING_INFO;
HTTP_LOG_DATA_TYPE = (
HttpLogDataTypeFields
);
HTTP_LOG_DATA = record
Typ : HTTP_LOG_DATA_TYPE
end;
PHTTP_LOG_DATA = ^HTTP_LOG_DATA;
HTTP_LOG_FIELDS_DATA = record
Base : HTTP_LOG_DATA;
UserNameLength : USHORT;
UriStemLength : USHORT;
ClientIpLength : USHORT;
ServerNameLength : USHORT;
ServiceNameLength : USHORT;
ServerIpLength : USHORT;
MethodLength : USHORT;
UriQueryLength : USHORT;
HostLength : USHORT;
UserAgentLength : USHORT;
CookieLength : USHORT;
ReferrerLength : USHORT;
UserName : PWideChar;
UriStem : PWideChar;
ClientIp : PAnsiChar;
ServerName : PAnsiChar;
ServiceName : PAnsiChar;
ServerIp : PAnsiChar;
Method : PAnsiChar;
UriQuery : PAnsiChar;
Host : PAnsiChar;
UserAgent : PAnsiChar;
Cookie : PAnsiChar;
Referrer : PAnsiChar;
ServerPort : USHORT;
ProtocolStatus : USHORT;
Win32Status : ULONG;
MethodNum : THttpVerb;
SubStatus : USHORT;
end;
PHTTP_LOG_FIELDS_DATA = ^HTTP_LOG_FIELDS_DATA;
HTTP_BINDING_INFO = record
Flags : HTTP_PROPERTY_FLAGS;
RequestQueueHandle : THandle;
end;
HTTP_PROTECTION_LEVEL_TYPE=(
HttpProtectionLevelUnrestricted,
HttpProtectionLevelEdgeRestricted,
HttpProtectionLevelRestricted
);
HTTP_PROTECTION_LEVEL_INFO = record
Flags : HTTP_PROPERTY_FLAGS;
Level : HTTP_PROTECTION_LEVEL_TYPE;
end;
PHTTP_PROTECTION_LEVEL_INFO = ^HTTP_PROTECTION_LEVEL_INFO;
const
// HTTP_VERSION_UNKNOWN : HTTPAPI_VERSION = (MajorVersion : 0; MinorVersion : 0);
// HTTP_VERSION_0_9 : HTTPAPI_VERSION = (MajorVersion : 0; MinorVersion : 9);
// HTTP_VERSION_1_0 : HTTPAPI_VERSION = (MajorVersion : 1; MinorVersion : 0);
// HTTP_VERSION_1_1 : HTTPAPI_VERSION = (MajorVersion : 1; MinorVersion : 1);
// HTTPAPI_VERSION_1 : HTTPAPI_VERSION = (MajorVersion : 1; MinorVersion : 0);
HTTPAPI_VERSION_2 : HTTPAPI_VERSION = (MajorVersion : 2; MinorVersion : 0);
// if set, available entity body is copied along with the request headers
// into pEntityChunks
HTTP_RECEIVE_REQUEST_FLAG_COPY_BODY = 1;
HTTP_RECEIVE_REQUEST_FLAG_FLUSH_BODY = 2;
// see http://msdn.microsoft.com/en-us/library/windows/desktop/aa364496
HTTP_RECEIVE_REQUEST_ENTITY_BODY_FLAG_FILL_BUFFER = 1;
// there is more entity body to be read for this request
HTTP_REQUEST_FLAG_MORE_ENTITY_BODY_EXISTS = 1;
// initialization for applications that use the HTTP Server API
HTTP_INITIALIZE_SERVER = 1;
// initialization for applications that use the HTTP configuration functions
HTTP_INITIALIZE_CONFIG = 2;
HTTP_URL_FLAG_REMOVE_ALL = 1;
HTTP_CREATE_REQUEST_QUEUE_FLAG_OPEN_EXISTING = 1;
HTTP_CREATE_REQUEST_QUEUE_FLAG_CONTROLLER = 2;
// see http://msdn.microsoft.com/en-us/library/windows/desktop/aa364499
HTTP_SEND_RESPONSE_FLAG_DISCONNECT = $00000001;
HTTP_SEND_RESPONSE_FLAG_MORE_DATA = $00000002;
HTTP_SEND_RESPONSE_FLAG_BUFFER_DATA = $00000004;
HTTP_SEND_RESPONSE_FLAG_ENABLE_NAGLING = $00000008;
HTTP_SEND_RESPONSE_FLAG_PROCESS_RANGES = $00000020;
HTTP_SEND_RESPONSE_FLAG_OPAQUE = $00000040;
HTTP_FLUSH_RESPONSE_FLAG_RECURSIVE = 1;
type
HTTP_SERVER_PROPERTY = (
HttpServerAuthenticationProperty,
HttpServerLoggingProperty,
HttpServerQosProperty,
HttpServerTimeoutsProperty,
HttpServerQueueLengthProperty,
HttpServerStateProperty,
HttpServer503VerbosityProperty,
HttpServerBindingProperty,
HttpServerExtendedAuthenticationProperty,
HttpServerListenEndpointProperty,
HttpServerChannelBindProperty,
HttpServerProtectionLevelProperty
);
THttpAPIs = (
hInitialize, hTerminate, hCreateHttpHandle,
hAddUrl, hRemoveUrl, hReceiveHttpRequest,
hSendHttpResponse, hReceiveRequestEntityBody, hSendResponseEntityBody,
hSetServiceConfiguration, hDeleteServiceConfiguration,
hFlushResponseCache,
hCancelHttpRequest,
hCreateServerSession, hCloseServerSession,
hCreateRequestQueue,
hSetServerSessionProperty, hQueryServerSessionProperty,
hCreateUrlGroup, hCloseUrlGroup,
hAddUrlToUrlGroup, hRemoveUrlFromUrlGroup,
hSetUrlGroupProperty, hQueryUrlGroupProperty);
const
HttpNames : array[THttpAPIs] of PChar = (
'HttpInitialize', 'HttpTerminate', 'HttpCreateHttpHandle',
'HttpAddUrl', 'HttpRemoveUrl', 'HttpReceiveHttpRequest',
'HttpSendHttpResponse', 'HttpReceiveRequestEntityBody', 'HttpSendResponseEntityBody',
'HttpSetServiceConfiguration', 'HttpDeleteServiceConfiguration',
'HttpFlushResponseCache',
'HttpCancelHttpRequest',
'HttpCreateServerSession', 'HttpCloseServerSession',
'HttpCreateRequestQueue',
'HttpSetServerSessionProperty', 'HttpQueryServerSessionProperty',
'HttpCreateUrlGroup', 'HttpCloseUrlGroup',
'HttpAddUrlToUrlGroup', 'HttpRemoveUrlFromUrlGroup',
'HttpSetUrlGroupProperty', 'HttpQueryUrlGroupProperty'
);
type
THttpAPI = packed record
Module : THandle;
{/ The HttpInitialize function initializes the HTTP Server API driver, starts it,
if it has not already been started, and allocates data structures for the
calling application to support response-queue creation and other operations.
Call this function before calling any other functions in the HTTP Server API. }
Initialize : function(Version : HTTPAPI_VERSION; Flags : cardinal;
pReserved : pointer = nil) : HRESULT; stdcall;
{/ The HttpTerminate function cleans up resources used by the HTTP Server API
to process calls by an application. An application should call HttpTerminate
once for every time it called HttpInitialize, with matching flag settings. }
Terminate : function(Flags : cardinal;
Reserved : integer = 0) : HRESULT; stdcall;
{/ The HttpCreateHttpHandle function creates an HTTP request queue for the
calling application and returns a handle to it. }
CreateHttpHandle : function(var ReqQueueHandle : THandle;
Reserved : integer = 0) : HRESULT; stdcall;
{/ The HttpAddUrl function registers a given URL so that requests that match
it are routed to a specified HTTP Server API request queue. An application
can register multiple URLs to a single request queue using repeated calls to
HttpAddUrl.
- a typical url prefix is 'http://+:80/vroot/', 'https://+:80/vroot/' or
'http://adatum.com:443/secure/database/' - here the '+' is called a
Strong wildcard, i.e. will match every IP or server name }
AddUrl : function(ReqQueueHandle : THandle; UrlPrefix : PWideChar;
Reserved : integer = 0) : HRESULT; stdcall;
{/ Unregisters a specified URL, so that requests for it are no longer
routed to a specified queue. }
RemoveUrl : function(ReqQueueHandle : THandle; UrlPrefix : PWideChar) : HRESULT; stdcall;
{/ retrieves the next available HTTP request from the specified request queue }
ReceiveHttpRequest : function(ReqQueueHandle : THandle; RequestId : HTTP_REQUEST_ID;
Flags : cardinal; var pRequestBuffer : HTTP_REQUEST_V2; RequestBufferLength : ULONG;
var pBytesReceived : ULONG; pOverlapped : pointer = nil) : HRESULT; stdcall;
{/ sent the response to a specified HTTP request }
SendHttpResponse : function(ReqQueueHandle : THandle; RequestId : HTTP_REQUEST_ID;
Flags : integer; var pHttpResponse : HTTP_RESPONSE_V2; pReserved1 : pointer;
var pBytesSent : cardinal; pReserved2 : pointer = nil; Reserved3 : ULONG = 0;
pOverlapped : pointer = nil; pLogData : PHTTP_LOG_DATA = nil) : HRESULT; stdcall;
{/ receives additional entity body data for a specified HTTP request }
ReceiveRequestEntityBody : function(ReqQueueHandle : THandle; RequestId : HTTP_REQUEST_ID;
Flags : ULONG; pBuffer : pointer; BufferLength : cardinal; var pBytesReceived : cardinal;
pOverlapped : pointer = nil) : HRESULT; stdcall;
{/ sends entity-body data associated with an HTTP response. }
SendResponseEntityBody : function(ReqQueueHandle : THandle; RequestId : HTTP_REQUEST_ID;
Flags : ULONG; EntityChunkCount: USHORT; pEntityChunks: pointer; var pBytesSent: ULONG;
pReserved1 : pointer = nil; pReserved2 : ULONG = 0; pOverlapped : pointer = nil;
pLogData : PHTTP_LOG_DATA = nil) : HRESULT; stdcall;
{/ set specified data, such as IP addresses or SSL Certificates, from the
HTTP Server API configuration store}
SetServiceConfiguration : function(ServiceHandle : THandle;
ConfigId : THttpServiceConfigID; pConfigInformation : pointer;
ConfigInformationLength : ULONG; pOverlapped : pointer = nil) : HRESULT; stdcall;
{/ deletes specified data, such as IP addresses or SSL Certificates, from the
HTTP Server API configuration store}
DeleteServiceConfiguration : function(ServiceHandle : THandle;
ConfigId : THttpServiceConfigID; pConfigInformation : pointer;
ConfigInformationLength : ULONG; pOverlapped : pointer = nil) : HRESULT; stdcall;
FlushResponseCache : function(ReqQueueHandle: THandle; pUrlPrefix: PWideChar; Flags: ULONG;
pOverlapped: POverlapped): ULONG; stdcall;
CancelHttpRequest : function(ReqQueueHandle : THandle; RequestId : HTTP_REQUEST_ID;
pOverlapped : pointer = nil) : HRESULT; stdcall;
CreateServerSession : function(Version : HTTPAPI_VERSION;
var ServerSessionId : HTTP_SERVER_SESSION_ID; Reserved : ULONG = 0) : HRESULT; stdcall;
CloseServerSession : function(ServerSessionId : HTTP_SERVER_SESSION_ID) : HRESULT; stdcall;
CreateRequestQueue : function(Version : HTTPAPI_VERSION;
pName : PWideChar; pSecurityAttributes : Pointer;
Flags : ULONG; var ReqQueueHandle : THandle) : HRESULT; stdcall;
SetServerSessionProperty : function(ServerSessionId : HTTP_SERVER_SESSION_ID;
aProperty : HTTP_SERVER_PROPERTY; pPropertyInformation : Pointer;
PropertyInformationLength : ULONG) : HRESULT; stdcall;
QueryServerSessionProperty : function(ServerSessionId : HTTP_SERVER_SESSION_ID;
aProperty : HTTP_SERVER_PROPERTY; pPropertyInformation : Pointer;
PropertyInformationLength : ULONG; pReturnLength : PULONG = nil) : HRESULT; stdcall;
CreateUrlGroup : function(ServerSessionId : HTTP_SERVER_SESSION_ID;
var UrlGroupId : HTTP_URL_GROUP_ID; Reserved : ULONG = 0) : HRESULT; stdcall;
CloseUrlGroup : function(UrlGroupId : HTTP_URL_GROUP_ID) : HRESULT; stdcall;
AddUrlToUrlGroup : function(UrlGroupId : HTTP_URL_GROUP_ID;
pFullyQualifiedUrl : PWideChar; UrlContext : HTTP_URL_CONTEXT = 0;
Reserved : ULONG = 0) : HRESULT; stdcall;
RemoveUrlFromUrlGroup : function(UrlGroupId : HTTP_URL_GROUP_ID;
pFullyQualifiedUrl : PWideChar; Flags : ULONG = HTTP_URL_FLAG_REMOVE_ALL) : HRESULT; stdcall;
SetUrlGroupProperty : function(UrlGroupId : HTTP_URL_GROUP_ID;
aProperty : HTTP_SERVER_PROPERTY; pPropertyInformation : Pointer;
PropertyInformationLength : ULONG) : HRESULT; stdcall;
QueryUrlGroupProperty : function(UrlGroupId : HTTP_URL_GROUP_ID;
aProperty : HTTP_SERVER_PROPERTY; pPropertyInformation : Pointer;
PropertyInformationLength : ULONG; pReturnLength : PULONG = nil) : HRESULT; stdcall;
class procedure InitializeAPI; static;
class procedure Check(error : HRESULT; api : THttpAPIs); static; inline;
end;
EHttpApiServer = class (Exception)
public
constructor Create(api : THttpAPIs; Error : integer);
end;
var
HttpAPI : THttpAPI;
/// retrieve the HTTP reason text from a code
// - e.g. StatusCodeToReason(200)='OK'
procedure StatusCodeToReason(code : integer; var result : RawByteString);
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// ------------------------------------------------------------------
implementation
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// ------------------------------------------------------------------
class procedure THttpAPI.InitializeAPI;
var
api : THttpAPIs;
P : PPointer;
begin
if HttpAPI.Module<>0 then
exit; // already loaded
try
if HttpAPI.Module = 0 then begin
HttpAPI.Module := LoadLibrary('httpapi.dll');
if HttpAPI.Module<=255 then
raise Exception.Create('Unable to find httpapi.dll');
{$ifdef FPC}
P := @Http.Initialize;
{$else}
P := @@HttpAPI.Initialize;
{$endif}
for api := low(api) to high(api) do begin
P^ := GetProcAddress(HttpAPI.Module, HttpNames[api]);
if P^ = nil then
raise Exception.CreateFmt('Unable to find %s in httpapi.dll', [HttpNames[api]]);
inc(P);
end;
end;
except
on E : Exception do begin
if HttpAPI.Module>255 then begin