blob: b26f6ba2a66befcc62e60574af2026ef8fe16470 [file] [log] [blame]
Jens Geyer02230912019-04-03 01:12:51 +02001(*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *)
19unit Thrift.WinHTTP;
20
21{$I Thrift.Defines.inc}
22{$SCOPEDENUMS ON}
23
24// packing according to winhttp.h
25{$IFDEF Win64} {$ALIGN 8} {$ELSE} {$ALIGN 4} {$ENDIF}
26
27interface
28
29uses
30 Windows,
31 Classes,
32 SysUtils,
33 Math,
34 Generics.Collections;
35
36
37type
38 HINTERNET = type Pointer;
39 INTERNET_PORT = type WORD;
40 INTERNET_SCHEME = type Integer;
41 LPLPCWSTR = ^LPCWSTR;
42
43 LPURL_COMPONENTS = ^URL_COMPONENTS;
44 URL_COMPONENTS = record
45 dwStructSize : DWORD; // set to SizeOf(URL_COMPONENTS)
46 lpszScheme : LPWSTR; // scheme name
47 dwSchemeLength : DWORD;
48 nScheme : INTERNET_SCHEME; // enumerated scheme type
49 lpszHostName : LPWSTR; // host name
50 dwHostNameLength : DWORD;
51 nPort : INTERNET_PORT; // port number
52 lpszUserName : LPWSTR; // user name
53 dwUserNameLength : DWORD;
54 lpszPassword : LPWSTR; // password
55 dwPasswordLength : DWORD;
56 lpszUrlPath : LPWSTR; // URL-path
57 dwUrlPathLength : DWORD;
58 lpszExtraInfo : LPWSTR; // extra information
59 dwExtraInfoLength : DWORD;
60 end;
61
62 URL_COMPONENTSW = URL_COMPONENTS;
63 LPURL_COMPONENTSW = LPURL_COMPONENTS;
64
65
Jens Geyer83ff7532019-06-06 22:46:03 +020066 // When retrieving proxy data, an application must free the lpszProxy and
67 // lpszProxyBypass strings contained in this structure (if they are non-NULL)
68 // using the GlobalFree function.
69 LPWINHTTP_PROXY_INFO = ^WINHTTP_PROXY_INFO;
70 WINHTTP_PROXY_INFO = record
71 dwAccessType : DWORD; // see WINHTTP_ACCESS_* types below
72 lpszProxy : LPWSTR; // proxy server list
73 lpszProxyBypass : LPWSTR; // proxy bypass list
74 end;
75
76 LPWINHTTP_PROXY_INFOW = ^WINHTTP_PROXY_INFOW;
77 WINHTTP_PROXY_INFOW = WINHTTP_PROXY_INFO;
78
79
80 WINHTTP_AUTOPROXY_OPTIONS = record
81 dwFlags : DWORD;
82 dwAutoDetectFlags : DWORD;
83 lpszAutoConfigUrl : LPCWSTR;
84 lpvReserved : LPVOID;
85 dwReserved : DWORD;
86 fAutoLogonIfChallenged : BOOL;
87 end;
88
89
90 WINHTTP_CURRENT_USER_IE_PROXY_CONFIG = record
91 fAutoDetect : BOOL;
92 lpszAutoConfigUrl : LPWSTR;
93 lpszProxy : LPWSTR;
94 lpszProxyBypass : LPWSTR;
95 end;
96
97
98
99
Jens Geyer02230912019-04-03 01:12:51 +0200100function WinHttpCloseHandle( aHandle : HINTERNET) : BOOL; stdcall;
101
102function WinHttpOpen( const pszAgentW : LPCWSTR;
103 const dwAccessType : DWORD;
104 const pszProxyW : LPCWSTR;
105 const pszProxyBypassW : LPCWSTR;
106 const dwFlags : DWORD
107 ) : HINTERNET; stdcall;
108
109function WinHttpConnect( const hSession : HINTERNET;
110 const pswzServerName : LPCWSTR;
111 const nServerPort : INTERNET_PORT;
112 const dwReserved : DWORD
113 ) : HINTERNET; stdcall;
114
115function WinHttpOpenRequest( const hConnect : HINTERNET;
116 const pwszVerb, pwszObjectName, pwszVersion, pwszReferrer : LPCWSTR;
117 const ppwszAcceptTypes : LPLPCWSTR;
118 const dwFlags : DWORD
119 ) : HINTERNET; stdcall;
120
Jens Geyer47f63172019-06-06 22:42:58 +0200121function WinHttpQueryOption( const hInternet : HINTERNET;
122 const dwOption : DWORD;
123 const pBuffer : Pointer;
124 var dwBufferLength : DWORD) : BOOL; stdcall;
125
126function WinHttpSetOption( const hInternet : HINTERNET;
127 const dwOption : DWORD;
128 const pBuffer : Pointer;
129 const dwBufferLength : DWORD) : BOOL; stdcall;
130
Jens Geyer02230912019-04-03 01:12:51 +0200131function WinHttpSetTimeouts( const hRequestOrSession : HINTERNET;
132 const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32
133 ) : BOOL; stdcall;
134
135function WinHttpAddRequestHeaders( const hRequest : HINTERNET;
136 const pwszHeaders : LPCWSTR;
137 const dwHeadersLengthInChars : DWORD;
138 const dwModifiers : DWORD
139 ) : BOOL; stdcall;
140
Jens Geyer83ff7532019-06-06 22:46:03 +0200141function WinHttpGetProxyForUrl( const hSession : HINTERNET;
142 const lpcwszUrl : LPCWSTR;
143 const options : WINHTTP_AUTOPROXY_OPTIONS;
144 const info : WINHTTP_PROXY_INFO
145 ) : BOOL; stdcall;
146
147function WinHttpGetIEProxyConfigForCurrentUser( var config : WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
148 ) : BOOL; stdcall;
149
150
Jens Geyer02230912019-04-03 01:12:51 +0200151function WinHttpSendRequest( const hRequest : HINTERNET;
152 const lpszHeaders : LPCWSTR;
153 const dwHeadersLength : DWORD;
154 const lpOptional : Pointer;
155 const dwOptionalLength : DWORD;
156 const dwTotalLength : DWORD;
157 const pContext : Pointer
158 ) : BOOL; stdcall;
159
160function WinHttpWriteData( const hRequest : HINTERNET;
161 const pBuf : Pointer;
162 const dwBytesToWrite : DWORD;
163 out dwBytesWritten : DWORD
164 ) : BOOL; stdcall;
165
166function WinHttpReceiveResponse( const hRequest : HINTERNET; const lpReserved : Pointer) : BOOL; stdcall;
167
168function WinHttpQueryHeaders( const hRequest : HINTERNET;
169 const dwInfoLevel : DWORD;
170 const pwszName : LPCWSTR;
171 const lpBuffer : Pointer;
172 var dwBufferLength : DWORD;
173 var dwIndex : DWORD
174 ) : BOOL; stdcall;
175
176function WinHttpQueryDataAvailable( const hRequest : HINTERNET;
177 var dwNumberOfBytesAvailable : DWORD
178 ) : BOOL; stdcall;
179
180function WinHttpReadData( const hRequest : HINTERNET;
181 const lpBuffer : Pointer;
182 const dwBytesToRead : DWORD;
183 out dwBytesRead : DWORD
184 ) : BOOL; stdcall;
185
186function WinHttpCrackUrl( const pwszUrl : LPCWSTR;
187 const dwUrlLength : DWORD;
188 const dwFlags : DWORD;
189 var urlComponents : URL_COMPONENTS
190 ) : BOOL; stdcall;
191
192function WinHttpCreateUrl( const UrlComponents : URL_COMPONENTS;
193 const dwFlags : DWORD;
194 const pwszUrl : LPCWSTR;
195 var pdwUrlLength : DWORD
196 ) : BOOL; stdcall;
197
198
199const
200 // WinHttpOpen dwAccessType values
201 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY = 0;
202 WINHTTP_ACCESS_TYPE_NO_PROXY = 1;
203 WINHTTP_ACCESS_TYPE_NAMED_PROXY = 3;
204
205 // flags for WinHttpOpen():
206 WINHTTP_FLAG_ASYNC = $10000000; // want async session, requires WinHttpSetStatusCallback() usage
207
208 // ports
209 INTERNET_DEFAULT_PORT = 0; // use the protocol-specific default (80 or 443)
210
211 // flags for WinHttpOpenRequest():
212 WINHTTP_FLAG_SECURE = $00800000; // use SSL if applicable (HTTPS)
213 WINHTTP_FLAG_ESCAPE_PERCENT = $00000004; // if escaping enabled, escape percent as well
214 WINHTTP_FLAG_NULL_CODEPAGE = $00000008; // assume all symbols are ASCII, use fast convertion
215 WINHTTP_FLAG_BYPASS_PROXY_CACHE = $00000100; // add "pragma: no-cache" request header
216 WINHTTP_FLAG_REFRESH = WINHTTP_FLAG_BYPASS_PROXY_CACHE;
217 WINHTTP_FLAG_ESCAPE_DISABLE = $00000040; // disable escaping
218 WINHTTP_FLAG_ESCAPE_DISABLE_QUERY = $00000080; // if escaping enabled escape path part, but do not escape query
219
220 // flags for WinHttpSendRequest():
221 WINHTTP_NO_ADDITIONAL_HEADERS = nil;
222 WINHTTP_NO_REQUEST_DATA = nil;
223
224 // WinHttpAddRequestHeaders() dwModifiers
225 WINHTTP_ADDREQ_INDEX_MASK = $0000FFFF;
226 WINHTTP_ADDREQ_FLAGS_MASK = $FFFF0000;
227
228 WINHTTP_ADDREQ_FLAG_ADD_IF_NEW = $10000000;
229 WINHTTP_ADDREQ_FLAG_ADD = $20000000;
230 WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA = $40000000;
231 WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON = $01000000;
232 WINHTTP_ADDREQ_FLAG_COALESCE = WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA;
233 WINHTTP_ADDREQ_FLAG_REPLACE = $80000000;
234
235 // URL functions
236 ICU_NO_ENCODE = $20000000; // Don't convert unsafe characters to escape sequence
237 ICU_DECODE = $10000000; // Convert %XX escape sequences to characters
238 ICU_NO_META = $08000000; // Don't convert .. etc. meta path sequences
239 ICU_ENCODE_SPACES_ONLY = $04000000; // Encode spaces only
240 ICU_BROWSER_MODE = $02000000; // Special encode/decode rules for browser
241 ICU_ENCODE_PERCENT = $00001000; // Encode any percent (ASCII25)
242
243 ICU_ESCAPE = $80000000; // (un)escape URL characters
244 ICU_ESCAPE_AUTHORITY = $00002000; // causes InternetCreateUrlA to escape chars in authority components (user, pwd, host)
245 ICU_REJECT_USERPWD = $00004000; // rejects usrls whick have username/pwd sections
246
247 INTERNET_SCHEME_HTTP = INTERNET_SCHEME(1);
248 INTERNET_SCHEME_HTTPS = INTERNET_SCHEME(2);
249
Jens Geyer47f63172019-06-06 22:42:58 +0200250 WINHTTP_NO_CLIENT_CERT_CONTEXT = nil;
251
252 // options manifests for WinHttp{Query|Set}Option
253 WINHTTP_OPTION_CALLBACK = 1;
254 WINHTTP_OPTION_RESOLVE_TIMEOUT = 2;
255 WINHTTP_OPTION_CONNECT_TIMEOUT = 3;
256 WINHTTP_OPTION_CONNECT_RETRIES = 4;
257 WINHTTP_OPTION_SEND_TIMEOUT = 5;
258 WINHTTP_OPTION_RECEIVE_TIMEOUT = 6;
259 WINHTTP_OPTION_RECEIVE_RESPONSE_TIMEOUT = 7;
260 WINHTTP_OPTION_HANDLE_TYPE = 9;
261 WINHTTP_OPTION_READ_BUFFER_SIZE = 12;
262 WINHTTP_OPTION_WRITE_BUFFER_SIZE = 13;
263 WINHTTP_OPTION_PARENT_HANDLE = 21;
264 WINHTTP_OPTION_EXTENDED_ERROR = 24;
265 WINHTTP_OPTION_SECURITY_FLAGS = 31;
266 WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT = 32;
267 WINHTTP_OPTION_URL = 34;
268 WINHTTP_OPTION_SECURITY_KEY_BITNESS = 36;
269 WINHTTP_OPTION_PROXY = 38;
270 WINHTTP_OPTION_USER_AGENT = 41;
271 WINHTTP_OPTION_CONTEXT_VALUE = 45;
272 WINHTTP_OPTION_CLIENT_CERT_CONTEXT = 47;
273 WINHTTP_OPTION_REQUEST_PRIORITY = 58;
274 WINHTTP_OPTION_HTTP_VERSION = 59;
275 WINHTTP_OPTION_DISABLE_FEATURE = 63;
276 WINHTTP_OPTION_CODEPAGE = 68;
277 WINHTTP_OPTION_MAX_CONNS_PER_SERVER = 73;
278 WINHTTP_OPTION_MAX_CONNS_PER_1_0_SERVER = 74;
279 WINHTTP_OPTION_AUTOLOGON_POLICY = 77;
280 WINHTTP_OPTION_SERVER_CERT_CONTEXT = 78;
281 WINHTTP_OPTION_ENABLE_FEATURE = 79;
282 WINHTTP_OPTION_WORKER_THREAD_COUNT = 80;
283 WINHTTP_OPTION_PASSPORT_COBRANDING_TEXT = 81;
284 WINHTTP_OPTION_PASSPORT_COBRANDING_URL = 82;
285 WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH = 83;
286 WINHTTP_OPTION_SECURE_PROTOCOLS = 84;
287 WINHTTP_OPTION_ENABLETRACING = 85;
288 WINHTTP_OPTION_PASSPORT_SIGN_OUT = 86;
289 WINHTTP_OPTION_PASSPORT_RETURN_URL = 87;
290 WINHTTP_OPTION_REDIRECT_POLICY = 88;
291 WINHTTP_OPTION_MAX_HTTP_AUTOMATIC_REDIRECTS = 89;
292 WINHTTP_OPTION_MAX_HTTP_STATUS_CONTINUE = 90;
293 WINHTTP_OPTION_MAX_RESPONSE_HEADER_SIZE = 91;
294 WINHTTP_OPTION_MAX_RESPONSE_DRAIN_SIZE = 92;
295 WINHTTP_OPTION_CONNECTION_INFO = 93;
296 WINHTTP_OPTION_CLIENT_CERT_ISSUER_LIST = 94;
297 WINHTTP_OPTION_SPN = 96;
298 WINHTTP_OPTION_GLOBAL_PROXY_CREDS = 97;
299 WINHTTP_OPTION_GLOBAL_SERVER_CREDS = 98;
300 WINHTTP_OPTION_UNLOAD_NOTIFY_EVENT = 99;
301 WINHTTP_OPTION_REJECT_USERPWD_IN_URL = 100;
302 WINHTTP_OPTION_USE_GLOBAL_SERVER_CREDENTIALS = 101;
303 WINHTTP_OPTION_RECEIVE_PROXY_CONNECT_RESPONSE = 103;
304 WINHTTP_OPTION_IS_PROXY_CONNECT_RESPONSE = 104;
305 WINHTTP_OPTION_SERVER_SPN_USED = 106;
306 WINHTTP_OPTION_PROXY_SPN_USED = 107;
307 WINHTTP_OPTION_SERVER_CBT = 108;
308 //
309 WINHTTP_FIRST_OPTION = WINHTTP_OPTION_CALLBACK;
310 WINHTTP_LAST_OPTION = WINHTTP_OPTION_SERVER_CBT;
311
312 WINHTTP_OPTION_USERNAME = $1000;
313 WINHTTP_OPTION_PASSWORD = $1001;
314 WINHTTP_OPTION_PROXY_USERNAME = $1002;
315 WINHTTP_OPTION_PROXY_PASSWORD = $1003;
316
317 // manifest value for WINHTTP_OPTION_MAX_CONNS_PER_SERVER and WINHTTP_OPTION_MAX_CONNS_PER_1_0_SERVER
318 WINHTTP_CONNS_PER_SERVER_UNLIMITED = $FFFFFFFF;
319
320 // values for WINHTTP_OPTION_AUTOLOGON_POLICY
321 WINHTTP_AUTOLOGON_SECURITY_LEVEL_MEDIUM = 0;
322 WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW = 1;
323 WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH = 2;
324
325 WINHTTP_AUTOLOGON_SECURITY_LEVEL_DEFAULT = WINHTTP_AUTOLOGON_SECURITY_LEVEL_MEDIUM;
326
327 // values for WINHTTP_OPTION_REDIRECT_POLICY
328 WINHTTP_OPTION_REDIRECT_POLICY_NEVER = 0;
329 WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP = 1;
330 WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS = 2;
331
332 WINHTTP_OPTION_REDIRECT_POLICY_LAST = WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS;
333 WINHTTP_OPTION_REDIRECT_POLICY_DEFAULT = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP;
334
335 WINHTTP_DISABLE_PASSPORT_AUTH = $00000000;
336 WINHTTP_ENABLE_PASSPORT_AUTH = $10000000;
337 WINHTTP_DISABLE_PASSPORT_KEYRING = $20000000;
338 WINHTTP_ENABLE_PASSPORT_KEYRING = $40000000;
339
340 // values for WINHTTP_OPTION_DISABLE_FEATURE
341 WINHTTP_DISABLE_COOKIES = $00000001;
342 WINHTTP_DISABLE_REDIRECTS = $00000002;
343 WINHTTP_DISABLE_AUTHENTICATION = $00000004;
344 WINHTTP_DISABLE_KEEP_ALIVE = $00000008;
345
346 // values for WINHTTP_OPTION_ENABLE_FEATURE
347 WINHTTP_ENABLE_SSL_REVOCATION = $00000001;
348 WINHTTP_ENABLE_SSL_REVERT_IMPERSONATION = $00000002;
349
350 // values for WINHTTP_OPTION_SPN
351 WINHTTP_DISABLE_SPN_SERVER_PORT = $00000000;
352 WINHTTP_ENABLE_SPN_SERVER_PORT = $00000001;
353 WINHTTP_OPTION_SPN_MASK = WINHTTP_ENABLE_SPN_SERVER_PORT;
354
355 // winhttp handle types
356 WINHTTP_HANDLE_TYPE_SESSION = 1;
357 WINHTTP_HANDLE_TYPE_CONNECT = 2;
358 WINHTTP_HANDLE_TYPE_REQUEST = 3;
359
360 // values for auth schemes
361 WINHTTP_AUTH_SCHEME_BASIC = $00000001;
362 WINHTTP_AUTH_SCHEME_NTLM = $00000002;
363 WINHTTP_AUTH_SCHEME_PASSPORT = $00000004;
364 WINHTTP_AUTH_SCHEME_DIGEST = $00000008;
365 WINHTTP_AUTH_SCHEME_NEGOTIATE = $00000010;
366
367 // WinHttp supported Authentication Targets
368 WINHTTP_AUTH_TARGET_SERVER = $00000000;
369 WINHTTP_AUTH_TARGET_PROXY = $00000001;
370
371 // values for WINHTTP_OPTION_SECURITY_FLAGS
372
373 // query only
374 SECURITY_FLAG_SECURE = $00000001; // can query only
375 SECURITY_FLAG_STRENGTH_WEAK = $10000000;
376 SECURITY_FLAG_STRENGTH_MEDIUM = $40000000;
377 SECURITY_FLAG_STRENGTH_STRONG = $20000000;
378
379 // Secure connection error status flags
380 WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED = $00000001;
381 WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CERT = $00000002;
382 WINHTTP_CALLBACK_STATUS_FLAG_CERT_REVOKED = $00000004;
383 WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA = $00000008;
384 WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID = $00000010;
385 WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID = $00000020;
386 WINHTTP_CALLBACK_STATUS_FLAG_CERT_WRONG_USAGE = $00000040;
387 WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR = $80000000;
388
389 WINHTTP_FLAG_SECURE_PROTOCOL_SSL2 = $00000008;
390 WINHTTP_FLAG_SECURE_PROTOCOL_SSL3 = $00000020;
391 WINHTTP_FLAG_SECURE_PROTOCOL_TLS1 = $00000080;
392 WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 = $00000200;
393 WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 = $00000800;
394
395 // Note: SECURE_PROTOCOL_ALL does not include TLS1.1 and higher!
396 WINHTTP_FLAG_SECURE_PROTOCOL_ALL = WINHTTP_FLAG_SECURE_PROTOCOL_SSL2
397 or WINHTTP_FLAG_SECURE_PROTOCOL_SSL3
398 or WINHTTP_FLAG_SECURE_PROTOCOL_TLS1;
399
Jens Geyer83ff7532019-06-06 22:46:03 +0200400 // AutoProxy
401 WINHTTP_AUTOPROXY_AUTO_DETECT = $00000001;
402 WINHTTP_AUTOPROXY_CONFIG_URL = $00000002;
403 WINHTTP_AUTOPROXY_HOST_KEEPCASE = $00000004;
404 WINHTTP_AUTOPROXY_HOST_LOWERCASE = $00000008;
405 WINHTTP_AUTOPROXY_RUN_INPROCESS = $00010000;
406 WINHTTP_AUTOPROXY_RUN_OUTPROCESS_ONLY = $00020000;
407
408 // Flags for dwAutoDetectFlags
409 WINHTTP_AUTO_DETECT_TYPE_DHCP = $00000001;
410 WINHTTP_AUTO_DETECT_TYPE_DNS_A = $00000002;
Jens Geyer47f63172019-06-06 22:42:58 +0200411
Jens Geyer02230912019-04-03 01:12:51 +0200412const
413 WINHTTP_ERROR_BASE = 12000;
414 ERROR_WINHTTP_OUT_OF_HANDLES = WINHTTP_ERROR_BASE + 1;
415 ERROR_WINHTTP_TIMEOUT = WINHTTP_ERROR_BASE + 2;
416 ERROR_WINHTTP_INTERNAL_ERROR = WINHTTP_ERROR_BASE + 4;
417 ERROR_WINHTTP_INVALID_URL = WINHTTP_ERROR_BASE + 5;
418 ERROR_WINHTTP_UNRECOGNIZED_SCHEME = WINHTTP_ERROR_BASE + 6;
419 ERROR_WINHTTP_NAME_NOT_RESOLVED = WINHTTP_ERROR_BASE + 7;
420 ERROR_WINHTTP_INVALID_OPTION = WINHTTP_ERROR_BASE + 9;
421 ERROR_WINHTTP_OPTION_NOT_SETTABLE = WINHTTP_ERROR_BASE + 11;
422 ERROR_WINHTTP_SHUTDOWN = WINHTTP_ERROR_BASE + 12;
423 ERROR_WINHTTP_LOGIN_FAILURE = WINHTTP_ERROR_BASE + 15;
424 ERROR_WINHTTP_OPERATION_CANCELLED = WINHTTP_ERROR_BASE + 17;
425 ERROR_WINHTTP_INCORRECT_HANDLE_TYPE = WINHTTP_ERROR_BASE + 18;
426 ERROR_WINHTTP_INCORRECT_HANDLE_STATE = WINHTTP_ERROR_BASE + 19;
427 ERROR_WINHTTP_CANNOT_CONNECT = WINHTTP_ERROR_BASE + 29;
428 ERROR_WINHTTP_CONNECTION_ERROR = WINHTTP_ERROR_BASE + 30;
429 ERROR_WINHTTP_RESEND_REQUEST = WINHTTP_ERROR_BASE + 32;
430 ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED = WINHTTP_ERROR_BASE + 44;
431 ERROR_WINHTTP_CANNOT_CALL_BEFORE_OPEN = WINHTTP_ERROR_BASE + 100;
432 ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND = WINHTTP_ERROR_BASE + 101;
433 ERROR_WINHTTP_CANNOT_CALL_AFTER_SEND = WINHTTP_ERROR_BASE + 102;
434 ERROR_WINHTTP_CANNOT_CALL_AFTER_OPEN = WINHTTP_ERROR_BASE + 103;
435 ERROR_WINHTTP_HEADER_NOT_FOUND = WINHTTP_ERROR_BASE + 150;
436 ERROR_WINHTTP_INVALID_SERVER_RESPONSE = WINHTTP_ERROR_BASE + 152;
437 ERROR_WINHTTP_INVALID_HEADER = WINHTTP_ERROR_BASE + 153;
438 ERROR_WINHTTP_INVALID_QUERY_REQUEST = WINHTTP_ERROR_BASE + 154;
439 ERROR_WINHTTP_HEADER_ALREADY_EXISTS = WINHTTP_ERROR_BASE + 155;
440 ERROR_WINHTTP_REDIRECT_FAILED = WINHTTP_ERROR_BASE + 156;
441 ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR = WINHTTP_ERROR_BASE + 178;
442 ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT = WINHTTP_ERROR_BASE + 166;
443 ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT = WINHTTP_ERROR_BASE + 167;
444 ERROR_WINHTTP_NOT_INITIALIZED = WINHTTP_ERROR_BASE + 172;
445 ERROR_WINHTTP_SECURE_FAILURE = WINHTTP_ERROR_BASE + 175;
446
447 // Certificate security errors. Additional information is provided
448 // via the WINHTTP_CALLBACK_STATUS_SECURE_FAILURE callback notification.
449 ERROR_WINHTTP_SECURE_CERT_DATE_INVALID = WINHTTP_ERROR_BASE + 37;
450 ERROR_WINHTTP_SECURE_CERT_CN_INVALID = WINHTTP_ERROR_BASE + 38;
451 ERROR_WINHTTP_SECURE_INVALID_CA = WINHTTP_ERROR_BASE + 45;
452 ERROR_WINHTTP_SECURE_CERT_REV_FAILED = WINHTTP_ERROR_BASE + 57;
453 ERROR_WINHTTP_SECURE_CHANNEL_ERROR = WINHTTP_ERROR_BASE + 157;
454 ERROR_WINHTTP_SECURE_INVALID_CERT = WINHTTP_ERROR_BASE + 169;
455 ERROR_WINHTTP_SECURE_CERT_REVOKED = WINHTTP_ERROR_BASE + 170;
456 ERROR_WINHTTP_SECURE_CERT_WRONG_USAGE = WINHTTP_ERROR_BASE + 179;
457
458 ERROR_WINHTTP_AUTODETECTION_FAILED = WINHTTP_ERROR_BASE + 180;
459 ERROR_WINHTTP_HEADER_COUNT_EXCEEDED = WINHTTP_ERROR_BASE + 181;
460 ERROR_WINHTTP_HEADER_SIZE_OVERFLOW = WINHTTP_ERROR_BASE + 182;
461 ERROR_WINHTTP_CHUNKED_ENCODING_HEADER_SIZE_OVERFLOW = WINHTTP_ERROR_BASE + 183;
462 ERROR_WINHTTP_RESPONSE_DRAIN_OVERFLOW = WINHTTP_ERROR_BASE + 184;
463 ERROR_WINHTTP_CLIENT_CERT_NO_PRIVATE_KEY = WINHTTP_ERROR_BASE + 185;
464 ERROR_WINHTTP_CLIENT_CERT_NO_ACCESS_PRIVATE_KEY = WINHTTP_ERROR_BASE + 186;
465
466
467const
468 WINHTTP_THRIFT_DEFAULTS = WINHTTP_FLAG_NULL_CODEPAGE
469 or WINHTTP_FLAG_BYPASS_PROXY_CACHE
470 or WINHTTP_FLAG_ESCAPE_DISABLE;
471
472
Jens Geyer47f63172019-06-06 22:42:58 +0200473
Jens Geyer02230912019-04-03 01:12:51 +0200474type
Jens Geyer83ff7532019-06-06 22:46:03 +0200475 IWinHTTPSession = interface;
476 IWinHTTPConnection = interface;
477
Jens Geyer02230912019-04-03 01:12:51 +0200478 IWinHTTPRequest = interface
Jens Geyer83ff7532019-06-06 22:46:03 +0200479 ['{0B7D095E-BB3D-4444-8686-5536E7D6437B}']
Jens Geyer02230912019-04-03 01:12:51 +0200480 function Handle : HINTERNET;
Jens Geyer83ff7532019-06-06 22:46:03 +0200481 function Connection : IWinHTTPConnection;
Jens Geyer02230912019-04-03 01:12:51 +0200482 function AddRequestHeader( const aHeader : string; const addflag : DWORD = WINHTTP_ADDREQ_FLAG_ADD) : Boolean;
483 function SetTimeouts( const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32) : Boolean;
Jens Geyer83ff7532019-06-06 22:46:03 +0200484 procedure TryAutoProxy( const aUrl : string);
Jens Geyer02230912019-04-03 01:12:51 +0200485 function SendRequest( const pBuf : Pointer; const dwBytes : DWORD; const dwExtra : DWORD = 0) : Boolean;
486 function WriteExtraData( const pBuf : Pointer; const dwBytes : DWORD) : DWORD;
487 function FlushAndReceiveResponse : Boolean;
488 function ReadData( const dwRead : DWORD) : TBytes; overload;
489 function ReadData( const pBuf : Pointer; const dwRead : DWORD) : DWORD; overload;
490 end;
491
492 IWinHTTPConnection = interface
Jens Geyer83ff7532019-06-06 22:46:03 +0200493 ['{ED5BCA49-84D6-4CFE-BF18-3238B1FF2AFB}']
Jens Geyer02230912019-04-03 01:12:51 +0200494 function Handle : HINTERNET;
Jens Geyer83ff7532019-06-06 22:46:03 +0200495 function Session : IWinHTTPSession;
Jens Geyer02230912019-04-03 01:12:51 +0200496 function OpenRequest( const secure : Boolean; const aVerb, aObjName, aAcceptTypes : UnicodeString) : IWinHTTPRequest;
497 end;
498
499 IWinHTTPSession = interface
Jens Geyer47f63172019-06-06 22:42:58 +0200500 ['{261ADCB7-5465-4407-8840-468C17F009F0}']
Jens Geyer02230912019-04-03 01:12:51 +0200501 function Handle : HINTERNET;
502 function Connect( const aHostName : UnicodeString; const aPort : INTERNET_PORT = INTERNET_DEFAULT_PORT) : IWinHTTPConnection;
503 function SetTimeouts( const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32) : Boolean;
Jens Geyer47f63172019-06-06 22:42:58 +0200504 function EnableSecureProtocols( const aFlagSet : DWORD) : Boolean;
Jens Geyer02230912019-04-03 01:12:51 +0200505 end;
506
507 IWinHTTPUrl = interface
508 ['{78BE977C-4171-4AF5-A250-FD2890205E63}']
509 // url parts getter
510 function GetScheme : UnicodeString;
511 function GetNumScheme : INTERNET_SCHEME;
512 function GetHostName : UnicodeString;
513 function GetPort : INTERNET_PORT;
514 function GetUserName : UnicodeString;
515 function GetPassword : UnicodeString;
516 function GetUrlPath : UnicodeString;
517 function GetExtraInfo : UnicodeString;
518
519 // url parts setter
520 procedure SetScheme( const value : UnicodeString);
521 procedure SetHostName ( const value : UnicodeString);
522 procedure SetPort( const value : INTERNET_PORT);
523 procedure SetUserName( const value : UnicodeString);
524 procedure SetPassword( const value : UnicodeString);
525 procedure SetUrlPath( const value : UnicodeString);
526 procedure SetExtraInfo( const value : UnicodeString);
527
528 // url as a whole
529 function BuildUrl : UnicodeString;
530 procedure CrackUrl( const value : UnicodeString);
531
532 // url parts
533 property Scheme : UnicodeString read GetScheme write SetScheme;
534 property NumScheme : INTERNET_SCHEME read GetNumScheme; // readonly
535 property HostName : UnicodeString read GetHostName write SetHostName;
536 property Port : INTERNET_PORT read GetPort write SetPort;
537 property UserName : UnicodeString read GetUserName write SetUserName;
538 property Password : UnicodeString read GetPassword write SetPassword;
539 property UrlPath : UnicodeString read GetUrlPath write SetUrlPath;
540 property ExtraInfo : UnicodeString read GetExtraInfo write SetExtraInfo;
541
542 // url as a whole
543 property CompleteURL : UnicodeString read BuildUrl write CrackUrl;
544 end;
545
546
547
548
549type
550 TWinHTTPHandleObjectImpl = class( TInterfacedObject)
551 strict protected
552 FHandle : HINTERNET;
553 function Handle : HINTERNET;
554 public
555 constructor Create( const aHandle : HINTERNET);
556 destructor Destroy; override;
557 end;
558
559
560 TWinHTTPSessionImpl = class( TWinHTTPHandleObjectImpl, IWinHTTPSession)
561 strict protected
562
563 // IWinHTTPSession
564 function Connect( const aHostName : UnicodeString; const aPort : INTERNET_PORT = INTERNET_DEFAULT_PORT) : IWinHTTPConnection;
565 function SetTimeouts( const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32) : Boolean;
Jens Geyer47f63172019-06-06 22:42:58 +0200566 function EnableSecureProtocols( const aFlagSet : DWORD) : Boolean;
Jens Geyer02230912019-04-03 01:12:51 +0200567 public
568 constructor Create( const aAgent : UnicodeString;
569 const aAccessType : DWORD = WINHTTP_ACCESS_TYPE_DEFAULT_PROXY;
570 const aProxy : UnicodeString = '';
571 const aProxyBypass : UnicodeString = '';
572 const aFlags : DWORD = 0);
573 destructor Destroy; override;
574 end;
575
576
577 TWinHTTPConnectionImpl = class( TWinHTTPHandleObjectImpl, IWinHTTPConnection)
578 strict protected
579 FSession : IWinHTTPSession;
580
581 // IWinHTTPConnection
582 function OpenRequest( const secure : Boolean; const aVerb, aObjName, aAcceptTypes : UnicodeString) : IWinHTTPRequest;
Jens Geyer83ff7532019-06-06 22:46:03 +0200583 function Session : IWinHTTPSession;
Jens Geyer02230912019-04-03 01:12:51 +0200584
585 public
586 constructor Create( const aSession : IWinHTTPSession; const aHostName : UnicodeString; const aPort : INTERNET_PORT);
587 destructor Destroy; override;
588 end;
589
590
591 TAcceptTypesArray = array of string;
592
593 TWinHTTPRequestImpl = class( TWinHTTPHandleObjectImpl, IWinHTTPRequest)
594 strict protected
595 FConnection : IWinHTTPConnection;
596
597 // IWinHTTPRequest
Jens Geyer83ff7532019-06-06 22:46:03 +0200598 function Connection : IWinHTTPConnection;
Jens Geyer02230912019-04-03 01:12:51 +0200599 function AddRequestHeader( const aHeader : string; const addflag : DWORD = WINHTTP_ADDREQ_FLAG_ADD) : Boolean;
600 function SetTimeouts( const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32) : Boolean;
Jens Geyer83ff7532019-06-06 22:46:03 +0200601 procedure TryAutoProxy( const aUrl : string);
Jens Geyer02230912019-04-03 01:12:51 +0200602 function SendRequest( const pBuf : Pointer; const dwBytes : DWORD; const dwExtra : DWORD = 0) : Boolean;
603 function WriteExtraData( const pBuf : Pointer; const dwBytes : DWORD) : DWORD;
604 function FlushAndReceiveResponse : Boolean;
605 function ReadData( const dwRead : DWORD) : TBytes; overload;
606 function ReadData( const pBuf : Pointer; const dwRead : DWORD) : DWORD; overload;
607
608 public
609 constructor Create( const aConnection : IWinHTTPConnection;
610 const aVerb, aObjName : UnicodeString;
611 const aVersion : UnicodeString = '';
612 const aReferrer : UnicodeString = '';
613 const aAcceptTypes : UnicodeString = '*/*';
614 const aFlags : DWORD = WINHTTP_THRIFT_DEFAULTS
615 );
616
617 destructor Destroy; override;
618 end;
619
620
621 TWinHTTPUrlImpl = class( TInterfacedObject, IWinHTTPUrl)
622 strict private
623 FScheme : UnicodeString;
624 FNumScheme : INTERNET_SCHEME;
625 FHostName : UnicodeString;
626 FPort : INTERNET_PORT;
627 FUserName : UnicodeString;
628 FPassword : UnicodeString;
629 FUrlPath : UnicodeString;
630 FExtraInfo : UnicodeString;
631
632 strict protected
633 // url parts getter
634 function GetScheme : UnicodeString;
635 function GetNumScheme : INTERNET_SCHEME;
636 function GetHostName : UnicodeString;
637 function GetPort : INTERNET_PORT;
638 function GetUserName : UnicodeString;
639 function GetPassword : UnicodeString;
640 function GetUrlPath : UnicodeString;
641 function GetExtraInfo : UnicodeString;
642
643 // url parts setter
644 procedure SetScheme( const value : UnicodeString);
645 procedure SetHostName ( const value : UnicodeString);
646 procedure SetPort( const value : INTERNET_PORT);
647 procedure SetUserName( const value : UnicodeString);
648 procedure SetPassword( const value : UnicodeString);
649 procedure SetUrlPath( const value : UnicodeString);
650 procedure SetExtraInfo( const value : UnicodeString);
651
652 // url as a whole
653 function BuildUrl : UnicodeString;
654 procedure CrackUrl( const value : UnicodeString);
655
656 public
657 constructor Create( const aUri : UnicodeString);
658 destructor Destroy; override;
659 end;
660
661
Jens Geyer83ff7532019-06-06 22:46:03 +0200662 WINHTTP_PROXY_INFO_Helper = record helper for WINHTTP_PROXY_INFO
663 procedure Initialize;
664 procedure FreeAllocatedResources;
665 end;
666
667
668 WINHTTP_CURRENT_USER_IE_PROXY_CONFIG_Helper = record helper for WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
669 procedure Initialize;
670 procedure FreeAllocatedResources;
671 end;
672
673
Jens Geyer02230912019-04-03 01:12:51 +0200674 EWinHTTPException = class(Exception);
675
676implementation
677
678const WINHTTP_DLL = 'WinHTTP.dll';
679
680function WinHttpCloseHandle; stdcall; external WINHTTP_DLL;
681function WinHttpOpen; stdcall; external WINHTTP_DLL;
682function WinHttpConnect; stdcall; external WINHTTP_DLL;
683function WinHttpOpenRequest; stdcall; external WINHTTP_DLL;
684function WinHttpSendRequest; stdcall; external WINHTTP_DLL;
685function WinHttpSetTimeouts; stdcall; external WINHTTP_DLL;
Jens Geyer47f63172019-06-06 22:42:58 +0200686function WinHttpQueryOption; stdcall; external WINHTTP_DLL;
687function WinHttpSetOption; stdcall; external WINHTTP_DLL;
Jens Geyer02230912019-04-03 01:12:51 +0200688function WinHttpAddRequestHeaders; stdcall; external WINHTTP_DLL;
Jens Geyer83ff7532019-06-06 22:46:03 +0200689function WinHttpGetProxyForUrl; stdcall; external WINHTTP_DLL;
690function WinHttpGetIEProxyConfigForCurrentUser; stdcall; external WINHTTP_DLL;
Jens Geyer02230912019-04-03 01:12:51 +0200691function WinHttpWriteData; stdcall; external WINHTTP_DLL;
692function WinHttpReceiveResponse; stdcall; external WINHTTP_DLL;
693function WinHttpQueryHeaders; stdcall; external WINHTTP_DLL;
694function WinHttpQueryDataAvailable; stdcall; external WINHTTP_DLL;
695function WinHttpReadData; stdcall; external WINHTTP_DLL;
696function WinHttpCrackUrl; stdcall; external WINHTTP_DLL;
697function WinHttpCreateUrl; stdcall; external WINHTTP_DLL;
698
699
Jens Geyer83ff7532019-06-06 22:46:03 +0200700{ misc. record helper }
701
702
703procedure GlobalFreeAndNil( var p : LPWSTR);
704begin
705 if p <> nil then begin
706 GlobalFree( HGLOBAL( p));
707 p := nil;
708 end;
709end;
710
711
712procedure WINHTTP_PROXY_INFO_Helper.Initialize;
713begin
714 FillChar( Self, SizeOf(Self), 0);
715end;
716
717
718procedure WINHTTP_PROXY_INFO_Helper.FreeAllocatedResources;
719// The caller must free the lpszProxy and lpszProxyBypass strings
720// if they are non-NULL. Use GlobalFree to free the strings.
721begin
722 GlobalFreeAndNil( lpszProxy);
723 GlobalFreeAndNil( lpszProxyBypass);
724 Initialize;
725end;
726
727
728procedure WINHTTP_CURRENT_USER_IE_PROXY_CONFIG_Helper.Initialize;
729begin
730 FillChar( Self, SizeOf(Self), 0);
731end;
732
733
734procedure WINHTTP_CURRENT_USER_IE_PROXY_CONFIG_Helper.FreeAllocatedResources;
735// The caller must free the lpszProxy, lpszProxyBypass and lpszAutoConfigUrl strings
736// if they are non-NULL. Use GlobalFree to free the strings.
737begin
738 GlobalFreeAndNil( lpszProxy);
739 GlobalFreeAndNil( lpszProxyBypass);
740 GlobalFreeAndNil( lpszAutoConfigUrl);
741 Initialize;
742end;
743
744
Jens Geyer02230912019-04-03 01:12:51 +0200745{ TWinHTTPHandleObjectImpl }
746
747constructor TWinHTTPHandleObjectImpl.Create( const aHandle : HINTERNET);
748begin
749 inherited Create;
750 FHandle := aHandle;
751
752 if FHandle = nil
753 then raise EWinHTTPException.Create('Invalid handle');
754end;
755
756
757destructor TWinHTTPHandleObjectImpl.Destroy;
758begin
759 try
760 if Assigned(FHandle) then begin
761 WinHttpCloseHandle(FHandle);
762 FHandle := nil;
763 end;
764
765 finally
766 inherited Destroy;
767 end;
768end;
769
770
771function TWinHTTPHandleObjectImpl.Handle : HINTERNET;
772begin
773 result := FHandle;
774end;
775
776
777{ TWinHTTPSessionImpl }
778
779
780constructor TWinHTTPSessionImpl.Create( const aAgent : UnicodeString; const aAccessType : DWORD;
781 const aProxy, aProxyBypass : UnicodeString; const aFlags : DWORD);
782var handle : HINTERNET;
783begin
784 handle := WinHttpOpen( PWideChar(aAgent), aAccessType,
785 PWideChar(Pointer(aProxy)), // may be nil
786 PWideChar(Pointer(aProxyBypass)), // may be nil
787 aFlags);
Jens Geyer14a9a122019-05-09 23:29:24 +0200788 if handle = nil then RaiseLastOSError;
Jens Geyer02230912019-04-03 01:12:51 +0200789 inherited Create( handle);
790end;
791
792
793destructor TWinHTTPSessionImpl.Destroy;
794begin
795 inherited Destroy;
796 // add code here
797end;
798
799
800function TWinHTTPSessionImpl.Connect( const aHostName : UnicodeString; const aPort : INTERNET_PORT) : IWinHTTPConnection;
801begin
802 result := TWinHTTPConnectionImpl.Create( Self, aHostName, aPort);
803end;
804
805
806function TWinHTTPSessionImpl.SetTimeouts( const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32) : Boolean;
807begin
808 result := WinHttpSetTimeouts( FHandle, aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout);
809end;
810
811
Jens Geyer47f63172019-06-06 22:42:58 +0200812function TWinHTTPSessionImpl.EnableSecureProtocols( const aFlagSet : DWORD) : Boolean;
813var dwSize : DWORD;
814begin
815 dwSize := SizeOf(aFlagSet);
816 result := WinHttpSetOption( Handle, WINHTTP_OPTION_SECURE_PROTOCOLS, @aFlagset, dwSize);
817end;
818
819
Jens Geyer02230912019-04-03 01:12:51 +0200820{ TWinHTTPConnectionImpl }
821
822constructor TWinHTTPConnectionImpl.Create( const aSession : IWinHTTPSession; const aHostName : UnicodeString; const aPort : INTERNET_PORT);
823var handle : HINTERNET;
824begin
825 FSession := aSession;
826 handle := WinHttpConnect( FSession.Handle, PWideChar(aHostName), aPort, 0);
Jens Geyer14a9a122019-05-09 23:29:24 +0200827 if handle = nil then RaiseLastOSError;
Jens Geyer02230912019-04-03 01:12:51 +0200828 inherited Create( handle);
829end;
830
831
832destructor TWinHTTPConnectionImpl.Destroy;
833begin
834 inherited Destroy;
835 FSession := nil;
836end;
837
838
Jens Geyer83ff7532019-06-06 22:46:03 +0200839function TWinHTTPConnectionImpl.Session : IWinHTTPSession;
840begin
841 result := FSession;
842end;
843
844
Jens Geyer02230912019-04-03 01:12:51 +0200845function TWinHTTPConnectionImpl.OpenRequest( const secure : Boolean; const aVerb, aObjName, aAcceptTypes : UnicodeString) : IWinHTTPRequest;
846var dwFlags : DWORD;
847begin
848 dwFlags := WINHTTP_THRIFT_DEFAULTS;
849 if secure
850 then dwFlags := dwFlags or WINHTTP_FLAG_SECURE
851 else dwFlags := dwFlags and not WINHTTP_FLAG_SECURE;
852
853 result := TWinHTTPRequestImpl.Create( Self, aVerb, aObjName, '', '', aAcceptTypes, dwFlags);
854end;
855
856
857{ TWinHTTPRequestImpl }
858
859constructor TWinHTTPRequestImpl.Create( const aConnection : IWinHTTPConnection;
860 const aVerb, aObjName, aVersion, aReferrer : UnicodeString;
861 const aAcceptTypes : UnicodeString;
862 const aFlags : DWORD
863 );
864var handle : HINTERNET;
865 accept : array[0..1] of PWideChar;
866begin
867 FConnection := aConnection;
868
869 accept[0] := PWideChar(aAcceptTypes);
870 accept[1] := nil;
871
872 handle := WinHttpOpenRequest( FConnection.Handle,
873 PWideChar(UpperCase(aVerb)),
874 PWideChar(aObjName),
875 PWideChar(aVersion),
876 PWideChar(aReferrer),
877 @accept,
878 aFlags);
Jens Geyer14a9a122019-05-09 23:29:24 +0200879 if handle = nil then RaiseLastOSError;
Jens Geyer02230912019-04-03 01:12:51 +0200880 inherited Create( handle);
881end;
882
883
884destructor TWinHTTPRequestImpl.Destroy;
885begin
886 inherited Destroy;
887 FConnection := nil;
888end;
889
890
Jens Geyer83ff7532019-06-06 22:46:03 +0200891function TWinHTTPRequestImpl.Connection : IWinHTTPConnection;
892begin
893 result := FConnection;
894end;
895
896
Jens Geyer02230912019-04-03 01:12:51 +0200897function TWinHTTPRequestImpl.SetTimeouts( const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32) : Boolean;
898begin
899 result := WinHttpSetTimeouts( FHandle, aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout);
900end;
901
902
903function TWinHTTPRequestImpl.AddRequestHeader( const aHeader : string; const addflag : DWORD) : Boolean;
904begin
905 result := WinHttpAddRequestHeaders( FHandle, PWideChar(aHeader), DWORD(-1), addflag);
906end;
907
908
Jens Geyer83ff7532019-06-06 22:46:03 +0200909procedure TWinHTTPRequestImpl.TryAutoProxy( const aUrl : string);
910// From MSDN:
911// AutoProxy support is not fully integrated into the HTTP stack in WinHTTP.
912// Before sending a request, the application must call WinHttpGetProxyForUrl
913// to obtain the name of a proxy server and then call WinHttpSetOption using
914// WINHTTP_OPTION_PROXY to set the proxy configuration on the WinHTTP request
915// handle created by WinHttpOpenRequest.
916// See https://docs.microsoft.com/en-us/windows/desktop/winhttp/winhttp-autoproxy-api
917var
918 options : WINHTTP_AUTOPROXY_OPTIONS;
919 proxy : WINHTTP_PROXY_INFO;
920 ieProxy : WINHTTP_CURRENT_USER_IE_PROXY_CONFIG;
921 dwSize : DWORD;
922begin
923 // try AutoProxy via PAC first
924 proxy.Initialize;
925 try
926 FillChar( options, SizeOf(options), 0);
927 options.dwFlags := WINHTTP_AUTOPROXY_AUTO_DETECT;
928 options.dwAutoDetectFlags := WINHTTP_AUTO_DETECT_TYPE_DHCP or WINHTTP_AUTO_DETECT_TYPE_DNS_A;
929 options.fAutoLogonIfChallenged := TRUE;
930 if WinHttpGetProxyForUrl( FConnection.Session.Handle, PChar(aUrl), options, proxy) then begin
931 dwSize := SizeOf(proxy);
932 WinHttpSetOption( Handle, WINHTTP_OPTION_PROXY, @proxy, dwSize);
933 Exit;
934 end;
935
936 finally
937 proxy.FreeAllocatedResources;
938 end;
939
940 // Use IE settings as a fallback, useful in client (i.e. non-server) environments
941 ieProxy.Initialize;
942 try
943 if WinHttpGetIEProxyConfigForCurrentUser( ieProxy)
944 then begin
945
946 // lpszAutoConfigUrl = "Use automatic proxy configuration"
947 if ieProxy.lpszAutoConfigUrl <> nil then begin
948 options.lpszAutoConfigUrl := ieProxy.lpszAutoConfigUrl;
949 options.dwFlags := options.dwFlags or WINHTTP_AUTOPROXY_CONFIG_URL;
950
951 proxy.Initialize;
952 try
953 if WinHttpGetProxyForUrl( FConnection.Session.Handle, PChar(aUrl), options, proxy) then begin
954 dwSize := SizeOf(proxy);
955 WinHttpSetOption( Handle, WINHTTP_OPTION_PROXY, @proxy, dwSize);
956 Exit;
957 end;
958 finally
959 proxy.FreeAllocatedResources;
960 end;
961 end;
962
963 // lpszProxy = "use a proxy server"
964 if ieProxy.lpszProxy <> nil then begin
965 proxy.Initialize;
966 try
967 proxy.dwAccessType := WINHTTP_ACCESS_TYPE_NAMED_PROXY;
968 proxy.lpszProxy := ieProxy.lpszProxy;
969 proxy.lpszProxyBypass := ieProxy.lpszProxyBypass;
970 dwSize := SizeOf(proxy);
971 WinHttpSetOption( Handle, WINHTTP_OPTION_PROXY, @proxy, dwSize);
972 Exit;
973 finally
974 proxy.Initialize; // not FreeAllocatedResources, we only hold pointer copies!
975 end;
976 end;
977
978 end;
979
980 finally
981 ieProxy.FreeAllocatedResources;
982 end;
983end;
984
985
986
987
Jens Geyer02230912019-04-03 01:12:51 +0200988function TWinHTTPRequestImpl.SendRequest( const pBuf : Pointer; const dwBytes, dwExtra : DWORD) : Boolean;
989begin
990 result := WinHttpSendRequest( FHandle,
991 WINHTTP_NO_ADDITIONAL_HEADERS, 0,
992 pBuf, dwBytes, // number of bytes in pBuf
993 dwBytes + dwExtra, // becomes the Content-Length
994 nil); // context for async operations
995end;
996
997
998function TWinHTTPRequestImpl.WriteExtraData( const pBuf : Pointer; const dwBytes : DWORD) : DWORD;
999begin
1000 if not WinHttpWriteData( FHandle, pBuf, dwBytes, result)
1001 then result := 0;
1002end;
1003
1004
1005function TWinHTTPRequestImpl.FlushAndReceiveResponse : Boolean;
1006begin
1007 result := WinHttpReceiveResponse( FHandle, nil);
1008end;
1009
1010
1011function TWinHTTPRequestImpl.ReadData( const dwRead : DWORD) : TBytes;
1012var dwAvailable, dwReceived : DWORD;
1013begin
1014 if WinHttpQueryDataAvailable( FHandle, dwAvailable)
1015 then dwAvailable := Min( dwRead, dwAvailable)
1016 else dwAvailable := 0;
1017
1018 SetLength( result, dwAvailable);
1019 if dwAvailable = 0 then Exit;
1020
1021 if WinHttpReadData( FHandle, @result[0], Length(result), dwReceived)
1022 then SetLength( result, dwReceived)
1023 else SetLength( result, 0);
1024end;
1025
1026
1027function TWinHTTPRequestImpl.ReadData( const pBuf : Pointer; const dwRead : DWORD) : DWORD;
1028var dwAvailable : DWORD;
1029begin
1030 if WinHttpQueryDataAvailable( FHandle, dwAvailable)
1031 then dwAvailable := Min( dwRead, dwAvailable)
1032 else dwAvailable := 0;
1033
1034 if (dwAvailable = 0)
1035 or not WinHttpReadData( FHandle, pBuf, dwAvailable, result)
1036 then result := 0;
1037end;
1038
1039
1040{ TWinHTTPUrlImpl }
1041
1042constructor TWinHTTPUrlImpl.Create(const aUri: UnicodeString);
1043begin
1044 inherited Create;
1045 CrackUrl( aUri)
1046end;
1047
1048
1049destructor TWinHTTPUrlImpl.Destroy;
1050begin
1051 inherited Destroy;
1052end;
1053
1054
1055procedure TWinHTTPUrlImpl.CrackURL( const value : UnicodeString);
1056const FLAGS = 0; // no special operations, leave components as-is
1057var components : URL_COMPONENTS;
1058begin
1059 FillChar(components, SizeOf(components), 0);
1060 components.dwStructSize := SizeOf(components);
1061
1062 if value <> '' then begin
1063 { For the WinHttpCrackUrl function, [...] if the pointer member is NULL but the
1064 length member is not zero, both the pointer and length members are returned. }
1065 components.dwSchemeLength := DWORD(-1);
1066 components.dwHostNameLength := DWORD(-1);
1067 components.dwUserNameLength := DWORD(-1);
1068 components.dwPasswordLength := DWORD(-1);
1069 components.dwUrlPathLength := DWORD(-1);
1070 components.dwExtraInfoLength := DWORD(-1);
1071
1072 WinHttpCrackUrl( PWideChar(value), Length(value), FLAGS, components);
1073 end;
1074
1075 FNumScheme := components.nScheme;
1076 FPort := components.nPort;
1077 SetString( FScheme, components.lpszScheme, components.dwSchemeLength);
1078 SetString( FHostName, components.lpszHostName, components.dwHostNameLength);
1079 SetString( FUserName, components.lpszUserName, components.dwUserNameLength);
1080 SetString( FPassword, components.lpszPassword, components.dwPasswordLength);
1081 SetString( FUrlPath, components.lpszUrlPath, components.dwUrlPathLength);
1082 SetString( FExtraInfo, components.lpszExtraInfo, components.dwExtraInfoLength);
1083end;
1084
1085
1086function TWinHTTPUrlImpl.BuildUrl : UnicodeString;
1087const FLAGS = 0; // no special operations, leave components as-is
1088var components : URL_COMPONENTS;
1089 dwChars : DWORD;
1090begin
1091 FillChar(components, SizeOf(components), 0);
1092 components.dwStructSize := SizeOf(components);
1093 components.lpszScheme := PWideChar(FScheme);
1094 components.dwSchemeLength := Length(FScheme);
1095 components.lpszHostName := PWideChar(FHostName);
1096 components.dwHostNameLength := Length(FHostName);
1097 components.nPort := FPort;
1098 components.lpszUserName := PWideChar(FUserName);
1099 components.dwUserNameLength := Length(FUserName);
1100 components.lpszPassword := PWideChar(FPassword);
1101 components.dwPasswordLength := Length(FPassword);
1102 components.lpszUrlPath := PWideChar(FUrlPath);
1103 components.dwUrlPathLength := Length(FUrlPath);
1104 components.lpszExtraInfo := PWideChar(FExtraInfo);
1105 components.dwExtraInfoLength := Length(FExtraInfo);
1106
1107 WinHttpCreateUrl( components, FLAGS, nil, dwChars);
1108 if dwChars = 0
1109 then result := ''
1110 else begin
1111 SetLength( result, dwChars + 1);
1112 WinHttpCreateUrl( components, FLAGS, @result[1], dwChars);
1113 SetLength( result, dwChars); // cut off terminating #0
1114 end;
1115end;
1116
1117
1118function TWinHTTPUrlImpl.GetExtraInfo: UnicodeString;
1119begin
1120 result := FExtraInfo;
1121end;
1122
1123function TWinHTTPUrlImpl.GetHostName: UnicodeString;
1124begin
1125 result := FHostName;
1126end;
1127
1128function TWinHTTPUrlImpl.GetNumScheme: INTERNET_SCHEME;
1129begin
1130 result := FNumScheme;
1131end;
1132
1133function TWinHTTPUrlImpl.GetPassword: UnicodeString;
1134begin
1135 result := FPassword;
1136end;
1137
1138function TWinHTTPUrlImpl.GetPort: INTERNET_PORT;
1139begin
1140 result := FPort;
1141end;
1142
1143function TWinHTTPUrlImpl.GetScheme: UnicodeString;
1144begin
1145 result := FScheme;
1146end;
1147
1148function TWinHTTPUrlImpl.GetUrlPath: UnicodeString;
1149begin
1150 result := FUrlPath;
1151end;
1152
1153function TWinHTTPUrlImpl.GetUserName: UnicodeString;
1154begin
1155 result := FUserName;
1156end;
1157
1158procedure TWinHTTPUrlImpl.SetExtraInfo(const value: UnicodeString);
1159begin
1160 FExtraInfo := value;
1161end;
1162
1163procedure TWinHTTPUrlImpl.SetHostName(const value: UnicodeString);
1164begin
1165 FHostName := value;
1166end;
1167
1168procedure TWinHTTPUrlImpl.SetPassword(const value: UnicodeString);
1169begin
1170 FPassword := value;
1171end;
1172
1173procedure TWinHTTPUrlImpl.SetPort(const value: INTERNET_PORT);
1174begin
1175 FPort := value;
1176end;
1177
1178procedure TWinHTTPUrlImpl.SetScheme(const value: UnicodeString);
1179begin
1180 FScheme := value;
1181end;
1182
1183procedure TWinHTTPUrlImpl.SetUrlPath(const value: UnicodeString);
1184begin
1185 FUrlPath := value;
1186end;
1187
1188procedure TWinHTTPUrlImpl.SetUserName(const value: UnicodeString);
1189begin
1190 FUserName := value;
1191end;
1192
1193
1194end.
1195
Jens Geyer83ff7532019-06-06 22:46:03 +02001196