forked from sara-nl/js-webdav-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdist-unminified.js
2950 lines (2700 loc) · 119 KB
/
dist-unminified.js
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function (root, factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(exports);
if (v !== undefined && v !== null) {
module.exports = v;
}
} else if (typeof define === "function" && define.amd) {
define(["exports"], factory);
} else {
// Browser globals
root.nl = factory({});
}
})(this, function (exports) {
"use strict";
// This flag is a necessity to support interoperability between CommonJS/AMD/UMD/ES6 module loaders.
// this flag triggers special compatibility behaviour of non-ES6 loaders to handle "default" exports
// of this module exactly like ES6.
//
// see https://github.com/esnext/es6-module-transpiler/issues/86
// https://github.com/esnext/es6-module-transpiler/issues/85
// https://github.com/Microsoft/TypeScript/issues/5844#issuecomment-161425239
Object.defineProperty(exports, "__esModule", { value: true });
/*
* Copyright ©2012 SARA bv, The Netherlands
*
* This file is part of js-webdav-client.
*
* js-webdav-client is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* js-webdav-client is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with js-webdav-client. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
/**
* A webDAV client library for JavaScript
* @author Niek Bosch
*/
// Create the namespace if that's not done yet
if (nl === undefined) {
/** @namespace */
var nl = {};
}
if (nl.sara === undefined) {
/** @namespace */
nl.sara = {};
}
if (nl.sara.webdav === undefined) {
/** @namespace The entire library is in this namespace. */
nl.sara.webdav = {};
}
if (nl.sara.webdav.codec === undefined) {
/** @namespace Holds all the standard codecs for converting properties */
nl.sara.webdav.codec = {};
}
/*
* Copyright ©2012 SARA bv, The Netherlands
*
* This file is part of js-webdav-client.
*
* js-webdav-client is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* js-webdav-client is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with js-webdav-client. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
// If nl.sara.webdav.Exception is already defined, we have a namespace clash!
if (nl.sara.webdav.Exception !== undefined) {
throw 'Class name nl.sara.webdav.Exception already taken, could not load JavaScript library for WebDAV connectivity.';
}
/**
* @class An exception
* @param {String} [message] Optional; A human readable message
* @param {Number} [code] Optional; The error code. It is best to use the class constants to set this.
* @property {String} message The exception message
* @property {Number} code The exception code
*/
nl.sara.webdav.Exception = function(message, code) {
// First define public attributes
Object.defineProperty(this, 'message', {
'value': null,
'enumerable': true,
'configurable': false,
'writable': true
});
Object.defineProperty(this, 'code', {
'value': null,
'enumerable': true,
'configurable': false,
'writable': true
});
// Constructor logic
if (message !== undefined) {
this.message = message;
}
if (code !== undefined) {
this.code = code;
}
};
/**#@+
* Declaration of the error code constants
*/
nl.sara.webdav.Exception.WRONG_TYPE = 1;
nl.sara.webdav.Exception.NAMESPACE_TAKEN = 2;
nl.sara.webdav.Exception.UNEXISTING_PROPERTY = 3;
nl.sara.webdav.Exception.WRONG_XML = 4;
nl.sara.webdav.Exception.WRONG_VALUE = 5;
nl.sara.webdav.Exception.MISSING_REQUIRED_PARAMETER = 6;
nl.sara.webdav.Exception.AJAX_ERROR = 7;
nl.sara.webdav.Exception.NOT_IMPLEMENTED = 8;
/**#@-*/
//########################## DEFINE PUBLIC METHODS #############################
/**
* Overloads the default toString() method so it returns the message of this exception
*
* @returns {String} A string representation of this property
*/
nl.sara.webdav.Exception.prototype.toString = function() {
return this.message;
};
// End of library
/*
* Copyright ©2012 SARA bv, The Netherlands
*
* This file is part of js-webdav-client.
*
* js-webdav-client is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* js-webdav-client is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with js-webdav-client. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
// If nl.sara.webdav.Ie is already defined, we have a namespace clash!
if (nl.sara.webdav.Ie !== undefined) {
throw 'Class name nl.sara.webdav.Ie already taken, could not load JavaScript library for WebDAV connectivity.';
}
/**
* @class This class holds all IE hacks
*/
nl.sara.webdav.Ie = {};
/**
* Although this library has no intent to work in IE older than versions 9, it should work in IE and sometimes requires some special attention for this wonderful browser
*
* @var Boolean True if the current browser is IE
*/
nl.sara.webdav.Ie.isIE = false;
/*@cc_on
nl.sara.webdav.Ie.isIE = true;
@*/
//########################## DEFINE PUBLIC METHODS #############################
/**
* Returns the localName of a DOM Node object
*
* @param {Node} node The node to determine the localname for
* @returns {String} The local name
*/
nl.sara.webdav.Ie.getLocalName = function(node) {
if (nl.sara.webdav.Ie.isIE && node.baseName) {
return node.baseName;
}else{
return node.localName;
}
};
// End of library
/*
* Copyright ©2012 SARA bv, The Netherlands
*
* This file is part of js-webdav-client.
*
* js-webdav-client is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* js-webdav-client is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with js-webdav-client. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
// If nl.sara.webdav.Client is already defined, we have a namespace clash!
if (nl.sara.webdav.Client !== undefined) {
throw new nl.sara.webdav.Exception('Namespace nl.sara.webdav.Client already taken, could not load JavaScript library for WebDAV connectivity.', nl.sara.webdav.Exception.NAMESPACE_TAKEN);
}
/**
* @class Connection to a WebDAV server
*
* @param {Array} [config] A configuration object. It can contain the following fields:
* - host; String containing the hostname to connect to (default: current host)
* - useHTTPS; If set to (boolean) false, a regular HTTP connection will be used, any other value (even 0 or '' which evaluate to false) and HTTPS will be used instead. Is ignored if host is not set. (default: true)
* - port; Integer with the port number to use. Is ignored if host is not set. (default: 443 for HTTPS and 80 for HTTP)
* - defaultHeaders; An array containing default headers to use for each request. Header names should be the keys. (default: {} i.e. no default headers)
* - username; A string with the username to use for authentication. (default: the current username if any)
* - password; A string with the password to use for authentication. This is only used if a username is supplied. (default: the current password if any)
* For backwards compatibility reasons, it is also
* possible to specify the 'host', 'useHTTPS', 'port'
* and 'defaultHeaders' as regular parameters (in
* that order). Note that useHTTPS works differently
* in that case; any other value than boolean True
* will result in regular HTTP. If you want to supply
* a username and password, you will have to use the
* config array, you cannot pass them as extra normal
* parameters.
*/
nl.sara.webdav.Client = function(config, useHTTPS, port, defaultHeaders) {
// First define private attributes
Object.defineProperty(this, '_baseUrl', {
'value': null,
'enumerable': false,
'configurable': false,
'writable': true
});
Object.defineProperty(this, '_username', {
'value': null,
'enumerable': false,
'configurable': false,
'writable': true
});
Object.defineProperty(this, '_password', {
'value': null,
'enumerable': false,
'configurable': false,
'writable': true
});
Object.defineProperty(this, '_headers', {
'value': {},
'enumerable': false,
'configurable': false,
'writable': true
});
// Constructor logic
if ( config !== undefined ) {
var host;
if ( typeof( config ) !== 'string' ) {
host = config['host'];
useHTTPS = ( config['useHTTPS'] !== false );
port = config['port'];
defaultHeaders = config['defaultHeaders'];
if ( config['username'] !== undefined ) {
this._username = config['username'];
if ( config['password'] !== undefined ) {
this._password = config['password'];
}else{
this._password = '';
}
}
}else{
host = config;
}
if ( host !== undefined ) {
// if the configuration item is a string, then we have to work in compatibility mode; first parameter is the host, second, the protocol, third the port and fourth aditional headers
var protocol = (useHTTPS === true) ? 'https' : 'http';
port = (port !== undefined) ? port : ((protocol === 'https') ? 443 : 80);
this._baseUrl = protocol + '://' + host + ((((protocol === 'http') && (port === 80)) || ((protocol === 'https') && (port === 443))) ? '' : ':' + port);
}
}
if (defaultHeaders !== undefined) {
this._headers = defaultHeaders;
}
};
/**#@+
* Class constant
*/
nl.sara.webdav.Client.PROPNAME = 1;
nl.sara.webdav.Client.ALLPROP = 2;
nl.sara.webdav.Client.INFINITY = 'infinity';
nl.sara.webdav.Client.FAIL_ON_OVERWRITE = 3;
nl.sara.webdav.Client.TRUNCATE_ON_OVERWRITE = 4;
nl.sara.webdav.Client.SILENT_OVERWRITE = 5;
/**#@-*/
//########################## DEFINE PUBLIC METHODS #############################
/**
* Converts a path to the full url (i.e. appends the protocol and host part)
*
* @param {String} path The path on the server
* @returns {String} The full url to the path
*/
nl.sara.webdav.Client.prototype.getUrl = function(path) {
if (path.substring(0,1) !== '/') {
path = '/' + path;
}
if (this._baseUrl !== null) {
return this._baseUrl + path;
}else{
return path;
}
};
/**
* Perform a WebDAV PROPFIND request
*
* @param {String} path The path get a PROPFIND for
* @param {Function(status,body,headers)} callback Querying the server is done asynchronously, this callback function is called when the results are in
* @param {String} [depth=0] Optional; Value for the 'depth' header, should be either '0', '1' or the class constant INFINITY. When omitted, '0' is used. See RFC 4918.
* @param {mixed} [props=ALLPROP] Optional; The properties to fetch. Should be either one of the class constants PROPNAME or ALLPROP or an array with Property objects. When omitted, ALLPROP is assumed. See RFC 4918.
* @param {nl.sara.webdav.Property[]} [include] Optional; An array with Property objects used for the <include> element. This is only used for ALLPROP requests. When omitted, no <include> element is send. See RFC 4918.
* @param {Array} headers Optional; Additional headers to set
* @returns {nl.sara.webdav.Client} The client itself for chaining methods
*/
nl.sara.webdav.Client.prototype.propfind = function(path, callback, depth, props, include, headers) {
if ((path === undefined) || (callback === undefined)) {
throw new nl.sara.webdav.Exception('PROPFIND requires the parameters path and callback', nl.sara.webdav.Exception.MISSING_REQUIRED_PARAMETER);
}
if (!(typeof path === "string")) {
throw new nl.sara.webdav.Exception('PROPFIND parameter; path should be a string', nl.sara.webdav.Exception.WRONG_TYPE);
}
// Get the full URL, based on the specified path
var url = this.getUrl(path);
// Check the depth header
if (depth === undefined) { // We default depth to 0, not to infinity as this is not supported by all servers
depth = 0;
}
var depthHeader = null;
switch (depth) {
case 0:
case 1:
depthHeader = depth;
break;
case nl.sara.webdav.Client.INFINITY:
depthHeader = 'infinity';
break;
default:
throw new nl.sara.webdav.Exception("Depth header should be '0', '1' or nl.sara.webdav.Client.INFINITY", nl.sara.webdav.Exception.WRONG_VALUE);
break;
}
// Create the request XML
if (props === undefined) {
props = nl.sara.webdav.Client.ALLPROP;
}
var propsBody = document.implementation.createDocument("DAV:", "propfind", null);
switch (props) { // Find out what the request is for
case nl.sara.webdav.Client.PROPNAME: // User wants all property names
propsBody.documentElement.appendChild(propsBody.createElementNS('DAV:', 'propname'));
break;
case nl.sara.webdav.Client.ALLPROP: // User wants all properties
propsBody.documentElement.appendChild(propsBody.createElementNS('DAV:', 'allprop'));
if (include !== undefined) { // There is content for the <DAV: include> tags, so parse it
if (!(include instanceof Array)) {
throw new nl.sara.webdav.Exception('Propfind parameter; include should be an array', nl.sara.webdav.Exception.WRONG_TYPE);
}
var includeBody = propsBody.createElementNS('DAV:', 'include');
for (var i = 0; i < include.length; i++) {
var item = include[i];
if (!nl.sara.webdav.Ie.isIE && !(item instanceof nl.sara.webdav.Property)) {
continue;
}
includeBody.appendChild(propsBody.createElementNS(item.namespace, item.tagname));
}
if (includeBody.hasChildNodes()) { // But only really add the <include> tag if there is valid content
propsBody.documentElement.appendChild(includeBody);
}
}
break;
default: // The default is to expect an array with Property objects; the user wants the values of these properties
if (!(props instanceof Array)) {
throw new nl.sara.webdav.Exception('Props parameter should be nl.sara.webdav.Client.PROPNAME, nl.sara.webdav.Client.ALLPROP or an array with Property objects', nl.sara.webdav.Exception.WRONG_VALUE);
}
var propertyBody = propsBody.createElementNS('DAV:', 'prop');
for (var i = 0; i < props.length; i++) { // Cycle through the array
var prop = props[i];
if (!nl.sara.webdav.Ie.isIE && !(prop instanceof nl.sara.webdav.Property)) {
continue;
}
propertyBody.appendChild(propsBody.createElementNS(prop.namespace, prop.tagname));
}
if (!propertyBody.hasChildNodes()) { // But if no properties are found, then the array didn't have Property objects in it
throw new nl.sara.webdav.Exception("Propfind parameter; if props is an array, it should be an array of Properties", nl.sara.webdav.Exception.WRONG_TYPE);
}
propsBody.documentElement.appendChild(propertyBody);
break;
}
var serializer = new XMLSerializer();
var body = '<?xml version="1.0" encoding="utf-8" ?>' + serializer.serializeToString(propsBody);
// And then send the request
var ajax = this.getAjax("PROPFIND", url, callback, headers);
ajax.setRequestHeader('Depth', depthHeader);
ajax.setRequestHeader('Content-Type', 'application/xml; charset="utf-8"');
ajax.send(body);
return this;
};
/**
* Perform a WebDAV PROPPATCH request
*
* @param {String} path The path do a PROPPATCH on
* @param {Function(status,body,headers)} callback Querying the server is done asynchronously, this callback function is called when the results are in
* @param {nl.sara.webdav.Property[]} [setProps] Optional; The properties to set. If not set, delProps should be set. Can be omitted with 'undefined'.
* @param {nl.sara.webdav.Property[]} [delProps] Optional; The properties to delete. If not set, setProps should be set.
* @param {Array} headers Optional; Additional headers to set
* @returns {nl.sara.webdav.Client} The client itself for chaining methods
*/
nl.sara.webdav.Client.prototype.proppatch = function(path, callback, setProps, delProps, headers) {
if ((path === undefined) || (callback === undefined) || ((setProps === undefined) && (delProps === undefined))) {
throw new nl.sara.webdav.Exception('PROPPATCH requires the parameters path, callback and at least one of setProps or delProps', nl.sara.webdav.Exception.MISSING_REQUIRED_PARAMETER);
}
if (!(typeof path === "string") || ((setProps !== undefined) && !(setProps instanceof Array)) || ((delProps !== undefined) && !(delProps instanceof Array))) {
throw new nl.sara.webdav.Exception('PROPPATCH parameter; path should be a string, setProps and delProps should be arrays', nl.sara.webdav.Exception.WRONG_TYPE);
}
// Get the full URL, based on the specified path
var url = this.getUrl(path);
// Create the request XML
var propsBody = document.implementation.createDocument("DAV:", "propertyupdate", null);
if (setProps !== undefined) {
var props = propsBody.createElementNS('DAV:', 'prop');
for (var i = 0; i < setProps.length; i++) { // Cycle through the array
var prop = setProps[i];
if (!nl.sara.webdav.Ie.isIE && !(prop instanceof nl.sara.webdav.Property)) {
continue;
}
var element = propsBody.createElementNS(prop.namespace, prop.tagname);
for (var j = 0; j < prop.xmlvalue.length; j++) {
var nodeCopy = propsBody.importNode(prop.xmlvalue.item(j), true);
element.appendChild(nodeCopy);
}
props.appendChild(element);
}
if (!props.hasChildNodes()) { // But if no properties are found, then the array didn't have Property objects in it
throw new nl.sara.webdav.Exception("Proppatch parameter; setProps should be an array of Properties", nl.sara.webdav.Exception.WRONG_TYPE);
}
var set = propsBody.createElementNS('DAV:', 'set');
set.appendChild(props);
propsBody.documentElement.appendChild(set);
}
if (delProps !== undefined) {
var props = propsBody.createElementNS('DAV:', 'prop');
for (var i = 0; i < delProps.length; i++) { // Cycle through the array
var prop = delProps[i];
if (!nl.sara.webdav.Ie.isIE && !(prop instanceof nl.sara.webdav.Property)) {
continue;
}
var element = propsBody.createElementNS(prop.namespace, prop.tagname);
props.appendChild(element);
}
if (!props.hasChildNodes()) { // But if no properties are found, then the array didn't have Property objects in it
throw new nl.sara.webdav.Exception("Proppatch parameter; delProps should be an array of Properties", nl.sara.webdav.Exception.WRONG_TYPE);
}
var remove = propsBody.createElementNS('DAV:', 'remove');
remove.appendChild(props);
propsBody.documentElement.appendChild(remove);
}
var serializer = new XMLSerializer();
var body = '<?xml version="1.0" encoding="utf-8" ?>' + serializer.serializeToString(propsBody);
// And then send the request
var ajax = this.getAjax("PROPPATCH", url, callback, headers);
ajax.setRequestHeader('Content-Type', 'application/xml; charset="utf-8"');
ajax.send(body);
return this;
};
/**
* Perform a WebDAV MKCOL request
*
* @param {String} path The path to perform MKCOL on
* @param {Function(status,body,headers)} callback Querying the server is done asynchronously, this callback function is called when the results are in
* @param {String} [body] Optional; a body to include in the MKCOL request.
* @param {String} [contenttype='application/xml; charset="utf-8"'] Optional; the content type of the body (i.e. value for the Content-Type header). If omitted, but body is specified, then 'application/xml; charset="utf-8"' is assumed
* @param {Array} headers Optional; Additional headers to set
* @returns {nl.sara.webdav.Client} The client itself for chaining methods
*/
nl.sara.webdav.Client.prototype.mkcol = function(path, callback, body, contenttype, headers) {
if ((path === undefined) || (callback === undefined)) {
throw new nl.sara.webdav.Exception('MKCOL requires the parameters path and callback', nl.sara.webdav.Exception.MISSING_REQUIRED_PARAMETER);
}
if ((typeof path !== "string") || ((contenttype !== undefined) && (typeof contenttype !== 'string'))) {
throw new nl.sara.webdav.Exception('MKCOL parameter; path and contenttype should be strings', nl.sara.webdav.Exception.WRONG_TYPE);
}
// Get the full URL, based on the specified path
var url = this.getUrl(path);
// And then send the request
var ajax = this.getAjax("MKCOL", url, callback, headers);
if (body !== undefined) {
if (contenttype !== undefined) {
ajax.setRequestHeader('Content-Type', contenttype);
}
ajax.send(body);
}else{
ajax.send();
}
return this;
};
/**
* Perform a WebDAV DELETE request
*
* Because 'delete' is an operator in JavaScript, I had to name this method
* 'remove'. However, performs a regular DELETE request as described in
* RFC 4918
*
* @param {String} path The path to perform DELETE on
* @param {Function(status,body,headers)} callback Querying the server is done asynchronously, this callback function is called when the results are in
* @param {Array} headers Optional; Additional headers to set
* @returns {nl.sara.webdav.Client} The client itself for chaining methods
*/
nl.sara.webdav.Client.prototype.remove = function(path, callback, headers) {
if ((path === undefined) || (callback === undefined)) {
throw new nl.sara.webdav.Exception('DELETE requires the parameters path and callback', nl.sara.webdav.Exception.MISSING_REQUIRED_PARAMETER);
}
if (typeof path !== "string"){
throw new nl.sara.webdav.Exception('DELETE parameter; path should be strings', nl.sara.webdav.Exception.WRONG_TYPE);
}
// Get the full URL, based on the specified path
var url = this.getUrl(path);
// And then send the request
var ajax = this.getAjax("DELETE", url, callback, headers);
ajax.send();
return this;
};
/**
* Perform a WebDAV GET request
*
* @param {String} path The path to GET
* @param {Function(status,content)} callback Querying the server is done asynchronously, this callback function is called when the results are in
* @param {Array} headers Optional; Additional headers to set
* @returns {nl.sara.webdav.Client} The client itself for chaining methods
*/
nl.sara.webdav.Client.prototype.get = function(path, callback, headers) {
if ((path === undefined) || (callback === undefined)) {
throw new nl.sara.webdav.Exception('GET requires the parameters path and callback', nl.sara.webdav.Exception.MISSING_REQUIRED_PARAMETER);
}
if (typeof path !== "string"){
throw new nl.sara.webdav.Exception('GET parameter; path should be strings', nl.sara.webdav.Exception.WRONG_TYPE);
}
// Get the full URL, based on the specified path
var url = this.getUrl(path);
// And then send the request
var ajax = null;
ajax = this.getAjax("GET", url, callback, headers);
ajax.send();
return this;
};
/**
* Perform a WebDAV HEAD request
*
* @param {String} path The path to perform HEAD on
* @param {Function(status,body,headers)} callback Querying the server is done asynchronously, this callback function is called when the results are in
* @param {Array} headers Optional; Additional headers to set
* @returns {nl.sara.webdav.Client} The client itself for chaining methods
*/
nl.sara.webdav.Client.prototype.head = function(path, callback, headers) {
if ((path === undefined) || (callback === undefined)) {
throw new nl.sara.webdav.Exception('HEAD requires the parameters path and callback', nl.sara.webdav.Exception.MISSING_REQUIRED_PARAMETER);
}
if (typeof path !== "string"){
throw new nl.sara.webdav.Exception('HEAD parameter; path should be strings', nl.sara.webdav.Exception.WRONG_TYPE);
}
// Get the full URL, based on the specified path
var url = this.getUrl(path);
// And then send the request
var ajax = null;
ajax = this.getAjax("HEAD", url, callback, headers);
ajax.send();
return this;
};
/**
* Perform a WebDAV PUT request
*
* @param {String} path The path to perform PUT on
* @param {Function(status,body,headers)} callback Querying the server is done asynchronously, this callback function is called when the results are in
* @param {String} body The content to include in the PUT request.
* @param {String} [contenttype] Optional; the content type of the body (i.e. value for the Content-Type header).
* @param {Array} headers Optional; Additional headers to set
* @returns {nl.sara.webdav.Client} The client itself for chaining methods
*/
nl.sara.webdav.Client.prototype.put = function(path, callback, body, contenttype, headers) {
if ((path === undefined) || (callback === undefined) || (body === undefined)) {
throw new nl.sara.webdav.Exception('PUT requires the parameters path, callback and body', nl.sara.webdav.Exception.MISSING_REQUIRED_PARAMETER);
}
if ((typeof path !== "string") || ((contenttype !== undefined) && (typeof contenttype !== 'string'))) {
throw new nl.sara.webdav.Exception('PUT parameter; path and contenttype should be strings', nl.sara.webdav.Exception.WRONG_TYPE);
}
// Get the full URL, based on the specified path
var url = this.getUrl(path);
// And then send the request
var ajax = null;
ajax = this.getAjax("PUT", url, callback, headers);
if (contenttype !== undefined) {
ajax.setRequestHeader('Content-Type', contenttype);
}
ajax.send(body);
return this;
};
/**
* Perform a WebDAV OPTIONS request
*
* @param {String} path The path to perform OPTIONS on
* @param {Function(status,body,headers)} callback Querying the server is done asynchronously, this callback function is called when the results are in
* @param {String} [body] Optional; a body to include in the OPTIONS request.
* @param {String} [contenttype='application/x-www-form-urlencoded'] Optional; the content type of the body (i.e. value for the Content-Type header). If omitted, but body is specified, then 'application/x-www-form-urlencoded' is assumed
* @param {Array} headers Optional; Additional headers to set
* @returns {nl.sara.webdav.Client} The client itself for chaining methods
*/
nl.sara.webdav.Client.prototype.options = function(path, callback, body, contenttype, headers) {
if ((path === undefined) || (callback === undefined)) {
throw new nl.sara.webdav.Exception('OPTIONS requires the parameters path and callback', nl.sara.webdav.Exception.MISSING_REQUIRED_PARAMETER);
}
if ((typeof path !== "string") || ((body !== undefined) && (typeof body !== 'string')) || ((contenttype !== undefined) && (typeof contenttype !== 'string'))) {
throw new nl.sara.webdav.Exception('OPTIONS parameter; path, body and contenttype should be strings', nl.sara.webdav.Exception.WRONG_TYPE);
}
// Get the full URL, based on the specified path
var url = this.getUrl(path);
// And then send the request
var ajax = null;
ajax = this.getAjax("OPTIONS", url, callback, headers);
if ( body !== undefined ) {
if (contenttype !== undefined) {
ajax.setRequestHeader('Content-Type', contenttype);
}else{
ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
ajax.send(body);
}else{
ajax.send();
}
return this;
};
/**
* Perform a WebDAV POST request
*
* @param {String} path The path to perform POST on
* @param {Function(status,body,headers)} callback Querying the server is done asynchronously, this callback function is called when the results are in
* @param {String} [body] Optional; a body to include in the POST request.
* @param {String} [contenttype='application/x-www-form-urlencoded'] Optional; the content type of the body (i.e. value for the Content-Type header). If omitted, but body is specified, then 'application/x-www-form-urlencoded' is assumed
* @param {Array} headers Optional; Additional headers to set
* @returns {nl.sara.webdav.Client} The client itself for chaining methods
*/
nl.sara.webdav.Client.prototype.post = function(path, callback, body, contenttype, headers) {
if ((path === undefined) || (callback === undefined)) {
throw new nl.sara.webdav.Exception('POST requires the parameters path and callback', nl.sara.webdav.Exception.MISSING_REQUIRED_PARAMETER);
}
if ((typeof path !== "string") || ((body !== undefined) && (typeof body !== 'string')) || ((contenttype !== undefined) && (typeof contenttype !== 'string'))) {
throw new nl.sara.webdav.Exception('POST parameter; path, body and contenttype should be strings', nl.sara.webdav.Exception.WRONG_TYPE);
}
// Get the full URL, based on the specified path
var url = this.getUrl(path);
// And then send the request
var ajax = null;
ajax = this.getAjax("POST", url, callback, headers);
if ( body !== undefined ) {
if (contenttype !== undefined) {
ajax.setRequestHeader('Content-Type', contenttype);
}else{
ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
ajax.send(body);
}else{
ajax.send();
}
return this;
};
/**
* Perform a WebDAV COPY request
*
* @param {String} path The path to perform COPY on
* @param {Function(status,body,headers)} callback Querying the server is done asynchronously, this callback function is called when the results are in
* @param {String} destination The destination to copy to. Should be either a full URL or a path from the root on this server (i.e. it should start with a /)
* @param {Integer} [overwriteMode=SILENT_OVERWRITE] Optional; Specify what to do when destination resource already exists. Should be either FAIL_ON_OVERWRITE or SILENT_OVERWRITE. The default is SILENT_OVERWRITE.
* @param {String} [depth] Optional; Should be '0' or 'infinity'. This is used in case of a collection; 0 means only copy the collection itself, infinity means copy also everything contained in the collection
* @param {Array} headers Optional; Additional headers to set
* @returns {nl.sara.webdav.Client} The client itself for chaining methods
*/
nl.sara.webdav.Client.prototype.copy = function(path, callback, destination, overwriteMode, depth, headers) {
if ((path === undefined) || (callback === undefined) || (destination === undefined)) {
throw new nl.sara.webdav.Exception('COPY requires the parameters path, callback and destination', nl.sara.webdav.Exception.MISSING_REQUIRED_PARAMETER);
}
if ((typeof path !== "string") || (typeof destination !== "string")){
throw new nl.sara.webdav.Exception('COPY parameter; path and destination should be strings', nl.sara.webdav.Exception.WRONG_TYPE);
}
// Get the full URL, based on the specified path
var url = this.getUrl(path);
// If the destination starts with a / it is a absolute url on the same host, so prepare a complete URL
if (destination.substr(0,1) === '/') {
destination = this.getUrl(destination);
} // Else I assume it is a complete URL already
// And then send the request
var ajax = this.getAjax("COPY", url, callback, headers);
ajax.setRequestHeader('Destination', destination);
if (depth !== undefined) {
if ((depth !== 0) && (depth !== 'infinity')) {
throw new nl.sara.webdav.Exception("COPY parameter; depth should be '0' or 'infinity'", nl.sara.webdav.Exception.WRONG_VALUE);
}
ajax.setRequestHeader('Depth', depth);
}
if (overwriteMode === nl.sara.webdav.Client.FAIL_ON_OVERWRITE) {
ajax.setRequestHeader('Overwrite', 'F');
}
ajax.send();
return this;
};
/**
* Perform a WebDAV MOVE request
*
* @param {String} path The path to perform MOVE on
* @param {Function(status,body,headers)} callback Querying the server is done asynchronously, this callback function is called when the results are in
* @param {String} destination The destination to move to. Should be either a full URL or a path from the root on this server (i.e. it should start with a /)
* @param {Number} [overwriteMode=SILENT_OVERWRITE] Optional; Specify what to do when destination resource already exists. Should be either FAIL_ON_OVERWRITE, TRUNCATE_ON_OVERWRITE or SILENT_OVERWRITE. The default is SILENT_OVERWRITE.
* @param {Array} headers Optional; Additional headers to set
* @returns {nl.sara.webdav.Client} The client itself for chaining methods
*/
nl.sara.webdav.Client.prototype.move = function(path, callback, destination, overwriteMode, headers) {
if ((path === undefined) || (callback === undefined) || (destination === undefined)) {
throw new nl.sara.webdav.Exception('MOVE requires the parameters path, callback and destination', nl.sara.webdav.Exception.MISSING_REQUIRED_PARAMETER);
}
if ((typeof path !== "string") || (typeof destination !== "string")){
throw new nl.sara.webdav.Exception('MOVE parameter; path and destination should be strings', nl.sara.webdav.Exception.WRONG_TYPE);
}
// Get the full URL, based on the specified path
var url = this.getUrl(path);
// If the destination starts with a / it is a absolute url on the same host, so prepare a complete URL
if (destination.substr(0,1) === '/') {
destination = this.getUrl(destination);
} // Else I assume it is a complete URL already
// And then send the request
var ajax = this.getAjax("MOVE", url, callback, headers);
ajax.setRequestHeader('Destination', destination);
if (overwriteMode === nl.sara.webdav.Client.FAIL_ON_OVERWRITE) {
ajax.setRequestHeader('Overwrite', 'F');
}else if (overwriteMode === nl.sara.webdav.Client.TRUNCATE_ON_OVERWRITE) {
ajax.setRequestHeader('Overwrite', 'T');
}
ajax.send();
return this;
};
/**
* Perform a WebDAV LOCK request
*
* @param {String} path The path to perform LOCK on
* @param {Function(status,body,headers)} callback Querying the server is done asynchronously, this callback function is called when the results are in
* @param {Document} body Optional; The (XML DOM) document to parse and send as the request body
* @param {Array} headers Optional; Additional headers to set
* @returns {nl.sara.webdav.Client} The client itself for chaining methods
*/
nl.sara.webdav.Client.prototype.lock = function(path, callback, body, headers) {
if ((path === undefined) || (callback === undefined)) {
throw new nl.sara.webdav.Exception('LOCK requires the parameters path and callback', nl.sara.webdav.Exception.MISSING_REQUIRED_PARAMETER);
}
if ((typeof path !== "string") || (!nl.sara.webdav.Ie.isIE && (body !== undefined) && !(body instanceof Document))) {
throw new nl.sara.webdav.Exception('LOCK parameter; path should be a string, body should be an instance of Document', nl.sara.webdav.Exception.WRONG_TYPE);
}
// Get the full URL, based on the specified path
var url = this.getUrl(path);
// Parse the body
var serializer = new XMLSerializer();
var body = '<?xml version="1.0" encoding="utf-8" ?>' + serializer.serializeToString(body);
// And then send the request
var ajax = null;
ajax = this.getAjax("LOCK", url, callback, headers);
if (body !== undefined) {
ajax.setRequestHeader('Content-Type', 'application/xml; charset="utf-8"');
ajax.send(body);
}else{
ajax.send();
}
return this;
};
/**
* Perform a WebDAV UNLOCK request
*
* @param {String} path The path to perform UNLOCK on
* @param {Function(status,body,headers)} callback Querying the server is done asynchronously, this callback function is called when the results are in
* @param {String} lockToken The lock token to unlock
* @param {Array} headers Optional; Additional headers to set
* @returns {nl.sara.webdav.Client} The client itself for chaining methods
*/
nl.sara.webdav.Client.prototype.unlock = function(path, callback, lockToken, headers) {
if ((path === undefined) || (callback === undefined) || (lockToken === undefined)) {
throw new nl.sara.webdav.Exception('UNLOCK requires the parameters path, callback and lockToken', nl.sara.webdav.Exception.MISSING_REQUIRED_PARAMETER);
}
if ( (typeof path !== "string") || (typeof lockToken !== "string") ){
throw new nl.sara.webdav.Exception('UNLOCK parameter; path and lockToken should be strings', nl.sara.webdav.Exception.WRONG_TYPE);
}
// Get the full URL, based on the specified path
var url = this.getUrl(path);
// And then send the request
headers['Lock-Token'] = lockToken;
var ajax = null;
ajax = this.getAjax("UNLOCK", url, callback, headers);
ajax.send();
return this;
};
/**
* Prepares a XMLHttpRequest object to be used for an AJAX request
*
* @static
* @param {String} method The HTTP/webDAV method to use (e.g. GET, POST, PROPFIND)
* @param {String} url The url to connect to
* @param {Function(status,body,headers)} callback Querying the server is done asynchronously, this callback function is called when the results are in
* @param {Array} headers Additional headers to set
* @returns {XMLHttpRequest} A prepared XMLHttpRequest
*/
nl.sara.webdav.Client.prototype.getAjax = function(method, url, callback, headers) {
var /** @type XMLHttpRequest */ ajax = ( ( ( typeof Components !== 'undefined' ) && ( typeof Components.classes !== 'undefined' ) ) ? Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest) : new XMLHttpRequest());
if ( this._username !== null ) {
ajax.open( method, url, true, this._username, this._password );
}else{
ajax.open( method, url, true );
}
ajax.onreadystatechange = function() {
nl.sara.webdav.Client.ajaxHandler( ajax, callback );
};
if (headers === undefined) {
headers = {};
}
for (var header in this._headers) {
if (headers[header] === undefined) {
ajax.setRequestHeader(header, this._headers[header]);
}
}
for (var header in headers) {
ajax.setRequestHeader(header, headers[header]);
}
return ajax;
};
/**
* AJAX request handler. Parses Multistatus (if available) and call a user specified callback function
*
* @static
* @param {XMLHttpRequest} ajax The XMLHttpRequest object which performed the request
* @param {Function(status,body,headers)} callback Querying the server is done asynchronously, this callback function is called when the results are in
* @returns {void}
*/
nl.sara.webdav.Client.ajaxHandler = function(ajax, callback) {
if (ajax.readyState === 4){ //if request has completed
var body = ajax.responseText;
if (ajax.status === 207) {
// Parse the response to a Multistatus object
for (var counter = 0; counter < ajax.responseXML.childNodes.length; counter++) {
if (nl.sara.webdav.Ie.getLocalName(ajax.responseXML.childNodes.item(counter)) === 'multistatus') {
body = new nl.sara.webdav.Multistatus(ajax.responseXML.childNodes.item(counter));
break;
}
}
}
callback(ajax.status, body, ajax.getAllResponseHeaders());
}
};
// End of library
/*
* Copyright ©2012 SARA bv, The Netherlands
*
* This file is part of js-webdav-client.
*
* js-webdav-client is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* js-webdav-client is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with js-webdav-client. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
// If nl.sara.webdav.Codec is already defined, we have a namespace clash!
if (nl.sara.webdav.Codec !== undefined) {
throw new nl.sara.webdav.Exception('Namespace name nl.sara.webdav.Codec already taken, could not load JavaScript library for WebDAV connectivity.', nl.sara.webdav.Exception.NAMESPACE_TAKEN);
}
/**
* @class A codec transcodes the xml to a custom Javascript object
*
* @param {String} [namespace] Optional; The namespace of the property to transcode
* @param {String} [tagname] Optional; The tag name of the property to transcode
* @param {function(mixed[,xmlDoc])} [toXML] Optional; Function which should return a Document with the NodeList of the documentElement as the value of this property
* @param {function(NodeList)} [fromXML] Optional; Functions which should return a representation of xmlvalue
* @property {String} namespace The namespace of the property to transcode
* @property {String} tagname The tag name of the property to transcode
* @property {function(mixed[,xmlDoc])} toXML Function which should return a Document with the NodeList of the documentElement as the value of this property
* @property {function(NodeList)} fromXML Functions which should return a representation of xmlvalue
*/
nl.sara.webdav.Codec = function(namespace, tagname, toXML, fromXML) {
// First define public attributes
Object.defineProperty(this, 'namespace', {
'value': null,
'enumerable': true,
'configurable': false,
'writable': true
});
Object.defineProperty(this, 'tagname', {
'value': null,
'enumerable': true,
'configurable': false,
'writable': true
});