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