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 | |
| 142 | session := TWinHTTPSessionImpl.Create('Apache Thrift Delphi Client'); |
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; |
| 148 | result := connect.OpenRequest( (url.Scheme = 'https'), 'POST', sPath, 'application/x-thrift'); |
| 149 | |
| 150 | // setting a timeout value to 0 (zero) means "no timeout" for that setting |
| 151 | result.SetTimeouts( DnsResolveTimeout, ConnectionTimeout, SendTimeout, ReadTimeout); |
| 152 | |
| 153 | result.AddRequestHeader( 'Content-Type: application/x-thrift', WINHTTP_ADDREQ_FLAG_ADD); |
| 154 | |
| 155 | for pair in FCustomHeaders do begin |
| 156 | Result.AddRequestHeader( pair.Key +': '+ pair.Value, WINHTTP_ADDREQ_FLAG_ADD); |
| 157 | end; |
| 158 | end; |
| 159 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame^] | 160 | |
| 161 | function TWinHTTPClientImpl.SecureProtocolsAsWinHTTPFlags : Cardinal; |
| 162 | const |
| 163 | PROTOCOL_MAPPING : array[TSecureProtocol] of Cardinal = ( |
| 164 | WINHTTP_FLAG_SECURE_PROTOCOL_SSL2, |
| 165 | WINHTTP_FLAG_SECURE_PROTOCOL_SSL3, |
| 166 | WINHTTP_FLAG_SECURE_PROTOCOL_TLS1, |
| 167 | WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1, |
| 168 | WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 |
| 169 | ); |
| 170 | var |
| 171 | prot : TSecureProtocol; |
| 172 | protos : TSecureProtocols; |
| 173 | begin |
| 174 | result := 0; |
| 175 | protos := GetSecureProtocols; |
| 176 | for prot := Low(TSecureProtocol) to High(TSecureProtocol) do begin |
| 177 | if prot in protos |
| 178 | then result := result or PROTOCOL_MAPPING[prot]; |
| 179 | end; |
| 180 | end; |
| 181 | |
| 182 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 183 | function TWinHTTPClientImpl.GetDnsResolveTimeout: Integer; |
| 184 | begin |
| 185 | Result := FDnsResolveTimeout; |
| 186 | end; |
| 187 | |
| 188 | procedure TWinHTTPClientImpl.SetDnsResolveTimeout(const Value: Integer); |
| 189 | begin |
| 190 | FDnsResolveTimeout := Value; |
| 191 | end; |
| 192 | |
| 193 | function TWinHTTPClientImpl.GetConnectionTimeout: Integer; |
| 194 | begin |
| 195 | Result := FConnectionTimeout; |
| 196 | end; |
| 197 | |
| 198 | procedure TWinHTTPClientImpl.SetConnectionTimeout(const Value: Integer); |
| 199 | begin |
| 200 | FConnectionTimeout := Value; |
| 201 | end; |
| 202 | |
| 203 | function TWinHTTPClientImpl.GetSendTimeout: Integer; |
| 204 | begin |
| 205 | Result := FSendTimeout; |
| 206 | end; |
| 207 | |
| 208 | procedure TWinHTTPClientImpl.SetSendTimeout(const Value: Integer); |
| 209 | begin |
| 210 | FSendTimeout := Value; |
| 211 | end; |
| 212 | |
| 213 | function TWinHTTPClientImpl.GetReadTimeout: Integer; |
| 214 | begin |
| 215 | Result := FReadTimeout; |
| 216 | end; |
| 217 | |
| 218 | procedure TWinHTTPClientImpl.SetReadTimeout(const Value: Integer); |
| 219 | begin |
| 220 | FReadTimeout := Value; |
| 221 | end; |
| 222 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame^] | 223 | function TWinHTTPClientImpl.GetSecureProtocols : TSecureProtocols; |
| 224 | begin |
| 225 | Result := FSecureProtocols; |
| 226 | end; |
| 227 | |
| 228 | procedure TWinHTTPClientImpl.SetSecureProtocols( const value : TSecureProtocols); |
| 229 | begin |
| 230 | FSecureProtocols := Value; |
| 231 | end; |
| 232 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 233 | function TWinHTTPClientImpl.GetCustomHeaders: IThriftDictionary<string,string>; |
| 234 | begin |
| 235 | Result := FCustomHeaders; |
| 236 | end; |
| 237 | |
| 238 | function TWinHTTPClientImpl.GetIsOpen: Boolean; |
| 239 | begin |
| 240 | Result := True; |
| 241 | end; |
| 242 | |
| 243 | procedure TWinHTTPClientImpl.Open; |
| 244 | begin |
| 245 | FreeAndNil( FOutputMemoryStream); |
| 246 | FOutputMemoryStream := TMemoryStream.Create; |
| 247 | end; |
| 248 | |
| 249 | procedure TWinHTTPClientImpl.Close; |
| 250 | begin |
| 251 | FInputStream := nil; |
| 252 | FreeAndNil( FOutputMemoryStream); |
| 253 | end; |
| 254 | |
| 255 | procedure TWinHTTPClientImpl.Flush; |
| 256 | begin |
| 257 | try |
| 258 | SendRequest; |
| 259 | finally |
| 260 | FreeAndNil( FOutputMemoryStream); |
| 261 | FOutputMemoryStream := TMemoryStream.Create; |
| 262 | ASSERT( FOutputMemoryStream <> nil); |
| 263 | end; |
| 264 | end; |
| 265 | |
| 266 | function TWinHTTPClientImpl.Read( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer): Integer; |
| 267 | begin |
| 268 | if FInputStream = nil then begin |
| 269 | raise TTransportExceptionNotOpen.Create('No request has been sent'); |
| 270 | end; |
| 271 | |
| 272 | try |
| 273 | Result := FInputStream.Read( pBuf, buflen, off, len) |
| 274 | except |
| 275 | on E: Exception |
| 276 | do raise TTransportExceptionUnknown.Create(E.Message); |
| 277 | end; |
| 278 | end; |
| 279 | |
| 280 | procedure TWinHTTPClientImpl.SendRequest; |
| 281 | var |
| 282 | http : IWinHTTPRequest; |
| 283 | pData : PByte; |
| 284 | len : Integer; |
| 285 | begin |
| 286 | http := CreateRequest; |
| 287 | |
| 288 | pData := FOutputMemoryStream.Memory; |
| 289 | len := FOutputMemoryStream.Size; |
| 290 | |
| 291 | // send all data immediately, since we have it in memory |
| 292 | if not http.SendRequest( pData, len, 0) |
| 293 | then raise TTransportExceptionUnknown.Create('send request error'); |
| 294 | |
| 295 | // end request and start receiving |
| 296 | if not http.FlushAndReceiveResponse |
| 297 | then raise TTransportExceptionInterrupted.Create('flush/receive error'); |
| 298 | |
| 299 | FInputStream := THTTPResponseStream.Create(http); |
| 300 | end; |
| 301 | |
| 302 | procedure TWinHTTPClientImpl.Write( const pBuf : Pointer; off, len : Integer); |
| 303 | var pTmp : PByte; |
| 304 | begin |
| 305 | pTmp := pBuf; |
| 306 | Inc(pTmp,off); |
| 307 | FOutputMemoryStream.Write( pTmp^, len); |
| 308 | end; |
| 309 | |
| 310 | |
| 311 | { TWinHTTPClientImpl.THTTPResponseStream } |
| 312 | |
| 313 | constructor TWinHTTPClientImpl.THTTPResponseStream.Create( const aRequest : IWinHTTPRequest); |
| 314 | begin |
| 315 | inherited Create; |
| 316 | FRequest := aRequest; |
| 317 | end; |
| 318 | |
| 319 | destructor TWinHTTPClientImpl.THTTPResponseStream.Destroy; |
| 320 | begin |
| 321 | try |
| 322 | Close; |
| 323 | finally |
| 324 | inherited Destroy; |
| 325 | end; |
| 326 | end; |
| 327 | |
| 328 | procedure TWinHTTPClientImpl.THTTPResponseStream.Close; |
| 329 | begin |
| 330 | FRequest := nil; |
| 331 | end; |
| 332 | |
| 333 | procedure TWinHTTPClientImpl.THTTPResponseStream.Flush; |
| 334 | begin |
| 335 | raise ENotImplemented(ClassName+'.Flush'); |
| 336 | end; |
| 337 | |
| 338 | function TWinHTTPClientImpl.THTTPResponseStream.IsOpen: Boolean; |
| 339 | begin |
| 340 | Result := FRequest <> nil; |
| 341 | end; |
| 342 | |
| 343 | procedure TWinHTTPClientImpl.THTTPResponseStream.Open; |
| 344 | begin |
| 345 | // nothing to do |
| 346 | end; |
| 347 | |
| 348 | procedure TWinHTTPClientImpl.THTTPResponseStream.Write(const pBuf : Pointer; offset, count: Integer); |
| 349 | begin |
| 350 | inherited; // check pointers |
| 351 | raise ENotImplemented(ClassName+'.Write'); |
| 352 | end; |
| 353 | |
| 354 | function TWinHTTPClientImpl.THTTPResponseStream.Read(const pBuf : Pointer; const buflen : Integer; offset, count: Integer): Integer; |
| 355 | var pTmp : PByte; |
| 356 | begin |
| 357 | inherited; // check pointers |
| 358 | |
| 359 | if count >= buflen-offset |
| 360 | then count := buflen-offset; |
| 361 | |
| 362 | if count > 0 then begin |
| 363 | pTmp := pBuf; |
| 364 | Inc( pTmp, offset); |
| 365 | Result := FRequest.ReadData( pTmp, count); |
| 366 | ASSERT( Result >= 0); |
| 367 | end |
| 368 | else Result := 0; |
| 369 | end; |
| 370 | |
| 371 | function TWinHTTPClientImpl.THTTPResponseStream.ToArray: TBytes; |
| 372 | begin |
| 373 | raise ENotImplemented(ClassName+'.ToArray'); |
| 374 | end; |
| 375 | |
| 376 | |
| 377 | end. |