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