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