-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathxmlrpc-c.js
311 lines (247 loc) · 7.53 KB
/
xmlrpc-c.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
/*
* xmlrpc-c module for node.js
*
* Copyright (c) 2009 Claudio Cicali <claudio.cicali@gmail.com>
* See COPYING for licensing information.
*
* Snippets and inspiration credits to xmlrpc_lib.js by Gaetano Giunta
*
*/
//var sys = require('sys');
var http = require('http');
var libxml = require('./libxmljs');
var headers = {
"User-Agent": "NodeJS XML-RPC Client",
"Content-Type": "text/xml",
"Accept": "text/xml",
"Accept-Charset": "UTF8"
}
var cookies;
var Client = function(httpclient, path) {
this.httpclient = httpclient;
this.path = path;
this.headers = headers;
this.headers.host = httpclient.host;
};
Client.prototype = new process.EventEmitter();
exports.createClient = function(port, hostname, path) {
var client = new Client(http.createClient(port, hostname), path);
return client;
}
exports.createHttpClient = function(client, path) {
var client = new Client(client, path);
return client;
}
Client.prototype.call = function(method, params, callback) {
params = params || [];
var client = this.httpclient;
var doc = new libxml.Document()
var body = doc.node('methodCall');
body.node('methodName', method);
body.node('params', function(p) {
for (var i=0; i < params.length; i++) {
_serialize(params[i], p.node('param'));
}
function _serialize(param, parent) {
var value = parent.node('value');
switch(param.constructor.name) {
case 'Number':
value.node(("" + param).match(/^\d*\.\d+$/) ? 'double' : 'int', "" + param);
break;
case 'Array':
var data = value.node('array').node('data');
for(var n=0; n < param.length; n++) {
_serialize(param[n], data);
}
break;
case 'Object':
var data = value.node('struct');
var member;
for(var key in param) {
if (param.hasOwnProperty(key)) {
member = data.node('member');
member.node('name', key)
_serialize(param[key], member);
}
}
break;
case 'Boolean':
value.node('boolean', "" + !!param);
break;
case 'Date':
value.node('dateTime.iso8601', H.iso8601Encode(param));
break;
case 'String':
default:
value.node('string', param);
break;
}
}
});
body = doc.toString();
headers['Content-Length'] = body.length;
if (cookies) {
var cookiehdr = '';
for (var key in cookies) {
cookiehdr += key + '=' + cookies[key] + '; ';
}
if (cookiehdr.length)
cookiehdr = cookiehdr.substring(0, cookiehdr.length - 2);
headers['Cookie'] = cookiehdr;
}
var req = client.request('POST', this.path, headers);
req.write(body, 'utf8');
var me = this;
req.addListener("response", function(res) {
var payload = "";
res.setEncoding("utf8");
if (res.statusCode != 200) {
return me.emit("error", method, "Status is not 200: " + res.statusCode);
}
var set_cookie = res.headers['set-cookie'];
if (set_cookie) {
for (var i in set_cookie) {
cookie = set_cookie[i];
cookie = cookie.split(';')[0];
cookie = cookie.split('=');
if (!cookies)
cookies = [];
cookies[cookie[0]] = cookie[1];
}
}
res.addListener("data", function(payload) {
payload = H.trim(payload);
// be tolerant of junk after methodResponse (e.g. javascript ads automatically inserted by free hosts)
var pos = payload.lastIndexOf('</methodResponse>');
if (pos >= 0) {
payload = payload.slice(0, pos + 17);
}
if (payload == '') {
me.emit('success', method, payload);
}
var doc, response, fault;
try {
doc = libxml.parseXmlString(payload);
response = doc.root();
} catch(e) {
return me.emit('error', method, "Response seems not a regular XMLRPC one");
}
var fault;
if (fault = H.getFault(response)) {
return me.emit('error', method, "XMLRPC error: " + fault.faultString + " (" + fault.faultCode + ")");
}
var value = response.get('params/param/value');
me.emit('success', method, H.parseValue(value));
});
});
req.end();
return this;
}
/* Various helpers */
var H = {
getFault: function(response) {
var fault;
if (fault = response.get('fault')) {
return H.parseValue(fault.get('value'));
}
return null;
},
parseValue: function(element) {
// Segmentation fault using element.child(0)
var container = H.getFirstChild(element);
var val;
switch(container.name()) {
case 'struct':
var struct = {};
var members = container.find('member');
var name;
for (var i=0; i < members.length; i++) {
name = members[i].get('name').text();
struct[name] = H.parseValue(members[i].get('value'));
}
return struct;
case 'i4':
case 'int':
var val = container.text();
if (!val.match(/^[+-]?[0123456789 \t]+$/)) {
return 0;
}
return parseInt(val);
case 'array':
var data = container.find('data/value');
var array = [];
for (var i=0; i < data.length; i++) {
array.push(H.parseValue(data[i]));
}
return array;
case 'float':
case 'double':
var val = container.text();
if (!val.match(/^[+-]?[eE0123456789 \t.]+$/)) {
return 0.0;
}
return parseFloat(val);
case 'boolean':
var val = container.text();
if (val == '1' || val.match(/^true$/i)) {
return true;
}
return false;
case 'dateTime.iso8601':
return H.iso8601Decode(container.text());
case 'string':
case 'base64':
default:
return container.text();
}
},
iso8601Decode: function(time) {
/* http://delete.me.uk/2005/03/iso8601.html */
var regexp = "([0-9]{4})([-]?([0-9]{2})([-]?([0-9]{2})" +
"(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
"(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
var d = time.match(new RegExp(regexp));
var offset = 0;
var date = new Date(d[1], 0, 1);
if (d[3]) { date.setMonth(d[3] - 1); }
if (d[5]) { date.setDate(d[5]); }
if (d[7]) { date.setHours(d[7]); }
if (d[8]) { date.setMinutes(d[8]); }
if (d[10]) { date.setSeconds(d[10]); }
if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
return date;
},
iso8601Encode: function(date) {
return H.zeroPad(date.getFullYear(), 4) +
H.zeroPad(date.getMonth() + 1, 2) +
H.zeroPad(date.getDate(), 2) +
"T" +
H.zeroPad(date.getHours(), 2) +
":" +
H.zeroPad(date.getMinutes(), 2) +
":" +
H.zeroPad(date.getSeconds(), 2);
},
zeroPad: function(v, l) {
var padded = "" + v;
while(padded.length < l) {
padded = "0" + padded;
}
return padded;
},
getFirstChild: function(element) {
var children = element.childNodes();
for (var i=0; i < children.length; i++) {
if (children[i].type() != 'text') {
return children[i];
}
}
return children[0];
},
encodeEntities: function (data) {
return new String(data).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
},
trim: function(text) {
return (text || "").replace( /^\s+|\s+$/g, "" );
},
}