-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuwebdav.pas
More file actions
388 lines (347 loc) · 11.2 KB
/
uwebdav.pas
File metadata and controls
388 lines (347 loc) · 11.2 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
unit uwebdav;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, fgl, fphttpclient;
const
//cWebDAVServer = 'https://webdav.yandex.ru/';
cWebDAVServer = 'https://{login}.stackstorage.com/';
cWebDAVSubdir = 'remote.php/webdav/';
type
{ TWDResource }
TWDResource = class
private
FHref: string;
FStatusCode: integer;
FContentLength: int64;
FCreationDate: TDateTime;
FLastmodified: TDateTime;
FDisplayName: string;
FContentType: string;
FCollection: boolean;
public
property Href: string read FHref write FHref;
property StatusCode: integer read FStatusCode write FStatusCode;
property ContentLength: int64 read FContentLength write FContentLength;
property CreationDate: TDateTime read FCreationDate write FCreationDate;
property Lastmodified: TDateTime read FLastmodified write FLastmodified;
property DisplayName: string read FDisplayName write FDisplayName;
property ContentType: string read FContentType write FContentType;
property Collection: boolean read FCollection write FCollection;
end;
TWDResourceList = specialize TFPGObjectList<TWDResource>;
type
TWebDAVDrive = class
private
FLogin: string;
FPassword: string;
FDirectory: string;
function EncodeUTF8URI(const URI: string): string;
function GetRequestURL(const Element: string; EncodePath: boolean = False): string;
public
constructor Create;
destructor Destroy; override;
property Login: string read FLogin write FLogin;
property Password: string read FPassword write FPassword;
property Directory: string read FDirectory write FDirectory;
// GET - Download a file
function GET(const ElementHref: string; var Stream: TStream; Callback: TDataEvent): boolean;
// PUT - Upload a file
function PUT(const ElementHref: string; var Stream: TStream; Callback: TDataEvent): boolean;
// DELETE - Delete a file
function Delete(const ElementHref: string): boolean;
// PROPFIND - Properties of files and directories
// COPY - Copy a file
// not implemented
// MOVE - Move a file
// not implemented
function PROPFIND(Depth: integer {0/1}; const Element: string): string;
// MKCOL - Create a directory
function MKCOL(const ElementPath: string): boolean;
// PROPPATCH - Change the properties of a file or directory
// not implemented
// LOCK - Zet een slot op het object
// not implemented
// OPEN - Unlock een bron
// not implemented
end;
procedure ParseWebDavResources(const AXMLStr: string; var Resources: TWDResourceList);
implementation
uses Dialogs, laz2_XMLRead, laz2_DOM,
ufphttphelper {ALWAYS LAST until fphttpclient is fixed};
{ TTWebDAVDrive }
constructor TWebDAVDrive.Create;
begin
inherited;
end;
destructor TWebDAVDrive.Destroy;
begin
inherited;
end;
type
TSpecials = set of AnsiChar;
const
URLSpecialChar: TSpecials =
[#$00..#$20, '<', '>', '"', '%', '{', '}', '|', '\', '^', '[', ']', '`', #$7F..#$FF];
function TWebDAVDrive.EncodeUTF8URI(const URI: string): string;
var
i: integer;
char: AnsiChar;
begin
Result := '';
for i := 1 to length(URI) do
begin
if (URI[i] in URLSpecialChar) then
begin
for char in UTF8String(URI[i]) do
Result := Result + '%' + IntToHex(Ord(char), 2);
end
else
Result := Result + URI[i];
end;
end;
function TWebDAVDrive.GetRequestURL(const Element: string; EncodePath: boolean): string;
var
URI: string;
WebDavStr: string;
begin
WebDavStr := StringReplace(cWebDAVServer, '{login}', Login, []);
if Length(Element) > 0 then
begin
URI := Element;
if URI[1] = '/' then
System.Delete(URI, 1, 1);
if EncodePath then
Result := WebDavStr + EncodeUTF8URI(URI)
else
Result := WebDavStr + URI;
end
else
Result := WebDavStr + cWebDAVSubdir;
end;
function TWebDAVDrive.GET(const ElementHref: string; var Stream: TStream;
Callback: TDataEvent): boolean;
var
URL: string;
HTTP: TFPHTTPClient;
begin
Result := False;
if not Assigned(Stream) then exit;
URL := GetRequestURL(ElementHref, False);
HTTP := TFPHTTPClient.Create(nil);
try
HTTP.UserName := Login;
HTTP.Password := Password;
HTTP.RequestHeaders.Add('Accept: */*');
HTTP.RequestHeaders.Add('Connection: Close');
HTTP.OnDataReceived := Callback;
try
HTTP.Get(URL, Stream);
Result := True;
except
on E: Exception do
ShowMessage(E.Message);
end;
finally
HTTP.Free;
end;
end;
function TWebDAVDrive.PUT(const ElementHref: string; var Stream: TStream; Callback: TDataEvent): boolean;
var
URL, Rs: string;
HTTP: TFPHTTPClient;
begin
Result := False;
if not Assigned(Stream) then Exit;
URL := GetRequestURL(ElementHref, True);
HTTP := TFPHTTPClient.Create(nil);
try
HTTP.RequestBody := Stream;
HTTP.UserName := Login;
HTTP.Password := Password;
HTTP.RequestHeaders.Add('Accept: */*');
HTTP.RequestHeaders.Add('Content-Type: application/binary');
HTTP.RequestHeaders.Add('Connection: Close');
HTTP.OnDataSent := Callback;
Rs := HTTP.Put(URL);
// ShowMessage(Rs); // created
Result := True;
finally
HTTP.Free;
end;
end;
function TWebDAVDrive.DELETE(const ElementHref: string): boolean;
var
HTTP: TFPHTTPClient;
SS: TStringStream;
begin
Result := False;
HTTP := TFPHTTPClient.Create(nil);
SS := TStringStream.Create('');
try
HTTP.UserName := Login;
HTTP.Password := Password;
HTTP.RequestHeaders.Add('Accept: */*');
HTTP.RequestHeaders.Add('Connection: Close');
HTTP.HTTPMethod('DELETE', GetRequestURL(ElementHref), SS, [204]);
Result := True;
//Result := SS.Datastring;
finally
SS.Free;
HTTP.Free;
end;
end;
function TWebDAVDrive.MKCOL(const ElementPath: string): boolean;
var
HTTP: TFPHTTPClient;
SS: TStringStream;
begin
Result := False;
HTTP := TFPHTTPClient.Create(nil);
SS := TStringStream.Create('');
try
HTTP.UserName := Login;
HTTP.Password := Password;
HTTP.RequestHeaders.Add('Accept: */*');
HTTP.RequestHeaders.Add('Connection: Close');
HTTP.HTTPMethod('MKCOL', GetRequestURL(ElementPath), SS, [201]);
//Result := SS.Datastring;
finally
SS.Free;
HTTP.Free;
end;
end;
function TWebDAVDrive.PROPFIND(Depth: integer; const Element: string): string;
var
HTTP: TFPHTTPClient;
SS: TStringStream;
begin
HTTP := TFPHTTPClient.Create(nil);
SS := TStringStream.Create('');
try
HTTP.UserName := Login;
HTTP.Password := Password;
HTTP.RequestHeaders.Add('Accept: */*');
HTTP.RequestHeaders.Add('Connection: Close');
HTTP.RequestHeaders.Add('Depth: ' + IntToStr(Depth));
HTTP.HTTPMethod('PROPFIND', GetRequestURL(Element), SS, [201, 207]);
Result := SS.DataString;
finally
SS.Free;
HTTP.Free;
end;
end;
{ Support functions }
function SeparateLeft(const Value, Delimiter: string): string;
var
x: integer;
begin
x := Pos(Delimiter, Value);
if x < 1 then
Result := Value
else
Result := Copy(Value, 1, x - 1);
end;
function SeparateRight(const Value, Delimiter: string): string;
var
x: integer;
begin
x := Pos(Delimiter, Value);
if x > 0 then
x := x + Length(Delimiter) - 1;
Result := Copy(Value, x + 1, Length(Value) - x);
end;
{ WebDavResources }
procedure ParseWebDavResources(const AXMLStr: string; var Resources: TWDResourceList);
var
XMLDoc: TXMLDocument;
ResponseNode, ChildNode, PropNodeChild, PropertyNode: TDOMNode;
s, su, Value: string;
procedure ReadXMLString(Value: string);
var
S: TStringStream;
begin
S := TStringStream.Create(Value);
try
ReadXMLFile(XMLDoc, S);
finally
S.Free;
end;
end;
begin
// XMLDoc := TXMLDocument.Create; wordt in ReadXMLString gedaan
//Showmessage(AXMLStr);
ReadXMLString(AXMLStr); //XMLDoc.LoadFromXML(AXMLStr);
try
//if not XMLDoc.IsEmptyDoc then
begin
// Select the first node d: response
ResponseNode := XMLDoc.DocumentElement.FirstChild;
// ResponseNode:=XMLDoc.DocumentElement.ChildNodes.First;
while Assigned(ResponseNode) do
begin
// Create a new resource record in the list
Resources.Add(TWDResource.Create);
// Iterate over the child nodes d: response
ChildNode := ResponseNode.FirstChild;
// ChildNode:=ResponseNode.ChildNodes.First;
while Assigned(ChildNode) do
begin
if ChildNode.NodeName = 'd:href' then
Resources.Last.Href := ChildNode.TextContent // .Text
else
// Find node with the resource properties
if ChildNode.NodeName = 'd:propstat' then
begin
// Select the first child node, usually - it is d: status
PropNodeChild := ChildNode.FirstChild; // .First;
while Assigned(PropNodeChild) do
begin
// Read the status code
if PropNodeChild.NodeName = 'd:status' then
begin
Value := PropNodeChild.TextContent; //.Text;
s := Trim(SeparateRight(Value, ' '));
su := Trim(SeparateLeft(s, ' '));
Resources.Last.StatusCode := StrToIntDef(su, 0);
end
else
// Find node d: prop - are passed on its child nodes
if PropNodeChild.NodeName = 'd:prop' then
begin
PropertyNode := PropNodeChild.FirstChild; //.First;
while Assigned(PropertyNode) do
begin
if PropertyNode.NodeName = 'd:creationdate' then
Resources.Last.CreationDate := 0 // PropertyNode.TextContent not used
else if PropertyNode.NodeName = 'd:displayname' then
Resources.Last.DisplayName := PropertyNode.TextContent
else if PropertyNode.NodeName = 'd:getcontentlength' then
Resources.Last.ContentLength :=
StrToInt64(PropertyNode.TextContent)
else if PropertyNode.NodeName = 'd:getlastmodified' then
Resources.Last.Lastmodified :=
0 // DecodeRfcDateTime(PropertyNode.TextContent) Fri, 15 Jul 2016 08:33:34 GMT
else if PropertyNode.NodeName = 'd:resourcetype' then
Resources.Last.Collection := PropertyNode.ChildNodes.Count > 0;
// Select the next child node have d: prop
PropertyNode := PropertyNode.NextSibling;
end;
end;
// Select the next child node have d: propstat
PropNodeChild := PropNodeChild.NextSibling;
end;
end;
// Select the next child node have d: response
ChildNode := ChildNode.NextSibling;
end;
// Select the next node d: response
ResponseNode := ResponseNode.NextSibling;
end;
end;
finally
XMLDoc.Free;
XMLDoc := nil;
end;
end;
end.