-
Notifications
You must be signed in to change notification settings - Fork 63
/
DN.HttpClient.WinHttp.pas
270 lines (239 loc) · 7.8 KB
/
DN.HttpClient.WinHttp.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
unit DN.HttpClient.WinHttp;
interface
uses
Classes,
SysUtils,
DN.ActiveX,
DN.HttpClient,
DN.Import.WinHttp,
DN.HttpClient.Cache.Intf;
type
TDNWinHttpClient = class(TDNHttpClient)
private
FRequest: IWinHttpRequest;
FLastThreadID: Cardinal;
FCache: IDNHttpCache;
FEvents: IWinHttpRequestEvents;
FEventsCookie: Integer;
FContentLength: Int64;
FContentProgress: Int64;
protected
procedure HandleResponseStart(Status: Integer; const ContentType: string);
procedure HandleResponseDataAvailable(var Data: PSafeArray);
procedure AddEvents(ARequest: IWinHttpRequest; var ACookie: Integer);
procedure RemoveEvents(ARequest: IWinHttpRequest; ACookie: Integer);
procedure RequiresRequest;
function GetResponseHeader(const AName: string): string; override;
public
constructor Create;
destructor Destroy; override;
function Get(const AUrl: string; AResponse: TStream): Integer; override;
procedure BeginWork; override;
procedure EndWork; override;
end;
TOnResponseStart = procedure(Status: Integer; const ContentType: string) of object;
TOnResponseDataAvailable = procedure(var Data: PSafeArray) of object;
TOnResponseFinished = procedure of object;
TOnError = procedure(ErrorNumber: Integer; const ErrorDescription: string) of object;
TDNWinHttpClientEvents = class(TInterfacedObject, IWinHttpRequestEvents)
private
FOnResponseDataAvailable: TOnResponseDataAvailable;
FOnResponseFinished: TOnResponseFinished;
FOnResponseStart: TOnResponseStart;
FOnError: TOnError;
procedure ResponseStart(Status: Integer; const ContentType: WideString); stdcall;
procedure ResponseDataAvailable(var Data: PSafeArray); stdcall;
procedure ResponseFinished; stdcall;
procedure Error(ErrorNumber: Integer; const ErrorDescription: WideString); stdcall;
//interface
procedure IWinHttpRequestEvents.OnResponseStart = ResponseStart;
procedure IWinHttpRequestEvents.OnResponseDataAvailable = ResponseDataAvailable;
procedure IWinHttpRequestEvents.OnResponseFinished = ResponseFinished;
procedure IWinHttpRequestEvents.OnError = Error;
public
property OnResponseStart: TOnResponseStart read FOnResponseStart write FOnResponseStart;
property OnResponseDataAvailable: TOnResponseDataAvailable read FOnResponseDataAvailable write FOnResponseDataAvailable;
property OnResponseFinished: TOnResponseFinished read FOnResponseFinished write FOnResponseFinished;
property OnError: TOnError read FOnError write FOnError;
end;
implementation
uses
Windows,
IOUtils,
DN.Environment,
DN.HttpClient.Intf,
DN.HttpClient.Cache;
{ TDNWinHttpClient }
procedure TDNWinHttpClient.AddEvents(ARequest: IWinHttpRequest;
var ACookie: Integer);
var
LContainer: IConnectionPointContainer;
LConnectionPoint: IConnectionPoint;
begin
LContainer := ARequest as IConnectionPointContainer;
if LContainer.FindConnectionPoint(IID_IWinHttpRequestEvents, LConnectionPoint) = S_OK then
LConnectionPoint.Advise(FEvents, ACookie);
end;
procedure TDNWinHttpClient.BeginWork;
begin
inherited;
FCache.OpenCache();
end;
constructor TDNWinHttpClient.Create;
var
LEvents: TDNWinHttpClientEvents;
begin
FCache := TDNHttpCache.Create(TPath.Combine(GetDelphinusTempFolder(), 'HttpCache'));
LEvents := TDNWinHttpClientEvents.Create();
LEvents.OnResponseStart := HandleResponseStart;
LEvents.OnResponseDataAvailable := HandleResponseDataAvailable;
FEvents := LEvents;
inherited;
end;
destructor TDNWinHttpClient.Destroy;
begin
if Assigned(FRequest) then
RemoveEvents(FRequest, FEventsCookie);
FCache := nil;
FRequest := nil;
FEvents := nil;
inherited;
end;
procedure TDNWinHttpClient.EndWork;
begin
inherited;
FCache.CloseCache();
end;
function TDNWinHttpClient.Get(const AUrl: string; AResponse: TStream): Integer;
var
LResponse: IStream;
LAdapter: IStream;
LRead, LWritten: UInt64;
LEntry: IDNHttpCacheEntry;
LETag, LCacheControl: WideString;
begin
FLastResponseSource := rsServer;
RequiresRequest();
FRequest.Open('Get', AUrl, False);
if Authentication <> '' then
FRequest.SetRequestHeader('Authorization', Authentication);
if Accept <> '' then
FRequest.SetRequestHeader('Accept', Accept);
FCache.OpenCache();
try
if FCache.TryGetCache(AUrl, LEntry) then
begin
if IgnoreCacheExpiration or not LEntry.IsExpired then
begin
LEntry.Load(AResponse);
FLastResponseSource := rsCache;
Exit(HTTPErrorOk);
end
else
begin
FRequest.SetRequestHeader('If-None-Match', LEntry.ETag);
end;
end;
FRequest.Send('');
Result := FRequest.Status;
if (Result = HTTPErrorNotModified) and Assigned(LEntry) then
begin
LEntry.Load(AResponse);
//if it expired but is still valid revalidate it for its last MaxAge
LEntry.LastModified := Now();
FLastResponseSource := rsCache;
Exit(HTTPErrorOk);
end;
if Result = HTTPErrorOk then
begin
LResponse := IUnknown(FRequest.ResponseStream) as IStream;
LAdapter := TStreamAdapter.Create(AResponse) as IStream;
LResponse.CopyTo(LAdapter, High(Int64), LRead, LWritten);
DoProgress(LWritten, LWritten);
if FRequest.GetResponseHeader('ETag', LETag) = S_OK then
begin
if not FRequest.GetResponseHeader('cache-control', LCacheControl) = S_OK then
LCacheControl := '';
FCache.AddCache(AUrl, AResponse, LCacheControl, LETag);
end;
end;
finally
FCache.CloseCache();
end;
end;
function TDNWinHttpClient.GetResponseHeader(const AName: string): string;
var
LResult: WideString;
begin
if FRequest.GetResponseHeader(AName, LResult) = S_OK then
Result := LResult
else
Result := '';
end;
procedure TDNWinHttpClient.HandleResponseDataAvailable(var Data: PSafeArray);
begin
if (FContentLength > 0) and (Data.cbElements > 0) then
begin
Inc(FContentProgress, Data.rgsabound[0].cElements);
DoProgress(FContentProgress, FContentLength);
end;
end;
procedure TDNWinHttpClient.HandleResponseStart(Status: Integer;
const ContentType: string);
var
LLength: WideString;
begin
FContentLength := -1;
FContentProgress := 0;
if FRequest.GetResponseHeader('Content-Length', LLength) = S_OK then
FContentLength := StrToInt64Def(LLength, -1);
end;
procedure TDNWinHttpClient.RemoveEvents(ARequest: IWinHttpRequest;
ACookie: Integer);
var
LContainer: IConnectionPointContainer;
LConnectionPoint: IConnectionPoint;
begin
//for some reason, we can not query the interface while the IDE is closing/unloading
//so in this case we just leave it as it is
if Supports(ARequest, IConnectionPointContainer, LContainer) then
begin
if LContainer.FindConnectionPoint(IID_IWinHttpRequestEvents, LConnectionPoint) = S_OK then
LConnectionPoint.Unadvise(ACookie);
end;
end;
procedure TDNWinHttpClient.RequiresRequest;
begin
if (not Assigned(FRequest)) or (FLastThreadID <> GetCurrentThreadId()) then
begin
if Assigned(FRequest) then
RemoveEvents(FRequest, FEventsCookie);
FRequest := CoWinHttpRequest.Create();
AddEvents(FRequest, FEventsCookie);
FLastThreadID := GetCurrentThreadId();
end;
end;
{ TDNWinHttpClientEvents }
procedure TDNWinHttpClientEvents.Error(ErrorNumber: Integer;
const ErrorDescription: WideString);
begin
if Assigned(FOnError) then
FOnError(ErrorNumber, ErrorDescription);
end;
procedure TDNWinHttpClientEvents.ResponseDataAvailable(var Data: PSafeArray);
begin
if Assigned(FOnResponseDataAvailable) then
FOnResponseDataAvailable(Data);
end;
procedure TDNWinHttpClientEvents.ResponseFinished;
begin
if Assigned(FOnResponseFinished) then
FOnResponseFinished();
end;
procedure TDNWinHttpClientEvents.ResponseStart(Status: Integer;
const ContentType: WideString);
begin
if Assigned(FOnResponseStart) then
FOnResponseStart(Status, ContentType);
end;
end.