Repository navigation
Expand file tree
/
Copy pathUEdgeBrowserZipServer.pas
More file actions
306 lines (254 loc) · 9.37 KB
/
Copy pathUEdgeBrowserZipServer.pas
File metadata and controls
306 lines (254 loc) · 9.37 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
unit UEdgeBrowserZipServer;
interface
uses
Vcl.Edge,
System.Zip,
System.SysUtils,
System.RTLConsts,
System.Net.Mime,
Winapi.WebView2,
Winapi.ActiveX,
IdURI,
System.Classes,
System.Win.ComObj,
Winapi.Windows;
type
TEdgeZipServer = class
private const
sSlash = '\';
sBackSlash = '/';
private
FEdgeBrowser: TCustomEdgeBrowser;
FZipFile: TZipFile;
FMimeTypes: TMimeTypes;
FZipFileNames: TArray<string>;
FCallOldHandlers: Boolean;
FUriFilter: string;
FOldWebResourceRequestedHandler: TWebResourceRequestedEvent;
FOldCreateWebViewCompleted: TWebViewStatusEvent;
/// <summary>
/// Gets zip file filenames and normalize paths to backslash-notation
/// </summary>
procedure LoadZipFileNames;
/// <summary>
/// TCustomEdgeBrowser.OnWebResourceRequested event handler
/// </summary>
procedure DoWebResourceRequested(Sender: TCustomEdgeBrowser;
Args: TWebResourceRequestedEventArgs);
/// <summary>
/// TCustomEdgeBrowser.OnCreateWebViewCompleted event handler
/// </summary>
procedure DoCreateWebViewCompleted(Sender: TCustomEdgeBrowser; AResult: HResult);
/// <summary>
/// Examine request, search for zip file and construct response
/// </summary>
procedure HandleRequest(Args: TWebResourceRequestedEventArgs);
protected const
indexFiles: array [0 .. 2] of string = ('index.html', 'index.htm', 'default.htm');
protected
function DetectMime(const Url: string; out AType: string): Boolean;
function CheckPathContainsRootFile(const Path: string): Integer;
function IndexOfPath(const Path: string): Integer;
function GetUriRelativePath(const uri: string): string;
function FindZipIndexForUri(const uri: string; out mimeStr: string): Integer;
public
/// <summary>
/// See filters format here
/// https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.addwebresourcerequestedfilter
/// </summary>
constructor Create(const Zip: TZipFile; const EdgeBrowser: TCustomEdgeBrowser;
const UriFilter: string);
destructor Destroy; override;
/// <summary>
/// When True call OnCreateWebViewCompleted and OnWebResourceRequested event handlers if assigned. Default False.
/// </summary>
property CallOldEventHandlers: Boolean read FCallOldHandlers write FCallOldHandlers;
/// <summary>
/// Link to assigned TZipFile
/// </summary>
property ZipFile: TZipFile read FZipFile;
property EdgeBrowser: TCustomEdgeBrowser read FEdgeBrowser;
end;
function IncludeTrailingSlash(const S: string): string;
function RemoveLeadingSlash(const S: string): string;
implementation
function IncludeTrailingSlash(const S: string): string;
begin
Result := S;
if (Result[High(Result)] <> '/') then
Result := Result + '/';
end;
function RemoveLeadingSlash(const S: string): string;
begin
Result := S;
if (Result[Low(Result)] = '/') then
Result := Result.TrimLeft(['/']);
end;
{ TEdgeBrowserZipServer }
function TEdgeZipServer.DetectMime(const Url: string; out AType: string): Boolean;
var
ext: string;
mimeKind: TMimeTypes.TKind;
begin
ext := ExtractFileExt(Url);
Result := FMimeTypes.GetExtInfo(ext, AType, mimeKind);
end;
function TEdgeZipServer.CheckPathContainsRootFile(const Path: string): Integer;
var
testPath, slashPath: string;
begin
Result := -1;
slashPath := StringReplace(Path, sSlash, sBackSlash, [rfReplaceAll]);
slashPath := IncludeTrailingSlash(slashPath);
slashPath := RemoveLeadingSlash(slashPath);
for var I := Low(indexFiles) to High(indexFiles) do
begin
testPath := slashPath + indexFiles[I];
Result := IndexOfPath(testPath);
if Result >= 0 then
Exit;
end;
end;
constructor TEdgeZipServer.Create(const Zip: TZipFile; const EdgeBrowser: TCustomEdgeBrowser;
const UriFilter: string);
begin
inherited Create;
if not Assigned(Zip) then
raise EArgumentNilException.Create(Format(SParamIsNil, ['Zip']));
if not Assigned(EdgeBrowser) then
raise EArgumentNilException.Create(Format(SParamIsNil, ['EdgeBrowser']));
if Zip.Mode = TZipMode.zmClosed then
raise EArgumentException.Create(SZipNotOpen);
// nobody told you but seems like filter is case sensetive
// it means that it won't fire up OnWebResourceRequested event
// if we add filter with upper case characters. For example
// filter contoSO.com will not work for requests for contoso.com
FUriFilter := UriFilter.ToLowerInvariant;
FZipFile := Zip;
LoadZipFileNames;
FMimeTypes := TMimeTypes.Create;
FMimeTypes.AddDefTypes;
{ Setting up EdgeBrowser }
FEdgeBrowser := EdgeBrowser;
FCallOldHandlers := False;
// save previous handlers and setting our own
// OnWebResourceRequested
if Assigned(EdgeBrowser.OnWebResourceRequested) then
FOldWebResourceRequestedHandler := FEdgeBrowser.OnWebResourceRequested
else
FOldWebResourceRequestedHandler := nil;
FEdgeBrowser.OnWebResourceRequested := DoWebResourceRequested;
// OnCreateWebViewCompleted
if Assigned(FEdgeBrowser.OnCreateWebViewCompleted) then
FOldCreateWebViewCompleted := FEdgeBrowser.OnCreateWebViewCompleted
else
FOldCreateWebViewCompleted := nil;
FEdgeBrowser.OnCreateWebViewCompleted := DoCreateWebViewCompleted;
// this call will work only if WebView already created
// in case you're trying to re-use EdgeBrowser where's content already loaded
FEdgeBrowser.AddWebResourceRequestedFilter(FUriFilter, COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL);
end;
destructor TEdgeZipServer.Destroy;
begin
FMimeTypes.Free;
SetLength(FZipFileNames, 0);
FZipFile := nil;
FEdgeBrowser.RemoveWebResourceRequestedFilter(FUriFilter, COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL);
FEdgeBrowser.OnCreateWebViewCompleted := FOldCreateWebViewCompleted;
FEdgeBrowser.OnWebResourceRequested := FOldWebResourceRequestedHandler;
inherited Destroy;
end;
procedure TEdgeZipServer.DoCreateWebViewCompleted(Sender: TCustomEdgeBrowser; AResult: HResult);
begin
if Succeeded(AResult) then
FEdgeBrowser.AddWebResourceRequestedFilter(FUriFilter, COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL)
else
OleCheck(AResult);
if (FCallOldHandlers and Assigned(FOldCreateWebViewCompleted)) then
FOldCreateWebViewCompleted(Sender, AResult);
end;
procedure TEdgeZipServer.DoWebResourceRequested(Sender: TCustomEdgeBrowser;
Args: TWebResourceRequestedEventArgs);
begin
HandleRequest(Args);
if (FCallOldHandlers and Assigned(FOldWebResourceRequestedHandler)) then
FOldWebResourceRequestedHandler(Sender, Args);
end;
function TEdgeZipServer.GetUriRelativePath(const Uri: string): string;
var
IdURI: TIdURI;
begin
IdURI := TIdURI.Create(Uri);
Result := IdURI.URLDecode(IdURI.Path + IdURI.Document);
IdURI.Free;
end;
function TEdgeZipServer.FindZipIndexForUri(const Uri: string; out MimeStr: string): Integer;
var
FileName: string;
begin
FileName := GetUriRelativePath(Uri);
// if user explicitly request for folder then we check if it contains index.html file first
// we need this in case when archive contains file and folder with the same names
if (fileName.EndsWith(sBackSlash)) then
Result := CheckPathContainsRootFile(FileName)
else
begin
Result := IndexOfPath(FileName);
if (Result = -1) then
Result := CheckPathContainsRootFile(FileName);
end;
if Result >= 0 then
FileName := FZipFileNames[Result];
// make sure it returns correct ext and mime
DetectMime(FileName, MimeStr);
end;
procedure TEdgeZipServer.HandleRequest(Args: TWebResourceRequestedEventArgs);
var
resourceRequest: ICoreWebView2WebResourceRequest;
response: ICoreWebView2WebResourceResponse;
puri: PWideChar;
memFile: TStream;
header: TZipHeader;
stream: IStream;
mimeStr: string;
idx: Integer;
begin
if Succeeded(Args.ArgsInterface.Get_Request(resourceRequest)) then
begin
resourceRequest.Get_uri(puri);
idx := FindZipIndexForUri(puri, mimeStr);
if (idx >= 0) then
begin
// make a response with a file from ZipArchive
memFile := TMemoryStream.Create;
FZipFile.Read(idx, memFile, header);
stream := TStreamAdapter.Create(memFile, TStreamOwnership.soOwned);
ResourceRequest.Set_Content(stream);
FEdgeBrowser.EnvironmentInterface.CreateWebResourceResponse(stream, 200, 'OK',
PWideChar('Content-Type: ' + mimeStr), response);
Args.ArgsInterface.Set_Response(response);
end;
CoTaskMemFree(puri);
end;
end;
function TEdgeZipServer.IndexOfPath(const Path: string): Integer;
var
slashPath: string;
begin
Result := -1;
slashPath := StringReplace(Path, sSlash, sBackSlash, [rfReplaceAll]);
slashPath := RemoveLeadingSlash(Path);
for var I := 0 to Length(FZipFileNames) - 1 do
begin
if (SameStr(slashPath, FZipFileNames[I])) then
Exit(I);
end;
end;
procedure TEdgeZipServer.LoadZipFileNames;
begin
FZipFileNames := Copy(FZipFile.FileNames, 0, Length(FZipFile.FileNames));
// once and for all we replace backslashes to slashes so we don't need to care about it anymore
for var I := Low(FZipFileNames) to High(FZipFileNames) do
FZipFileNames[I] := StringReplace(FZipFileNames[I], sSlash, sBackSlash, [rfReplaceAll]);
end;
end.