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 | |
| 66 | function WinHttpCloseHandle( aHandle : HINTERNET) : BOOL; stdcall; |
| 67 | |
| 68 | function WinHttpOpen( const pszAgentW : LPCWSTR; |
| 69 | const dwAccessType : DWORD; |
| 70 | const pszProxyW : LPCWSTR; |
| 71 | const pszProxyBypassW : LPCWSTR; |
| 72 | const dwFlags : DWORD |
| 73 | ) : HINTERNET; stdcall; |
| 74 | |
| 75 | function WinHttpConnect( const hSession : HINTERNET; |
| 76 | const pswzServerName : LPCWSTR; |
| 77 | const nServerPort : INTERNET_PORT; |
| 78 | const dwReserved : DWORD |
| 79 | ) : HINTERNET; stdcall; |
| 80 | |
| 81 | function WinHttpOpenRequest( const hConnect : HINTERNET; |
| 82 | const pwszVerb, pwszObjectName, pwszVersion, pwszReferrer : LPCWSTR; |
| 83 | const ppwszAcceptTypes : LPLPCWSTR; |
| 84 | const dwFlags : DWORD |
| 85 | ) : HINTERNET; stdcall; |
| 86 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame^] | 87 | function WinHttpQueryOption( const hInternet : HINTERNET; |
| 88 | const dwOption : DWORD; |
| 89 | const pBuffer : Pointer; |
| 90 | var dwBufferLength : DWORD) : BOOL; stdcall; |
| 91 | |
| 92 | function WinHttpSetOption( const hInternet : HINTERNET; |
| 93 | const dwOption : DWORD; |
| 94 | const pBuffer : Pointer; |
| 95 | const dwBufferLength : DWORD) : BOOL; stdcall; |
| 96 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 97 | function WinHttpSetTimeouts( const hRequestOrSession : HINTERNET; |
| 98 | const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32 |
| 99 | ) : BOOL; stdcall; |
| 100 | |
| 101 | function WinHttpAddRequestHeaders( const hRequest : HINTERNET; |
| 102 | const pwszHeaders : LPCWSTR; |
| 103 | const dwHeadersLengthInChars : DWORD; |
| 104 | const dwModifiers : DWORD |
| 105 | ) : BOOL; stdcall; |
| 106 | |
| 107 | function WinHttpSendRequest( const hRequest : HINTERNET; |
| 108 | const lpszHeaders : LPCWSTR; |
| 109 | const dwHeadersLength : DWORD; |
| 110 | const lpOptional : Pointer; |
| 111 | const dwOptionalLength : DWORD; |
| 112 | const dwTotalLength : DWORD; |
| 113 | const pContext : Pointer |
| 114 | ) : BOOL; stdcall; |
| 115 | |
| 116 | function WinHttpWriteData( const hRequest : HINTERNET; |
| 117 | const pBuf : Pointer; |
| 118 | const dwBytesToWrite : DWORD; |
| 119 | out dwBytesWritten : DWORD |
| 120 | ) : BOOL; stdcall; |
| 121 | |
| 122 | function WinHttpReceiveResponse( const hRequest : HINTERNET; const lpReserved : Pointer) : BOOL; stdcall; |
| 123 | |
| 124 | function WinHttpQueryHeaders( const hRequest : HINTERNET; |
| 125 | const dwInfoLevel : DWORD; |
| 126 | const pwszName : LPCWSTR; |
| 127 | const lpBuffer : Pointer; |
| 128 | var dwBufferLength : DWORD; |
| 129 | var dwIndex : DWORD |
| 130 | ) : BOOL; stdcall; |
| 131 | |
| 132 | function WinHttpQueryDataAvailable( const hRequest : HINTERNET; |
| 133 | var dwNumberOfBytesAvailable : DWORD |
| 134 | ) : BOOL; stdcall; |
| 135 | |
| 136 | function WinHttpReadData( const hRequest : HINTERNET; |
| 137 | const lpBuffer : Pointer; |
| 138 | const dwBytesToRead : DWORD; |
| 139 | out dwBytesRead : DWORD |
| 140 | ) : BOOL; stdcall; |
| 141 | |
| 142 | function WinHttpCrackUrl( const pwszUrl : LPCWSTR; |
| 143 | const dwUrlLength : DWORD; |
| 144 | const dwFlags : DWORD; |
| 145 | var urlComponents : URL_COMPONENTS |
| 146 | ) : BOOL; stdcall; |
| 147 | |
| 148 | function WinHttpCreateUrl( const UrlComponents : URL_COMPONENTS; |
| 149 | const dwFlags : DWORD; |
| 150 | const pwszUrl : LPCWSTR; |
| 151 | var pdwUrlLength : DWORD |
| 152 | ) : BOOL; stdcall; |
| 153 | |
| 154 | |
| 155 | const |
| 156 | // WinHttpOpen dwAccessType values |
| 157 | WINHTTP_ACCESS_TYPE_DEFAULT_PROXY = 0; |
| 158 | WINHTTP_ACCESS_TYPE_NO_PROXY = 1; |
| 159 | WINHTTP_ACCESS_TYPE_NAMED_PROXY = 3; |
| 160 | |
| 161 | // flags for WinHttpOpen(): |
| 162 | WINHTTP_FLAG_ASYNC = $10000000; // want async session, requires WinHttpSetStatusCallback() usage |
| 163 | |
| 164 | // ports |
| 165 | INTERNET_DEFAULT_PORT = 0; // use the protocol-specific default (80 or 443) |
| 166 | |
| 167 | // flags for WinHttpOpenRequest(): |
| 168 | WINHTTP_FLAG_SECURE = $00800000; // use SSL if applicable (HTTPS) |
| 169 | WINHTTP_FLAG_ESCAPE_PERCENT = $00000004; // if escaping enabled, escape percent as well |
| 170 | WINHTTP_FLAG_NULL_CODEPAGE = $00000008; // assume all symbols are ASCII, use fast convertion |
| 171 | WINHTTP_FLAG_BYPASS_PROXY_CACHE = $00000100; // add "pragma: no-cache" request header |
| 172 | WINHTTP_FLAG_REFRESH = WINHTTP_FLAG_BYPASS_PROXY_CACHE; |
| 173 | WINHTTP_FLAG_ESCAPE_DISABLE = $00000040; // disable escaping |
| 174 | WINHTTP_FLAG_ESCAPE_DISABLE_QUERY = $00000080; // if escaping enabled escape path part, but do not escape query |
| 175 | |
| 176 | // flags for WinHttpSendRequest(): |
| 177 | WINHTTP_NO_ADDITIONAL_HEADERS = nil; |
| 178 | WINHTTP_NO_REQUEST_DATA = nil; |
| 179 | |
| 180 | // WinHttpAddRequestHeaders() dwModifiers |
| 181 | WINHTTP_ADDREQ_INDEX_MASK = $0000FFFF; |
| 182 | WINHTTP_ADDREQ_FLAGS_MASK = $FFFF0000; |
| 183 | |
| 184 | WINHTTP_ADDREQ_FLAG_ADD_IF_NEW = $10000000; |
| 185 | WINHTTP_ADDREQ_FLAG_ADD = $20000000; |
| 186 | WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA = $40000000; |
| 187 | WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON = $01000000; |
| 188 | WINHTTP_ADDREQ_FLAG_COALESCE = WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA; |
| 189 | WINHTTP_ADDREQ_FLAG_REPLACE = $80000000; |
| 190 | |
| 191 | // URL functions |
| 192 | ICU_NO_ENCODE = $20000000; // Don't convert unsafe characters to escape sequence |
| 193 | ICU_DECODE = $10000000; // Convert %XX escape sequences to characters |
| 194 | ICU_NO_META = $08000000; // Don't convert .. etc. meta path sequences |
| 195 | ICU_ENCODE_SPACES_ONLY = $04000000; // Encode spaces only |
| 196 | ICU_BROWSER_MODE = $02000000; // Special encode/decode rules for browser |
| 197 | ICU_ENCODE_PERCENT = $00001000; // Encode any percent (ASCII25) |
| 198 | |
| 199 | ICU_ESCAPE = $80000000; // (un)escape URL characters |
| 200 | ICU_ESCAPE_AUTHORITY = $00002000; // causes InternetCreateUrlA to escape chars in authority components (user, pwd, host) |
| 201 | ICU_REJECT_USERPWD = $00004000; // rejects usrls whick have username/pwd sections |
| 202 | |
| 203 | INTERNET_SCHEME_HTTP = INTERNET_SCHEME(1); |
| 204 | INTERNET_SCHEME_HTTPS = INTERNET_SCHEME(2); |
| 205 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame^] | 206 | WINHTTP_NO_CLIENT_CERT_CONTEXT = nil; |
| 207 | |
| 208 | // options manifests for WinHttp{Query|Set}Option |
| 209 | WINHTTP_OPTION_CALLBACK = 1; |
| 210 | WINHTTP_OPTION_RESOLVE_TIMEOUT = 2; |
| 211 | WINHTTP_OPTION_CONNECT_TIMEOUT = 3; |
| 212 | WINHTTP_OPTION_CONNECT_RETRIES = 4; |
| 213 | WINHTTP_OPTION_SEND_TIMEOUT = 5; |
| 214 | WINHTTP_OPTION_RECEIVE_TIMEOUT = 6; |
| 215 | WINHTTP_OPTION_RECEIVE_RESPONSE_TIMEOUT = 7; |
| 216 | WINHTTP_OPTION_HANDLE_TYPE = 9; |
| 217 | WINHTTP_OPTION_READ_BUFFER_SIZE = 12; |
| 218 | WINHTTP_OPTION_WRITE_BUFFER_SIZE = 13; |
| 219 | WINHTTP_OPTION_PARENT_HANDLE = 21; |
| 220 | WINHTTP_OPTION_EXTENDED_ERROR = 24; |
| 221 | WINHTTP_OPTION_SECURITY_FLAGS = 31; |
| 222 | WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT = 32; |
| 223 | WINHTTP_OPTION_URL = 34; |
| 224 | WINHTTP_OPTION_SECURITY_KEY_BITNESS = 36; |
| 225 | WINHTTP_OPTION_PROXY = 38; |
| 226 | WINHTTP_OPTION_USER_AGENT = 41; |
| 227 | WINHTTP_OPTION_CONTEXT_VALUE = 45; |
| 228 | WINHTTP_OPTION_CLIENT_CERT_CONTEXT = 47; |
| 229 | WINHTTP_OPTION_REQUEST_PRIORITY = 58; |
| 230 | WINHTTP_OPTION_HTTP_VERSION = 59; |
| 231 | WINHTTP_OPTION_DISABLE_FEATURE = 63; |
| 232 | WINHTTP_OPTION_CODEPAGE = 68; |
| 233 | WINHTTP_OPTION_MAX_CONNS_PER_SERVER = 73; |
| 234 | WINHTTP_OPTION_MAX_CONNS_PER_1_0_SERVER = 74; |
| 235 | WINHTTP_OPTION_AUTOLOGON_POLICY = 77; |
| 236 | WINHTTP_OPTION_SERVER_CERT_CONTEXT = 78; |
| 237 | WINHTTP_OPTION_ENABLE_FEATURE = 79; |
| 238 | WINHTTP_OPTION_WORKER_THREAD_COUNT = 80; |
| 239 | WINHTTP_OPTION_PASSPORT_COBRANDING_TEXT = 81; |
| 240 | WINHTTP_OPTION_PASSPORT_COBRANDING_URL = 82; |
| 241 | WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH = 83; |
| 242 | WINHTTP_OPTION_SECURE_PROTOCOLS = 84; |
| 243 | WINHTTP_OPTION_ENABLETRACING = 85; |
| 244 | WINHTTP_OPTION_PASSPORT_SIGN_OUT = 86; |
| 245 | WINHTTP_OPTION_PASSPORT_RETURN_URL = 87; |
| 246 | WINHTTP_OPTION_REDIRECT_POLICY = 88; |
| 247 | WINHTTP_OPTION_MAX_HTTP_AUTOMATIC_REDIRECTS = 89; |
| 248 | WINHTTP_OPTION_MAX_HTTP_STATUS_CONTINUE = 90; |
| 249 | WINHTTP_OPTION_MAX_RESPONSE_HEADER_SIZE = 91; |
| 250 | WINHTTP_OPTION_MAX_RESPONSE_DRAIN_SIZE = 92; |
| 251 | WINHTTP_OPTION_CONNECTION_INFO = 93; |
| 252 | WINHTTP_OPTION_CLIENT_CERT_ISSUER_LIST = 94; |
| 253 | WINHTTP_OPTION_SPN = 96; |
| 254 | WINHTTP_OPTION_GLOBAL_PROXY_CREDS = 97; |
| 255 | WINHTTP_OPTION_GLOBAL_SERVER_CREDS = 98; |
| 256 | WINHTTP_OPTION_UNLOAD_NOTIFY_EVENT = 99; |
| 257 | WINHTTP_OPTION_REJECT_USERPWD_IN_URL = 100; |
| 258 | WINHTTP_OPTION_USE_GLOBAL_SERVER_CREDENTIALS = 101; |
| 259 | WINHTTP_OPTION_RECEIVE_PROXY_CONNECT_RESPONSE = 103; |
| 260 | WINHTTP_OPTION_IS_PROXY_CONNECT_RESPONSE = 104; |
| 261 | WINHTTP_OPTION_SERVER_SPN_USED = 106; |
| 262 | WINHTTP_OPTION_PROXY_SPN_USED = 107; |
| 263 | WINHTTP_OPTION_SERVER_CBT = 108; |
| 264 | // |
| 265 | WINHTTP_FIRST_OPTION = WINHTTP_OPTION_CALLBACK; |
| 266 | WINHTTP_LAST_OPTION = WINHTTP_OPTION_SERVER_CBT; |
| 267 | |
| 268 | WINHTTP_OPTION_USERNAME = $1000; |
| 269 | WINHTTP_OPTION_PASSWORD = $1001; |
| 270 | WINHTTP_OPTION_PROXY_USERNAME = $1002; |
| 271 | WINHTTP_OPTION_PROXY_PASSWORD = $1003; |
| 272 | |
| 273 | // manifest value for WINHTTP_OPTION_MAX_CONNS_PER_SERVER and WINHTTP_OPTION_MAX_CONNS_PER_1_0_SERVER |
| 274 | WINHTTP_CONNS_PER_SERVER_UNLIMITED = $FFFFFFFF; |
| 275 | |
| 276 | // values for WINHTTP_OPTION_AUTOLOGON_POLICY |
| 277 | WINHTTP_AUTOLOGON_SECURITY_LEVEL_MEDIUM = 0; |
| 278 | WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW = 1; |
| 279 | WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH = 2; |
| 280 | |
| 281 | WINHTTP_AUTOLOGON_SECURITY_LEVEL_DEFAULT = WINHTTP_AUTOLOGON_SECURITY_LEVEL_MEDIUM; |
| 282 | |
| 283 | // values for WINHTTP_OPTION_REDIRECT_POLICY |
| 284 | WINHTTP_OPTION_REDIRECT_POLICY_NEVER = 0; |
| 285 | WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP = 1; |
| 286 | WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS = 2; |
| 287 | |
| 288 | WINHTTP_OPTION_REDIRECT_POLICY_LAST = WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS; |
| 289 | WINHTTP_OPTION_REDIRECT_POLICY_DEFAULT = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP; |
| 290 | |
| 291 | WINHTTP_DISABLE_PASSPORT_AUTH = $00000000; |
| 292 | WINHTTP_ENABLE_PASSPORT_AUTH = $10000000; |
| 293 | WINHTTP_DISABLE_PASSPORT_KEYRING = $20000000; |
| 294 | WINHTTP_ENABLE_PASSPORT_KEYRING = $40000000; |
| 295 | |
| 296 | // values for WINHTTP_OPTION_DISABLE_FEATURE |
| 297 | WINHTTP_DISABLE_COOKIES = $00000001; |
| 298 | WINHTTP_DISABLE_REDIRECTS = $00000002; |
| 299 | WINHTTP_DISABLE_AUTHENTICATION = $00000004; |
| 300 | WINHTTP_DISABLE_KEEP_ALIVE = $00000008; |
| 301 | |
| 302 | // values for WINHTTP_OPTION_ENABLE_FEATURE |
| 303 | WINHTTP_ENABLE_SSL_REVOCATION = $00000001; |
| 304 | WINHTTP_ENABLE_SSL_REVERT_IMPERSONATION = $00000002; |
| 305 | |
| 306 | // values for WINHTTP_OPTION_SPN |
| 307 | WINHTTP_DISABLE_SPN_SERVER_PORT = $00000000; |
| 308 | WINHTTP_ENABLE_SPN_SERVER_PORT = $00000001; |
| 309 | WINHTTP_OPTION_SPN_MASK = WINHTTP_ENABLE_SPN_SERVER_PORT; |
| 310 | |
| 311 | // winhttp handle types |
| 312 | WINHTTP_HANDLE_TYPE_SESSION = 1; |
| 313 | WINHTTP_HANDLE_TYPE_CONNECT = 2; |
| 314 | WINHTTP_HANDLE_TYPE_REQUEST = 3; |
| 315 | |
| 316 | // values for auth schemes |
| 317 | WINHTTP_AUTH_SCHEME_BASIC = $00000001; |
| 318 | WINHTTP_AUTH_SCHEME_NTLM = $00000002; |
| 319 | WINHTTP_AUTH_SCHEME_PASSPORT = $00000004; |
| 320 | WINHTTP_AUTH_SCHEME_DIGEST = $00000008; |
| 321 | WINHTTP_AUTH_SCHEME_NEGOTIATE = $00000010; |
| 322 | |
| 323 | // WinHttp supported Authentication Targets |
| 324 | WINHTTP_AUTH_TARGET_SERVER = $00000000; |
| 325 | WINHTTP_AUTH_TARGET_PROXY = $00000001; |
| 326 | |
| 327 | // values for WINHTTP_OPTION_SECURITY_FLAGS |
| 328 | |
| 329 | // query only |
| 330 | SECURITY_FLAG_SECURE = $00000001; // can query only |
| 331 | SECURITY_FLAG_STRENGTH_WEAK = $10000000; |
| 332 | SECURITY_FLAG_STRENGTH_MEDIUM = $40000000; |
| 333 | SECURITY_FLAG_STRENGTH_STRONG = $20000000; |
| 334 | |
| 335 | // Secure connection error status flags |
| 336 | WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED = $00000001; |
| 337 | WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CERT = $00000002; |
| 338 | WINHTTP_CALLBACK_STATUS_FLAG_CERT_REVOKED = $00000004; |
| 339 | WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA = $00000008; |
| 340 | WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID = $00000010; |
| 341 | WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID = $00000020; |
| 342 | WINHTTP_CALLBACK_STATUS_FLAG_CERT_WRONG_USAGE = $00000040; |
| 343 | WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR = $80000000; |
| 344 | |
| 345 | WINHTTP_FLAG_SECURE_PROTOCOL_SSL2 = $00000008; |
| 346 | WINHTTP_FLAG_SECURE_PROTOCOL_SSL3 = $00000020; |
| 347 | WINHTTP_FLAG_SECURE_PROTOCOL_TLS1 = $00000080; |
| 348 | WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 = $00000200; |
| 349 | WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 = $00000800; |
| 350 | |
| 351 | // Note: SECURE_PROTOCOL_ALL does not include TLS1.1 and higher! |
| 352 | WINHTTP_FLAG_SECURE_PROTOCOL_ALL = WINHTTP_FLAG_SECURE_PROTOCOL_SSL2 |
| 353 | or WINHTTP_FLAG_SECURE_PROTOCOL_SSL3 |
| 354 | or WINHTTP_FLAG_SECURE_PROTOCOL_TLS1; |
| 355 | |
| 356 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 357 | const |
| 358 | WINHTTP_ERROR_BASE = 12000; |
| 359 | ERROR_WINHTTP_OUT_OF_HANDLES = WINHTTP_ERROR_BASE + 1; |
| 360 | ERROR_WINHTTP_TIMEOUT = WINHTTP_ERROR_BASE + 2; |
| 361 | ERROR_WINHTTP_INTERNAL_ERROR = WINHTTP_ERROR_BASE + 4; |
| 362 | ERROR_WINHTTP_INVALID_URL = WINHTTP_ERROR_BASE + 5; |
| 363 | ERROR_WINHTTP_UNRECOGNIZED_SCHEME = WINHTTP_ERROR_BASE + 6; |
| 364 | ERROR_WINHTTP_NAME_NOT_RESOLVED = WINHTTP_ERROR_BASE + 7; |
| 365 | ERROR_WINHTTP_INVALID_OPTION = WINHTTP_ERROR_BASE + 9; |
| 366 | ERROR_WINHTTP_OPTION_NOT_SETTABLE = WINHTTP_ERROR_BASE + 11; |
| 367 | ERROR_WINHTTP_SHUTDOWN = WINHTTP_ERROR_BASE + 12; |
| 368 | ERROR_WINHTTP_LOGIN_FAILURE = WINHTTP_ERROR_BASE + 15; |
| 369 | ERROR_WINHTTP_OPERATION_CANCELLED = WINHTTP_ERROR_BASE + 17; |
| 370 | ERROR_WINHTTP_INCORRECT_HANDLE_TYPE = WINHTTP_ERROR_BASE + 18; |
| 371 | ERROR_WINHTTP_INCORRECT_HANDLE_STATE = WINHTTP_ERROR_BASE + 19; |
| 372 | ERROR_WINHTTP_CANNOT_CONNECT = WINHTTP_ERROR_BASE + 29; |
| 373 | ERROR_WINHTTP_CONNECTION_ERROR = WINHTTP_ERROR_BASE + 30; |
| 374 | ERROR_WINHTTP_RESEND_REQUEST = WINHTTP_ERROR_BASE + 32; |
| 375 | ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED = WINHTTP_ERROR_BASE + 44; |
| 376 | ERROR_WINHTTP_CANNOT_CALL_BEFORE_OPEN = WINHTTP_ERROR_BASE + 100; |
| 377 | ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND = WINHTTP_ERROR_BASE + 101; |
| 378 | ERROR_WINHTTP_CANNOT_CALL_AFTER_SEND = WINHTTP_ERROR_BASE + 102; |
| 379 | ERROR_WINHTTP_CANNOT_CALL_AFTER_OPEN = WINHTTP_ERROR_BASE + 103; |
| 380 | ERROR_WINHTTP_HEADER_NOT_FOUND = WINHTTP_ERROR_BASE + 150; |
| 381 | ERROR_WINHTTP_INVALID_SERVER_RESPONSE = WINHTTP_ERROR_BASE + 152; |
| 382 | ERROR_WINHTTP_INVALID_HEADER = WINHTTP_ERROR_BASE + 153; |
| 383 | ERROR_WINHTTP_INVALID_QUERY_REQUEST = WINHTTP_ERROR_BASE + 154; |
| 384 | ERROR_WINHTTP_HEADER_ALREADY_EXISTS = WINHTTP_ERROR_BASE + 155; |
| 385 | ERROR_WINHTTP_REDIRECT_FAILED = WINHTTP_ERROR_BASE + 156; |
| 386 | ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR = WINHTTP_ERROR_BASE + 178; |
| 387 | ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT = WINHTTP_ERROR_BASE + 166; |
| 388 | ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT = WINHTTP_ERROR_BASE + 167; |
| 389 | ERROR_WINHTTP_NOT_INITIALIZED = WINHTTP_ERROR_BASE + 172; |
| 390 | ERROR_WINHTTP_SECURE_FAILURE = WINHTTP_ERROR_BASE + 175; |
| 391 | |
| 392 | // Certificate security errors. Additional information is provided |
| 393 | // via the WINHTTP_CALLBACK_STATUS_SECURE_FAILURE callback notification. |
| 394 | ERROR_WINHTTP_SECURE_CERT_DATE_INVALID = WINHTTP_ERROR_BASE + 37; |
| 395 | ERROR_WINHTTP_SECURE_CERT_CN_INVALID = WINHTTP_ERROR_BASE + 38; |
| 396 | ERROR_WINHTTP_SECURE_INVALID_CA = WINHTTP_ERROR_BASE + 45; |
| 397 | ERROR_WINHTTP_SECURE_CERT_REV_FAILED = WINHTTP_ERROR_BASE + 57; |
| 398 | ERROR_WINHTTP_SECURE_CHANNEL_ERROR = WINHTTP_ERROR_BASE + 157; |
| 399 | ERROR_WINHTTP_SECURE_INVALID_CERT = WINHTTP_ERROR_BASE + 169; |
| 400 | ERROR_WINHTTP_SECURE_CERT_REVOKED = WINHTTP_ERROR_BASE + 170; |
| 401 | ERROR_WINHTTP_SECURE_CERT_WRONG_USAGE = WINHTTP_ERROR_BASE + 179; |
| 402 | |
| 403 | ERROR_WINHTTP_AUTODETECTION_FAILED = WINHTTP_ERROR_BASE + 180; |
| 404 | ERROR_WINHTTP_HEADER_COUNT_EXCEEDED = WINHTTP_ERROR_BASE + 181; |
| 405 | ERROR_WINHTTP_HEADER_SIZE_OVERFLOW = WINHTTP_ERROR_BASE + 182; |
| 406 | ERROR_WINHTTP_CHUNKED_ENCODING_HEADER_SIZE_OVERFLOW = WINHTTP_ERROR_BASE + 183; |
| 407 | ERROR_WINHTTP_RESPONSE_DRAIN_OVERFLOW = WINHTTP_ERROR_BASE + 184; |
| 408 | ERROR_WINHTTP_CLIENT_CERT_NO_PRIVATE_KEY = WINHTTP_ERROR_BASE + 185; |
| 409 | ERROR_WINHTTP_CLIENT_CERT_NO_ACCESS_PRIVATE_KEY = WINHTTP_ERROR_BASE + 186; |
| 410 | |
| 411 | |
| 412 | const |
| 413 | WINHTTP_THRIFT_DEFAULTS = WINHTTP_FLAG_NULL_CODEPAGE |
| 414 | or WINHTTP_FLAG_BYPASS_PROXY_CACHE |
| 415 | or WINHTTP_FLAG_ESCAPE_DISABLE; |
| 416 | |
| 417 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame^] | 418 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 419 | type |
| 420 | IWinHTTPRequest = interface |
| 421 | ['{35C6D9D4-FDCE-42C6-B84C-9294E6FB904C}'] |
| 422 | function Handle : HINTERNET; |
| 423 | function AddRequestHeader( const aHeader : string; const addflag : DWORD = WINHTTP_ADDREQ_FLAG_ADD) : Boolean; |
| 424 | function SetTimeouts( const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32) : Boolean; |
| 425 | function SendRequest( const pBuf : Pointer; const dwBytes : DWORD; const dwExtra : DWORD = 0) : Boolean; |
| 426 | function WriteExtraData( const pBuf : Pointer; const dwBytes : DWORD) : DWORD; |
| 427 | function FlushAndReceiveResponse : Boolean; |
| 428 | function ReadData( const dwRead : DWORD) : TBytes; overload; |
| 429 | function ReadData( const pBuf : Pointer; const dwRead : DWORD) : DWORD; overload; |
| 430 | end; |
| 431 | |
| 432 | IWinHTTPConnection = interface |
| 433 | ['{1C4F78B5-1525-4788-B638-A0E41BCF4D43}'] |
| 434 | function Handle : HINTERNET; |
| 435 | function OpenRequest( const secure : Boolean; const aVerb, aObjName, aAcceptTypes : UnicodeString) : IWinHTTPRequest; |
| 436 | end; |
| 437 | |
| 438 | IWinHTTPSession = interface |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame^] | 439 | ['{261ADCB7-5465-4407-8840-468C17F009F0}'] |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 440 | function Handle : HINTERNET; |
| 441 | function Connect( const aHostName : UnicodeString; const aPort : INTERNET_PORT = INTERNET_DEFAULT_PORT) : IWinHTTPConnection; |
| 442 | function SetTimeouts( const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32) : Boolean; |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame^] | 443 | function EnableSecureProtocols( const aFlagSet : DWORD) : Boolean; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 444 | end; |
| 445 | |
| 446 | IWinHTTPUrl = interface |
| 447 | ['{78BE977C-4171-4AF5-A250-FD2890205E63}'] |
| 448 | // url parts getter |
| 449 | function GetScheme : UnicodeString; |
| 450 | function GetNumScheme : INTERNET_SCHEME; |
| 451 | function GetHostName : UnicodeString; |
| 452 | function GetPort : INTERNET_PORT; |
| 453 | function GetUserName : UnicodeString; |
| 454 | function GetPassword : UnicodeString; |
| 455 | function GetUrlPath : UnicodeString; |
| 456 | function GetExtraInfo : UnicodeString; |
| 457 | |
| 458 | // url parts setter |
| 459 | procedure SetScheme( const value : UnicodeString); |
| 460 | procedure SetHostName ( const value : UnicodeString); |
| 461 | procedure SetPort( const value : INTERNET_PORT); |
| 462 | procedure SetUserName( const value : UnicodeString); |
| 463 | procedure SetPassword( const value : UnicodeString); |
| 464 | procedure SetUrlPath( const value : UnicodeString); |
| 465 | procedure SetExtraInfo( const value : UnicodeString); |
| 466 | |
| 467 | // url as a whole |
| 468 | function BuildUrl : UnicodeString; |
| 469 | procedure CrackUrl( const value : UnicodeString); |
| 470 | |
| 471 | // url parts |
| 472 | property Scheme : UnicodeString read GetScheme write SetScheme; |
| 473 | property NumScheme : INTERNET_SCHEME read GetNumScheme; // readonly |
| 474 | property HostName : UnicodeString read GetHostName write SetHostName; |
| 475 | property Port : INTERNET_PORT read GetPort write SetPort; |
| 476 | property UserName : UnicodeString read GetUserName write SetUserName; |
| 477 | property Password : UnicodeString read GetPassword write SetPassword; |
| 478 | property UrlPath : UnicodeString read GetUrlPath write SetUrlPath; |
| 479 | property ExtraInfo : UnicodeString read GetExtraInfo write SetExtraInfo; |
| 480 | |
| 481 | // url as a whole |
| 482 | property CompleteURL : UnicodeString read BuildUrl write CrackUrl; |
| 483 | end; |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | type |
| 489 | TWinHTTPHandleObjectImpl = class( TInterfacedObject) |
| 490 | strict protected |
| 491 | FHandle : HINTERNET; |
| 492 | function Handle : HINTERNET; |
| 493 | public |
| 494 | constructor Create( const aHandle : HINTERNET); |
| 495 | destructor Destroy; override; |
| 496 | end; |
| 497 | |
| 498 | |
| 499 | TWinHTTPSessionImpl = class( TWinHTTPHandleObjectImpl, IWinHTTPSession) |
| 500 | strict protected |
| 501 | |
| 502 | // IWinHTTPSession |
| 503 | function Connect( const aHostName : UnicodeString; const aPort : INTERNET_PORT = INTERNET_DEFAULT_PORT) : IWinHTTPConnection; |
| 504 | function SetTimeouts( const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32) : Boolean; |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame^] | 505 | function EnableSecureProtocols( const aFlagSet : DWORD) : Boolean; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 506 | public |
| 507 | constructor Create( const aAgent : UnicodeString; |
| 508 | const aAccessType : DWORD = WINHTTP_ACCESS_TYPE_DEFAULT_PROXY; |
| 509 | const aProxy : UnicodeString = ''; |
| 510 | const aProxyBypass : UnicodeString = ''; |
| 511 | const aFlags : DWORD = 0); |
| 512 | destructor Destroy; override; |
| 513 | end; |
| 514 | |
| 515 | |
| 516 | TWinHTTPConnectionImpl = class( TWinHTTPHandleObjectImpl, IWinHTTPConnection) |
| 517 | strict protected |
| 518 | FSession : IWinHTTPSession; |
| 519 | |
| 520 | // IWinHTTPConnection |
| 521 | function OpenRequest( const secure : Boolean; const aVerb, aObjName, aAcceptTypes : UnicodeString) : IWinHTTPRequest; |
| 522 | |
| 523 | public |
| 524 | constructor Create( const aSession : IWinHTTPSession; const aHostName : UnicodeString; const aPort : INTERNET_PORT); |
| 525 | destructor Destroy; override; |
| 526 | end; |
| 527 | |
| 528 | |
| 529 | TAcceptTypesArray = array of string; |
| 530 | |
| 531 | TWinHTTPRequestImpl = class( TWinHTTPHandleObjectImpl, IWinHTTPRequest) |
| 532 | strict protected |
| 533 | FConnection : IWinHTTPConnection; |
| 534 | |
| 535 | // IWinHTTPRequest |
| 536 | function AddRequestHeader( const aHeader : string; const addflag : DWORD = WINHTTP_ADDREQ_FLAG_ADD) : Boolean; |
| 537 | function SetTimeouts( const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32) : Boolean; |
| 538 | function SendRequest( const pBuf : Pointer; const dwBytes : DWORD; const dwExtra : DWORD = 0) : Boolean; |
| 539 | function WriteExtraData( const pBuf : Pointer; const dwBytes : DWORD) : DWORD; |
| 540 | function FlushAndReceiveResponse : Boolean; |
| 541 | function ReadData( const dwRead : DWORD) : TBytes; overload; |
| 542 | function ReadData( const pBuf : Pointer; const dwRead : DWORD) : DWORD; overload; |
| 543 | |
| 544 | public |
| 545 | constructor Create( const aConnection : IWinHTTPConnection; |
| 546 | const aVerb, aObjName : UnicodeString; |
| 547 | const aVersion : UnicodeString = ''; |
| 548 | const aReferrer : UnicodeString = ''; |
| 549 | const aAcceptTypes : UnicodeString = '*/*'; |
| 550 | const aFlags : DWORD = WINHTTP_THRIFT_DEFAULTS |
| 551 | ); |
| 552 | |
| 553 | destructor Destroy; override; |
| 554 | end; |
| 555 | |
| 556 | |
| 557 | TWinHTTPUrlImpl = class( TInterfacedObject, IWinHTTPUrl) |
| 558 | strict private |
| 559 | FScheme : UnicodeString; |
| 560 | FNumScheme : INTERNET_SCHEME; |
| 561 | FHostName : UnicodeString; |
| 562 | FPort : INTERNET_PORT; |
| 563 | FUserName : UnicodeString; |
| 564 | FPassword : UnicodeString; |
| 565 | FUrlPath : UnicodeString; |
| 566 | FExtraInfo : UnicodeString; |
| 567 | |
| 568 | strict protected |
| 569 | // url parts getter |
| 570 | function GetScheme : UnicodeString; |
| 571 | function GetNumScheme : INTERNET_SCHEME; |
| 572 | function GetHostName : UnicodeString; |
| 573 | function GetPort : INTERNET_PORT; |
| 574 | function GetUserName : UnicodeString; |
| 575 | function GetPassword : UnicodeString; |
| 576 | function GetUrlPath : UnicodeString; |
| 577 | function GetExtraInfo : UnicodeString; |
| 578 | |
| 579 | // url parts setter |
| 580 | procedure SetScheme( const value : UnicodeString); |
| 581 | procedure SetHostName ( const value : UnicodeString); |
| 582 | procedure SetPort( const value : INTERNET_PORT); |
| 583 | procedure SetUserName( const value : UnicodeString); |
| 584 | procedure SetPassword( const value : UnicodeString); |
| 585 | procedure SetUrlPath( const value : UnicodeString); |
| 586 | procedure SetExtraInfo( const value : UnicodeString); |
| 587 | |
| 588 | // url as a whole |
| 589 | function BuildUrl : UnicodeString; |
| 590 | procedure CrackUrl( const value : UnicodeString); |
| 591 | |
| 592 | public |
| 593 | constructor Create( const aUri : UnicodeString); |
| 594 | destructor Destroy; override; |
| 595 | end; |
| 596 | |
| 597 | |
| 598 | EWinHTTPException = class(Exception); |
| 599 | |
| 600 | implementation |
| 601 | |
| 602 | const WINHTTP_DLL = 'WinHTTP.dll'; |
| 603 | |
| 604 | function WinHttpCloseHandle; stdcall; external WINHTTP_DLL; |
| 605 | function WinHttpOpen; stdcall; external WINHTTP_DLL; |
| 606 | function WinHttpConnect; stdcall; external WINHTTP_DLL; |
| 607 | function WinHttpOpenRequest; stdcall; external WINHTTP_DLL; |
| 608 | function WinHttpSendRequest; stdcall; external WINHTTP_DLL; |
| 609 | function WinHttpSetTimeouts; stdcall; external WINHTTP_DLL; |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame^] | 610 | function WinHttpQueryOption; stdcall; external WINHTTP_DLL; |
| 611 | function WinHttpSetOption; stdcall; external WINHTTP_DLL; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 612 | function WinHttpAddRequestHeaders; stdcall; external WINHTTP_DLL; |
| 613 | function WinHttpWriteData; stdcall; external WINHTTP_DLL; |
| 614 | function WinHttpReceiveResponse; stdcall; external WINHTTP_DLL; |
| 615 | function WinHttpQueryHeaders; stdcall; external WINHTTP_DLL; |
| 616 | function WinHttpQueryDataAvailable; stdcall; external WINHTTP_DLL; |
| 617 | function WinHttpReadData; stdcall; external WINHTTP_DLL; |
| 618 | function WinHttpCrackUrl; stdcall; external WINHTTP_DLL; |
| 619 | function WinHttpCreateUrl; stdcall; external WINHTTP_DLL; |
| 620 | |
| 621 | |
| 622 | { TWinHTTPHandleObjectImpl } |
| 623 | |
| 624 | constructor TWinHTTPHandleObjectImpl.Create( const aHandle : HINTERNET); |
| 625 | begin |
| 626 | inherited Create; |
| 627 | FHandle := aHandle; |
| 628 | |
| 629 | if FHandle = nil |
| 630 | then raise EWinHTTPException.Create('Invalid handle'); |
| 631 | end; |
| 632 | |
| 633 | |
| 634 | destructor TWinHTTPHandleObjectImpl.Destroy; |
| 635 | begin |
| 636 | try |
| 637 | if Assigned(FHandle) then begin |
| 638 | WinHttpCloseHandle(FHandle); |
| 639 | FHandle := nil; |
| 640 | end; |
| 641 | |
| 642 | finally |
| 643 | inherited Destroy; |
| 644 | end; |
| 645 | end; |
| 646 | |
| 647 | |
| 648 | function TWinHTTPHandleObjectImpl.Handle : HINTERNET; |
| 649 | begin |
| 650 | result := FHandle; |
| 651 | end; |
| 652 | |
| 653 | |
| 654 | { TWinHTTPSessionImpl } |
| 655 | |
| 656 | |
| 657 | constructor TWinHTTPSessionImpl.Create( const aAgent : UnicodeString; const aAccessType : DWORD; |
| 658 | const aProxy, aProxyBypass : UnicodeString; const aFlags : DWORD); |
| 659 | var handle : HINTERNET; |
| 660 | begin |
| 661 | handle := WinHttpOpen( PWideChar(aAgent), aAccessType, |
| 662 | PWideChar(Pointer(aProxy)), // may be nil |
| 663 | PWideChar(Pointer(aProxyBypass)), // may be nil |
| 664 | aFlags); |
Jens Geyer | 14a9a12 | 2019-05-09 23:29:24 +0200 | [diff] [blame] | 665 | if handle = nil then RaiseLastOSError; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 666 | inherited Create( handle); |
| 667 | end; |
| 668 | |
| 669 | |
| 670 | destructor TWinHTTPSessionImpl.Destroy; |
| 671 | begin |
| 672 | inherited Destroy; |
| 673 | // add code here |
| 674 | end; |
| 675 | |
| 676 | |
| 677 | function TWinHTTPSessionImpl.Connect( const aHostName : UnicodeString; const aPort : INTERNET_PORT) : IWinHTTPConnection; |
| 678 | begin |
| 679 | result := TWinHTTPConnectionImpl.Create( Self, aHostName, aPort); |
| 680 | end; |
| 681 | |
| 682 | |
| 683 | function TWinHTTPSessionImpl.SetTimeouts( const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32) : Boolean; |
| 684 | begin |
| 685 | result := WinHttpSetTimeouts( FHandle, aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout); |
| 686 | end; |
| 687 | |
| 688 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame^] | 689 | function TWinHTTPSessionImpl.EnableSecureProtocols( const aFlagSet : DWORD) : Boolean; |
| 690 | var dwSize : DWORD; |
| 691 | begin |
| 692 | dwSize := SizeOf(aFlagSet); |
| 693 | result := WinHttpSetOption( Handle, WINHTTP_OPTION_SECURE_PROTOCOLS, @aFlagset, dwSize); |
| 694 | end; |
| 695 | |
| 696 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 697 | { TWinHTTPConnectionImpl } |
| 698 | |
| 699 | constructor TWinHTTPConnectionImpl.Create( const aSession : IWinHTTPSession; const aHostName : UnicodeString; const aPort : INTERNET_PORT); |
| 700 | var handle : HINTERNET; |
| 701 | begin |
| 702 | FSession := aSession; |
| 703 | handle := WinHttpConnect( FSession.Handle, PWideChar(aHostName), aPort, 0); |
Jens Geyer | 14a9a12 | 2019-05-09 23:29:24 +0200 | [diff] [blame] | 704 | if handle = nil then RaiseLastOSError; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 705 | inherited Create( handle); |
| 706 | end; |
| 707 | |
| 708 | |
| 709 | destructor TWinHTTPConnectionImpl.Destroy; |
| 710 | begin |
| 711 | inherited Destroy; |
| 712 | FSession := nil; |
| 713 | end; |
| 714 | |
| 715 | |
| 716 | function TWinHTTPConnectionImpl.OpenRequest( const secure : Boolean; const aVerb, aObjName, aAcceptTypes : UnicodeString) : IWinHTTPRequest; |
| 717 | var dwFlags : DWORD; |
| 718 | begin |
| 719 | dwFlags := WINHTTP_THRIFT_DEFAULTS; |
| 720 | if secure |
| 721 | then dwFlags := dwFlags or WINHTTP_FLAG_SECURE |
| 722 | else dwFlags := dwFlags and not WINHTTP_FLAG_SECURE; |
| 723 | |
| 724 | result := TWinHTTPRequestImpl.Create( Self, aVerb, aObjName, '', '', aAcceptTypes, dwFlags); |
| 725 | end; |
| 726 | |
| 727 | |
| 728 | { TWinHTTPRequestImpl } |
| 729 | |
| 730 | constructor TWinHTTPRequestImpl.Create( const aConnection : IWinHTTPConnection; |
| 731 | const aVerb, aObjName, aVersion, aReferrer : UnicodeString; |
| 732 | const aAcceptTypes : UnicodeString; |
| 733 | const aFlags : DWORD |
| 734 | ); |
| 735 | var handle : HINTERNET; |
| 736 | accept : array[0..1] of PWideChar; |
| 737 | begin |
| 738 | FConnection := aConnection; |
| 739 | |
| 740 | accept[0] := PWideChar(aAcceptTypes); |
| 741 | accept[1] := nil; |
| 742 | |
| 743 | handle := WinHttpOpenRequest( FConnection.Handle, |
| 744 | PWideChar(UpperCase(aVerb)), |
| 745 | PWideChar(aObjName), |
| 746 | PWideChar(aVersion), |
| 747 | PWideChar(aReferrer), |
| 748 | @accept, |
| 749 | aFlags); |
Jens Geyer | 14a9a12 | 2019-05-09 23:29:24 +0200 | [diff] [blame] | 750 | if handle = nil then RaiseLastOSError; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 751 | inherited Create( handle); |
| 752 | end; |
| 753 | |
| 754 | |
| 755 | destructor TWinHTTPRequestImpl.Destroy; |
| 756 | begin |
| 757 | inherited Destroy; |
| 758 | FConnection := nil; |
| 759 | end; |
| 760 | |
| 761 | |
| 762 | function TWinHTTPRequestImpl.SetTimeouts( const aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout : Int32) : Boolean; |
| 763 | begin |
| 764 | result := WinHttpSetTimeouts( FHandle, aResolveTimeout, aConnectTimeout, aSendTimeout, aReceiveTimeout); |
| 765 | end; |
| 766 | |
| 767 | |
| 768 | function TWinHTTPRequestImpl.AddRequestHeader( const aHeader : string; const addflag : DWORD) : Boolean; |
| 769 | begin |
| 770 | result := WinHttpAddRequestHeaders( FHandle, PWideChar(aHeader), DWORD(-1), addflag); |
| 771 | end; |
| 772 | |
| 773 | |
| 774 | function TWinHTTPRequestImpl.SendRequest( const pBuf : Pointer; const dwBytes, dwExtra : DWORD) : Boolean; |
| 775 | begin |
| 776 | result := WinHttpSendRequest( FHandle, |
| 777 | WINHTTP_NO_ADDITIONAL_HEADERS, 0, |
| 778 | pBuf, dwBytes, // number of bytes in pBuf |
| 779 | dwBytes + dwExtra, // becomes the Content-Length |
| 780 | nil); // context for async operations |
| 781 | end; |
| 782 | |
| 783 | |
| 784 | function TWinHTTPRequestImpl.WriteExtraData( const pBuf : Pointer; const dwBytes : DWORD) : DWORD; |
| 785 | begin |
| 786 | if not WinHttpWriteData( FHandle, pBuf, dwBytes, result) |
| 787 | then result := 0; |
| 788 | end; |
| 789 | |
| 790 | |
| 791 | function TWinHTTPRequestImpl.FlushAndReceiveResponse : Boolean; |
| 792 | begin |
| 793 | result := WinHttpReceiveResponse( FHandle, nil); |
| 794 | end; |
| 795 | |
| 796 | |
| 797 | function TWinHTTPRequestImpl.ReadData( const dwRead : DWORD) : TBytes; |
| 798 | var dwAvailable, dwReceived : DWORD; |
| 799 | begin |
| 800 | if WinHttpQueryDataAvailable( FHandle, dwAvailable) |
| 801 | then dwAvailable := Min( dwRead, dwAvailable) |
| 802 | else dwAvailable := 0; |
| 803 | |
| 804 | SetLength( result, dwAvailable); |
| 805 | if dwAvailable = 0 then Exit; |
| 806 | |
| 807 | if WinHttpReadData( FHandle, @result[0], Length(result), dwReceived) |
| 808 | then SetLength( result, dwReceived) |
| 809 | else SetLength( result, 0); |
| 810 | end; |
| 811 | |
| 812 | |
| 813 | function TWinHTTPRequestImpl.ReadData( const pBuf : Pointer; const dwRead : DWORD) : DWORD; |
| 814 | var dwAvailable : DWORD; |
| 815 | begin |
| 816 | if WinHttpQueryDataAvailable( FHandle, dwAvailable) |
| 817 | then dwAvailable := Min( dwRead, dwAvailable) |
| 818 | else dwAvailable := 0; |
| 819 | |
| 820 | if (dwAvailable = 0) |
| 821 | or not WinHttpReadData( FHandle, pBuf, dwAvailable, result) |
| 822 | then result := 0; |
| 823 | end; |
| 824 | |
| 825 | |
| 826 | { TWinHTTPUrlImpl } |
| 827 | |
| 828 | constructor TWinHTTPUrlImpl.Create(const aUri: UnicodeString); |
| 829 | begin |
| 830 | inherited Create; |
| 831 | CrackUrl( aUri) |
| 832 | end; |
| 833 | |
| 834 | |
| 835 | destructor TWinHTTPUrlImpl.Destroy; |
| 836 | begin |
| 837 | inherited Destroy; |
| 838 | end; |
| 839 | |
| 840 | |
| 841 | procedure TWinHTTPUrlImpl.CrackURL( const value : UnicodeString); |
| 842 | const FLAGS = 0; // no special operations, leave components as-is |
| 843 | var components : URL_COMPONENTS; |
| 844 | begin |
| 845 | FillChar(components, SizeOf(components), 0); |
| 846 | components.dwStructSize := SizeOf(components); |
| 847 | |
| 848 | if value <> '' then begin |
| 849 | { For the WinHttpCrackUrl function, [...] if the pointer member is NULL but the |
| 850 | length member is not zero, both the pointer and length members are returned. } |
| 851 | components.dwSchemeLength := DWORD(-1); |
| 852 | components.dwHostNameLength := DWORD(-1); |
| 853 | components.dwUserNameLength := DWORD(-1); |
| 854 | components.dwPasswordLength := DWORD(-1); |
| 855 | components.dwUrlPathLength := DWORD(-1); |
| 856 | components.dwExtraInfoLength := DWORD(-1); |
| 857 | |
| 858 | WinHttpCrackUrl( PWideChar(value), Length(value), FLAGS, components); |
| 859 | end; |
| 860 | |
| 861 | FNumScheme := components.nScheme; |
| 862 | FPort := components.nPort; |
| 863 | SetString( FScheme, components.lpszScheme, components.dwSchemeLength); |
| 864 | SetString( FHostName, components.lpszHostName, components.dwHostNameLength); |
| 865 | SetString( FUserName, components.lpszUserName, components.dwUserNameLength); |
| 866 | SetString( FPassword, components.lpszPassword, components.dwPasswordLength); |
| 867 | SetString( FUrlPath, components.lpszUrlPath, components.dwUrlPathLength); |
| 868 | SetString( FExtraInfo, components.lpszExtraInfo, components.dwExtraInfoLength); |
| 869 | end; |
| 870 | |
| 871 | |
| 872 | function TWinHTTPUrlImpl.BuildUrl : UnicodeString; |
| 873 | const FLAGS = 0; // no special operations, leave components as-is |
| 874 | var components : URL_COMPONENTS; |
| 875 | dwChars : DWORD; |
| 876 | begin |
| 877 | FillChar(components, SizeOf(components), 0); |
| 878 | components.dwStructSize := SizeOf(components); |
| 879 | components.lpszScheme := PWideChar(FScheme); |
| 880 | components.dwSchemeLength := Length(FScheme); |
| 881 | components.lpszHostName := PWideChar(FHostName); |
| 882 | components.dwHostNameLength := Length(FHostName); |
| 883 | components.nPort := FPort; |
| 884 | components.lpszUserName := PWideChar(FUserName); |
| 885 | components.dwUserNameLength := Length(FUserName); |
| 886 | components.lpszPassword := PWideChar(FPassword); |
| 887 | components.dwPasswordLength := Length(FPassword); |
| 888 | components.lpszUrlPath := PWideChar(FUrlPath); |
| 889 | components.dwUrlPathLength := Length(FUrlPath); |
| 890 | components.lpszExtraInfo := PWideChar(FExtraInfo); |
| 891 | components.dwExtraInfoLength := Length(FExtraInfo); |
| 892 | |
| 893 | WinHttpCreateUrl( components, FLAGS, nil, dwChars); |
| 894 | if dwChars = 0 |
| 895 | then result := '' |
| 896 | else begin |
| 897 | SetLength( result, dwChars + 1); |
| 898 | WinHttpCreateUrl( components, FLAGS, @result[1], dwChars); |
| 899 | SetLength( result, dwChars); // cut off terminating #0 |
| 900 | end; |
| 901 | end; |
| 902 | |
| 903 | |
| 904 | function TWinHTTPUrlImpl.GetExtraInfo: UnicodeString; |
| 905 | begin |
| 906 | result := FExtraInfo; |
| 907 | end; |
| 908 | |
| 909 | function TWinHTTPUrlImpl.GetHostName: UnicodeString; |
| 910 | begin |
| 911 | result := FHostName; |
| 912 | end; |
| 913 | |
| 914 | function TWinHTTPUrlImpl.GetNumScheme: INTERNET_SCHEME; |
| 915 | begin |
| 916 | result := FNumScheme; |
| 917 | end; |
| 918 | |
| 919 | function TWinHTTPUrlImpl.GetPassword: UnicodeString; |
| 920 | begin |
| 921 | result := FPassword; |
| 922 | end; |
| 923 | |
| 924 | function TWinHTTPUrlImpl.GetPort: INTERNET_PORT; |
| 925 | begin |
| 926 | result := FPort; |
| 927 | end; |
| 928 | |
| 929 | function TWinHTTPUrlImpl.GetScheme: UnicodeString; |
| 930 | begin |
| 931 | result := FScheme; |
| 932 | end; |
| 933 | |
| 934 | function TWinHTTPUrlImpl.GetUrlPath: UnicodeString; |
| 935 | begin |
| 936 | result := FUrlPath; |
| 937 | end; |
| 938 | |
| 939 | function TWinHTTPUrlImpl.GetUserName: UnicodeString; |
| 940 | begin |
| 941 | result := FUserName; |
| 942 | end; |
| 943 | |
| 944 | procedure TWinHTTPUrlImpl.SetExtraInfo(const value: UnicodeString); |
| 945 | begin |
| 946 | FExtraInfo := value; |
| 947 | end; |
| 948 | |
| 949 | procedure TWinHTTPUrlImpl.SetHostName(const value: UnicodeString); |
| 950 | begin |
| 951 | FHostName := value; |
| 952 | end; |
| 953 | |
| 954 | procedure TWinHTTPUrlImpl.SetPassword(const value: UnicodeString); |
| 955 | begin |
| 956 | FPassword := value; |
| 957 | end; |
| 958 | |
| 959 | procedure TWinHTTPUrlImpl.SetPort(const value: INTERNET_PORT); |
| 960 | begin |
| 961 | FPort := value; |
| 962 | end; |
| 963 | |
| 964 | procedure TWinHTTPUrlImpl.SetScheme(const value: UnicodeString); |
| 965 | begin |
| 966 | FScheme := value; |
| 967 | end; |
| 968 | |
| 969 | procedure TWinHTTPUrlImpl.SetUrlPath(const value: UnicodeString); |
| 970 | begin |
| 971 | FUrlPath := value; |
| 972 | end; |
| 973 | |
| 974 | procedure TWinHTTPUrlImpl.SetUserName(const value: UnicodeString); |
| 975 | begin |
| 976 | FUserName := value; |
| 977 | end; |
| 978 | |
| 979 | |
| 980 | end. |
| 981 | |