forked from SeattleTestbed/seattlelib_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlrpc_common.r2py
617 lines (433 loc) · 14.3 KB
/
xmlrpc_common.r2py
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
"""
<Program Name>
xmlrpc_common.r2py
<Started>
April 26, 2009
<Author>
Michael Phan-Ba
<Purpose>
Provides common methods related to XML-RPC.
Encoding dateTime.iso8601 are not currently supported.
<Changes>
2009-04-26 Michael Phan-Ba <[email protected]>
* Initial release
2009-05-24 Michael Phan-Ba <[email protected]>
* Added change log
* Fixed base64 name error
* Set property svn:keyword to "Id"
"""
base64 = dy_import_module('base64.r2py')
xmlparse = dy_import_module('xmlparse.r2py')
class xmlrpc_common_Binary(object):
"""
<Purpose>
Wrapper class for base64-encoded binary data in XML-RPC requests and
responses. This class is used when sending and receiving binary
data through XML-RPC.
<Side Effects>
None.
<Example Use>
blob = xmlrpc_common_Binary("\x00\x01\x00")
"""
def __init__(self, data=""):
"""
<Purpose>
Create a new Binary wrapper object for use with the XML-RPC
libraries.
<Arguments>
data:
The unencoded binary data.
<Exceptions>
None.
"""
self.data = data
class xmlrpc_common_Fault(ValueError):
"""
<Purpose>
Exception representing a XML-RPC Fault. The exception is returned
by the parsing functions when a XML-RPC server returns a fault.
<Side Effects>
None.
<Example Use>
raise xmlrpc_common_Fault("An error occurred", -1)
"""
def __init__(self, message, code):
"""
<Purpose>
Create a new Fault exception.
<Arguments>
message:
A string describing the fault.
code:
The integer code associated with the fault.
<Exceptions>
None.
"""
self.strerror = message
self.code = code
ValueError.__init__(self, message)
class xmlrpc_common_Timeout(Exception):
"""
<Purpose>
Exception representing a normal timeout occuring.
<Side Effects>
None.
<Example Use>
raise xmlrpc_common_Timeout()
"""
class xmlrpc_common_XMLParseError(ValueError):
"""
<Purpose>
Exception representing an error in parsing XML-RPC data. The
exception is thrown when bad XML data is encountered.
<Side Effects>
None.
<Example Use>
raise xmlrpc_common_XMLParseError()
"""
class xmlrpc_common_ConnectionError(ValueError):
"""
<Purpose>
Exception representing an error in the connection to an XMLRPC server.
Thrown when the server closes the connection unexpectedly.
<Side Effects>
None.
<Example Use>
raise xmlrpc_common_ConnectionError()
"""
def xmlrpc_common_call2xml(method_name, params):
"""
<Purpose>
Build a XML-RPC method call to send to a XML-RPC server.
<Arguments>
method_name:
The method name.
params:
A sequence type of XML-RPC parameters. A dictionary may also be
passed, but the keys are ignored.
<Exceptions>
None.
<Side Effects>
None.
<Returns>
The XML-RPC method call string.
"""
xml_string = ['<?xml version="1.0"?>',
"<methodCall><methodName>%s</methodName>" % method_name,
_xmlrpc_common_params2xml(params),
"</methodCall>"]
return "".join(xml_string)
def xmlrpc_common_response2xml(param):
"""
<Purpose>
Build a XML-RPC method response to send to a XML-RPC client. This
is the XML document that represents the return values or fault from
a XML-RPC call.
<Arguments>
param:
The value to be returned.
<Exceptions>
None.
<Side Effects>
None.
<Returns>
The XML-RPC method response string.
"""
xml_string = ['<?xml version="1.0"?><methodResponse>',
_xmlrpc_common_params2xml((param,)),
"</methodResponse>"]
return "".join(xml_string)
def xmlrpc_common_fault2xml(message, code):
"""
<Purpose>
Build a XML-RPC fault response to send to a XML-RPC client. A fault
response can occur from a server failure, an incorrectly generated
XML request, or bad program arguments.
<Arguments>
message:
A string describing the fault.
code:
The integer code associated with the fault.
<Exceptions>
None.
<Side Effects>
None.
<Returns>
The XML-RPC fault response string.
"""
struct = {"faultCode": code, "faultString": message}
xml_string = ['<?xml version="1.0"?><methodResponse><fault>',
_xmlrpc_common_value2xml(struct),
"</fault></methodResponse>"]
return "".join(xml_string)
def _xmlrpc_common_params2xml(params):
"""
<Purpose>
Translate Python parameter values to XML-RPC for use in building a
XML-RPC request or response.
<Arguments>
params:
A sequence type of XML-RPC parameters. A dictionary may also be
passed, but the keys are ignored.
<Exceptions>
None.
<Side Effects>
None.
<Returns>
The XML-RPC parameters string.
"""
if params is None or params is ():
return ""
xml_string = ["<params>"]
for param in params:
xml_string.append("<param>%s</param>" % _xmlrpc_common_value2xml(param))
xml_string.append("</params>")
return "".join(xml_string)
def _xmlrpc_common_value2xml(obj):
"""
<Purpose>
Translate a Python value to XML-RPC for use in building the params
portion of a request or response.
<Arguments>
obj:
The Python object to convert.
<Exceptions>
None.
<Side Effects>
None.
<Returns>
The XML-RPC value string.
"""
object_type = type(obj)
xml_string = ["<value>"]
if obj is None:
xml_string.append("<nil/>")
elif object_type is bool:
xml_string.append("<boolean>%d</boolean>" % int(obj))
elif object_type in (int, long):
xml_string.append("<int>%d</int>" % obj)
elif object_type is float:
xml_string.append("<double>%f</double>" % obj)
elif object_type in (str, unicode, basestring):
xml_string.append("<string>%s</string>" % obj)
elif object_type in (list, tuple, xrange, set, frozenset):
xml_string.append("<array><data>")
for value in obj:
xml_string.append(_xmlrpc_common_value2xml(value))
xml_string.append("</data></array>")
elif object_type is dict:
xml_string.append("<struct>")
for key, value in obj.iteritems():
xml_string.append("<member><name>%s</name>" % key)
xml_string.append(_xmlrpc_common_value2xml(value))
xml_string.append("</member>")
xml_string.append("</struct>")
# This requires the new object inheritance model to be used. e.g. do
# class Foo(object): pass
# rather than
# class Foo: pass
elif object_type is xmlrpc_common_Binary:
xml_string.append("<base64>%s</base64>" % base64.base64_standard_b64encode(obj.data))
else:
raise ValueError("Marshaller: Unsupported type '%s'" % type(obj))
xml_string.append("</value>")
return "".join(xml_string)
def xmlrpc_common_call2python(xml):
"""
<Purpose>
Convert a XML-RPC method call to its Python equivalent.
The request from a XML-RPC client is parsed into native Python
types so that the server may use the data to execute a method, as
appropriate.
<Arguments>
xml:
The XML-RPC string to convert.
<Exceptions>
xmlrpc_common_XMLParseError on a XML-RPC structural parse error.
xmlparse_XMLParseError on a general XML parse error.
<Side Effects>
None.
<Returns>
A tuple containing (1) the method name and (2) a list of the
parameters.
"""
xml_node = xmlparse.xmlparse_parse(xml)
if xml_node.tag_name != "methodCall":
message = "Unexpected root node: %s" % xml_node.tag_name
raise xmlrpc_common_XMLParseError(message)
elif xml_node.children is None:
raise xmlrpc_common_XMLParseError("No parameters found")
elif len(xml_node.children) > 2:
raise xmlrpc_common_XMLParseError("Too many children for 'methodCall'")
try:
method_name_node = xml_node.children[0]
if method_name_node.tag_name != "methodName":
message = "Unexpected XML node: %s" % method_name_node.tag_name
raise xmlrpc_common_XMLParseError(message)
method_name = method_name_node.content
except IndexError:
raise xmlrpc_common_XMLParseError("No method name found")
try:
params = _xmlrpc_common_params2python(xml_node.children[1])
except IndexError:
return (method_name, ())
if not params:
raise xmlrpc_common_XMLParseError("No parameters found")
return (method_name, params)
def xmlrpc_common_response2python(xml):
"""
<Purpose>
Convert a XML-RPC method response to its Python equivalent.
The response from a XML-RPC server is parsed into native Python
types so that the client may use the data as appropriate.
<Arguments>
xml:
The XML-RPC string to convert.
<Exceptions>
xmlrpc_common_XMLParseError on a XML-RPC structural parse error.
xmlparse_XMLParseError on a general XML parse error.
<Side Effects>
None.
<Returns>
The method results or a xmlrpc_common_Fault on reading a fault.
"""
xml_node = xmlparse.xmlparse_parse(xml)
if xml_node.tag_name != "methodResponse":
message = "Unexpected root node: %s" % xml_node.tag_name
raise xmlrpc_common_XMLParseError(message)
elif xml_node.children is None:
raise xmlrpc_common_XMLParseError("No parameters found")
elif len(xml_node.children) > 1:
raise xmlrpc_common_XMLParseError("Too many children for 'methodCall'")
fault_node = xml_node.children[0]
if fault_node.tag_name == "fault":
if fault_node.children is None:
raise xmlrpc_common_XMLParseError("No children found for 'fault'")
elif len(fault_node.children) != 1:
raise xmlrpc_common_XMLParseError("Too many children for 'fault'")
params = _xmlrpc_common_value2python(fault_node.children[0])
try:
return xmlrpc_common_Fault(params["faultString"], params["faultCode"])
except KeyError:
raise xmlrpc_common_XMLParseError("Invalid fault object")
try:
params = _xmlrpc_common_params2python(xml_node.children[0])
except KeyError:
raise xmlrpc_common_XMLParseError("No parameters found")
if len(params) != 1:
raise xmlrpc_common_XMLParseError("Too many children for 'params'")
return params[0]
def _xmlrpc_common_params2python(xml_node):
"""
<Purpose>
Convert XML-RPC params the Python equivalent.
The parameters portion of a XML-RPC request or response is parsed
into Python equivalents so that the method request and response
parsing functions can return the relevant data.
<Arguments>
xml_node:
The XML node to consider.
<Exceptions>
xmlrpc_common_XMLParseError on a XML-RPC structural parse error.
<Side Effects>
None.
<Returns>
The method results.
"""
if xml_node.tag_name != "params":
message = "Unexpected XML node: %s" % xml_node.tag_name
raise xmlrpc_common_XMLParseError(message)
if xml_node.children is None or len(xml_node.children) < 1:
return []
params = []
for param_node in xml_node.children:
if param_node.tag_name != "param":
message = "Unexpected XML node: %s" % param_node.tag_name
raise xmlrpc_common_XMLParseError(message)
elif param_node.children is None:
raise xmlrpc_common_XMLParseError("Unexpected empty param node")
elif len(param_node.children) > 1:
raise xmlrpc_common_XMLParseError("Too many children for 'param'")
params.append(_xmlrpc_common_value2python(param_node.children[0]))
return params
def _xmlrpc_common_value2python(xml_node):
"""
<Purpose>
Convert a XML-RPC value the Python equivalent.
A XML-RPC value is converted to its Python equivalent for use in the
parameters parser.
<Arguments>
xml_node:
The XML node to consider.
<Exceptions>
xmlrpc_common_XMLParseError on a XML-RPC structural parse error.
<Side Effects>
None.
<Returns>
The method results.
"""
if xml_node.tag_name not in ("value",):
message = "Unexpected XML node: %s" % xml_node.tag_name
raise xmlrpc_common_XMLParseError(message)
# The values that XMLRPC can encode have an optional type-specifier.
# If the type-specifier is not included, the data is simply a string
# and doesn't need any other special interpretation. Additionally, there
# is an optional <string> type specifier, but e.g. openDHT doesn't use
# it. If xml_node.children is None here, the data lacks a type-specifying
# tag, so it is to be interpreted as a string.
elif xml_node.children is not None and len(xml_node.children) > 1:
raise xmlrpc_common_XMLParseError("Too many children for 'value'")
value_node = xml_node
# Assume string by default, as explained earlier.
tag = "string"
if xml_node.children is not None:
# If the xml specifies a type, override the default.
value_node = xml_node.children[0]
tag = value_node.tag_name
# The string contents of the <value> tag (or of the type-specifying tag
# inside <value>, if one exists).
value = value_node.content
if tag == "nil":
return None
elif tag == "boolean":
return bool(int(value))
elif tag in ("i4", "int"):
return int(value)
elif tag == "double":
return float(value)
elif tag == "string":
return value
elif tag == "array":
if len(value_node.children) > 1:
raise xmlrpc_common_XMLParseError("Too many children for 'array'")
# Arrays are encoded as: <array><data>
# <value>...</value>
# ...
# </data></array>
data_node = value_node.children[0]
result = []
if data_node.children:
for item_node in data_node.children:
result.append(_xmlrpc_common_value2python(item_node))
return result
elif tag == "struct":
result = {}
# Structs are encoded as: <struct>
# <member><name>...</name><value>...</value></member>
# ...
# </struct>
# Keys (<name>) do not contain type information, so they are strings
# as far as XMLRPC is concerned.
for member_node in value_node.children:
if len(member_node.children) != 2:
message = "Incorrect number of children for 'member'"
raise xmlrpc_common_XMLParseError(message)
key = member_node.children[0].content
value = _xmlrpc_common_value2python(member_node.children[1])
result[key] = value
return result
elif tag == "base64":
return xmlrpc_common_Binary(base64.base64_standard_b64decode(value_node.content))
else:
message = "Demarshaller: Unsupported value type: %s" % value_node.tag_name
raise xmlrpc_common_XMLParseError(message)