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