]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/thrift/lib/delphi/src/Thrift.Transport.MsxmlHTTP.pas
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / delphi / src / Thrift.Transport.MsxmlHTTP.pas
CommitLineData
f67539c2
TL
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 *)
19unit Thrift.Transport.MsxmlHTTP;
20
21{$I Thrift.Defines.inc}
22{$SCOPEDENUMS ON}
23
24interface
25
26uses
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
42type
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;
71 function GetSecureProtocols : TSecureProtocols;
72 procedure SetSecureProtocols( const value : TSecureProtocols);
73
74 function GetCustomHeaders: IThriftDictionary<string,string>;
75 procedure SendRequest;
76
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
88implementation
89
90
91{ TMsxmlHTTPClientImpl }
92
93constructor TMsxmlHTTPClientImpl.Create(const AUri: string);
94begin
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);
106end;
107
108function TMsxmlHTTPClientImpl.CreateRequest: IXMLHTTPRequest;
109var
110 pair : TPair<string,string>;
111 srvHttp : IServerXMLHTTPRequest;
112begin
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, '', '');
124 Result.setRequestHeader( 'Content-Type', THRIFT_MIMETYPE);
125 Result.setRequestHeader( 'Accept', THRIFT_MIMETYPE);
126 Result.setRequestHeader( 'User-Agent', 'Delphi/IHTTPClient');
127
128 for pair in FCustomHeaders do begin
129 Result.setRequestHeader( pair.Key, pair.Value );
130 end;
131end;
132
133destructor TMsxmlHTTPClientImpl.Destroy;
134begin
135 Close;
136 inherited;
137end;
138
139function TMsxmlHTTPClientImpl.GetDnsResolveTimeout: Integer;
140begin
141 Result := FDnsResolveTimeout;
142end;
143
144procedure TMsxmlHTTPClientImpl.SetDnsResolveTimeout(const Value: Integer);
145begin
146 FDnsResolveTimeout := Value;
147end;
148
149function TMsxmlHTTPClientImpl.GetConnectionTimeout: Integer;
150begin
151 Result := FConnectionTimeout;
152end;
153
154procedure TMsxmlHTTPClientImpl.SetConnectionTimeout(const Value: Integer);
155begin
156 FConnectionTimeout := Value;
157end;
158
159function TMsxmlHTTPClientImpl.GetSendTimeout: Integer;
160begin
161 Result := FSendTimeout;
162end;
163
164procedure TMsxmlHTTPClientImpl.SetSendTimeout(const Value: Integer);
165begin
166 FSendTimeout := Value;
167end;
168
169function TMsxmlHTTPClientImpl.GetReadTimeout: Integer;
170begin
171 Result := FReadTimeout;
172end;
173
174procedure TMsxmlHTTPClientImpl.SetReadTimeout(const Value: Integer);
175begin
176 FReadTimeout := Value;
177end;
178
179function TMsxmlHTTPClientImpl.GetSecureProtocols : TSecureProtocols;
180begin
181 Result := [];
182end;
183
184procedure TMsxmlHTTPClientImpl.SetSecureProtocols( const value : TSecureProtocols);
185begin
186 raise TTransportExceptionBadArgs.Create('SetSecureProtocols: Not supported with '+ClassName);
187end;
188
189function TMsxmlHTTPClientImpl.GetCustomHeaders: IThriftDictionary<string,string>;
190begin
191 Result := FCustomHeaders;
192end;
193
194function TMsxmlHTTPClientImpl.GetIsOpen: Boolean;
195begin
196 Result := True;
197end;
198
199procedure TMsxmlHTTPClientImpl.Open;
200begin
201 FOutputStream := TThriftStreamAdapterDelphi.Create( TMemoryStream.Create, True);
202end;
203
204procedure TMsxmlHTTPClientImpl.Close;
205begin
206 FInputStream := nil;
207 FOutputStream := nil;
208end;
209
210procedure TMsxmlHTTPClientImpl.Flush;
211begin
212 try
213 SendRequest;
214 finally
215 FOutputStream := nil;
216 FOutputStream := TThriftStreamAdapterDelphi.Create( TMemoryStream.Create, True);
217 ASSERT( FOutputStream <> nil);
218 end;
219end;
220
221function TMsxmlHTTPClientImpl.Read( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer): Integer;
222begin
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;
233end;
234
235procedure TMsxmlHTTPClientImpl.SendRequest;
236var
237 xmlhttp : IXMLHTTPRequest;
238 ms : TMemoryStream;
239 a : TBytes;
240 len : Integer;
241begin
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;
258end;
259
260procedure TMsxmlHTTPClientImpl.Write( const pBuf : Pointer; off, len : Integer);
261begin
262 FOutputStream.Write( pBuf, off, len);
263end;
264
265
266
267end.
268