Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 1 | (* |
| 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 | *) |
| 19 | unit 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 | |
| 27 | interface |
| 28 | |
| 29 | uses |
| 30 | Windows, |
| 31 | Classes, |
| 32 | SysUtils, |
| 33 | Math, |
| 34 | Generics.Collections; |
| 35 | |
| 36 | |
| 37 | type |
| 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 Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 66 | // 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 Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 100 | function WinHttpCloseHandle( aHandle : HINTERNET) : BOOL; stdcall; |
| 101 | |
| 102 | function WinHttpOpen( const pszAgentW : LPCWSTR; |
| 103 | const dwAccessType : DWORD; |
| 104 | const pszProxyW : LPCWSTR; |
| 105 | const pszProxyBypassW : LPCWSTR; |
| 106 | const dwFlags : DWORD |
| 107 | ) : HINTERNET; stdcall; |
| 108 | |
| 109 | function WinHttpConnect( const hSession : HINTERNET; |
| 110 | const pswzServerName : LPCWSTR; |
| 111 | const nServerPort : INTERNET_PORT; |
| 112 | const dwReserved : DWORD |
| 113 | ) : HINTERNET; stdcall; |
| 114 | |
| 115 | function WinHttpOpenRequest( const hConnect : HINTERNET; |
| 116 | const pwszVerb, pwszObjectName, pwszVersion, pwszReferrer : LPCWSTR; |
| 117 | const ppwszAcceptTypes : LPLPCWSTR; |
| 118 | const dwFlags : DWORD |
| 119 | ) : HINTERNET; stdcall; |
| 120 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 121 | function WinHttpQueryOption( const hInternet : HINTERNET; |
| 122 | const dwOption : DWORD; |
| 123 | const pBuffer : Pointer; |
| 124 | var dwBufferLength : DWORD) : BOOL; stdcall; |
| 125 | |
| 126 | function WinHttpSetOption( const hInternet : HINTERNET; |
| 127 | const dwOption : DWORD; |
| 128 | const pBuffer : Pointer; |
| 129 | const dwBufferLength : DWORD) : BOOL; stdcall; |
| 130 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 131 | function WinHttpSetTimeouts( const hRequestOrSession : HINTERNET; |
| 132 | const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32 |
| 133 | ) : BOOL; stdcall; |
| 134 | |
| 135 | function WinHttpAddRequestHeaders( const hRequest : HINTERNET; |
| 136 | const pwszHeaders : LPCWSTR; |
| 137 | const dwHeadersLengthInChars : DWORD; |
| 138 | const dwModifiers : DWORD |
| 139 | ) : BOOL; stdcall; |
| 140 | |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 141 | function WinHttpGetProxyForUrl( const hSession : HINTERNET; |
| 142 | const lpcwszUrl : LPCWSTR; |
| 143 | const options : WINHTTP_AUTOPROXY_OPTIONS; |
| 144 | const info : WINHTTP_PROXY_INFO |
| 145 | ) : BOOL; stdcall; |
| 146 | |
| 147 | function WinHttpGetIEProxyConfigForCurrentUser( var config : WINHTTP_CURRENT_USER_IE_PROXY_CONFIG |
| 148 | ) : BOOL; stdcall; |
| 149 | |
| 150 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 151 | function 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 | |
| 160 | function WinHttpWriteData( const hRequest : HINTERNET; |
| 161 | const pBuf : Pointer; |
| 162 | const dwBytesToWrite : DWORD; |
| 163 | out dwBytesWritten : DWORD |
| 164 | ) : BOOL; stdcall; |
| 165 | |
| 166 | function WinHttpReceiveResponse( const hRequest : HINTERNET; const lpReserved : Pointer) : BOOL; stdcall; |
| 167 | |
| 168 | function 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 | |
| 176 | function WinHttpQueryDataAvailable( const hRequest : HINTERNET; |
| 177 | var dwNumberOfBytesAvailable : DWORD |
| 178 | ) : BOOL; stdcall; |
| 179 | |
| 180 | function WinHttpReadData( const hRequest : HINTERNET; |
| 181 | const lpBuffer : Pointer; |
| 182 | const dwBytesToRead : DWORD; |
| 183 | out dwBytesRead : DWORD |
| 184 | ) : BOOL; stdcall; |
| 185 | |
| 186 | function WinHttpCrackUrl( const pwszUrl : LPCWSTR; |
| 187 | const dwUrlLength : DWORD; |
| 188 | const dwFlags : DWORD; |
| 189 | var urlComponents : URL_COMPONENTS |
| 190 | ) : BOOL; stdcall; |
| 191 | |
| 192 | function WinHttpCreateUrl( const UrlComponents : URL_COMPONENTS; |
| 193 | const dwFlags : DWORD; |
| 194 | const pwszUrl : LPCWSTR; |
| 195 | var pdwUrlLength : DWORD |
| 196 | ) : BOOL; stdcall; |
| 197 | |
| 198 | |
| 199 | const |
| 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 | |
Jens Geyer | 528a0f0 | 2019-11-18 20:17:03 +0100 | [diff] [blame] | 208 | WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH = 0; |
| 209 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 210 | // ports |
| 211 | INTERNET_DEFAULT_PORT = 0; // use the protocol-specific default (80 or 443) |
| 212 | |
| 213 | // flags for WinHttpOpenRequest(): |
| 214 | WINHTTP_FLAG_SECURE = $00800000; // use SSL if applicable (HTTPS) |
| 215 | WINHTTP_FLAG_ESCAPE_PERCENT = $00000004; // if escaping enabled, escape percent as well |
| 216 | WINHTTP_FLAG_NULL_CODEPAGE = $00000008; // assume all symbols are ASCII, use fast convertion |
| 217 | WINHTTP_FLAG_BYPASS_PROXY_CACHE = $00000100; // add "pragma: no-cache" request header |
| 218 | WINHTTP_FLAG_REFRESH = WINHTTP_FLAG_BYPASS_PROXY_CACHE; |
| 219 | WINHTTP_FLAG_ESCAPE_DISABLE = $00000040; // disable escaping |
| 220 | WINHTTP_FLAG_ESCAPE_DISABLE_QUERY = $00000080; // if escaping enabled escape path part, but do not escape query |
| 221 | |
| 222 | // flags for WinHttpSendRequest(): |
Jens Geyer | 528a0f0 | 2019-11-18 20:17:03 +0100 | [diff] [blame] | 223 | WINHTTP_NO_PROXY_NAME = nil; |
| 224 | WINHTTP_NO_PROXY_BYPASS = nil; |
| 225 | WINHTTP_NO_CLIENT_CERT_CONTEXT = nil; |
| 226 | WINHTTP_NO_REFERER = nil; |
| 227 | WINHTTP_DEFAULT_ACCEPT_TYPES = nil; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 228 | WINHTTP_NO_ADDITIONAL_HEADERS = nil; |
| 229 | WINHTTP_NO_REQUEST_DATA = nil; |
Jens Geyer | 528a0f0 | 2019-11-18 20:17:03 +0100 | [diff] [blame] | 230 | WINHTTP_HEADER_NAME_BY_INDEX = nil; |
| 231 | WINHTTP_NO_OUTPUT_BUFFER = nil; |
| 232 | WINHTTP_NO_HEADER_INDEX = nil; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 233 | |
| 234 | // WinHttpAddRequestHeaders() dwModifiers |
| 235 | WINHTTP_ADDREQ_INDEX_MASK = $0000FFFF; |
| 236 | WINHTTP_ADDREQ_FLAGS_MASK = $FFFF0000; |
| 237 | |
| 238 | WINHTTP_ADDREQ_FLAG_ADD_IF_NEW = $10000000; |
| 239 | WINHTTP_ADDREQ_FLAG_ADD = $20000000; |
| 240 | WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA = $40000000; |
| 241 | WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON = $01000000; |
| 242 | WINHTTP_ADDREQ_FLAG_COALESCE = WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA; |
| 243 | WINHTTP_ADDREQ_FLAG_REPLACE = $80000000; |
| 244 | |
| 245 | // URL functions |
| 246 | ICU_NO_ENCODE = $20000000; // Don't convert unsafe characters to escape sequence |
| 247 | ICU_DECODE = $10000000; // Convert %XX escape sequences to characters |
| 248 | ICU_NO_META = $08000000; // Don't convert .. etc. meta path sequences |
| 249 | ICU_ENCODE_SPACES_ONLY = $04000000; // Encode spaces only |
| 250 | ICU_BROWSER_MODE = $02000000; // Special encode/decode rules for browser |
| 251 | ICU_ENCODE_PERCENT = $00001000; // Encode any percent (ASCII25) |
| 252 | |
| 253 | ICU_ESCAPE = $80000000; // (un)escape URL characters |
| 254 | ICU_ESCAPE_AUTHORITY = $00002000; // causes InternetCreateUrlA to escape chars in authority components (user, pwd, host) |
| 255 | ICU_REJECT_USERPWD = $00004000; // rejects usrls whick have username/pwd sections |
| 256 | |
| 257 | INTERNET_SCHEME_HTTP = INTERNET_SCHEME(1); |
| 258 | INTERNET_SCHEME_HTTPS = INTERNET_SCHEME(2); |
| 259 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 260 | // options manifests for WinHttp{Query|Set}Option |
| 261 | WINHTTP_OPTION_CALLBACK = 1; |
| 262 | WINHTTP_OPTION_RESOLVE_TIMEOUT = 2; |
| 263 | WINHTTP_OPTION_CONNECT_TIMEOUT = 3; |
| 264 | WINHTTP_OPTION_CONNECT_RETRIES = 4; |
| 265 | WINHTTP_OPTION_SEND_TIMEOUT = 5; |
| 266 | WINHTTP_OPTION_RECEIVE_TIMEOUT = 6; |
| 267 | WINHTTP_OPTION_RECEIVE_RESPONSE_TIMEOUT = 7; |
| 268 | WINHTTP_OPTION_HANDLE_TYPE = 9; |
| 269 | WINHTTP_OPTION_READ_BUFFER_SIZE = 12; |
| 270 | WINHTTP_OPTION_WRITE_BUFFER_SIZE = 13; |
| 271 | WINHTTP_OPTION_PARENT_HANDLE = 21; |
| 272 | WINHTTP_OPTION_EXTENDED_ERROR = 24; |
| 273 | WINHTTP_OPTION_SECURITY_FLAGS = 31; |
| 274 | WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT = 32; |
| 275 | WINHTTP_OPTION_URL = 34; |
| 276 | WINHTTP_OPTION_SECURITY_KEY_BITNESS = 36; |
| 277 | WINHTTP_OPTION_PROXY = 38; |
| 278 | WINHTTP_OPTION_USER_AGENT = 41; |
| 279 | WINHTTP_OPTION_CONTEXT_VALUE = 45; |
| 280 | WINHTTP_OPTION_CLIENT_CERT_CONTEXT = 47; |
| 281 | WINHTTP_OPTION_REQUEST_PRIORITY = 58; |
| 282 | WINHTTP_OPTION_HTTP_VERSION = 59; |
| 283 | WINHTTP_OPTION_DISABLE_FEATURE = 63; |
| 284 | WINHTTP_OPTION_CODEPAGE = 68; |
| 285 | WINHTTP_OPTION_MAX_CONNS_PER_SERVER = 73; |
| 286 | WINHTTP_OPTION_MAX_CONNS_PER_1_0_SERVER = 74; |
| 287 | WINHTTP_OPTION_AUTOLOGON_POLICY = 77; |
| 288 | WINHTTP_OPTION_SERVER_CERT_CONTEXT = 78; |
| 289 | WINHTTP_OPTION_ENABLE_FEATURE = 79; |
| 290 | WINHTTP_OPTION_WORKER_THREAD_COUNT = 80; |
| 291 | WINHTTP_OPTION_PASSPORT_COBRANDING_TEXT = 81; |
| 292 | WINHTTP_OPTION_PASSPORT_COBRANDING_URL = 82; |
| 293 | WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH = 83; |
| 294 | WINHTTP_OPTION_SECURE_PROTOCOLS = 84; |
| 295 | WINHTTP_OPTION_ENABLETRACING = 85; |
| 296 | WINHTTP_OPTION_PASSPORT_SIGN_OUT = 86; |
| 297 | WINHTTP_OPTION_PASSPORT_RETURN_URL = 87; |
| 298 | WINHTTP_OPTION_REDIRECT_POLICY = 88; |
| 299 | WINHTTP_OPTION_MAX_HTTP_AUTOMATIC_REDIRECTS = 89; |
| 300 | WINHTTP_OPTION_MAX_HTTP_STATUS_CONTINUE = 90; |
| 301 | WINHTTP_OPTION_MAX_RESPONSE_HEADER_SIZE = 91; |
| 302 | WINHTTP_OPTION_MAX_RESPONSE_DRAIN_SIZE = 92; |
| 303 | WINHTTP_OPTION_CONNECTION_INFO = 93; |
| 304 | WINHTTP_OPTION_CLIENT_CERT_ISSUER_LIST = 94; |
| 305 | WINHTTP_OPTION_SPN = 96; |
| 306 | WINHTTP_OPTION_GLOBAL_PROXY_CREDS = 97; |
| 307 | WINHTTP_OPTION_GLOBAL_SERVER_CREDS = 98; |
| 308 | WINHTTP_OPTION_UNLOAD_NOTIFY_EVENT = 99; |
| 309 | WINHTTP_OPTION_REJECT_USERPWD_IN_URL = 100; |
| 310 | WINHTTP_OPTION_USE_GLOBAL_SERVER_CREDENTIALS = 101; |
| 311 | WINHTTP_OPTION_RECEIVE_PROXY_CONNECT_RESPONSE = 103; |
| 312 | WINHTTP_OPTION_IS_PROXY_CONNECT_RESPONSE = 104; |
| 313 | WINHTTP_OPTION_SERVER_SPN_USED = 106; |
| 314 | WINHTTP_OPTION_PROXY_SPN_USED = 107; |
| 315 | WINHTTP_OPTION_SERVER_CBT = 108; |
Jens Geyer | 19505c3 | 2019-06-22 00:59:54 +0200 | [diff] [blame] | 316 | // options for newer WinHTTP versions |
| 317 | WINHTTP_OPTION_DECOMPRESSION = 118; |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 318 | // |
| 319 | WINHTTP_FIRST_OPTION = WINHTTP_OPTION_CALLBACK; |
Jens Geyer | 19505c3 | 2019-06-22 00:59:54 +0200 | [diff] [blame] | 320 | //WINHTTP_LAST_OPTION = WINHTTP_OPTION_SERVER_CBT; |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 321 | |
| 322 | WINHTTP_OPTION_USERNAME = $1000; |
| 323 | WINHTTP_OPTION_PASSWORD = $1001; |
| 324 | WINHTTP_OPTION_PROXY_USERNAME = $1002; |
| 325 | WINHTTP_OPTION_PROXY_PASSWORD = $1003; |
| 326 | |
| 327 | // manifest value for WINHTTP_OPTION_MAX_CONNS_PER_SERVER and WINHTTP_OPTION_MAX_CONNS_PER_1_0_SERVER |
| 328 | WINHTTP_CONNS_PER_SERVER_UNLIMITED = $FFFFFFFF; |
| 329 | |
| 330 | // values for WINHTTP_OPTION_AUTOLOGON_POLICY |
| 331 | WINHTTP_AUTOLOGON_SECURITY_LEVEL_MEDIUM = 0; |
| 332 | WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW = 1; |
| 333 | WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH = 2; |
| 334 | |
| 335 | WINHTTP_AUTOLOGON_SECURITY_LEVEL_DEFAULT = WINHTTP_AUTOLOGON_SECURITY_LEVEL_MEDIUM; |
| 336 | |
| 337 | // values for WINHTTP_OPTION_REDIRECT_POLICY |
| 338 | WINHTTP_OPTION_REDIRECT_POLICY_NEVER = 0; |
| 339 | WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP = 1; |
| 340 | WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS = 2; |
| 341 | |
| 342 | WINHTTP_OPTION_REDIRECT_POLICY_LAST = WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS; |
| 343 | WINHTTP_OPTION_REDIRECT_POLICY_DEFAULT = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP; |
| 344 | |
| 345 | WINHTTP_DISABLE_PASSPORT_AUTH = $00000000; |
| 346 | WINHTTP_ENABLE_PASSPORT_AUTH = $10000000; |
| 347 | WINHTTP_DISABLE_PASSPORT_KEYRING = $20000000; |
| 348 | WINHTTP_ENABLE_PASSPORT_KEYRING = $40000000; |
| 349 | |
| 350 | // values for WINHTTP_OPTION_DISABLE_FEATURE |
| 351 | WINHTTP_DISABLE_COOKIES = $00000001; |
| 352 | WINHTTP_DISABLE_REDIRECTS = $00000002; |
| 353 | WINHTTP_DISABLE_AUTHENTICATION = $00000004; |
| 354 | WINHTTP_DISABLE_KEEP_ALIVE = $00000008; |
| 355 | |
| 356 | // values for WINHTTP_OPTION_ENABLE_FEATURE |
| 357 | WINHTTP_ENABLE_SSL_REVOCATION = $00000001; |
| 358 | WINHTTP_ENABLE_SSL_REVERT_IMPERSONATION = $00000002; |
| 359 | |
| 360 | // values for WINHTTP_OPTION_SPN |
| 361 | WINHTTP_DISABLE_SPN_SERVER_PORT = $00000000; |
| 362 | WINHTTP_ENABLE_SPN_SERVER_PORT = $00000001; |
| 363 | WINHTTP_OPTION_SPN_MASK = WINHTTP_ENABLE_SPN_SERVER_PORT; |
| 364 | |
| 365 | // winhttp handle types |
| 366 | WINHTTP_HANDLE_TYPE_SESSION = 1; |
| 367 | WINHTTP_HANDLE_TYPE_CONNECT = 2; |
| 368 | WINHTTP_HANDLE_TYPE_REQUEST = 3; |
| 369 | |
| 370 | // values for auth schemes |
| 371 | WINHTTP_AUTH_SCHEME_BASIC = $00000001; |
| 372 | WINHTTP_AUTH_SCHEME_NTLM = $00000002; |
| 373 | WINHTTP_AUTH_SCHEME_PASSPORT = $00000004; |
| 374 | WINHTTP_AUTH_SCHEME_DIGEST = $00000008; |
| 375 | WINHTTP_AUTH_SCHEME_NEGOTIATE = $00000010; |
| 376 | |
| 377 | // WinHttp supported Authentication Targets |
| 378 | WINHTTP_AUTH_TARGET_SERVER = $00000000; |
| 379 | WINHTTP_AUTH_TARGET_PROXY = $00000001; |
| 380 | |
Jens Geyer | 19505c3 | 2019-06-22 00:59:54 +0200 | [diff] [blame] | 381 | // options for WINHTTP_OPTION_DECOMPRESSION |
| 382 | WINHTTP_DECOMPRESSION_FLAG_GZIP = $00000001; |
| 383 | WINHTTP_DECOMPRESSION_FLAG_DEFLATE = $00000002; |
| 384 | WINHTTP_DECOMPRESSION_FLAG_ALL = WINHTTP_DECOMPRESSION_FLAG_GZIP |
| 385 | or WINHTTP_DECOMPRESSION_FLAG_DEFLATE; |
| 386 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 387 | // values for WINHTTP_OPTION_SECURITY_FLAGS |
| 388 | |
| 389 | // query only |
| 390 | SECURITY_FLAG_SECURE = $00000001; // can query only |
| 391 | SECURITY_FLAG_STRENGTH_WEAK = $10000000; |
| 392 | SECURITY_FLAG_STRENGTH_MEDIUM = $40000000; |
| 393 | SECURITY_FLAG_STRENGTH_STRONG = $20000000; |
| 394 | |
Jens Geyer | 528a0f0 | 2019-11-18 20:17:03 +0100 | [diff] [blame] | 395 | // query flags |
| 396 | WINHTTP_QUERY_MIME_VERSION = 0; |
| 397 | WINHTTP_QUERY_CONTENT_TYPE = 1; |
| 398 | WINHTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2; |
| 399 | WINHTTP_QUERY_CONTENT_ID = 3; |
| 400 | WINHTTP_QUERY_CONTENT_DESCRIPTION = 4; |
| 401 | WINHTTP_QUERY_CONTENT_LENGTH = 5; |
| 402 | WINHTTP_QUERY_CONTENT_LANGUAGE = 6; |
| 403 | WINHTTP_QUERY_ALLOW = 7; |
| 404 | WINHTTP_QUERY_PUBLIC = 8; |
| 405 | WINHTTP_QUERY_DATE = 9; |
| 406 | WINHTTP_QUERY_EXPIRES = 10; |
| 407 | WINHTTP_QUERY_LAST_MODIFIED = 11; |
| 408 | WINHTTP_QUERY_MESSAGE_ID = 12; |
| 409 | WINHTTP_QUERY_URI = 13; |
| 410 | WINHTTP_QUERY_DERIVED_FROM = 14; |
| 411 | WINHTTP_QUERY_COST = 15; |
| 412 | WINHTTP_QUERY_LINK = 16; |
| 413 | WINHTTP_QUERY_PRAGMA = 17; |
| 414 | WINHTTP_QUERY_VERSION = 18; |
| 415 | WINHTTP_QUERY_STATUS_CODE = 19; |
| 416 | WINHTTP_QUERY_STATUS_TEXT = 20; |
| 417 | WINHTTP_QUERY_RAW_HEADERS = 21; |
| 418 | WINHTTP_QUERY_RAW_HEADERS_CRLF = 22; |
| 419 | WINHTTP_QUERY_CONNECTION = 23; |
| 420 | WINHTTP_QUERY_ACCEPT = 24; |
| 421 | WINHTTP_QUERY_ACCEPT_CHARSET = 25; |
| 422 | WINHTTP_QUERY_ACCEPT_ENCODING = 26; |
| 423 | WINHTTP_QUERY_ACCEPT_LANGUAGE = 27; |
| 424 | WINHTTP_QUERY_AUTHORIZATION = 28; |
| 425 | WINHTTP_QUERY_CONTENT_ENCODING = 29; |
| 426 | WINHTTP_QUERY_FORWARDED = 30; |
| 427 | WINHTTP_QUERY_FROM = 31; |
| 428 | WINHTTP_QUERY_IF_MODIFIED_SINCE = 32; |
| 429 | WINHTTP_QUERY_LOCATION = 33; |
| 430 | WINHTTP_QUERY_ORIG_URI = 34; |
| 431 | WINHTTP_QUERY_REFERER = 35; |
| 432 | WINHTTP_QUERY_RETRY_AFTER = 36; |
| 433 | WINHTTP_QUERY_SERVER = 37; |
| 434 | WINHTTP_QUERY_TITLE = 38; |
| 435 | WINHTTP_QUERY_USER_AGENT = 39; |
| 436 | WINHTTP_QUERY_WWW_AUTHENTICATE = 40; |
| 437 | WINHTTP_QUERY_PROXY_AUTHENTICATE = 41; |
| 438 | WINHTTP_QUERY_ACCEPT_RANGES = 42; |
| 439 | WINHTTP_QUERY_SET_COOKIE = 43; |
| 440 | WINHTTP_QUERY_COOKIE = 44; |
| 441 | WINHTTP_QUERY_REQUEST_METHOD = 45; |
| 442 | WINHTTP_QUERY_REFRESH = 46; |
| 443 | WINHTTP_QUERY_CONTENT_DISPOSITION = 47; |
| 444 | WINHTTP_QUERY_AGE = 48; |
| 445 | WINHTTP_QUERY_CACHE_CONTROL = 49; |
| 446 | WINHTTP_QUERY_CONTENT_BASE = 50; |
| 447 | WINHTTP_QUERY_CONTENT_LOCATION = 51; |
| 448 | WINHTTP_QUERY_CONTENT_MD5 = 52; |
| 449 | WINHTTP_QUERY_CONTENT_RANGE = 53; |
| 450 | WINHTTP_QUERY_ETAG = 54; |
| 451 | WINHTTP_QUERY_HOST = 55; |
| 452 | WINHTTP_QUERY_IF_MATCH = 56; |
| 453 | WINHTTP_QUERY_IF_NONE_MATCH = 57; |
| 454 | WINHTTP_QUERY_IF_RANGE = 58; |
| 455 | WINHTTP_QUERY_IF_UNMODIFIED_SINCE = 59; |
| 456 | WINHTTP_QUERY_MAX_FORWARDS = 60; |
| 457 | WINHTTP_QUERY_PROXY_AUTHORIZATION = 61; |
| 458 | WINHTTP_QUERY_RANGE = 62; |
| 459 | WINHTTP_QUERY_TRANSFER_ENCODING = 63; |
| 460 | WINHTTP_QUERY_UPGRADE = 64; |
| 461 | WINHTTP_QUERY_VARY = 65; |
| 462 | WINHTTP_QUERY_VIA = 66; |
| 463 | WINHTTP_QUERY_WARNING = 67; |
| 464 | WINHTTP_QUERY_EXPECT = 68; |
| 465 | WINHTTP_QUERY_PROXY_CONNECTION = 69; |
| 466 | WINHTTP_QUERY_UNLESS_MODIFIED_SINCE = 70; |
| 467 | WINHTTP_QUERY_PROXY_SUPPORT = 75; |
| 468 | WINHTTP_QUERY_AUTHENTICATION_INFO = 76; |
| 469 | WINHTTP_QUERY_PASSPORT_URLS = 77; |
| 470 | WINHTTP_QUERY_PASSPORT_CONFIG = 78; |
| 471 | WINHTTP_QUERY_MAX = 78; |
| 472 | WINHTTP_QUERY_CUSTOM = 65535; |
| 473 | WINHTTP_QUERY_FLAG_REQUEST_HEADERS = $80000000; |
| 474 | WINHTTP_QUERY_FLAG_SYSTEMTIME = $40000000; |
| 475 | WINHTTP_QUERY_FLAG_NUMBER = $20000000; |
| 476 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 477 | // Secure connection error status flags |
| 478 | WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED = $00000001; |
| 479 | WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CERT = $00000002; |
| 480 | WINHTTP_CALLBACK_STATUS_FLAG_CERT_REVOKED = $00000004; |
| 481 | WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA = $00000008; |
| 482 | WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID = $00000010; |
| 483 | WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID = $00000020; |
| 484 | WINHTTP_CALLBACK_STATUS_FLAG_CERT_WRONG_USAGE = $00000040; |
| 485 | WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR = $80000000; |
| 486 | |
| 487 | WINHTTP_FLAG_SECURE_PROTOCOL_SSL2 = $00000008; |
| 488 | WINHTTP_FLAG_SECURE_PROTOCOL_SSL3 = $00000020; |
| 489 | WINHTTP_FLAG_SECURE_PROTOCOL_TLS1 = $00000080; |
| 490 | WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 = $00000200; |
| 491 | WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 = $00000800; |
| 492 | |
| 493 | // Note: SECURE_PROTOCOL_ALL does not include TLS1.1 and higher! |
| 494 | WINHTTP_FLAG_SECURE_PROTOCOL_ALL = WINHTTP_FLAG_SECURE_PROTOCOL_SSL2 |
| 495 | or WINHTTP_FLAG_SECURE_PROTOCOL_SSL3 |
| 496 | or WINHTTP_FLAG_SECURE_PROTOCOL_TLS1; |
| 497 | |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 498 | // AutoProxy |
| 499 | WINHTTP_AUTOPROXY_AUTO_DETECT = $00000001; |
| 500 | WINHTTP_AUTOPROXY_CONFIG_URL = $00000002; |
| 501 | WINHTTP_AUTOPROXY_HOST_KEEPCASE = $00000004; |
| 502 | WINHTTP_AUTOPROXY_HOST_LOWERCASE = $00000008; |
| 503 | WINHTTP_AUTOPROXY_RUN_INPROCESS = $00010000; |
| 504 | WINHTTP_AUTOPROXY_RUN_OUTPROCESS_ONLY = $00020000; |
| 505 | |
| 506 | // Flags for dwAutoDetectFlags |
| 507 | WINHTTP_AUTO_DETECT_TYPE_DHCP = $00000001; |
| 508 | WINHTTP_AUTO_DETECT_TYPE_DNS_A = $00000002; |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 509 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 510 | const |
| 511 | WINHTTP_ERROR_BASE = 12000; |
| 512 | ERROR_WINHTTP_OUT_OF_HANDLES = WINHTTP_ERROR_BASE + 1; |
| 513 | ERROR_WINHTTP_TIMEOUT = WINHTTP_ERROR_BASE + 2; |
| 514 | ERROR_WINHTTP_INTERNAL_ERROR = WINHTTP_ERROR_BASE + 4; |
| 515 | ERROR_WINHTTP_INVALID_URL = WINHTTP_ERROR_BASE + 5; |
| 516 | ERROR_WINHTTP_UNRECOGNIZED_SCHEME = WINHTTP_ERROR_BASE + 6; |
| 517 | ERROR_WINHTTP_NAME_NOT_RESOLVED = WINHTTP_ERROR_BASE + 7; |
| 518 | ERROR_WINHTTP_INVALID_OPTION = WINHTTP_ERROR_BASE + 9; |
| 519 | ERROR_WINHTTP_OPTION_NOT_SETTABLE = WINHTTP_ERROR_BASE + 11; |
| 520 | ERROR_WINHTTP_SHUTDOWN = WINHTTP_ERROR_BASE + 12; |
| 521 | ERROR_WINHTTP_LOGIN_FAILURE = WINHTTP_ERROR_BASE + 15; |
| 522 | ERROR_WINHTTP_OPERATION_CANCELLED = WINHTTP_ERROR_BASE + 17; |
| 523 | ERROR_WINHTTP_INCORRECT_HANDLE_TYPE = WINHTTP_ERROR_BASE + 18; |
| 524 | ERROR_WINHTTP_INCORRECT_HANDLE_STATE = WINHTTP_ERROR_BASE + 19; |
| 525 | ERROR_WINHTTP_CANNOT_CONNECT = WINHTTP_ERROR_BASE + 29; |
| 526 | ERROR_WINHTTP_CONNECTION_ERROR = WINHTTP_ERROR_BASE + 30; |
| 527 | ERROR_WINHTTP_RESEND_REQUEST = WINHTTP_ERROR_BASE + 32; |
| 528 | ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED = WINHTTP_ERROR_BASE + 44; |
| 529 | ERROR_WINHTTP_CANNOT_CALL_BEFORE_OPEN = WINHTTP_ERROR_BASE + 100; |
| 530 | ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND = WINHTTP_ERROR_BASE + 101; |
| 531 | ERROR_WINHTTP_CANNOT_CALL_AFTER_SEND = WINHTTP_ERROR_BASE + 102; |
| 532 | ERROR_WINHTTP_CANNOT_CALL_AFTER_OPEN = WINHTTP_ERROR_BASE + 103; |
| 533 | ERROR_WINHTTP_HEADER_NOT_FOUND = WINHTTP_ERROR_BASE + 150; |
| 534 | ERROR_WINHTTP_INVALID_SERVER_RESPONSE = WINHTTP_ERROR_BASE + 152; |
| 535 | ERROR_WINHTTP_INVALID_HEADER = WINHTTP_ERROR_BASE + 153; |
| 536 | ERROR_WINHTTP_INVALID_QUERY_REQUEST = WINHTTP_ERROR_BASE + 154; |
| 537 | ERROR_WINHTTP_HEADER_ALREADY_EXISTS = WINHTTP_ERROR_BASE + 155; |
| 538 | ERROR_WINHTTP_REDIRECT_FAILED = WINHTTP_ERROR_BASE + 156; |
| 539 | ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR = WINHTTP_ERROR_BASE + 178; |
| 540 | ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT = WINHTTP_ERROR_BASE + 166; |
| 541 | ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT = WINHTTP_ERROR_BASE + 167; |
| 542 | ERROR_WINHTTP_NOT_INITIALIZED = WINHTTP_ERROR_BASE + 172; |
| 543 | ERROR_WINHTTP_SECURE_FAILURE = WINHTTP_ERROR_BASE + 175; |
| 544 | |
| 545 | // Certificate security errors. Additional information is provided |
| 546 | // via the WINHTTP_CALLBACK_STATUS_SECURE_FAILURE callback notification. |
| 547 | ERROR_WINHTTP_SECURE_CERT_DATE_INVALID = WINHTTP_ERROR_BASE + 37; |
| 548 | ERROR_WINHTTP_SECURE_CERT_CN_INVALID = WINHTTP_ERROR_BASE + 38; |
| 549 | ERROR_WINHTTP_SECURE_INVALID_CA = WINHTTP_ERROR_BASE + 45; |
| 550 | ERROR_WINHTTP_SECURE_CERT_REV_FAILED = WINHTTP_ERROR_BASE + 57; |
| 551 | ERROR_WINHTTP_SECURE_CHANNEL_ERROR = WINHTTP_ERROR_BASE + 157; |
| 552 | ERROR_WINHTTP_SECURE_INVALID_CERT = WINHTTP_ERROR_BASE + 169; |
| 553 | ERROR_WINHTTP_SECURE_CERT_REVOKED = WINHTTP_ERROR_BASE + 170; |
| 554 | ERROR_WINHTTP_SECURE_CERT_WRONG_USAGE = WINHTTP_ERROR_BASE + 179; |
| 555 | |
| 556 | ERROR_WINHTTP_AUTODETECTION_FAILED = WINHTTP_ERROR_BASE + 180; |
| 557 | ERROR_WINHTTP_HEADER_COUNT_EXCEEDED = WINHTTP_ERROR_BASE + 181; |
| 558 | ERROR_WINHTTP_HEADER_SIZE_OVERFLOW = WINHTTP_ERROR_BASE + 182; |
| 559 | ERROR_WINHTTP_CHUNKED_ENCODING_HEADER_SIZE_OVERFLOW = WINHTTP_ERROR_BASE + 183; |
| 560 | ERROR_WINHTTP_RESPONSE_DRAIN_OVERFLOW = WINHTTP_ERROR_BASE + 184; |
| 561 | ERROR_WINHTTP_CLIENT_CERT_NO_PRIVATE_KEY = WINHTTP_ERROR_BASE + 185; |
| 562 | ERROR_WINHTTP_CLIENT_CERT_NO_ACCESS_PRIVATE_KEY = WINHTTP_ERROR_BASE + 186; |
| 563 | |
Jens Geyer | 433a649 | 2019-06-19 23:14:08 +0200 | [diff] [blame] | 564 | WINHTTP_ERROR_LAST = WINHTTP_ERROR_BASE + 186; |
| 565 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 566 | |
| 567 | const |
| 568 | WINHTTP_THRIFT_DEFAULTS = WINHTTP_FLAG_NULL_CODEPAGE |
| 569 | or WINHTTP_FLAG_BYPASS_PROXY_CACHE |
| 570 | or WINHTTP_FLAG_ESCAPE_DISABLE; |
| 571 | |
| 572 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 573 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 574 | type |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 575 | IWinHTTPSession = interface; |
| 576 | IWinHTTPConnection = interface; |
| 577 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 578 | IWinHTTPRequest = interface |
Jens Geyer | b012318 | 2020-02-12 12:16:19 +0100 | [diff] [blame^] | 579 | ['{7AE6F63F-49B6-436B-8AAB-159E58EB900A}'] |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 580 | function Handle : HINTERNET; |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 581 | function Connection : IWinHTTPConnection; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 582 | function AddRequestHeader( const aHeader : string; const addflag : DWORD = WINHTTP_ADDREQ_FLAG_ADD) : Boolean; |
| 583 | function SetTimeouts( const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32) : Boolean; |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 584 | procedure TryAutoProxy( const aUrl : string); |
Jens Geyer | 19505c3 | 2019-06-22 00:59:54 +0200 | [diff] [blame] | 585 | procedure EnableAutomaticContentDecompression( const aEnable : Boolean); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 586 | function SendRequest( const pBuf : Pointer; const dwBytes : DWORD; const dwExtra : DWORD = 0) : Boolean; |
| 587 | function WriteExtraData( const pBuf : Pointer; const dwBytes : DWORD) : DWORD; |
| 588 | function FlushAndReceiveResponse : Boolean; |
| 589 | function ReadData( const dwRead : DWORD) : TBytes; overload; |
| 590 | function ReadData( const pBuf : Pointer; const dwRead : DWORD) : DWORD; overload; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 591 | function QueryDataAvailable : DWORD; |
Jens Geyer | b012318 | 2020-02-12 12:16:19 +0100 | [diff] [blame^] | 592 | function QueryTotalResponseSize( out dwSize : DWORD) : Boolean; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 593 | end; |
| 594 | |
| 595 | IWinHTTPConnection = interface |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 596 | ['{ED5BCA49-84D6-4CFE-BF18-3238B1FF2AFB}'] |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 597 | function Handle : HINTERNET; |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 598 | function Session : IWinHTTPSession; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 599 | function OpenRequest( const secure : Boolean; const aVerb, aObjName, aAcceptTypes : UnicodeString) : IWinHTTPRequest; |
| 600 | end; |
| 601 | |
| 602 | IWinHTTPSession = interface |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 603 | ['{261ADCB7-5465-4407-8840-468C17F009F0}'] |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 604 | function Handle : HINTERNET; |
| 605 | function Connect( const aHostName : UnicodeString; const aPort : INTERNET_PORT = INTERNET_DEFAULT_PORT) : IWinHTTPConnection; |
| 606 | function SetTimeouts( const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32) : Boolean; |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 607 | function EnableSecureProtocols( const aFlagSet : DWORD) : Boolean; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 608 | end; |
| 609 | |
| 610 | IWinHTTPUrl = interface |
| 611 | ['{78BE977C-4171-4AF5-A250-FD2890205E63}'] |
| 612 | // url parts getter |
| 613 | function GetScheme : UnicodeString; |
| 614 | function GetNumScheme : INTERNET_SCHEME; |
| 615 | function GetHostName : UnicodeString; |
| 616 | function GetPort : INTERNET_PORT; |
| 617 | function GetUserName : UnicodeString; |
| 618 | function GetPassword : UnicodeString; |
| 619 | function GetUrlPath : UnicodeString; |
| 620 | function GetExtraInfo : UnicodeString; |
| 621 | |
| 622 | // url parts setter |
| 623 | procedure SetScheme( const value : UnicodeString); |
| 624 | procedure SetHostName ( const value : UnicodeString); |
| 625 | procedure SetPort( const value : INTERNET_PORT); |
| 626 | procedure SetUserName( const value : UnicodeString); |
| 627 | procedure SetPassword( const value : UnicodeString); |
| 628 | procedure SetUrlPath( const value : UnicodeString); |
| 629 | procedure SetExtraInfo( const value : UnicodeString); |
| 630 | |
| 631 | // url as a whole |
| 632 | function BuildUrl : UnicodeString; |
| 633 | procedure CrackUrl( const value : UnicodeString); |
| 634 | |
| 635 | // url parts |
| 636 | property Scheme : UnicodeString read GetScheme write SetScheme; |
| 637 | property NumScheme : INTERNET_SCHEME read GetNumScheme; // readonly |
| 638 | property HostName : UnicodeString read GetHostName write SetHostName; |
| 639 | property Port : INTERNET_PORT read GetPort write SetPort; |
| 640 | property UserName : UnicodeString read GetUserName write SetUserName; |
| 641 | property Password : UnicodeString read GetPassword write SetPassword; |
| 642 | property UrlPath : UnicodeString read GetUrlPath write SetUrlPath; |
| 643 | property ExtraInfo : UnicodeString read GetExtraInfo write SetExtraInfo; |
| 644 | |
| 645 | // url as a whole |
| 646 | property CompleteURL : UnicodeString read BuildUrl write CrackUrl; |
| 647 | end; |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | type |
| 653 | TWinHTTPHandleObjectImpl = class( TInterfacedObject) |
| 654 | strict protected |
| 655 | FHandle : HINTERNET; |
| 656 | function Handle : HINTERNET; |
| 657 | public |
| 658 | constructor Create( const aHandle : HINTERNET); |
| 659 | destructor Destroy; override; |
| 660 | end; |
| 661 | |
| 662 | |
| 663 | TWinHTTPSessionImpl = class( TWinHTTPHandleObjectImpl, IWinHTTPSession) |
| 664 | strict protected |
| 665 | |
| 666 | // IWinHTTPSession |
| 667 | function Connect( const aHostName : UnicodeString; const aPort : INTERNET_PORT = INTERNET_DEFAULT_PORT) : IWinHTTPConnection; |
| 668 | function SetTimeouts( const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32) : Boolean; |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 669 | function EnableSecureProtocols( const aFlagSet : DWORD) : Boolean; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 670 | public |
| 671 | constructor Create( const aAgent : UnicodeString; |
| 672 | const aAccessType : DWORD = WINHTTP_ACCESS_TYPE_DEFAULT_PROXY; |
| 673 | const aProxy : UnicodeString = ''; |
| 674 | const aProxyBypass : UnicodeString = ''; |
| 675 | const aFlags : DWORD = 0); |
| 676 | destructor Destroy; override; |
| 677 | end; |
| 678 | |
| 679 | |
| 680 | TWinHTTPConnectionImpl = class( TWinHTTPHandleObjectImpl, IWinHTTPConnection) |
| 681 | strict protected |
| 682 | FSession : IWinHTTPSession; |
| 683 | |
| 684 | // IWinHTTPConnection |
| 685 | function OpenRequest( const secure : Boolean; const aVerb, aObjName, aAcceptTypes : UnicodeString) : IWinHTTPRequest; |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 686 | function Session : IWinHTTPSession; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 687 | |
| 688 | public |
| 689 | constructor Create( const aSession : IWinHTTPSession; const aHostName : UnicodeString; const aPort : INTERNET_PORT); |
| 690 | destructor Destroy; override; |
| 691 | end; |
| 692 | |
| 693 | |
| 694 | TAcceptTypesArray = array of string; |
| 695 | |
| 696 | TWinHTTPRequestImpl = class( TWinHTTPHandleObjectImpl, IWinHTTPRequest) |
| 697 | strict protected |
| 698 | FConnection : IWinHTTPConnection; |
| 699 | |
| 700 | // IWinHTTPRequest |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 701 | function Connection : IWinHTTPConnection; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 702 | function AddRequestHeader( const aHeader : string; const addflag : DWORD = WINHTTP_ADDREQ_FLAG_ADD) : Boolean; |
| 703 | function SetTimeouts( const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32) : Boolean; |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 704 | procedure TryAutoProxy( const aUrl : string); |
Jens Geyer | 19505c3 | 2019-06-22 00:59:54 +0200 | [diff] [blame] | 705 | procedure EnableAutomaticContentDecompression( const aEnable : Boolean); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 706 | function SendRequest( const pBuf : Pointer; const dwBytes : DWORD; const dwExtra : DWORD = 0) : Boolean; |
| 707 | function WriteExtraData( const pBuf : Pointer; const dwBytes : DWORD) : DWORD; |
| 708 | function FlushAndReceiveResponse : Boolean; |
| 709 | function ReadData( const dwRead : DWORD) : TBytes; overload; |
| 710 | function ReadData( const pBuf : Pointer; const dwRead : DWORD) : DWORD; overload; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 711 | function QueryDataAvailable : DWORD; |
Jens Geyer | b012318 | 2020-02-12 12:16:19 +0100 | [diff] [blame^] | 712 | function QueryTotalResponseSize( out dwSize : DWORD) : Boolean; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 713 | |
| 714 | public |
| 715 | constructor Create( const aConnection : IWinHTTPConnection; |
| 716 | const aVerb, aObjName : UnicodeString; |
| 717 | const aVersion : UnicodeString = ''; |
| 718 | const aReferrer : UnicodeString = ''; |
| 719 | const aAcceptTypes : UnicodeString = '*/*'; |
| 720 | const aFlags : DWORD = WINHTTP_THRIFT_DEFAULTS |
| 721 | ); |
| 722 | |
| 723 | destructor Destroy; override; |
| 724 | end; |
| 725 | |
| 726 | |
| 727 | TWinHTTPUrlImpl = class( TInterfacedObject, IWinHTTPUrl) |
| 728 | strict private |
| 729 | FScheme : UnicodeString; |
| 730 | FNumScheme : INTERNET_SCHEME; |
| 731 | FHostName : UnicodeString; |
| 732 | FPort : INTERNET_PORT; |
| 733 | FUserName : UnicodeString; |
| 734 | FPassword : UnicodeString; |
| 735 | FUrlPath : UnicodeString; |
| 736 | FExtraInfo : UnicodeString; |
| 737 | |
| 738 | strict protected |
| 739 | // url parts getter |
| 740 | function GetScheme : UnicodeString; |
| 741 | function GetNumScheme : INTERNET_SCHEME; |
| 742 | function GetHostName : UnicodeString; |
| 743 | function GetPort : INTERNET_PORT; |
| 744 | function GetUserName : UnicodeString; |
| 745 | function GetPassword : UnicodeString; |
| 746 | function GetUrlPath : UnicodeString; |
| 747 | function GetExtraInfo : UnicodeString; |
| 748 | |
| 749 | // url parts setter |
| 750 | procedure SetScheme( const value : UnicodeString); |
| 751 | procedure SetHostName ( const value : UnicodeString); |
| 752 | procedure SetPort( const value : INTERNET_PORT); |
| 753 | procedure SetUserName( const value : UnicodeString); |
| 754 | procedure SetPassword( const value : UnicodeString); |
| 755 | procedure SetUrlPath( const value : UnicodeString); |
| 756 | procedure SetExtraInfo( const value : UnicodeString); |
| 757 | |
| 758 | // url as a whole |
| 759 | function BuildUrl : UnicodeString; |
| 760 | procedure CrackUrl( const value : UnicodeString); |
| 761 | |
| 762 | public |
| 763 | constructor Create( const aUri : UnicodeString); |
| 764 | destructor Destroy; override; |
| 765 | end; |
| 766 | |
| 767 | |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 768 | WINHTTP_PROXY_INFO_Helper = record helper for WINHTTP_PROXY_INFO |
| 769 | procedure Initialize; |
| 770 | procedure FreeAllocatedResources; |
| 771 | end; |
| 772 | |
| 773 | |
| 774 | WINHTTP_CURRENT_USER_IE_PROXY_CONFIG_Helper = record helper for WINHTTP_CURRENT_USER_IE_PROXY_CONFIG |
| 775 | procedure Initialize; |
| 776 | procedure FreeAllocatedResources; |
| 777 | end; |
| 778 | |
| 779 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 780 | EWinHTTPException = class(Exception); |
| 781 | |
Jens Geyer | 433a649 | 2019-06-19 23:14:08 +0200 | [diff] [blame] | 782 | { helper functions } |
| 783 | |
| 784 | function WinHttpSysErrorMessage( const error : Cardinal): string; |
| 785 | procedure RaiseLastWinHttpError; |
| 786 | |
| 787 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 788 | implementation |
| 789 | |
| 790 | const WINHTTP_DLL = 'WinHTTP.dll'; |
| 791 | |
| 792 | function WinHttpCloseHandle; stdcall; external WINHTTP_DLL; |
| 793 | function WinHttpOpen; stdcall; external WINHTTP_DLL; |
| 794 | function WinHttpConnect; stdcall; external WINHTTP_DLL; |
| 795 | function WinHttpOpenRequest; stdcall; external WINHTTP_DLL; |
| 796 | function WinHttpSendRequest; stdcall; external WINHTTP_DLL; |
| 797 | function WinHttpSetTimeouts; stdcall; external WINHTTP_DLL; |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 798 | function WinHttpQueryOption; stdcall; external WINHTTP_DLL; |
| 799 | function WinHttpSetOption; stdcall; external WINHTTP_DLL; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 800 | function WinHttpAddRequestHeaders; stdcall; external WINHTTP_DLL; |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 801 | function WinHttpGetProxyForUrl; stdcall; external WINHTTP_DLL; |
| 802 | function WinHttpGetIEProxyConfigForCurrentUser; stdcall; external WINHTTP_DLL; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 803 | function WinHttpWriteData; stdcall; external WINHTTP_DLL; |
| 804 | function WinHttpReceiveResponse; stdcall; external WINHTTP_DLL; |
| 805 | function WinHttpQueryHeaders; stdcall; external WINHTTP_DLL; |
| 806 | function WinHttpQueryDataAvailable; stdcall; external WINHTTP_DLL; |
| 807 | function WinHttpReadData; stdcall; external WINHTTP_DLL; |
| 808 | function WinHttpCrackUrl; stdcall; external WINHTTP_DLL; |
| 809 | function WinHttpCreateUrl; stdcall; external WINHTTP_DLL; |
| 810 | |
| 811 | |
Jens Geyer | 433a649 | 2019-06-19 23:14:08 +0200 | [diff] [blame] | 812 | { helper functions } |
| 813 | |
| 814 | function WinHttpSysErrorMessage( const error : Cardinal): string; |
| 815 | const FLAGS = FORMAT_MESSAGE_ALLOCATE_BUFFER |
| 816 | or FORMAT_MESSAGE_IGNORE_INSERTS |
| 817 | or FORMAT_MESSAGE_FROM_SYSTEM |
| 818 | or FORMAT_MESSAGE_FROM_HMODULE; |
| 819 | var pBuffer : PChar; |
| 820 | nChars : Cardinal; |
| 821 | begin |
| 822 | if (error < WINHTTP_ERROR_BASE) |
| 823 | or (error > WINHTTP_ERROR_LAST) |
| 824 | then Exit( SysUtils.SysErrorMessage( error)); |
| 825 | |
| 826 | pBuffer := nil; |
| 827 | try |
| 828 | nChars := FormatMessage( FLAGS, |
| 829 | Pointer( GetModuleHandle( WINHTTP_DLL)), |
| 830 | error, |
| 831 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language |
| 832 | @pBuffer, 0, |
| 833 | nil); |
| 834 | SetString( result, pBuffer, nChars); |
| 835 | finally |
Jens Geyer | 62238d1 | 2019-06-26 20:51:26 +0200 | [diff] [blame] | 836 | LocalFree( NativeUInt( pBuffer)); |
Jens Geyer | 433a649 | 2019-06-19 23:14:08 +0200 | [diff] [blame] | 837 | end; |
| 838 | end; |
| 839 | |
| 840 | |
| 841 | procedure RaiseLastWinHttpError; |
| 842 | var error : Cardinal; |
| 843 | sMsg : string; |
| 844 | begin |
| 845 | error := Cardinal( GetLastError); |
| 846 | if error <> NOERROR then begin |
| 847 | sMSg := IntToStr(Integer(error))+' '+WinHttpSysErrorMessage(error); |
| 848 | raise EWinHTTPException.Create( sMsg); |
| 849 | end; |
| 850 | end; |
| 851 | |
| 852 | |
| 853 | |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 854 | { misc. record helper } |
| 855 | |
| 856 | |
| 857 | procedure GlobalFreeAndNil( var p : LPWSTR); |
| 858 | begin |
| 859 | if p <> nil then begin |
| 860 | GlobalFree( HGLOBAL( p)); |
| 861 | p := nil; |
| 862 | end; |
| 863 | end; |
| 864 | |
| 865 | |
| 866 | procedure WINHTTP_PROXY_INFO_Helper.Initialize; |
| 867 | begin |
| 868 | FillChar( Self, SizeOf(Self), 0); |
| 869 | end; |
| 870 | |
| 871 | |
| 872 | procedure WINHTTP_PROXY_INFO_Helper.FreeAllocatedResources; |
| 873 | // The caller must free the lpszProxy and lpszProxyBypass strings |
| 874 | // if they are non-NULL. Use GlobalFree to free the strings. |
| 875 | begin |
| 876 | GlobalFreeAndNil( lpszProxy); |
| 877 | GlobalFreeAndNil( lpszProxyBypass); |
| 878 | Initialize; |
| 879 | end; |
| 880 | |
| 881 | |
| 882 | procedure WINHTTP_CURRENT_USER_IE_PROXY_CONFIG_Helper.Initialize; |
| 883 | begin |
| 884 | FillChar( Self, SizeOf(Self), 0); |
| 885 | end; |
| 886 | |
| 887 | |
| 888 | procedure WINHTTP_CURRENT_USER_IE_PROXY_CONFIG_Helper.FreeAllocatedResources; |
| 889 | // The caller must free the lpszProxy, lpszProxyBypass and lpszAutoConfigUrl strings |
| 890 | // if they are non-NULL. Use GlobalFree to free the strings. |
| 891 | begin |
| 892 | GlobalFreeAndNil( lpszProxy); |
| 893 | GlobalFreeAndNil( lpszProxyBypass); |
| 894 | GlobalFreeAndNil( lpszAutoConfigUrl); |
| 895 | Initialize; |
| 896 | end; |
| 897 | |
| 898 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 899 | { TWinHTTPHandleObjectImpl } |
| 900 | |
| 901 | constructor TWinHTTPHandleObjectImpl.Create( const aHandle : HINTERNET); |
| 902 | begin |
| 903 | inherited Create; |
| 904 | FHandle := aHandle; |
| 905 | |
| 906 | if FHandle = nil |
| 907 | then raise EWinHTTPException.Create('Invalid handle'); |
| 908 | end; |
| 909 | |
| 910 | |
| 911 | destructor TWinHTTPHandleObjectImpl.Destroy; |
| 912 | begin |
| 913 | try |
| 914 | if Assigned(FHandle) then begin |
| 915 | WinHttpCloseHandle(FHandle); |
| 916 | FHandle := nil; |
| 917 | end; |
| 918 | |
| 919 | finally |
| 920 | inherited Destroy; |
| 921 | end; |
| 922 | end; |
| 923 | |
| 924 | |
| 925 | function TWinHTTPHandleObjectImpl.Handle : HINTERNET; |
| 926 | begin |
| 927 | result := FHandle; |
| 928 | end; |
| 929 | |
| 930 | |
| 931 | { TWinHTTPSessionImpl } |
| 932 | |
| 933 | |
| 934 | constructor TWinHTTPSessionImpl.Create( const aAgent : UnicodeString; const aAccessType : DWORD; |
| 935 | const aProxy, aProxyBypass : UnicodeString; const aFlags : DWORD); |
| 936 | var handle : HINTERNET; |
| 937 | begin |
| 938 | handle := WinHttpOpen( PWideChar(aAgent), aAccessType, |
| 939 | PWideChar(Pointer(aProxy)), // may be nil |
| 940 | PWideChar(Pointer(aProxyBypass)), // may be nil |
| 941 | aFlags); |
Jens Geyer | 433a649 | 2019-06-19 23:14:08 +0200 | [diff] [blame] | 942 | if handle = nil then RaiseLastWinHttpError; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 943 | inherited Create( handle); |
| 944 | end; |
| 945 | |
| 946 | |
| 947 | destructor TWinHTTPSessionImpl.Destroy; |
| 948 | begin |
| 949 | inherited Destroy; |
| 950 | // add code here |
| 951 | end; |
| 952 | |
| 953 | |
| 954 | function TWinHTTPSessionImpl.Connect( const aHostName : UnicodeString; const aPort : INTERNET_PORT) : IWinHTTPConnection; |
| 955 | begin |
| 956 | result := TWinHTTPConnectionImpl.Create( Self, aHostName, aPort); |
| 957 | end; |
| 958 | |
| 959 | |
| 960 | function TWinHTTPSessionImpl.SetTimeouts( const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32) : Boolean; |
| 961 | begin |
| 962 | result := WinHttpSetTimeouts( FHandle, aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout); |
| 963 | end; |
| 964 | |
| 965 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 966 | function TWinHTTPSessionImpl.EnableSecureProtocols( const aFlagSet : DWORD) : Boolean; |
| 967 | var dwSize : DWORD; |
| 968 | begin |
| 969 | dwSize := SizeOf(aFlagSet); |
| 970 | result := WinHttpSetOption( Handle, WINHTTP_OPTION_SECURE_PROTOCOLS, @aFlagset, dwSize); |
| 971 | end; |
| 972 | |
| 973 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 974 | { TWinHTTPConnectionImpl } |
| 975 | |
| 976 | constructor TWinHTTPConnectionImpl.Create( const aSession : IWinHTTPSession; const aHostName : UnicodeString; const aPort : INTERNET_PORT); |
| 977 | var handle : HINTERNET; |
| 978 | begin |
| 979 | FSession := aSession; |
| 980 | handle := WinHttpConnect( FSession.Handle, PWideChar(aHostName), aPort, 0); |
Jens Geyer | 433a649 | 2019-06-19 23:14:08 +0200 | [diff] [blame] | 981 | if handle = nil then RaiseLastWinHttpError; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 982 | inherited Create( handle); |
| 983 | end; |
| 984 | |
| 985 | |
| 986 | destructor TWinHTTPConnectionImpl.Destroy; |
| 987 | begin |
| 988 | inherited Destroy; |
| 989 | FSession := nil; |
| 990 | end; |
| 991 | |
| 992 | |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 993 | function TWinHTTPConnectionImpl.Session : IWinHTTPSession; |
| 994 | begin |
| 995 | result := FSession; |
| 996 | end; |
| 997 | |
| 998 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 999 | function TWinHTTPConnectionImpl.OpenRequest( const secure : Boolean; const aVerb, aObjName, aAcceptTypes : UnicodeString) : IWinHTTPRequest; |
| 1000 | var dwFlags : DWORD; |
| 1001 | begin |
| 1002 | dwFlags := WINHTTP_THRIFT_DEFAULTS; |
| 1003 | if secure |
| 1004 | then dwFlags := dwFlags or WINHTTP_FLAG_SECURE |
| 1005 | else dwFlags := dwFlags and not WINHTTP_FLAG_SECURE; |
| 1006 | |
| 1007 | result := TWinHTTPRequestImpl.Create( Self, aVerb, aObjName, '', '', aAcceptTypes, dwFlags); |
| 1008 | end; |
| 1009 | |
| 1010 | |
| 1011 | { TWinHTTPRequestImpl } |
| 1012 | |
| 1013 | constructor TWinHTTPRequestImpl.Create( const aConnection : IWinHTTPConnection; |
| 1014 | const aVerb, aObjName, aVersion, aReferrer : UnicodeString; |
| 1015 | const aAcceptTypes : UnicodeString; |
| 1016 | const aFlags : DWORD |
| 1017 | ); |
| 1018 | var handle : HINTERNET; |
| 1019 | accept : array[0..1] of PWideChar; |
| 1020 | begin |
| 1021 | FConnection := aConnection; |
| 1022 | |
| 1023 | accept[0] := PWideChar(aAcceptTypes); |
| 1024 | accept[1] := nil; |
| 1025 | |
| 1026 | handle := WinHttpOpenRequest( FConnection.Handle, |
| 1027 | PWideChar(UpperCase(aVerb)), |
| 1028 | PWideChar(aObjName), |
| 1029 | PWideChar(aVersion), |
| 1030 | PWideChar(aReferrer), |
| 1031 | @accept, |
| 1032 | aFlags); |
Jens Geyer | 433a649 | 2019-06-19 23:14:08 +0200 | [diff] [blame] | 1033 | if handle = nil then RaiseLastWinHttpError; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 1034 | inherited Create( handle); |
| 1035 | end; |
| 1036 | |
| 1037 | |
| 1038 | destructor TWinHTTPRequestImpl.Destroy; |
| 1039 | begin |
| 1040 | inherited Destroy; |
| 1041 | FConnection := nil; |
| 1042 | end; |
| 1043 | |
| 1044 | |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 1045 | function TWinHTTPRequestImpl.Connection : IWinHTTPConnection; |
| 1046 | begin |
| 1047 | result := FConnection; |
| 1048 | end; |
| 1049 | |
| 1050 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 1051 | function TWinHTTPRequestImpl.SetTimeouts( const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32) : Boolean; |
| 1052 | begin |
| 1053 | result := WinHttpSetTimeouts( FHandle, aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout); |
| 1054 | end; |
| 1055 | |
| 1056 | |
| 1057 | function TWinHTTPRequestImpl.AddRequestHeader( const aHeader : string; const addflag : DWORD) : Boolean; |
| 1058 | begin |
| 1059 | result := WinHttpAddRequestHeaders( FHandle, PWideChar(aHeader), DWORD(-1), addflag); |
| 1060 | end; |
| 1061 | |
| 1062 | |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 1063 | procedure TWinHTTPRequestImpl.TryAutoProxy( const aUrl : string); |
| 1064 | // From MSDN: |
| 1065 | // AutoProxy support is not fully integrated into the HTTP stack in WinHTTP. |
| 1066 | // Before sending a request, the application must call WinHttpGetProxyForUrl |
| 1067 | // to obtain the name of a proxy server and then call WinHttpSetOption using |
| 1068 | // WINHTTP_OPTION_PROXY to set the proxy configuration on the WinHTTP request |
| 1069 | // handle created by WinHttpOpenRequest. |
| 1070 | // See https://docs.microsoft.com/en-us/windows/desktop/winhttp/winhttp-autoproxy-api |
| 1071 | var |
| 1072 | options : WINHTTP_AUTOPROXY_OPTIONS; |
| 1073 | proxy : WINHTTP_PROXY_INFO; |
| 1074 | ieProxy : WINHTTP_CURRENT_USER_IE_PROXY_CONFIG; |
| 1075 | dwSize : DWORD; |
| 1076 | begin |
| 1077 | // try AutoProxy via PAC first |
| 1078 | proxy.Initialize; |
| 1079 | try |
| 1080 | FillChar( options, SizeOf(options), 0); |
| 1081 | options.dwFlags := WINHTTP_AUTOPROXY_AUTO_DETECT; |
| 1082 | options.dwAutoDetectFlags := WINHTTP_AUTO_DETECT_TYPE_DHCP or WINHTTP_AUTO_DETECT_TYPE_DNS_A; |
| 1083 | options.fAutoLogonIfChallenged := TRUE; |
| 1084 | if WinHttpGetProxyForUrl( FConnection.Session.Handle, PChar(aUrl), options, proxy) then begin |
| 1085 | dwSize := SizeOf(proxy); |
| 1086 | WinHttpSetOption( Handle, WINHTTP_OPTION_PROXY, @proxy, dwSize); |
| 1087 | Exit; |
| 1088 | end; |
| 1089 | |
| 1090 | finally |
| 1091 | proxy.FreeAllocatedResources; |
| 1092 | end; |
| 1093 | |
| 1094 | // Use IE settings as a fallback, useful in client (i.e. non-server) environments |
| 1095 | ieProxy.Initialize; |
| 1096 | try |
| 1097 | if WinHttpGetIEProxyConfigForCurrentUser( ieProxy) |
| 1098 | then begin |
| 1099 | |
| 1100 | // lpszAutoConfigUrl = "Use automatic proxy configuration" |
| 1101 | if ieProxy.lpszAutoConfigUrl <> nil then begin |
| 1102 | options.lpszAutoConfigUrl := ieProxy.lpszAutoConfigUrl; |
| 1103 | options.dwFlags := options.dwFlags or WINHTTP_AUTOPROXY_CONFIG_URL; |
| 1104 | |
| 1105 | proxy.Initialize; |
| 1106 | try |
| 1107 | if WinHttpGetProxyForUrl( FConnection.Session.Handle, PChar(aUrl), options, proxy) then begin |
| 1108 | dwSize := SizeOf(proxy); |
| 1109 | WinHttpSetOption( Handle, WINHTTP_OPTION_PROXY, @proxy, dwSize); |
| 1110 | Exit; |
| 1111 | end; |
| 1112 | finally |
| 1113 | proxy.FreeAllocatedResources; |
| 1114 | end; |
| 1115 | end; |
| 1116 | |
| 1117 | // lpszProxy = "use a proxy server" |
| 1118 | if ieProxy.lpszProxy <> nil then begin |
| 1119 | proxy.Initialize; |
| 1120 | try |
| 1121 | proxy.dwAccessType := WINHTTP_ACCESS_TYPE_NAMED_PROXY; |
| 1122 | proxy.lpszProxy := ieProxy.lpszProxy; |
| 1123 | proxy.lpszProxyBypass := ieProxy.lpszProxyBypass; |
| 1124 | dwSize := SizeOf(proxy); |
| 1125 | WinHttpSetOption( Handle, WINHTTP_OPTION_PROXY, @proxy, dwSize); |
| 1126 | Exit; |
| 1127 | finally |
| 1128 | proxy.Initialize; // not FreeAllocatedResources, we only hold pointer copies! |
| 1129 | end; |
| 1130 | end; |
| 1131 | |
| 1132 | end; |
| 1133 | |
| 1134 | finally |
| 1135 | ieProxy.FreeAllocatedResources; |
| 1136 | end; |
| 1137 | end; |
| 1138 | |
| 1139 | |
Jens Geyer | 19505c3 | 2019-06-22 00:59:54 +0200 | [diff] [blame] | 1140 | procedure TWinHTTPRequestImpl.EnableAutomaticContentDecompression( const aEnable : Boolean); |
| 1141 | // Enable automatic gzip,deflate decompression on systems that support this option |
| 1142 | // From the docs: WinHTTP will automatically set an appropriate Accept-Encoding header, |
| 1143 | // overriding any value supplied by the caller -> we don't have to do this |
| 1144 | // Available on Win 8.1 or higher |
| 1145 | var value : DWORD; |
| 1146 | begin |
| 1147 | if aEnable |
| 1148 | then value := WINHTTP_DECOMPRESSION_FLAG_ALL |
| 1149 | else value := 0; |
| 1150 | |
| 1151 | // ignore returned value, the option is not supported with older WinHTTP versions |
| 1152 | WinHttpSetOption( Handle, WINHTTP_OPTION_DECOMPRESSION, @value, SizeOf(DWORD)); |
| 1153 | end; |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 1154 | |
| 1155 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 1156 | function TWinHTTPRequestImpl.SendRequest( const pBuf : Pointer; const dwBytes, dwExtra : DWORD) : Boolean; |
| 1157 | begin |
| 1158 | result := WinHttpSendRequest( FHandle, |
| 1159 | WINHTTP_NO_ADDITIONAL_HEADERS, 0, |
| 1160 | pBuf, dwBytes, // number of bytes in pBuf |
| 1161 | dwBytes + dwExtra, // becomes the Content-Length |
| 1162 | nil); // context for async operations |
| 1163 | end; |
| 1164 | |
| 1165 | |
| 1166 | function TWinHTTPRequestImpl.WriteExtraData( const pBuf : Pointer; const dwBytes : DWORD) : DWORD; |
| 1167 | begin |
| 1168 | if not WinHttpWriteData( FHandle, pBuf, dwBytes, result) |
| 1169 | then result := 0; |
| 1170 | end; |
| 1171 | |
| 1172 | |
| 1173 | function TWinHTTPRequestImpl.FlushAndReceiveResponse : Boolean; |
| 1174 | begin |
| 1175 | result := WinHttpReceiveResponse( FHandle, nil); |
| 1176 | end; |
| 1177 | |
| 1178 | |
| 1179 | function TWinHTTPRequestImpl.ReadData( const dwRead : DWORD) : TBytes; |
| 1180 | var dwAvailable, dwReceived : DWORD; |
| 1181 | begin |
| 1182 | if WinHttpQueryDataAvailable( FHandle, dwAvailable) |
| 1183 | then dwAvailable := Min( dwRead, dwAvailable) |
| 1184 | else dwAvailable := 0; |
| 1185 | |
| 1186 | SetLength( result, dwAvailable); |
| 1187 | if dwAvailable = 0 then Exit; |
| 1188 | |
| 1189 | if WinHttpReadData( FHandle, @result[0], Length(result), dwReceived) |
| 1190 | then SetLength( result, dwReceived) |
| 1191 | else SetLength( result, 0); |
| 1192 | end; |
| 1193 | |
| 1194 | |
| 1195 | function TWinHTTPRequestImpl.ReadData( const pBuf : Pointer; const dwRead : DWORD) : DWORD; |
| 1196 | var dwAvailable : DWORD; |
| 1197 | begin |
| 1198 | if WinHttpQueryDataAvailable( FHandle, dwAvailable) |
| 1199 | then dwAvailable := Min( dwRead, dwAvailable) |
| 1200 | else dwAvailable := 0; |
| 1201 | |
| 1202 | if (dwAvailable = 0) |
| 1203 | or not WinHttpReadData( FHandle, pBuf, dwAvailable, result) |
| 1204 | then result := 0; |
| 1205 | end; |
| 1206 | |
| 1207 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1208 | function TWinHTTPRequestImpl.QueryDataAvailable : DWORD; |
| 1209 | begin |
| 1210 | if not WinHttpQueryDataAvailable( FHandle, result) |
| 1211 | then result := 0; |
| 1212 | end; |
| 1213 | |
| 1214 | |
Jens Geyer | b012318 | 2020-02-12 12:16:19 +0100 | [diff] [blame^] | 1215 | function TWinHTTPRequestImpl.QueryTotalResponseSize( out dwSize : DWORD) : Boolean; |
Jens Geyer | 528a0f0 | 2019-11-18 20:17:03 +0100 | [diff] [blame] | 1216 | var dwBytes, dwError, dwIndex : DWORD; |
| 1217 | begin |
| 1218 | dwBytes := SizeOf( result); |
| 1219 | dwIndex := DWORD( WINHTTP_NO_HEADER_INDEX); |
Jens Geyer | b012318 | 2020-02-12 12:16:19 +0100 | [diff] [blame^] | 1220 | result := WinHttpQueryHeaders( FHandle, |
| 1221 | WINHTTP_QUERY_CONTENT_LENGTH or WINHTTP_QUERY_FLAG_NUMBER, |
| 1222 | WINHTTP_HEADER_NAME_BY_INDEX, |
| 1223 | @dwSize, dwBytes, |
| 1224 | dwIndex); |
| 1225 | if not result then begin |
| 1226 | dwSize := MAXINT; // we don't know, just return something useful |
Jens Geyer | 528a0f0 | 2019-11-18 20:17:03 +0100 | [diff] [blame] | 1227 | dwError := GetLastError; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1228 | if dwError <> ERROR_WINHTTP_HEADER_NOT_FOUND then ASSERT(FALSE); // anything else would be an real error |
Jens Geyer | 528a0f0 | 2019-11-18 20:17:03 +0100 | [diff] [blame] | 1229 | end; |
| 1230 | end; |
| 1231 | |
| 1232 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1233 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 1234 | { TWinHTTPUrlImpl } |
| 1235 | |
| 1236 | constructor TWinHTTPUrlImpl.Create(const aUri: UnicodeString); |
| 1237 | begin |
| 1238 | inherited Create; |
| 1239 | CrackUrl( aUri) |
| 1240 | end; |
| 1241 | |
| 1242 | |
| 1243 | destructor TWinHTTPUrlImpl.Destroy; |
| 1244 | begin |
| 1245 | inherited Destroy; |
| 1246 | end; |
| 1247 | |
| 1248 | |
| 1249 | procedure TWinHTTPUrlImpl.CrackURL( const value : UnicodeString); |
| 1250 | const FLAGS = 0; // no special operations, leave components as-is |
| 1251 | var components : URL_COMPONENTS; |
| 1252 | begin |
| 1253 | FillChar(components, SizeOf(components), 0); |
| 1254 | components.dwStructSize := SizeOf(components); |
| 1255 | |
| 1256 | if value <> '' then begin |
| 1257 | { For the WinHttpCrackUrl function, [...] if the pointer member is NULL but the |
| 1258 | length member is not zero, both the pointer and length members are returned. } |
| 1259 | components.dwSchemeLength := DWORD(-1); |
| 1260 | components.dwHostNameLength := DWORD(-1); |
| 1261 | components.dwUserNameLength := DWORD(-1); |
| 1262 | components.dwPasswordLength := DWORD(-1); |
| 1263 | components.dwUrlPathLength := DWORD(-1); |
| 1264 | components.dwExtraInfoLength := DWORD(-1); |
| 1265 | |
| 1266 | WinHttpCrackUrl( PWideChar(value), Length(value), FLAGS, components); |
| 1267 | end; |
| 1268 | |
| 1269 | FNumScheme := components.nScheme; |
| 1270 | FPort := components.nPort; |
| 1271 | SetString( FScheme, components.lpszScheme, components.dwSchemeLength); |
| 1272 | SetString( FHostName, components.lpszHostName, components.dwHostNameLength); |
| 1273 | SetString( FUserName, components.lpszUserName, components.dwUserNameLength); |
| 1274 | SetString( FPassword, components.lpszPassword, components.dwPasswordLength); |
| 1275 | SetString( FUrlPath, components.lpszUrlPath, components.dwUrlPathLength); |
| 1276 | SetString( FExtraInfo, components.lpszExtraInfo, components.dwExtraInfoLength); |
| 1277 | end; |
| 1278 | |
| 1279 | |
| 1280 | function TWinHTTPUrlImpl.BuildUrl : UnicodeString; |
| 1281 | const FLAGS = 0; // no special operations, leave components as-is |
| 1282 | var components : URL_COMPONENTS; |
| 1283 | dwChars : DWORD; |
| 1284 | begin |
| 1285 | FillChar(components, SizeOf(components), 0); |
| 1286 | components.dwStructSize := SizeOf(components); |
| 1287 | components.lpszScheme := PWideChar(FScheme); |
| 1288 | components.dwSchemeLength := Length(FScheme); |
| 1289 | components.lpszHostName := PWideChar(FHostName); |
| 1290 | components.dwHostNameLength := Length(FHostName); |
| 1291 | components.nPort := FPort; |
| 1292 | components.lpszUserName := PWideChar(FUserName); |
| 1293 | components.dwUserNameLength := Length(FUserName); |
| 1294 | components.lpszPassword := PWideChar(FPassword); |
| 1295 | components.dwPasswordLength := Length(FPassword); |
| 1296 | components.lpszUrlPath := PWideChar(FUrlPath); |
| 1297 | components.dwUrlPathLength := Length(FUrlPath); |
| 1298 | components.lpszExtraInfo := PWideChar(FExtraInfo); |
| 1299 | components.dwExtraInfoLength := Length(FExtraInfo); |
| 1300 | |
| 1301 | WinHttpCreateUrl( components, FLAGS, nil, dwChars); |
| 1302 | if dwChars = 0 |
| 1303 | then result := '' |
| 1304 | else begin |
| 1305 | SetLength( result, dwChars + 1); |
| 1306 | WinHttpCreateUrl( components, FLAGS, @result[1], dwChars); |
| 1307 | SetLength( result, dwChars); // cut off terminating #0 |
| 1308 | end; |
| 1309 | end; |
| 1310 | |
| 1311 | |
| 1312 | function TWinHTTPUrlImpl.GetExtraInfo: UnicodeString; |
| 1313 | begin |
| 1314 | result := FExtraInfo; |
| 1315 | end; |
| 1316 | |
| 1317 | function TWinHTTPUrlImpl.GetHostName: UnicodeString; |
| 1318 | begin |
| 1319 | result := FHostName; |
| 1320 | end; |
| 1321 | |
| 1322 | function TWinHTTPUrlImpl.GetNumScheme: INTERNET_SCHEME; |
| 1323 | begin |
| 1324 | result := FNumScheme; |
| 1325 | end; |
| 1326 | |
| 1327 | function TWinHTTPUrlImpl.GetPassword: UnicodeString; |
| 1328 | begin |
| 1329 | result := FPassword; |
| 1330 | end; |
| 1331 | |
| 1332 | function TWinHTTPUrlImpl.GetPort: INTERNET_PORT; |
| 1333 | begin |
| 1334 | result := FPort; |
| 1335 | end; |
| 1336 | |
| 1337 | function TWinHTTPUrlImpl.GetScheme: UnicodeString; |
| 1338 | begin |
| 1339 | result := FScheme; |
| 1340 | end; |
| 1341 | |
| 1342 | function TWinHTTPUrlImpl.GetUrlPath: UnicodeString; |
| 1343 | begin |
| 1344 | result := FUrlPath; |
| 1345 | end; |
| 1346 | |
| 1347 | function TWinHTTPUrlImpl.GetUserName: UnicodeString; |
| 1348 | begin |
| 1349 | result := FUserName; |
| 1350 | end; |
| 1351 | |
| 1352 | procedure TWinHTTPUrlImpl.SetExtraInfo(const value: UnicodeString); |
| 1353 | begin |
| 1354 | FExtraInfo := value; |
| 1355 | end; |
| 1356 | |
| 1357 | procedure TWinHTTPUrlImpl.SetHostName(const value: UnicodeString); |
| 1358 | begin |
| 1359 | FHostName := value; |
| 1360 | end; |
| 1361 | |
| 1362 | procedure TWinHTTPUrlImpl.SetPassword(const value: UnicodeString); |
| 1363 | begin |
| 1364 | FPassword := value; |
| 1365 | end; |
| 1366 | |
| 1367 | procedure TWinHTTPUrlImpl.SetPort(const value: INTERNET_PORT); |
| 1368 | begin |
| 1369 | FPort := value; |
| 1370 | end; |
| 1371 | |
| 1372 | procedure TWinHTTPUrlImpl.SetScheme(const value: UnicodeString); |
| 1373 | begin |
| 1374 | FScheme := value; |
| 1375 | end; |
| 1376 | |
| 1377 | procedure TWinHTTPUrlImpl.SetUrlPath(const value: UnicodeString); |
| 1378 | begin |
| 1379 | FUrlPath := value; |
| 1380 | end; |
| 1381 | |
| 1382 | procedure TWinHTTPUrlImpl.SetUserName(const value: UnicodeString); |
| 1383 | begin |
| 1384 | FUserName := value; |
| 1385 | end; |
| 1386 | |
| 1387 | |
Jens Geyer | 433a649 | 2019-06-19 23:14:08 +0200 | [diff] [blame] | 1388 | initialization |
| 1389 | OutputDebugString( PChar( SysErrorMessage( 12002))); |
| 1390 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 1391 | end. |
| 1392 | |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 1393 | |