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.MsxmlHTTP; |
| 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 | {$IFDEF OLD_UNIT_NAMES} |
| 32 | ActiveX, msxml, |
| 33 | {$ELSE} |
| 34 | Winapi.ActiveX, Winapi.msxml, |
| 35 | {$ENDIF} |
| 36 | Thrift.Collections, |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 37 | Thrift.Configuration, |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 38 | Thrift.Transport, |
| 39 | Thrift.Exception, |
| 40 | Thrift.Utils, |
| 41 | Thrift.Stream; |
| 42 | |
| 43 | type |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 44 | TMsxmlHTTPClientImpl = class( TEndpointTransportBase, IHTTPClient) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 45 | strict private |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 46 | FUri : string; |
| 47 | FInputStream : IThriftStream; |
| 48 | FOutputStream : IThriftStream; |
| 49 | FDnsResolveTimeout : Integer; |
| 50 | FConnectionTimeout : Integer; |
| 51 | FSendTimeout : Integer; |
| 52 | FReadTimeout : Integer; |
| 53 | FCustomHeaders : IThriftDictionary<string,string>; |
| 54 | |
| 55 | function CreateRequest: IXMLHTTPRequest; |
Jens Geyer | 72c8111 | 2025-03-10 21:46:20 +0100 | [diff] [blame] | 56 | class procedure EnsureSuccessHttpStatus( const aRequest : IXMLHTTPRequest); |
| 57 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 58 | strict protected |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 59 | function GetIsOpen: Boolean; override; |
| 60 | procedure Open(); override; |
| 61 | procedure Close(); override; |
| 62 | function Read( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer): Integer; override; |
| 63 | procedure Write( const pBuf : Pointer; off, len : Integer); override; |
| 64 | procedure Flush; override; |
| 65 | |
| 66 | procedure SetDnsResolveTimeout(const Value: Integer); |
| 67 | function GetDnsResolveTimeout: Integer; |
| 68 | procedure SetConnectionTimeout(const Value: Integer); |
| 69 | function GetConnectionTimeout: Integer; |
| 70 | procedure SetSendTimeout(const Value: Integer); |
| 71 | function GetSendTimeout: Integer; |
| 72 | procedure SetReadTimeout(const Value: Integer); |
| 73 | function GetReadTimeout: Integer; |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 74 | function GetSecureProtocols : TSecureProtocols; |
| 75 | procedure SetSecureProtocols( const value : TSecureProtocols); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 76 | |
| 77 | function GetCustomHeaders: IThriftDictionary<string,string>; |
| 78 | procedure SendRequest; |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 79 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 80 | property DnsResolveTimeout: Integer read GetDnsResolveTimeout write SetDnsResolveTimeout; |
| 81 | property ConnectionTimeout: Integer read GetConnectionTimeout write SetConnectionTimeout; |
| 82 | property SendTimeout: Integer read GetSendTimeout write SetSendTimeout; |
| 83 | property ReadTimeout: Integer read GetReadTimeout write SetReadTimeout; |
| 84 | property CustomHeaders: IThriftDictionary<string,string> read GetCustomHeaders; |
| 85 | public |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 86 | constructor Create( const aUri: string; const aConfig : IThriftConfiguration); reintroduce; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 87 | destructor Destroy; override; |
| 88 | end; |
| 89 | |
| 90 | |
| 91 | implementation |
| 92 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 93 | const |
| 94 | XMLHTTP_CONNECTION_TIMEOUT = 60 * 1000; |
| 95 | XMLHTTP_SENDRECV_TIMEOUT = 30 * 1000; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 96 | |
| 97 | { TMsxmlHTTPClientImpl } |
| 98 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 99 | constructor TMsxmlHTTPClientImpl.Create( const aUri: string; const aConfig : IThriftConfiguration); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 100 | begin |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 101 | inherited Create( aConfig); |
| 102 | FUri := aUri; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 103 | |
| 104 | // defaults according to MSDN |
| 105 | FDnsResolveTimeout := 0; // no timeout |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 106 | FConnectionTimeout := XMLHTTP_CONNECTION_TIMEOUT; |
| 107 | FSendTimeout := XMLHTTP_SENDRECV_TIMEOUT; |
| 108 | FReadTimeout := XMLHTTP_SENDRECV_TIMEOUT; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 109 | |
| 110 | FCustomHeaders := TThriftDictionaryImpl<string,string>.Create; |
Jens Geyer | f726ae3 | 2021-06-04 11:17:26 +0200 | [diff] [blame] | 111 | FOutputStream := TThriftStreamAdapterDelphi.Create( TThriftMemoryStream.Create, True); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 112 | end; |
| 113 | |
| 114 | function TMsxmlHTTPClientImpl.CreateRequest: IXMLHTTPRequest; |
| 115 | var |
| 116 | pair : TPair<string,string>; |
| 117 | srvHttp : IServerXMLHTTPRequest; |
| 118 | begin |
| 119 | {$IF CompilerVersion >= 21.0} |
| 120 | Result := CoServerXMLHTTP.Create; |
| 121 | {$ELSE} |
| 122 | Result := CoXMLHTTPRequest.Create; |
| 123 | {$IFEND} |
| 124 | |
| 125 | // setting a timeout value to 0 (zero) means "no timeout" for that setting |
| 126 | if Supports( result, IServerXMLHTTPRequest, srvHttp) |
| 127 | then srvHttp.setTimeouts( DnsResolveTimeout, ConnectionTimeout, SendTimeout, ReadTimeout); |
| 128 | |
| 129 | Result.open('POST', FUri, False, '', ''); |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 130 | Result.setRequestHeader( 'Content-Type', THRIFT_MIMETYPE); |
| 131 | Result.setRequestHeader( 'Accept', THRIFT_MIMETYPE); |
Jens Geyer | aad7583 | 2022-06-01 22:06:29 +0200 | [diff] [blame] | 132 | Result.setRequestHeader( 'User-Agent', 'ApacheThriftDelphi/msxml'); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 133 | |
| 134 | for pair in FCustomHeaders do begin |
| 135 | Result.setRequestHeader( pair.Key, pair.Value ); |
| 136 | end; |
| 137 | end; |
| 138 | |
| 139 | destructor TMsxmlHTTPClientImpl.Destroy; |
| 140 | begin |
| 141 | Close; |
| 142 | inherited; |
| 143 | end; |
| 144 | |
| 145 | function TMsxmlHTTPClientImpl.GetDnsResolveTimeout: Integer; |
| 146 | begin |
| 147 | Result := FDnsResolveTimeout; |
| 148 | end; |
| 149 | |
| 150 | procedure TMsxmlHTTPClientImpl.SetDnsResolveTimeout(const Value: Integer); |
| 151 | begin |
| 152 | FDnsResolveTimeout := Value; |
| 153 | end; |
| 154 | |
| 155 | function TMsxmlHTTPClientImpl.GetConnectionTimeout: Integer; |
| 156 | begin |
| 157 | Result := FConnectionTimeout; |
| 158 | end; |
| 159 | |
| 160 | procedure TMsxmlHTTPClientImpl.SetConnectionTimeout(const Value: Integer); |
| 161 | begin |
| 162 | FConnectionTimeout := Value; |
| 163 | end; |
| 164 | |
| 165 | function TMsxmlHTTPClientImpl.GetSendTimeout: Integer; |
| 166 | begin |
| 167 | Result := FSendTimeout; |
| 168 | end; |
| 169 | |
| 170 | procedure TMsxmlHTTPClientImpl.SetSendTimeout(const Value: Integer); |
| 171 | begin |
| 172 | FSendTimeout := Value; |
| 173 | end; |
| 174 | |
| 175 | function TMsxmlHTTPClientImpl.GetReadTimeout: Integer; |
| 176 | begin |
| 177 | Result := FReadTimeout; |
| 178 | end; |
| 179 | |
| 180 | procedure TMsxmlHTTPClientImpl.SetReadTimeout(const Value: Integer); |
| 181 | begin |
| 182 | FReadTimeout := Value; |
| 183 | end; |
| 184 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 185 | function TMsxmlHTTPClientImpl.GetSecureProtocols : TSecureProtocols; |
| 186 | begin |
| 187 | Result := []; |
| 188 | end; |
| 189 | |
| 190 | procedure TMsxmlHTTPClientImpl.SetSecureProtocols( const value : TSecureProtocols); |
| 191 | begin |
| 192 | raise TTransportExceptionBadArgs.Create('SetSecureProtocols: Not supported with '+ClassName); |
| 193 | end; |
| 194 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 195 | function TMsxmlHTTPClientImpl.GetCustomHeaders: IThriftDictionary<string,string>; |
| 196 | begin |
| 197 | Result := FCustomHeaders; |
| 198 | end; |
| 199 | |
| 200 | function TMsxmlHTTPClientImpl.GetIsOpen: Boolean; |
| 201 | begin |
Jens Geyer | cc8c2c6 | 2021-03-29 22:38:30 +0200 | [diff] [blame] | 202 | Result := Assigned(FOutputStream); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 203 | end; |
| 204 | |
| 205 | procedure TMsxmlHTTPClientImpl.Open; |
| 206 | begin |
Jens Geyer | f726ae3 | 2021-06-04 11:17:26 +0200 | [diff] [blame] | 207 | FOutputStream := TThriftStreamAdapterDelphi.Create( TThriftMemoryStream.Create, True); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 208 | end; |
| 209 | |
| 210 | procedure TMsxmlHTTPClientImpl.Close; |
| 211 | begin |
| 212 | FInputStream := nil; |
| 213 | FOutputStream := nil; |
| 214 | end; |
| 215 | |
| 216 | procedure TMsxmlHTTPClientImpl.Flush; |
| 217 | begin |
| 218 | try |
| 219 | SendRequest; |
| 220 | finally |
| 221 | FOutputStream := nil; |
Jens Geyer | f726ae3 | 2021-06-04 11:17:26 +0200 | [diff] [blame] | 222 | FOutputStream := TThriftStreamAdapterDelphi.Create( TThriftMemoryStream.Create, True); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 223 | ASSERT( FOutputStream <> nil); |
| 224 | end; |
| 225 | end; |
| 226 | |
| 227 | function TMsxmlHTTPClientImpl.Read( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer): Integer; |
| 228 | begin |
| 229 | if FInputStream = nil then begin |
| 230 | raise TTransportExceptionNotOpen.Create('No request has been sent'); |
| 231 | end; |
| 232 | |
| 233 | try |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 234 | Result := FInputStream.Read( pBuf, buflen, off, len); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 235 | except |
| 236 | on E: Exception |
| 237 | do raise TTransportExceptionUnknown.Create(E.Message); |
| 238 | end; |
| 239 | end; |
| 240 | |
| 241 | procedure TMsxmlHTTPClientImpl.SendRequest; |
| 242 | var |
| 243 | xmlhttp : IXMLHTTPRequest; |
Jens Geyer | f726ae3 | 2021-06-04 11:17:26 +0200 | [diff] [blame] | 244 | ms : TThriftMemoryStream; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 245 | a : TBytes; |
| 246 | len : Integer; |
| 247 | begin |
| 248 | xmlhttp := CreateRequest; |
| 249 | |
Jens Geyer | f726ae3 | 2021-06-04 11:17:26 +0200 | [diff] [blame] | 250 | ms := TThriftMemoryStream.Create; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 251 | try |
| 252 | a := FOutputStream.ToArray; |
| 253 | len := Length(a); |
| 254 | if len > 0 then begin |
| 255 | ms.WriteBuffer( Pointer(@a[0])^, len); |
| 256 | end; |
| 257 | ms.Position := 0; |
| 258 | xmlhttp.send( IUnknown( TStreamAdapter.Create( ms, soReference ))); |
| 259 | FInputStream := nil; |
Jens Geyer | 72c8111 | 2025-03-10 21:46:20 +0100 | [diff] [blame] | 260 | EnsureSuccessHttpStatus(xmlhttp); // throws if not |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 261 | FInputStream := TThriftStreamAdapterCOM.Create( IUnknown( xmlhttp.responseStream) as IStream); |
Jens Geyer | 5a781c2 | 2025-02-04 23:35:55 +0100 | [diff] [blame] | 262 | ResetMessageSizeAndConsumedBytes; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 263 | UpdateKnownMessageSize( FInputStream.Size); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 264 | finally |
| 265 | ms.Free; |
| 266 | end; |
| 267 | end; |
| 268 | |
| 269 | procedure TMsxmlHTTPClientImpl.Write( const pBuf : Pointer; off, len : Integer); |
| 270 | begin |
Jens Geyer | cc8c2c6 | 2021-03-29 22:38:30 +0200 | [diff] [blame] | 271 | if FOutputStream <> nil |
| 272 | then FOutputStream.Write( pBuf, off, len) |
| 273 | else raise TTransportExceptionNotOpen.Create('Transport closed'); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 274 | end; |
| 275 | |
| 276 | |
Jens Geyer | 72c8111 | 2025-03-10 21:46:20 +0100 | [diff] [blame] | 277 | class procedure TMsxmlHTTPClientImpl.EnsureSuccessHttpStatus( const aRequest : IXMLHTTPRequest); |
| 278 | var iStatus : Integer; |
| 279 | sText : string; |
| 280 | begin |
| 281 | if aRequest = nil |
| 282 | then raise TTransportExceptionNotOpen.Create('Invalid HTTP request data'); |
| 283 | |
| 284 | iStatus := aRequest.status; |
| 285 | sText := aRequest.statusText; |
| 286 | |
| 287 | if (200 > iStatus) or (iStatus > 299) |
| 288 | then raise TTransportExceptionEndOfFile.Create('HTTP '+IntToStr(iStatus)+' '+sText); |
| 289 | end; |
| 290 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 291 | |
| 292 | end. |
| 293 | |