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
|
(function(module, ns) {
var SAX = ns.require('sax');
var XHR = ns.require('xhr');
var Errors = ns.require('errors');
function determineHttpStatusError(status) {
var message = 'Cannot handle request due to server response';
var err = 'Unknown';
if (status === 500)
err = 'ServerFailure';
if (status === 401)
err = 'Authentication';
return new Errors[err](message);
}
/**
* Creates an (Web/Cal)Dav request.
*
* @param {Caldav.Connection} connection connection details.
* @param {Object} options additional options for request.
*/
function Abstract(connection, options) {
if (typeof(options) === 'undefined') {
options = {};
}
var key;
var xhrOptions = {};
this.sax = new SAX();
for (key in options) {
if (Object.hasOwnProperty.call(options, key)) {
this[key] = options[key];
}
}
if (!connection) {
throw new Error('must pass connection object');
}
this.connection = connection;
this.xhr = this.connection.request({
url: this.url,
headers: { 'Content-Type': 'text/xml' }
});
}
Abstract.prototype = {
_createPayload: function() {
return '';
},
_processResult: function(req, callback) {
callback.call(this, null, this.sax.root, req);
},
/**
* Sends request to server.
*
* @param {Function} callback node style callback.
* Receives three arguments
* error, parsedData, xhr.
* @return {Caldav.Xhr} The xhr request so that the caller
* has a chance to abort the request.
*/
send: function(callback) {
var self = this;
var req = this.xhr;
req.data = this._createPayload();
req.ondata = function xhrOnData(chunk) {
self.sax.write(chunk);
};
// in the future we may stream data somehow
req.send(function xhrResult(err, xhr) {
if (err) {
return callback(err);
}
// handle the success case
if (xhr.status > 199 && xhr.status < 300) {
self.sax.close();
return self._processResult(req, callback);
}
// probable error cases
callback(
determineHttpStatusError(xhr.status),
xhr
);
});
return req;
}
};
module.exports = Abstract;
}.apply(
this,
(this.Caldav) ?
[Caldav('request/abstract'), Caldav] :
[module, require('../caldav')]
));
|