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.Transport.WinHTTP; |
| 20 | |
| 21 | {$I Thrift.Defines.inc} |
| 22 | {$SCOPEDENUMS ON} |
| 23 | |
| 24 | interface |
| 25 | |
| 26 | uses |
| 27 | Classes, |
| 28 | SysUtils, |
| 29 | Math, |
| 30 | Generics.Collections, |
| 31 | Thrift.Collections, |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 32 | Thrift.Configuration, |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 33 | Thrift.Transport, |
| 34 | Thrift.Exception, |
| 35 | Thrift.Utils, |
| 36 | Thrift.WinHTTP, |
| 37 | Thrift.Stream; |
| 38 | |
| 39 | type |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 40 | TWinHTTPClientImpl = class( TEndpointTransportBase, IHTTPClient) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 41 | strict private |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 42 | FUri : string; |
| 43 | FInputStream : IThriftStream; |
Jens Geyer | f726ae3 | 2021-06-04 11:17:26 +0200 | [diff] [blame] | 44 | FOutputMemoryStream : TThriftMemoryStream; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 45 | FDnsResolveTimeout : Integer; |
| 46 | FConnectionTimeout : Integer; |
| 47 | FSendTimeout : Integer; |
| 48 | FReadTimeout : Integer; |
| 49 | FCustomHeaders : IThriftDictionary<string,string>; |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 50 | FSecureProtocols : TSecureProtocols; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 51 | |
| 52 | function CreateRequest: IWinHTTPRequest; |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 53 | function SecureProtocolsAsWinHTTPFlags : Cardinal; |
Jens Geyer | 72c8111 | 2025-03-10 21:46:20 +0100 | [diff] [blame] | 54 | class procedure EnsureSuccessHttpStatus( const aRequest : IWinHTTPRequest); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 55 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 56 | strict private |
Jens Geyer | 19fdca8 | 2019-06-12 22:09:05 +0200 | [diff] [blame] | 57 | type |
| 58 | TErrorInfo = ( SplitUrl, WinHTTPSession, WinHTTPConnection, WinHTTPRequest, RequestSetup, AutoProxy ); |
| 59 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 60 | THTTPResponseStream = class( TThriftStreamImpl) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 61 | strict private |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 62 | FRequest : IWinHTTPRequest; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 63 | strict protected |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 64 | procedure Write( const pBuf : Pointer; offset: Integer; count: Integer); override; |
| 65 | function Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; override; |
| 66 | procedure Open; override; |
| 67 | procedure Close; override; |
| 68 | procedure Flush; override; |
| 69 | function IsOpen: Boolean; override; |
| 70 | function ToArray: TBytes; override; |
| 71 | public |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 72 | constructor Create( const aRequest : IWinHTTPRequest); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 73 | destructor Destroy; override; |
| 74 | end; |
| 75 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 76 | strict protected |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 77 | function GetIsOpen: Boolean; override; |
| 78 | procedure Open(); override; |
| 79 | procedure Close(); override; |
| 80 | function Read( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer): Integer; override; |
| 81 | procedure Write( const pBuf : Pointer; off, len : Integer); override; |
| 82 | procedure Flush; override; |
| 83 | |
| 84 | procedure SetDnsResolveTimeout(const Value: Integer); |
| 85 | function GetDnsResolveTimeout: Integer; |
| 86 | procedure SetConnectionTimeout(const Value: Integer); |
| 87 | function GetConnectionTimeout: Integer; |
| 88 | procedure SetSendTimeout(const Value: Integer); |
| 89 | function GetSendTimeout: Integer; |
| 90 | procedure SetReadTimeout(const Value: Integer); |
| 91 | function GetReadTimeout: Integer; |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 92 | function GetSecureProtocols : TSecureProtocols; |
| 93 | procedure SetSecureProtocols( const value : TSecureProtocols); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 94 | |
| 95 | function GetCustomHeaders: IThriftDictionary<string,string>; |
| 96 | procedure SendRequest; |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 97 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 98 | property DnsResolveTimeout: Integer read GetDnsResolveTimeout write SetDnsResolveTimeout; |
| 99 | property ConnectionTimeout: Integer read GetConnectionTimeout write SetConnectionTimeout; |
| 100 | property SendTimeout: Integer read GetSendTimeout write SetSendTimeout; |
| 101 | property ReadTimeout: Integer read GetReadTimeout write SetReadTimeout; |
| 102 | property CustomHeaders: IThriftDictionary<string,string> read GetCustomHeaders; |
| 103 | public |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 104 | constructor Create( const aUri: string; const aConfig : IThriftConfiguration = nil); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 105 | destructor Destroy; override; |
| 106 | end; |
| 107 | |
| 108 | implementation |
| 109 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 110 | const |
| 111 | WINHTTP_CONNECTION_TIMEOUT = 60 * 1000; |
| 112 | WINHTTP_SENDRECV_TIMEOUT = 30 * 1000; |
| 113 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 114 | |
| 115 | { TWinHTTPClientImpl } |
| 116 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 117 | constructor TWinHTTPClientImpl.Create( const aUri: string; const aConfig : IThriftConfiguration); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 118 | begin |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 119 | inherited Create( aConfig); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 120 | FUri := AUri; |
| 121 | |
| 122 | // defaults according to MSDN |
| 123 | FDnsResolveTimeout := 0; // no timeout |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 124 | FConnectionTimeout := WINHTTP_CONNECTION_TIMEOUT; |
| 125 | FSendTimeout := WINHTTP_SENDRECV_TIMEOUT; |
| 126 | FReadTimeout := WINHTTP_SENDRECV_TIMEOUT; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 127 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 128 | FSecureProtocols := DEFAULT_THRIFT_SECUREPROTOCOLS; |
| 129 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 130 | FCustomHeaders := TThriftDictionaryImpl<string,string>.Create; |
Jens Geyer | f726ae3 | 2021-06-04 11:17:26 +0200 | [diff] [blame] | 131 | FOutputMemoryStream := TThriftMemoryStream.Create; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 132 | end; |
| 133 | |
| 134 | destructor TWinHTTPClientImpl.Destroy; |
| 135 | begin |
| 136 | Close; |
| 137 | FreeAndNil( FOutputMemoryStream); |
| 138 | inherited; |
| 139 | end; |
| 140 | |
| 141 | function TWinHTTPClientImpl.CreateRequest: IWinHTTPRequest; |
| 142 | var |
Jens Geyer | 19fdca8 | 2019-06-12 22:09:05 +0200 | [diff] [blame] | 143 | pair : TPair<string,string>; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 144 | session : IWinHTTPSession; |
| 145 | connect : IWinHTTPConnection; |
| 146 | url : IWinHTTPUrl; |
| 147 | sPath : string; |
Jens Geyer | 19fdca8 | 2019-06-12 22:09:05 +0200 | [diff] [blame] | 148 | info : TErrorInfo; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 149 | begin |
Jens Geyer | 19fdca8 | 2019-06-12 22:09:05 +0200 | [diff] [blame] | 150 | info := TErrorInfo.SplitUrl; |
| 151 | try |
| 152 | url := TWinHTTPUrlImpl.Create( FUri); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 153 | |
Jens Geyer | 19fdca8 | 2019-06-12 22:09:05 +0200 | [diff] [blame] | 154 | info := TErrorInfo.WinHTTPSession; |
Jens Geyer | aad7583 | 2022-06-01 22:06:29 +0200 | [diff] [blame] | 155 | session := TWinHTTPSessionImpl.Create('ApacheThriftDelphi/WinHTTP'); |
Jens Geyer | 19fdca8 | 2019-06-12 22:09:05 +0200 | [diff] [blame] | 156 | session.EnableSecureProtocols( SecureProtocolsAsWinHTTPFlags); |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 157 | |
Jens Geyer | 19fdca8 | 2019-06-12 22:09:05 +0200 | [diff] [blame] | 158 | info := TErrorInfo.WinHTTPConnection; |
| 159 | connect := session.Connect( url.HostName, url.Port); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 160 | |
Jens Geyer | 19fdca8 | 2019-06-12 22:09:05 +0200 | [diff] [blame] | 161 | info := TErrorInfo.WinHTTPRequest; |
| 162 | sPath := url.UrlPath + url.ExtraInfo; |
| 163 | result := connect.OpenRequest( (url.Scheme = 'https'), 'POST', sPath, THRIFT_MIMETYPE); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 164 | |
Jens Geyer | 19fdca8 | 2019-06-12 22:09:05 +0200 | [diff] [blame] | 165 | // setting a timeout value to 0 (zero) means "no timeout" for that setting |
| 166 | info := TErrorInfo.RequestSetup; |
| 167 | result.SetTimeouts( DnsResolveTimeout, ConnectionTimeout, SendTimeout, ReadTimeout); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 168 | |
Jens Geyer | 19fdca8 | 2019-06-12 22:09:05 +0200 | [diff] [blame] | 169 | // headers |
| 170 | result.AddRequestHeader( 'Content-Type: '+THRIFT_MIMETYPE, WINHTTP_ADDREQ_FLAG_ADD); |
| 171 | for pair in FCustomHeaders do begin |
| 172 | Result.AddRequestHeader( pair.Key +': '+ pair.Value, WINHTTP_ADDREQ_FLAG_ADD); |
| 173 | end; |
| 174 | |
Jens Geyer | 19505c3 | 2019-06-22 00:59:54 +0200 | [diff] [blame] | 175 | // enable automatic gzip,deflate decompression |
| 176 | result.EnableAutomaticContentDecompression(TRUE); |
| 177 | |
Jens Geyer | 19fdca8 | 2019-06-12 22:09:05 +0200 | [diff] [blame] | 178 | // AutoProxy support |
| 179 | info := TErrorInfo.AutoProxy; |
| 180 | result.TryAutoProxy( FUri); |
| 181 | except |
| 182 | on e:TException do raise; |
| 183 | on e:Exception do raise TTransportExceptionUnknown.Create( e.Message+' (at '+EnumUtils<TErrorInfo>.ToString(Ord(info))+')'); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 184 | end; |
| 185 | end; |
| 186 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 187 | |
| 188 | function TWinHTTPClientImpl.SecureProtocolsAsWinHTTPFlags : Cardinal; |
| 189 | const |
| 190 | PROTOCOL_MAPPING : array[TSecureProtocol] of Cardinal = ( |
| 191 | WINHTTP_FLAG_SECURE_PROTOCOL_SSL2, |
| 192 | WINHTTP_FLAG_SECURE_PROTOCOL_SSL3, |
| 193 | WINHTTP_FLAG_SECURE_PROTOCOL_TLS1, |
| 194 | WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1, |
| 195 | WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 |
| 196 | ); |
| 197 | var |
| 198 | prot : TSecureProtocol; |
| 199 | protos : TSecureProtocols; |
| 200 | begin |
| 201 | result := 0; |
| 202 | protos := GetSecureProtocols; |
| 203 | for prot := Low(TSecureProtocol) to High(TSecureProtocol) do begin |
| 204 | if prot in protos |
| 205 | then result := result or PROTOCOL_MAPPING[prot]; |
| 206 | end; |
| 207 | end; |
| 208 | |
| 209 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 210 | function TWinHTTPClientImpl.GetDnsResolveTimeout: Integer; |
| 211 | begin |
| 212 | Result := FDnsResolveTimeout; |
| 213 | end; |
| 214 | |
| 215 | procedure TWinHTTPClientImpl.SetDnsResolveTimeout(const Value: Integer); |
| 216 | begin |
| 217 | FDnsResolveTimeout := Value; |
| 218 | end; |
| 219 | |
| 220 | function TWinHTTPClientImpl.GetConnectionTimeout: Integer; |
| 221 | begin |
| 222 | Result := FConnectionTimeout; |
| 223 | end; |
| 224 | |
| 225 | procedure TWinHTTPClientImpl.SetConnectionTimeout(const Value: Integer); |
| 226 | begin |
| 227 | FConnectionTimeout := Value; |
| 228 | end; |
| 229 | |
| 230 | function TWinHTTPClientImpl.GetSendTimeout: Integer; |
| 231 | begin |
| 232 | Result := FSendTimeout; |
| 233 | end; |
| 234 | |
| 235 | procedure TWinHTTPClientImpl.SetSendTimeout(const Value: Integer); |
| 236 | begin |
| 237 | FSendTimeout := Value; |
| 238 | end; |
| 239 | |
| 240 | function TWinHTTPClientImpl.GetReadTimeout: Integer; |
| 241 | begin |
| 242 | Result := FReadTimeout; |
| 243 | end; |
| 244 | |
| 245 | procedure TWinHTTPClientImpl.SetReadTimeout(const Value: Integer); |
| 246 | begin |
| 247 | FReadTimeout := Value; |
| 248 | end; |
| 249 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 250 | function TWinHTTPClientImpl.GetSecureProtocols : TSecureProtocols; |
| 251 | begin |
| 252 | Result := FSecureProtocols; |
| 253 | end; |
| 254 | |
| 255 | procedure TWinHTTPClientImpl.SetSecureProtocols( const value : TSecureProtocols); |
| 256 | begin |
| 257 | FSecureProtocols := Value; |
| 258 | end; |
| 259 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 260 | function TWinHTTPClientImpl.GetCustomHeaders: IThriftDictionary<string,string>; |
| 261 | begin |
| 262 | Result := FCustomHeaders; |
| 263 | end; |
| 264 | |
| 265 | function TWinHTTPClientImpl.GetIsOpen: Boolean; |
| 266 | begin |
Jens Geyer | 528a0f0 | 2019-11-18 20:17:03 +0100 | [diff] [blame] | 267 | Result := Assigned( FOutputMemoryStream); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 268 | end; |
| 269 | |
| 270 | procedure TWinHTTPClientImpl.Open; |
| 271 | begin |
| 272 | FreeAndNil( FOutputMemoryStream); |
Jens Geyer | f726ae3 | 2021-06-04 11:17:26 +0200 | [diff] [blame] | 273 | FOutputMemoryStream := TThriftMemoryStream.Create; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 274 | end; |
| 275 | |
| 276 | procedure TWinHTTPClientImpl.Close; |
| 277 | begin |
| 278 | FInputStream := nil; |
| 279 | FreeAndNil( FOutputMemoryStream); |
| 280 | end; |
| 281 | |
| 282 | procedure TWinHTTPClientImpl.Flush; |
| 283 | begin |
| 284 | try |
| 285 | SendRequest; |
| 286 | finally |
| 287 | FreeAndNil( FOutputMemoryStream); |
Jens Geyer | f726ae3 | 2021-06-04 11:17:26 +0200 | [diff] [blame] | 288 | FOutputMemoryStream := TThriftMemoryStream.Create; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 289 | ASSERT( FOutputMemoryStream <> nil); |
| 290 | end; |
| 291 | end; |
| 292 | |
| 293 | function TWinHTTPClientImpl.Read( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer): Integer; |
| 294 | begin |
| 295 | if FInputStream = nil then begin |
| 296 | raise TTransportExceptionNotOpen.Create('No request has been sent'); |
| 297 | end; |
| 298 | |
| 299 | try |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 300 | Result := FInputStream.Read( pBuf, buflen, off, len); |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 301 | CountConsumedMessageBytes( result); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 302 | except |
| 303 | on E: Exception |
| 304 | do raise TTransportExceptionUnknown.Create(E.Message); |
| 305 | end; |
| 306 | end; |
| 307 | |
| 308 | procedure TWinHTTPClientImpl.SendRequest; |
| 309 | var |
Jens Geyer | 433a649 | 2019-06-19 23:14:08 +0200 | [diff] [blame] | 310 | http : IWinHTTPRequest; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 311 | pData : PByte; |
Jens Geyer | 433a649 | 2019-06-19 23:14:08 +0200 | [diff] [blame] | 312 | len : Integer; |
Jens Geyer | b012318 | 2020-02-12 12:16:19 +0100 | [diff] [blame] | 313 | error, dwSize : Cardinal; |
Jens Geyer | 433a649 | 2019-06-19 23:14:08 +0200 | [diff] [blame] | 314 | sMsg : string; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 315 | begin |
| 316 | http := CreateRequest; |
| 317 | |
| 318 | pData := FOutputMemoryStream.Memory; |
| 319 | len := FOutputMemoryStream.Size; |
| 320 | |
| 321 | // send all data immediately, since we have it in memory |
Jens Geyer | 433a649 | 2019-06-19 23:14:08 +0200 | [diff] [blame] | 322 | if not http.SendRequest( pData, len, 0) then begin |
| 323 | error := Cardinal( GetLastError); |
| 324 | sMsg := 'WinHTTP send error '+IntToStr(Int64(error))+' '+WinHttpSysErrorMessage(error); |
| 325 | raise TTransportExceptionUnknown.Create(sMsg); |
| 326 | end; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 327 | |
| 328 | // end request and start receiving |
Jens Geyer | 433a649 | 2019-06-19 23:14:08 +0200 | [diff] [blame] | 329 | if not http.FlushAndReceiveResponse then begin |
| 330 | error := Cardinal( GetLastError); |
| 331 | sMsg := 'WinHTTP recv error '+IntToStr(Int64(error))+' '+WinHttpSysErrorMessage(error); |
| 332 | if error = ERROR_WINHTTP_TIMEOUT |
| 333 | then raise TTransportExceptionTimedOut.Create( sMsg) |
| 334 | else raise TTransportExceptionInterrupted.Create( sMsg); |
| 335 | end; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 336 | |
Jens Geyer | 6762cad | 2020-10-30 17:15:18 +0100 | [diff] [blame] | 337 | // we're about to receive a new message, so reset everyting |
Jens Geyer | 5a781c2 | 2025-02-04 23:35:55 +0100 | [diff] [blame] | 338 | ResetMessageSizeAndConsumedBytes(-1); |
Jens Geyer | 72c8111 | 2025-03-10 21:46:20 +0100 | [diff] [blame] | 339 | EnsureSuccessHttpStatus(http); // throws if not |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 340 | FInputStream := THTTPResponseStream.Create( http); |
Jens Geyer | b012318 | 2020-02-12 12:16:19 +0100 | [diff] [blame] | 341 | if http.QueryTotalResponseSize( dwSize) // FALSE indicates "no info available" |
| 342 | then UpdateKnownMessageSize( dwSize); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 343 | end; |
| 344 | |
| 345 | procedure TWinHTTPClientImpl.Write( const pBuf : Pointer; off, len : Integer); |
| 346 | var pTmp : PByte; |
| 347 | begin |
| 348 | pTmp := pBuf; |
| 349 | Inc(pTmp,off); |
| 350 | FOutputMemoryStream.Write( pTmp^, len); |
| 351 | end; |
| 352 | |
| 353 | |
Jens Geyer | 72c8111 | 2025-03-10 21:46:20 +0100 | [diff] [blame] | 354 | class procedure TWinHTTPClientImpl.EnsureSuccessHttpStatus( const aRequest : IWinHTTPRequest); |
| 355 | var dwStatus : Cardinal; |
| 356 | sText : string; |
| 357 | begin |
| 358 | if (aRequest <> nil) |
| 359 | then aRequest.QueryHttpStatus( dwStatus, sText) |
| 360 | else raise TTransportExceptionNotOpen.Create('Invalid HTTP request data'); |
| 361 | |
| 362 | if (200 > dwStatus) or (dwStatus > 299) |
| 363 | then raise TTransportExceptionEndOfFile.Create('HTTP '+UIntToStr(dwStatus)+' '+sText); |
| 364 | end; |
| 365 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 366 | { TWinHTTPClientImpl.THTTPResponseStream } |
| 367 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 368 | constructor TWinHTTPClientImpl.THTTPResponseStream.Create( const aRequest : IWinHTTPRequest); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 369 | begin |
| 370 | inherited Create; |
| 371 | FRequest := aRequest; |
| 372 | end; |
| 373 | |
| 374 | destructor TWinHTTPClientImpl.THTTPResponseStream.Destroy; |
| 375 | begin |
| 376 | try |
| 377 | Close; |
| 378 | finally |
| 379 | inherited Destroy; |
| 380 | end; |
| 381 | end; |
| 382 | |
| 383 | procedure TWinHTTPClientImpl.THTTPResponseStream.Close; |
| 384 | begin |
| 385 | FRequest := nil; |
| 386 | end; |
| 387 | |
| 388 | procedure TWinHTTPClientImpl.THTTPResponseStream.Flush; |
| 389 | begin |
| 390 | raise ENotImplemented(ClassName+'.Flush'); |
| 391 | end; |
| 392 | |
| 393 | function TWinHTTPClientImpl.THTTPResponseStream.IsOpen: Boolean; |
| 394 | begin |
| 395 | Result := FRequest <> nil; |
| 396 | end; |
| 397 | |
| 398 | procedure TWinHTTPClientImpl.THTTPResponseStream.Open; |
| 399 | begin |
| 400 | // nothing to do |
| 401 | end; |
| 402 | |
| 403 | procedure TWinHTTPClientImpl.THTTPResponseStream.Write(const pBuf : Pointer; offset, count: Integer); |
| 404 | begin |
| 405 | inherited; // check pointers |
| 406 | raise ENotImplemented(ClassName+'.Write'); |
| 407 | end; |
| 408 | |
| 409 | function TWinHTTPClientImpl.THTTPResponseStream.Read(const pBuf : Pointer; const buflen : Integer; offset, count: Integer): Integer; |
| 410 | var pTmp : PByte; |
| 411 | begin |
| 412 | inherited; // check pointers |
| 413 | |
| 414 | if count >= buflen-offset |
| 415 | then count := buflen-offset; |
| 416 | |
| 417 | if count > 0 then begin |
| 418 | pTmp := pBuf; |
| 419 | Inc( pTmp, offset); |
| 420 | Result := FRequest.ReadData( pTmp, count); |
| 421 | ASSERT( Result >= 0); |
| 422 | end |
| 423 | else Result := 0; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 424 | end; |
| 425 | |
| 426 | function TWinHTTPClientImpl.THTTPResponseStream.ToArray: TBytes; |
| 427 | begin |
| 428 | raise ENotImplemented(ClassName+'.ToArray'); |
| 429 | end; |
| 430 | |
| 431 | |
| 432 | end. |