You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xmlrpc.py 20KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. # -*- test-case-name: twisted.web.test.test_xmlrpc -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. A generic resource for publishing objects via XML-RPC.
  6. Maintainer: Itamar Shtull-Trauring
  7. @var Fault: See L{xmlrpclib.Fault}
  8. @type Fault: L{xmlrpclib.Fault}
  9. """
  10. from __future__ import division, absolute_import
  11. from twisted.python.compat import _PY3, intToBytes, nativeString, urllib_parse
  12. from twisted.python.compat import unicode
  13. # System Imports
  14. import base64
  15. if _PY3:
  16. import xmlrpc.client as xmlrpclib
  17. else:
  18. import xmlrpclib
  19. # Sibling Imports
  20. from twisted.web import resource, server, http
  21. from twisted.internet import defer, protocol, reactor
  22. from twisted.python import reflect, failure
  23. from twisted.logger import Logger
  24. # These are deprecated, use the class level definitions
  25. NOT_FOUND = 8001
  26. FAILURE = 8002
  27. # Useful so people don't need to import xmlrpclib directly
  28. Fault = xmlrpclib.Fault
  29. Binary = xmlrpclib.Binary
  30. Boolean = xmlrpclib.Boolean
  31. DateTime = xmlrpclib.DateTime
  32. def withRequest(f):
  33. """
  34. Decorator to cause the request to be passed as the first argument
  35. to the method.
  36. If an I{xmlrpc_} method is wrapped with C{withRequest}, the
  37. request object is passed as the first argument to that method.
  38. For example::
  39. @withRequest
  40. def xmlrpc_echo(self, request, s):
  41. return s
  42. @since: 10.2
  43. """
  44. f.withRequest = True
  45. return f
  46. class NoSuchFunction(Fault):
  47. """
  48. There is no function by the given name.
  49. """
  50. class Handler:
  51. """
  52. Handle a XML-RPC request and store the state for a request in progress.
  53. Override the run() method and return result using self.result,
  54. a Deferred.
  55. We require this class since we're not using threads, so we can't
  56. encapsulate state in a running function if we're going to have
  57. to wait for results.
  58. For example, lets say we want to authenticate against twisted.cred,
  59. run a LDAP query and then pass its result to a database query, all
  60. as a result of a single XML-RPC command. We'd use a Handler instance
  61. to store the state of the running command.
  62. """
  63. def __init__(self, resource, *args):
  64. self.resource = resource # the XML-RPC resource we are connected to
  65. self.result = defer.Deferred()
  66. self.run(*args)
  67. def run(self, *args):
  68. # event driven equivalent of 'raise UnimplementedError'
  69. self.result.errback(
  70. NotImplementedError("Implement run() in subclasses"))
  71. class XMLRPC(resource.Resource):
  72. """
  73. A resource that implements XML-RPC.
  74. You probably want to connect this to '/RPC2'.
  75. Methods published can return XML-RPC serializable results, Faults,
  76. Binary, Boolean, DateTime, Deferreds, or Handler instances.
  77. By default methods beginning with 'xmlrpc_' are published.
  78. Sub-handlers for prefixed methods (e.g., system.listMethods)
  79. can be added with putSubHandler. By default, prefixes are
  80. separated with a '.'. Override self.separator to change this.
  81. @ivar allowNone: Permit XML translating of Python constant None.
  82. @type allowNone: C{bool}
  83. @ivar useDateTime: Present C{datetime} values as C{datetime.datetime}
  84. objects?
  85. @type useDateTime: C{bool}
  86. """
  87. # Error codes for Twisted, if they conflict with yours then
  88. # modify them at runtime.
  89. NOT_FOUND = 8001
  90. FAILURE = 8002
  91. isLeaf = 1
  92. separator = '.'
  93. allowedMethods = (b'POST',)
  94. _log = Logger()
  95. def __init__(self, allowNone=False, useDateTime=False):
  96. resource.Resource.__init__(self)
  97. self.subHandlers = {}
  98. self.allowNone = allowNone
  99. self.useDateTime = useDateTime
  100. def __setattr__(self, name, value):
  101. self.__dict__[name] = value
  102. def putSubHandler(self, prefix, handler):
  103. self.subHandlers[prefix] = handler
  104. def getSubHandler(self, prefix):
  105. return self.subHandlers.get(prefix, None)
  106. def getSubHandlerPrefixes(self):
  107. return list(self.subHandlers.keys())
  108. def render_POST(self, request):
  109. request.content.seek(0, 0)
  110. request.setHeader(b"content-type", b"text/xml; charset=utf-8")
  111. try:
  112. args, functionPath = xmlrpclib.loads(request.content.read(),
  113. use_datetime=self.useDateTime)
  114. except Exception as e:
  115. f = Fault(self.FAILURE, "Can't deserialize input: %s" % (e,))
  116. self._cbRender(f, request)
  117. else:
  118. try:
  119. function = self.lookupProcedure(functionPath)
  120. except Fault as f:
  121. self._cbRender(f, request)
  122. else:
  123. # Use this list to track whether the response has failed or not.
  124. # This will be used later on to decide if the result of the
  125. # Deferred should be written out and Request.finish called.
  126. responseFailed = []
  127. request.notifyFinish().addErrback(responseFailed.append)
  128. if getattr(function, 'withRequest', False):
  129. d = defer.maybeDeferred(function, request, *args)
  130. else:
  131. d = defer.maybeDeferred(function, *args)
  132. d.addErrback(self._ebRender)
  133. d.addCallback(self._cbRender, request, responseFailed)
  134. return server.NOT_DONE_YET
  135. def _cbRender(self, result, request, responseFailed=None):
  136. if responseFailed:
  137. return
  138. if isinstance(result, Handler):
  139. result = result.result
  140. if not isinstance(result, Fault):
  141. result = (result,)
  142. try:
  143. try:
  144. content = xmlrpclib.dumps(
  145. result, methodresponse=True,
  146. allow_none=self.allowNone)
  147. except Exception as e:
  148. f = Fault(self.FAILURE, "Can't serialize output: %s" % (e,))
  149. content = xmlrpclib.dumps(f, methodresponse=True,
  150. allow_none=self.allowNone)
  151. if isinstance(content, unicode):
  152. content = content.encode('utf8')
  153. request.setHeader(
  154. b"content-length", intToBytes(len(content)))
  155. request.write(content)
  156. except:
  157. self._log.failure('')
  158. request.finish()
  159. def _ebRender(self, failure):
  160. if isinstance(failure.value, Fault):
  161. return failure.value
  162. self._log.failure('', failure)
  163. return Fault(self.FAILURE, "error")
  164. def lookupProcedure(self, procedurePath):
  165. """
  166. Given a string naming a procedure, return a callable object for that
  167. procedure or raise NoSuchFunction.
  168. The returned object will be called, and should return the result of the
  169. procedure, a Deferred, or a Fault instance.
  170. Override in subclasses if you want your own policy. The base
  171. implementation that given C{'foo'}, C{self.xmlrpc_foo} will be returned.
  172. If C{procedurePath} contains C{self.separator}, the sub-handler for the
  173. initial prefix is used to search for the remaining path.
  174. If you override C{lookupProcedure}, you may also want to override
  175. C{listProcedures} to accurately report the procedures supported by your
  176. resource, so that clients using the I{system.listMethods} procedure
  177. receive accurate results.
  178. @since: 11.1
  179. """
  180. if procedurePath.find(self.separator) != -1:
  181. prefix, procedurePath = procedurePath.split(self.separator, 1)
  182. handler = self.getSubHandler(prefix)
  183. if handler is None:
  184. raise NoSuchFunction(self.NOT_FOUND,
  185. "no such subHandler %s" % prefix)
  186. return handler.lookupProcedure(procedurePath)
  187. f = getattr(self, "xmlrpc_%s" % procedurePath, None)
  188. if not f:
  189. raise NoSuchFunction(self.NOT_FOUND,
  190. "procedure %s not found" % procedurePath)
  191. elif not callable(f):
  192. raise NoSuchFunction(self.NOT_FOUND,
  193. "procedure %s not callable" % procedurePath)
  194. else:
  195. return f
  196. def listProcedures(self):
  197. """
  198. Return a list of the names of all xmlrpc procedures.
  199. @since: 11.1
  200. """
  201. return reflect.prefixedMethodNames(self.__class__, 'xmlrpc_')
  202. class XMLRPCIntrospection(XMLRPC):
  203. """
  204. Implement the XML-RPC Introspection API.
  205. By default, the methodHelp method returns the 'help' method attribute,
  206. if it exists, otherwise the __doc__ method attribute, if it exists,
  207. otherwise the empty string.
  208. To enable the methodSignature method, add a 'signature' method attribute
  209. containing a list of lists. See methodSignature's documentation for the
  210. format. Note the type strings should be XML-RPC types, not Python types.
  211. """
  212. def __init__(self, parent):
  213. """
  214. Implement Introspection support for an XMLRPC server.
  215. @param parent: the XMLRPC server to add Introspection support to.
  216. @type parent: L{XMLRPC}
  217. """
  218. XMLRPC.__init__(self)
  219. self._xmlrpc_parent = parent
  220. def xmlrpc_listMethods(self):
  221. """
  222. Return a list of the method names implemented by this server.
  223. """
  224. functions = []
  225. todo = [(self._xmlrpc_parent, '')]
  226. while todo:
  227. obj, prefix = todo.pop(0)
  228. functions.extend([prefix + name for name in obj.listProcedures()])
  229. todo.extend([ (obj.getSubHandler(name),
  230. prefix + name + obj.separator)
  231. for name in obj.getSubHandlerPrefixes() ])
  232. return functions
  233. xmlrpc_listMethods.signature = [['array']]
  234. def xmlrpc_methodHelp(self, method):
  235. """
  236. Return a documentation string describing the use of the given method.
  237. """
  238. method = self._xmlrpc_parent.lookupProcedure(method)
  239. return (getattr(method, 'help', None)
  240. or getattr(method, '__doc__', None) or '')
  241. xmlrpc_methodHelp.signature = [['string', 'string']]
  242. def xmlrpc_methodSignature(self, method):
  243. """
  244. Return a list of type signatures.
  245. Each type signature is a list of the form [rtype, type1, type2, ...]
  246. where rtype is the return type and typeN is the type of the Nth
  247. argument. If no signature information is available, the empty
  248. string is returned.
  249. """
  250. method = self._xmlrpc_parent.lookupProcedure(method)
  251. return getattr(method, 'signature', None) or ''
  252. xmlrpc_methodSignature.signature = [['array', 'string'],
  253. ['string', 'string']]
  254. def addIntrospection(xmlrpc):
  255. """
  256. Add Introspection support to an XMLRPC server.
  257. @param parent: the XMLRPC server to add Introspection support to.
  258. @type parent: L{XMLRPC}
  259. """
  260. xmlrpc.putSubHandler('system', XMLRPCIntrospection(xmlrpc))
  261. class QueryProtocol(http.HTTPClient):
  262. def connectionMade(self):
  263. self._response = None
  264. self.sendCommand(b'POST', self.factory.path)
  265. self.sendHeader(b'User-Agent', b'Twisted/XMLRPClib')
  266. self.sendHeader(b'Host', self.factory.host)
  267. self.sendHeader(b'Content-type', b'text/xml; charset=utf-8')
  268. payload = self.factory.payload
  269. self.sendHeader(b'Content-length', intToBytes(len(payload)))
  270. if self.factory.user:
  271. auth = b':'.join([self.factory.user, self.factory.password])
  272. authHeader = b''.join([b'Basic ', base64.b64encode(auth)])
  273. self.sendHeader(b'Authorization', authHeader)
  274. self.endHeaders()
  275. self.transport.write(payload)
  276. def handleStatus(self, version, status, message):
  277. if status != b'200':
  278. self.factory.badStatus(status, message)
  279. def handleResponse(self, contents):
  280. """
  281. Handle the XML-RPC response received from the server.
  282. Specifically, disconnect from the server and store the XML-RPC
  283. response so that it can be properly handled when the disconnect is
  284. finished.
  285. """
  286. self.transport.loseConnection()
  287. self._response = contents
  288. def connectionLost(self, reason):
  289. """
  290. The connection to the server has been lost.
  291. If we have a full response from the server, then parse it and fired a
  292. Deferred with the return value or C{Fault} that the server gave us.
  293. """
  294. http.HTTPClient.connectionLost(self, reason)
  295. if self._response is not None:
  296. response, self._response = self._response, None
  297. self.factory.parseResponse(response)
  298. payloadTemplate = """<?xml version="1.0"?>
  299. <methodCall>
  300. <methodName>%s</methodName>
  301. %s
  302. </methodCall>
  303. """
  304. class _QueryFactory(protocol.ClientFactory):
  305. """
  306. XML-RPC Client Factory
  307. @ivar path: The path portion of the URL to which to post method calls.
  308. @type path: L{bytes}
  309. @ivar host: The value to use for the Host HTTP header.
  310. @type host: L{bytes}
  311. @ivar user: The username with which to authenticate with the server
  312. when making calls.
  313. @type user: L{bytes} or L{None}
  314. @ivar password: The password with which to authenticate with the server
  315. when making calls.
  316. @type password: L{bytes} or L{None}
  317. @ivar useDateTime: Accept datetime values as datetime.datetime objects.
  318. also passed to the underlying xmlrpclib implementation. Defaults to
  319. C{False}.
  320. @type useDateTime: C{bool}
  321. """
  322. deferred = None
  323. protocol = QueryProtocol
  324. def __init__(self, path, host, method, user=None, password=None,
  325. allowNone=False, args=(), canceller=None, useDateTime=False):
  326. """
  327. @param method: The name of the method to call.
  328. @type method: C{str}
  329. @param allowNone: allow the use of None values in parameters. It's
  330. passed to the underlying xmlrpclib implementation. Defaults to
  331. C{False}.
  332. @type allowNone: C{bool} or L{None}
  333. @param args: the arguments to pass to the method.
  334. @type args: C{tuple}
  335. @param canceller: A 1-argument callable passed to the deferred as the
  336. canceller callback.
  337. @type canceller: callable or L{None}
  338. """
  339. self.path, self.host = path, host
  340. self.user, self.password = user, password
  341. self.payload = payloadTemplate % (method,
  342. xmlrpclib.dumps(args, allow_none=allowNone))
  343. if isinstance(self.payload, unicode):
  344. self.payload = self.payload.encode('utf8')
  345. self.deferred = defer.Deferred(canceller)
  346. self.useDateTime = useDateTime
  347. def parseResponse(self, contents):
  348. if not self.deferred:
  349. return
  350. try:
  351. response = xmlrpclib.loads(contents,
  352. use_datetime=self.useDateTime)[0][0]
  353. except:
  354. deferred, self.deferred = self.deferred, None
  355. deferred.errback(failure.Failure())
  356. else:
  357. deferred, self.deferred = self.deferred, None
  358. deferred.callback(response)
  359. def clientConnectionLost(self, _, reason):
  360. if self.deferred is not None:
  361. deferred, self.deferred = self.deferred, None
  362. deferred.errback(reason)
  363. clientConnectionFailed = clientConnectionLost
  364. def badStatus(self, status, message):
  365. deferred, self.deferred = self.deferred, None
  366. deferred.errback(ValueError(status, message))
  367. class Proxy:
  368. """
  369. A Proxy for making remote XML-RPC calls.
  370. Pass the URL of the remote XML-RPC server to the constructor.
  371. Use C{proxy.callRemote('foobar', *args)} to call remote method
  372. 'foobar' with *args.
  373. @ivar user: The username with which to authenticate with the server
  374. when making calls. If specified, overrides any username information
  375. embedded in C{url}. If not specified, a value may be taken from
  376. C{url} if present.
  377. @type user: L{bytes} or L{None}
  378. @ivar password: The password with which to authenticate with the server
  379. when making calls. If specified, overrides any password information
  380. embedded in C{url}. If not specified, a value may be taken from
  381. C{url} if present.
  382. @type password: L{bytes} or L{None}
  383. @ivar allowNone: allow the use of None values in parameters. It's
  384. passed to the underlying L{xmlrpclib} implementation. Defaults to
  385. C{False}.
  386. @type allowNone: C{bool} or L{None}
  387. @ivar useDateTime: Accept datetime values as datetime.datetime objects.
  388. also passed to the underlying L{xmlrpclib} implementation. Defaults to
  389. C{False}.
  390. @type useDateTime: C{bool}
  391. @ivar connectTimeout: Number of seconds to wait before assuming the
  392. connection has failed.
  393. @type connectTimeout: C{float}
  394. @ivar _reactor: The reactor used to create connections.
  395. @type _reactor: Object providing L{twisted.internet.interfaces.IReactorTCP}
  396. @ivar queryFactory: Object returning a factory for XML-RPC protocol. Mainly
  397. useful for tests.
  398. """
  399. queryFactory = _QueryFactory
  400. def __init__(self, url, user=None, password=None, allowNone=False,
  401. useDateTime=False, connectTimeout=30.0, reactor=reactor):
  402. """
  403. @param url: The URL to which to post method calls. Calls will be made
  404. over SSL if the scheme is HTTPS. If netloc contains username or
  405. password information, these will be used to authenticate, as long as
  406. the C{user} and C{password} arguments are not specified.
  407. @type url: L{bytes}
  408. """
  409. scheme, netloc, path, params, query, fragment = urllib_parse.urlparse(
  410. url)
  411. netlocParts = netloc.split(b'@')
  412. if len(netlocParts) == 2:
  413. userpass = netlocParts.pop(0).split(b':')
  414. self.user = userpass.pop(0)
  415. try:
  416. self.password = userpass.pop(0)
  417. except:
  418. self.password = None
  419. else:
  420. self.user = self.password = None
  421. hostport = netlocParts[0].split(b':')
  422. self.host = hostport.pop(0)
  423. try:
  424. self.port = int(hostport.pop(0))
  425. except:
  426. self.port = None
  427. self.path = path
  428. if self.path in [b'', None]:
  429. self.path = b'/'
  430. self.secure = (scheme == b'https')
  431. if user is not None:
  432. self.user = user
  433. if password is not None:
  434. self.password = password
  435. self.allowNone = allowNone
  436. self.useDateTime = useDateTime
  437. self.connectTimeout = connectTimeout
  438. self._reactor = reactor
  439. def callRemote(self, method, *args):
  440. """
  441. Call remote XML-RPC C{method} with given arguments.
  442. @return: a L{defer.Deferred} that will fire with the method response,
  443. or a failure if the method failed. Generally, the failure type will
  444. be L{Fault}, but you can also have an C{IndexError} on some buggy
  445. servers giving empty responses.
  446. If the deferred is cancelled before the request completes, the
  447. connection is closed and the deferred will fire with a
  448. L{defer.CancelledError}.
  449. """
  450. def cancel(d):
  451. factory.deferred = None
  452. connector.disconnect()
  453. factory = self.queryFactory(
  454. self.path, self.host, method, self.user,
  455. self.password, self.allowNone, args, cancel, self.useDateTime)
  456. if self.secure:
  457. from twisted.internet import ssl
  458. connector = self._reactor.connectSSL(
  459. nativeString(self.host), self.port or 443,
  460. factory, ssl.ClientContextFactory(),
  461. timeout=self.connectTimeout)
  462. else:
  463. connector = self._reactor.connectTCP(
  464. nativeString(self.host), self.port or 80, factory,
  465. timeout=self.connectTimeout)
  466. return factory.deferred
  467. __all__ = [
  468. "XMLRPC", "Handler", "NoSuchFunction", "Proxy",
  469. "Fault", "Binary", "Boolean", "DateTime"]