diff --git a/.gitignore b/.gitignore index f7d6c1a..e741c39 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,13 @@ +# Bytecoded form *.pyc +__pycache__ + +# Tox +.tox + +# When installed in develop mode +*.egg-info/ + +# setup.py folders build/* dist/* diff --git a/.project b/.project new file mode 100644 index 0000000..96b0cfe --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + jsonrpclib-pelix + + + + + + org.python.pydev.PyDevBuilder + + + + + + org.python.pydev.pythonNature + + diff --git a/.pydevproject b/.pydevproject new file mode 100644 index 0000000..270437d --- /dev/null +++ b/.pydevproject @@ -0,0 +1,8 @@ + + +Default +python 2.7 + +/jsonrpclib-pelix + + diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..86594dc --- /dev/null +++ b/.travis.yml @@ -0,0 +1,13 @@ +language: python +# python: +# - "3.3" +# - "3.2" +# - "2.7" +# - "2.6" + +before_install: + - pip install tox + - sudo apt-get install python2.6-dev python2.7-dev python3.2-dev python3.3-dev + +script: tox -e py26,py27,py32,py33 + diff --git a/MANIFEST.in b/MANIFEST.in index ab30e9a..eb0014a 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1 +1,2 @@ include *.txt +include README.rst diff --git a/README.md b/README.md deleted file mode 100644 index 85a157e..0000000 --- a/README.md +++ /dev/null @@ -1,219 +0,0 @@ -JSONRPClib -========== -This library is an implementation of the JSON-RPC specification. -It supports both the original 1.0 specification, as well as the -new (proposed) 2.0 spec, which includes batch submission, keyword -arguments, etc. - -It is licensed under the Apache License, Version 2.0 -(http://www.apache.org/licenses/LICENSE-2.0.html). - -Communication -------------- -Feel free to send any questions, comments, or patches to our Google Group -mailing list (you'll need to join to send a message): -http://groups.google.com/group/jsonrpclib - -Summary -------- -This library implements the JSON-RPC 2.0 proposed specification in pure Python. -It is designed to be as compatible with the syntax of xmlrpclib as possible -(it extends where possible), so that projects using xmlrpclib could easily be -modified to use JSON and experiment with the differences. - -It is backwards-compatible with the 1.0 specification, and supports all of the -new proposed features of 2.0, including: - -* Batch submission (via MultiCall) -* Keyword arguments -* Notifications (both in a batch and 'normal') -* Class translation using the 'jsonclass' key. - -I've added a "SimpleJSONRPCServer", which is intended to emulate the -"SimpleXMLRPCServer" from the default Python distribution. - -Requirements ------------- -It supports cjson and simplejson, and looks for the parsers in that order -(searching first for cjson, then for the "built-in" simplejson as json in 2.6+, -and then the simplejson external library). One of these must be installed to -use this library, although if you have a standard distribution of 2.6+, you -should already have one. Keep in mind that cjson is supposed to be the -quickest, I believe, so if you are going for full-on optimization you may -want to pick it up. - -Installation ------------- -You can install this from PyPI with one of the following commands (sudo -may be required): - - easy_install jsonrpclib - pip install jsonrpclib - -Alternatively, you can download the source from the github repository -at http://github.com/joshmarshall/jsonrpclib and manually install it -with the following commands: - - git clone git://github.com/joshmarshall/jsonrpclib.git - cd jsonrpclib - python setup.py install - -Client Usage ------------- - -This is (obviously) taken from a console session. - - >>> import jsonrpclib - >>> server = jsonrpclib.Server('http://localhost:8080') - >>> server.add(5,6) - 11 - >>> print jsonrpclib.history.request - {"jsonrpc": "2.0", "params": [5, 6], "id": "gb3c9g37", "method": "add"} - >>> print jsonrpclib.history.response - {'jsonrpc': '2.0', 'result': 11, 'id': 'gb3c9g37'} - >>> server.add(x=5, y=10) - 15 - >>> server._notify.add(5,6) - # No result returned... - >>> batch = jsonrpclib.MultiCall(server) - >>> batch.add(5, 6) - >>> batch.ping({'key':'value'}) - >>> batch._notify.add(4, 30) - >>> results = batch() - >>> for result in results: - >>> ... print result - 11 - {'key': 'value'} - # Note that there are only two responses -- this is according to spec. - -If you need 1.0 functionality, there are a bunch of places you can pass that -in, although the best is just to change the value on -jsonrpclib.config.version: - - >>> import jsonrpclib - >>> jsonrpclib.config.version - 2.0 - >>> jsonrpclib.config.version = 1.0 - >>> server = jsonrpclib.Server('http://localhost:8080') - >>> server.add(7, 10) - 17 - >>> print jsonrpclib..history.request - {"params": [7, 10], "id": "thes7tl2", "method": "add"} - >>> print jsonrpclib.history.response - {'id': 'thes7tl2', 'result': 17, 'error': None} - >>> - -The equivalent loads and dumps functions also exist, although with minor -modifications. The dumps arguments are almost identical, but it adds three -arguments: rpcid for the 'id' key, version to specify the JSON-RPC -compatibility, and notify if it's a request that you want to be a -notification. - -Additionally, the loads method does not return the params and method like -xmlrpclib, but instead a.) parses for errors, raising ProtocolErrors, and -b.) returns the entire structure of the request / response for manual parsing. - -SimpleJSONRPCServer -------------------- -This is identical in usage (or should be) to the SimpleXMLRPCServer in the default Python install. Some of the differences in features are that it obviously supports notification, batch calls, class translation (if left on), etc. Note: The import line is slightly different from the regular SimpleXMLRPCServer, since the SimpleJSONRPCServer is distributed within the jsonrpclib library. - - from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer - - server = SimpleJSONRPCServer(('localhost', 8080)) - server.register_function(pow) - server.register_function(lambda x,y: x+y, 'add') - server.register_function(lambda x: x, 'ping') - server.serve_forever() - -Class Translation ------------------ -I've recently added "automatic" class translation support, although it is -turned off by default. This can be devastatingly slow if improperly used, so -the following is just a short list of things to keep in mind when using it. - -* Keep It (the object) Simple Stupid. (for exceptions, keep reading.) -* Do not require init params (for exceptions, keep reading) -* Getter properties without setters could be dangerous (read: not tested) - -If any of the above are issues, use the _serialize method. (see usage below) -The server and client must BOTH have use_jsonclass configuration item on and -they must both have access to the same libraries used by the objects for -this to work. - -If you have excessively nested arguments, it would be better to turn off the -translation and manually invoke it on specific objects using -jsonrpclib.jsonclass.dump / jsonrpclib.jsonclass.load (since the default -behavior recursively goes through attributes and lists / dicts / tuples). - -[test_obj.py] - - # This object is /very/ simple, and the system will look through the - # attributes and serialize what it can. - class TestObj(object): - foo = 'bar' - - # This object requires __init__ params, so it uses the _serialize method - # and returns a tuple of init params and attribute values (the init params - # can be a dict or a list, but the attribute values must be a dict.) - class TestSerial(object): - foo = 'bar' - def __init__(self, *args): - self.args = args - def _serialize(self): - return (self.args, {'foo':self.foo,}) - -[usage] - - import jsonrpclib - import test_obj - - jsonrpclib.config.use_jsonclass = True - - testobj1 = test_obj.TestObj() - testobj2 = test_obj.TestSerial() - server = jsonrpclib.Server('http://localhost:8080') - # The 'ping' just returns whatever is sent - ping1 = server.ping(testobj1) - ping2 = server.ping(testobj2) - print jsonrpclib.history.request - # {"jsonrpc": "2.0", "params": [{"__jsonclass__": ["test_obj.TestSerial", ["foo"]]}], "id": "a0l976iv", "method": "ping"} - print jsonrpclib.history.result - # {'jsonrpc': '2.0', 'result': , 'id': 'a0l976iv'} - -To turn on this behaviour, just set jsonrpclib.config.use_jsonclass to True. -If you want to use a different method for serialization, just set -jsonrpclib.config.serialize_method to the method name. Finally, if you are -using classes that you have defined in the implementation (as in, not a -separate library), you'll need to add those (on BOTH the server and the -client) using the jsonrpclib.config.classes.add() method. -(Examples forthcoming.) - -Feedback on this "feature" is very, VERY much appreciated. - -Why JSON-RPC? -------------- -In my opinion, there are several reasons to choose JSON over XML for RPC: - -* Much simpler to read (I suppose this is opinion, but I know I'm right. :) -* Size / Bandwidth - Main reason, a JSON object representation is just much smaller. -* Parsing - JSON should be much quicker to parse than XML. -* Easy class passing with jsonclass (when enabled) - -In the interest of being fair, there are also a few reasons to choose XML -over JSON: - -* Your server doesn't do JSON (rather obvious) -* Wider XML-RPC support across APIs (can we change this? :)) -* Libraries are more established, i.e. more stable (Let's change this too.) - -TESTS ------ -I've dropped almost-verbatim tests from the JSON-RPC spec 2.0 page. -You can run it with: - - python tests.py - -TODO ----- -* Use HTTP error codes on SimpleJSONRPCServer -* Test, test, test and optimize diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..7f1478c --- /dev/null +++ b/README.rst @@ -0,0 +1,307 @@ +JSONRPClib (patched for Pelix) +############################## + +This library is an implementation of the JSON-RPC specification. +It supports both the original 1.0 specification, as well as the +new (proposed) 2.0 specification, which includes batch submission, keyword +arguments, etc. + +It is licensed under the Apache License, Version 2.0 +(http://www.apache.org/licenses/LICENSE-2.0.html). + + +About this version +****************** + +This is a patched version of the original ``jsonrpclib`` project by +Josh Marshall, available at https://github.com/joshmarshall/jsonrpclib. + +The suffix *-pelix* only indicates that this version works with Pelix Remote +Services, but it is **not** a Pelix specific implementation. + +* This version adds support for Python 3, staying compatible with Python 2. +* It is now possible to use the dispatch_method argument while extending + the SimpleJSONRPCDispatcher, to use a custom dispatcher. + This allows to use this package by Pelix Remote Services. +* The modifications added in other forks of this project have been added: + + * From https://github.com/drdaeman/jsonrpclib: + + * Improved JSON-RPC 1.0 support + * Less strict error response handling + + * From https://github.com/tuomassalo/jsonrpclib: + + * In case of a non-pre-defined error, raise an AppError and give access to + *error.data* + + * From https://github.com/dejw/jsonrpclib: + + * Custom headers can be sent with request and associated tests + +* The support for Unix sockets has been removed, as it is not trivial to convert + to Python 3 (and I don't use them) +* This version cannot be installed with the original ``jsonrpclib``, as it uses + the same package name. + + +Summary +******* + +This library implements the JSON-RPC 2.0 proposed specification in pure Python. +It is designed to be as compatible with the syntax of ``xmlrpclib`` as possible +(it extends where possible), so that projects using ``xmlrpclib`` could easily +be modified to use JSON and experiment with the differences. + +It is backwards-compatible with the 1.0 specification, and supports all of the +new proposed features of 2.0, including: + +* Batch submission (via MultiCall) +* Keyword arguments +* Notifications (both in a batch and 'normal') +* Class translation using the ``__jsonclass__`` key. + +I've added a "SimpleJSONRPCServer", which is intended to emulate the +"SimpleXMLRPCServer" from the default Python distribution. + +Requirements +************ + +It supports ``cjson`` and ``simplejson``, and looks for the parsers in that +order (searching first for ``cjson``, then for the *built-in* ``simplejson`` as +``json`` in 2.6+, and then the ``simplejson`` external library). +One of these must be installed to use this library, although if you have a +standard distribution of 2.6+, you should already have one. +Keep in mind that ``cjson`` is supposed to be the quickest, I believe, so if +you are going for full-on optimization you may want to pick it up. + +Since library uses ``contextlib`` module, you should have at least Python 2.5 +installed. + + +Installation +************ + +You can install this from PyPI with one of the following commands (sudo +may be required): + +.. code-block:: console + + easy_install jsonrpclib-pelix + pip install jsonrpclib-pelix + +Alternatively, you can download the source from the github repository +at http://github.com/tcalmant/jsonrpclib and manually install it +with the following commands: + +.. code-block:: console + + git clone git://github.com/tcalmant/jsonrpclib.git + cd jsonrpclib + python setup.py install + + +Client Usage +************ + +This is (obviously) taken from a console session. + +.. code-block:: python + + >>> import jsonrpclib + >>> server = jsonrpclib.Server('http://localhost:8080') + >>> server.add(5,6) + 11 + >>> print jsonrpclib.history.request + {"jsonrpc": "2.0", "params": [5, 6], "id": "gb3c9g37", "method": "add"} + >>> print jsonrpclib.history.response + {'jsonrpc': '2.0', 'result': 11, 'id': 'gb3c9g37'} + >>> server.add(x=5, y=10) + 15 + >>> server._notify.add(5,6) + # No result returned... + >>> batch = jsonrpclib.MultiCall(server) + >>> batch.add(5, 6) + >>> batch.ping({'key':'value'}) + >>> batch._notify.add(4, 30) + >>> results = batch() + >>> for result in results: + >>> ... print result + 11 + {'key': 'value'} + # Note that there are only two responses -- this is according to spec. + +If you need 1.0 functionality, there are a bunch of places you can pass that +in, although the best is just to change the value on +``jsonrpclib``.config.version: + +.. code-block:: python + + >>> import jsonrpclib + >>> jsonrpclib.config.version + 2.0 + >>> jsonrpclib.config.version = 1.0 + >>> server = jsonrpclib.Server('http://localhost:8080') + >>> server.add(7, 10) + 17 + >>> print jsonrpclib..history.request + {"params": [7, 10], "id": "thes7tl2", "method": "add"} + >>> print jsonrpclib.history.response + {'id': 'thes7tl2', 'result': 17, 'error': None} + >>> + +The equivalent loads and dumps functions also exist, although with minor +modifications. The dumps arguments are almost identical, but it adds three +arguments: rpcid for the 'id' key, version to specify the JSON-RPC +compatibility, and notify if it's a request that you want to be a +notification. + +Additionally, the loads method does not return the params and method like +``xmlrpclib``, but instead a.) parses for errors, raising ProtocolErrors, and +b.) returns the entire structure of the request / response for manual parsing. + + +Additional headers +****************** + +If your remote service requires custom headers in request, you can pass them +as as a ``headers`` keyword argument, when creating the ``ServerProxy``: + +.. code-block:: python + + >>> import jsonrpclib + >>> server = jsonrpclib.ServerProxy("http://localhost:8080", + headers={'X-Test' : 'Test'}) + +You can also put additional request headers only for certain method invocation: + +.. code-block:: python + + >>> import jsonrpclib + >>> server = jsonrpclib.Server("http://localhost:8080") + >>> with server._additional_headers({'X-Test' : 'Test'}) as test_server: + ... test_server.ping() + ... + >>> # X-Test header will be no longer sent in requests + +Of course ``_additional_headers`` contexts can be nested as well. + + +SimpleJSONRPCServer +******************* + +This is identical in usage (or should be) to the SimpleXMLRPCServer in the +default Python install. Some of the differences in features are that it +obviously supports notification, batch calls, class translation (if left on), +etc. +Note: The import line is slightly different from the regular SimpleXMLRPCServer, +since the SimpleJSONRPCServer is distributed within the ``jsonrpclib`` library. + +.. code-block:: python + + from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer + + server = SimpleJSONRPCServer(('localhost', 8080)) + server.register_function(pow) + server.register_function(lambda x,y: x+y, 'add') + server.register_function(lambda x: x, 'ping') + server.serve_forever() + + +Class Translation +***************** + +I've recently added "automatic" class translation support, although it is +turned off by default. This can be devastatingly slow if improperly used, so +the following is just a short list of things to keep in mind when using it. + +* Keep It (the object) Simple Stupid. (for exceptions, keep reading.) +* Do not require init params (for exceptions, keep reading) +* Getter properties without setters could be dangerous (read: not tested) + +If any of the above are issues, use the _serialize method. (see usage below) +The server and client must BOTH have use_jsonclass configuration item on and +they must both have access to the same libraries used by the objects for +this to work. + +If you have excessively nested arguments, it would be better to turn off the +translation and manually invoke it on specific objects using +``jsonrpclib.jsonclass.dump`` / ``jsonrpclib.jsonclass.load`` (since the default +behavior recursively goes through attributes and lists / dicts / tuples). + + Sample file: *test_obj.py* + +.. code-block:: python + + # This object is /very/ simple, and the system will look through the + # attributes and serialize what it can. + class TestObj(object): + foo = 'bar' + + # This object requires __init__ params, so it uses the _serialize method + # and returns a tuple of init params and attribute values (the init params + # can be a dict or a list, but the attribute values must be a dict.) + class TestSerial(object): + foo = 'bar' + def __init__(self, *args): + self.args = args + def _serialize(self): + return (self.args, {'foo':self.foo,}) + +* Sample usage + +.. code-block:: python + + import jsonrpclib + import test_obj + + jsonrpclib.config.use_jsonclass = True + + testobj1 = test_obj.TestObj() + testobj2 = test_obj.TestSerial() + server = jsonrpclib.Server('http://localhost:8080') + # The 'ping' just returns whatever is sent + ping1 = server.ping(testobj1) + ping2 = server.ping(testobj2) + print jsonrpclib.history.request + # {"jsonrpc": "2.0", "params": [{"__jsonclass__": ["test_obj.TestSerial", ["foo"]]}], "id": "a0l976iv", "method": "ping"} + print jsonrpclib.history.result + # {'jsonrpc': '2.0', 'result': , 'id': 'a0l976iv'} + +To turn on this behaviour, just set ``jsonrpclib``.config.use_jsonclass to True. +If you want to use a different method for serialization, just set +``jsonrpclib``.config.serialize_method to the method name. Finally, if you are +using classes that you have defined in the implementation (as in, not a +separate library), you'll need to add those (on BOTH the server and the +client) using the ``jsonrpclib``.config.classes.add() method. +(Examples forthcoming.) + +Feedback on this "feature" is very, VERY much appreciated. + +Why JSON-RPC? +************* + +In my opinion, there are several reasons to choose JSON over XML for RPC: + +* Much simpler to read (I suppose this is opinion, but I know I'm right. :) +* Size / Bandwidth - Main reason, a JSON object representation is just much smaller. +* Parsing - JSON should be much quicker to parse than XML. +* Easy class passing with ``jsonclass`` (when enabled) + +In the interest of being fair, there are also a few reasons to choose XML +over JSON: + +* Your server doesn't do JSON (rather obvious) +* Wider XML-RPC support across APIs (can we change this? :)) +* Libraries are more established, i.e. more stable (Let's change this too.) + +Tests +***** + +I've dropped almost-verbatim tests from the JSON-RPC spec 2.0 page. +You can run it with: + +.. code-block:: console + + python tests.py + python3 tests.py diff --git a/jsonrpclib/SimpleJSONRPCServer.py b/jsonrpclib/SimpleJSONRPCServer.py old mode 100644 new mode 100755 index d76da73..e7ecd73 --- a/jsonrpclib/SimpleJSONRPCServer.py +++ b/jsonrpclib/SimpleJSONRPCServer.py @@ -1,229 +1,508 @@ -import jsonrpclib +#!/usr/bin/python +# -- Content-Encoding: UTF-8 -- +""" +Defines a request dispatcher, a HTTP request handler, a HTTP server and a +CGI request handler. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +:license: Apache License 2.0 +:version: 0.1.6 +""" + +# Module version +__version_info__ = (0, 1, 6) +__version__ = ".".join(str(x) for x in __version_info__) + +# Documentation strings format +__docformat__ = "restructuredtext en" + +# ------------------------------------------------------------------------------ +# Local modules from jsonrpclib import Fault -from jsonrpclib.jsonrpc import USE_UNIX_SOCKETS -import SimpleXMLRPCServer -import SocketServer +import jsonrpclib +import jsonrpclib.utils as utils + +# Standard library import socket -import logging -import os -import types -import traceback import sys +import traceback + try: + # Python 3 + import xmlrpc.server as xmlrpcserver + import socketserver + +except ImportError: + # Python 2 + import SimpleXMLRPCServer as xmlrpcserver + import SocketServer as socketserver + +try: + # Windows import fcntl + except ImportError: - # For Windows + # Others fcntl = None +# ------------------------------------------------------------------------------ + def get_version(request): - # must be a dict - if 'jsonrpc' in request.keys(): + """ + Computes the JSON-RPC version + + :param request: A request dictionary + :return: The JSON-RPC version or None + """ + if 'jsonrpc' in request: return 2.0 - if 'id' in request.keys(): + + elif 'id' in request: return 1.0 + return None - -def validate_request(request): - if type(request) is not types.DictType: - fault = Fault( - -32600, 'Request must be {}, not %s.' % type(request) - ) - return fault + + +def validate_request(request, json_config): + """ + Validates the format of a request dictionary + + :param request: A request dictionary + :param json_config: A JSONRPClib Config instance + :return: True if the dictionary is valid, else a Fault object + """ + if not isinstance(request, utils.DictType): + # Invalid request type + return Fault(-32600, 'Request must be a dict, not {0}' \ + .format(type(request).__name__), + config=json_config) + + # Get the request ID rpcid = request.get('id', None) + + # Check request version version = get_version(request) if not version: - fault = Fault(-32600, 'Request %s invalid.' % request, rpcid=rpcid) - return fault + return Fault(-32600, 'Request {0} invalid.'.format(request), + rpcid=rpcid, config=json_config) + + # Default parameters: empty list request.setdefault('params', []) + + # Check parameters method = request.get('method', None) params = request.get('params') - param_types = (types.ListType, types.DictType, types.TupleType) - if not method or type(method) not in types.StringTypes or \ - type(params) not in param_types: - fault = Fault( - -32600, 'Invalid request parameters or method.', rpcid=rpcid - ) - return fault + param_types = (utils.ListType, utils.DictType, utils.TupleType) + + if not method or not isinstance(method, utils.StringTypes) or \ + not isinstance(params, param_types): + # Invalid type of method name or parameters + return Fault(-32600, 'Invalid request parameters or method.', + rpcid=rpcid, config=json_config) + + # Valid request return True -class SimpleJSONRPCDispatcher(SimpleXMLRPCServer.SimpleXMLRPCDispatcher): +# ------------------------------------------------------------------------------ - def __init__(self, encoding=None): - SimpleXMLRPCServer.SimpleXMLRPCDispatcher.__init__(self, - allow_none=True, - encoding=encoding) +class NoMulticallResult(Exception): + """ + No result in multicall + """ + pass - def _marshaled_dispatch(self, data, dispatch_method = None): - response = None - try: - request = jsonrpclib.loads(data) - except Exception, e: - fault = Fault(-32700, 'Request %s invalid. (%s)' % (data, e)) - response = fault.response() - return response + +class SimpleJSONRPCDispatcher(xmlrpcserver.SimpleXMLRPCDispatcher): + + def __init__(self, encoding=None, config=jsonrpclib.config.DEFAULT): + """ + Sets up the dispatcher with the given encoding. + None values are allowed. + """ + if not encoding: + # Default encoding + encoding = "UTF-8" + + + xmlrpcserver.SimpleXMLRPCDispatcher.__init__(self, + allow_none=True, + encoding=encoding) + self.json_config = config + + + def _unmarshaled_dispatch(self, request, dispatch_method=None): + """ + Loads the request dictionary (unmarshaled), calls the method(s) + accordingly and returns a JSON-RPC dictionary (not marshaled) + + :param request: JSON-RPC request dictionary (or list of) + :param dispatch_method: Custom dispatch method (for method resolution) + :return: A JSON-RPC dictionary (or an array of) or None if the request + was a notification + :raise NoMulticallResult: No result in batch + """ if not request: - fault = Fault(-32600, 'Request invalid -- no request data.') - return fault.response() - if type(request) is types.ListType: + # Invalid request dictionary + fault = Fault(-32600, 'Request invalid -- no request data.', + config=self.json_config) + return fault.dump() + + if type(request) is utils.ListType: # This SHOULD be a batch, by spec responses = [] for req_entry in request: - result = validate_request(req_entry) + # Validate the request + result = validate_request(req_entry, self.json_config) if type(result) is Fault: - responses.append(result.response()) + responses.append(result.dump()) continue - resp_entry = self._marshaled_single_dispatch(req_entry) - if resp_entry is not None: + + # Call the method + resp_entry = self._marshaled_single_dispatch(req_entry, + dispatch_method) + + # Store its result + if isinstance(resp_entry, Fault): + responses.append(resp_entry.dump()) + + elif resp_entry is not None: responses.append(resp_entry) - if len(responses) > 0: - response = '[%s]' % ','.join(responses) - else: - response = '' - else: - result = validate_request(request) + + if len(responses) == 0: + # No non-None result + raise NoMulticallResult("No result") + + return responses + + else: + # Single call + result = validate_request(request, self.json_config) if type(result) is Fault: - return result.response() - response = self._marshaled_single_dispatch(request) - return response + return result.dump() + + # Call the method + response = self._marshaled_single_dispatch(request, dispatch_method) + + if isinstance(response, Fault): + return response.dump() + + return response + + + def _marshaled_dispatch(self, data, dispatch_method=None): + """ + Parses the request data (marshaled), calls method(s) and returns a + JSON string (marshaled) + + :param data: A JSON request string + :param dispatch_method: Custom dispatch method (for method resolution) + :return: A JSON-RPC response string (marshaled) + """ + # Parse the request + try: + request = jsonrpclib.loads(data, self.json_config) + + except Exception as ex: + # Parsing/loading error + fault = Fault(-32700, 'Request {0} invalid. ({1}:{2})' \ + .format(data, type(ex).__name__, ex), + config=self.json_config) + return fault.response() + + # Get the response dictionary + try: + response = self._unmarshaled_dispatch(request, dispatch_method) + + if response is not None: + # Compute the string representation of the dictionary/list + return jsonrpclib.jdumps(response, self.encoding) + + else: + # No result (notification) + return '' + + except NoMulticallResult: + # Return an empty string (jsonrpclib internal behaviour) + return '' + - def _marshaled_single_dispatch(self, request): + def _marshaled_single_dispatch(self, request, dispatch_method=None): + """ + Dispatches a single method call + + :param request: A validated request dictionary + :param dispatch_method: Custom dispatch method (for method resolution) + :return: A JSON-RPC response dictionary, or None if it was a + notification request + """ # TODO - Use the multiprocessing and skip the response if # it is a notification - # Put in support for custom dispatcher here - # (See SimpleXMLRPCServer._marshaled_dispatch) method = request.get('method') params = request.get('params') try: - response = self._dispatch(method, params) + # Call the method + if dispatch_method is not None: + response = dispatch_method(method, params) + else: + response = self._dispatch(method, params) + except: - exc_type, exc_value, exc_tb = sys.exc_info() - fault = Fault(-32603, '%s:%s' % (exc_type, exc_value)) - return fault.response() - if 'id' not in request.keys() or request['id'] == None: - # It's a notification + # Return a fault + exc_type, exc_value, _ = sys.exc_info() + fault = Fault(-32603, '{0}:{1}'.format(exc_type, exc_value), + config=self.json_config) + return fault.dump() + + if 'id' not in request or request['id'] in (None, ''): + # It's a notification, no result needed + # Do not use 'not id' as it might be the integer 0 return None + + # Prepare a JSON-RPC dictionary try: - response = jsonrpclib.dumps(response, - methodresponse=True, - rpcid=request['id'] - ) - return response + return jsonrpclib.dump(response, rpcid=request['id'], + is_response=True, config=self.json_config) + except: - exc_type, exc_value, exc_tb = sys.exc_info() - fault = Fault(-32603, '%s:%s' % (exc_type, exc_value)) - return fault.response() + # JSON conversion exception + exc_type, exc_value, _ = sys.exc_info() + fault = Fault(-32603, '{0}:{1}'.format(exc_type, exc_value), + config=self.json_config) + return fault.dump() + def _dispatch(self, method, params): + """ + Default method resolver and caller + + :param method: Name of the method to call + :param params: List of arguments to give to the method + :return: The result of the method + """ func = None try: + # Try with registered methods func = self.funcs[method] + except KeyError: if self.instance is not None: + # Try with the registered instance if hasattr(self.instance, '_dispatch'): + # Instance has a custom dispatcher return self.instance._dispatch(method, params) + else: + # Resolve the method name in the instance try: - func = SimpleXMLRPCServer.resolve_dotted_attribute( - self.instance, - method, - True - ) + func = xmlrpcserver.resolve_dotted_attribute(\ + self.instance, method, True) except AttributeError: + # Unknown method pass + if func is not None: try: - if type(params) is types.ListType: - response = func(*params) + # Call the method + if type(params) is utils.ListType: + return func(*params) + else: - response = func(**params) - return response - except TypeError: - return Fault(-32602, 'Invalid parameters.') + return func(**params) + + except TypeError as ex: + # Maybe the parameters are wrong + return Fault(-32602, 'Invalid parameters: {0}'.format(ex), + config=self.json_config) + except: + # Method exception err_lines = traceback.format_exc().splitlines() - trace_string = '%s | %s' % (err_lines[-3], err_lines[-1]) - fault = jsonrpclib.Fault(-32603, 'Server error: %s' % - trace_string) - return fault + trace_string = '{0} | {1}'.format(err_lines[-3], err_lines[-1]) + return Fault(-32603, 'Server error: {0}'.format(trace_string), + config=self.json_config) + else: - return Fault(-32601, 'Method %s not supported.' % method) + # Unknown method + return Fault(-32601, 'Method {0} not supported.'.format(method), + config=self.json_config) + +# ------------------------------------------------------------------------------ + +class SimpleJSONRPCRequestHandler(xmlrpcserver.SimpleXMLRPCRequestHandler): + """ + HTTP request handler. -class SimpleJSONRPCRequestHandler( - SimpleXMLRPCServer.SimpleXMLRPCRequestHandler): - + The server that receives the requests must have a json_config member, + containing a JSONRPClib Config instance + """ def do_POST(self): + """ + Handles POST requests + """ if not self.is_rpc_path_valid(): self.report_404() return + + # Retrieve the configuration + config = getattr(self.server, 'json_config', jsonrpclib.config.DEFAULT) + try: - max_chunk_size = 10*1024*1024 + # Read the request body + max_chunk_size = 10 * 1024 * 1024 size_remaining = int(self.headers["content-length"]) - L = [] + chunks = [] while size_remaining: chunk_size = min(size_remaining, max_chunk_size) - L.append(self.rfile.read(chunk_size)) - size_remaining -= len(L[-1]) - data = ''.join(L) + chunks.append(utils.from_bytes(self.rfile.read(chunk_size))) + size_remaining -= len(chunks[-1]) + data = ''.join(chunks) + + # Execute the method response = self.server._marshaled_dispatch(data) + + # No exception: send a 200 OK self.send_response(200) - except Exception, e: + + except Exception: + # Exception: send 500 Server Error self.send_response(500) err_lines = traceback.format_exc().splitlines() - trace_string = '%s | %s' % (err_lines[-3], err_lines[-1]) - fault = jsonrpclib.Fault(-32603, 'Server error: %s' % trace_string) + trace_string = '{0} | {1}'.format(err_lines[-3], err_lines[-1]) + fault = jsonrpclib.Fault(-32603, 'Server error: {0}'\ + .format(trace_string), config=config) response = fault.response() - if response == None: + + if response is None: + # Avoid to send None response = '' - self.send_header("Content-type", "application/json-rpc") + + # Convert the response to the valid string format + response = utils.to_bytes(response) + + # Send it + self.send_header("Content-type", config.content_type) self.send_header("Content-length", str(len(response))) self.end_headers() self.wfile.write(response) self.wfile.flush() self.connection.shutdown(1) -class SimpleJSONRPCServer(SocketServer.TCPServer, SimpleJSONRPCDispatcher): +# ------------------------------------------------------------------------------ +class SimpleJSONRPCServer(socketserver.TCPServer, SimpleJSONRPCDispatcher): + """ + JSON-RPC server (and dispatcher) + """ + # This simplifies server restart after error allow_reuse_address = True def __init__(self, addr, requestHandler=SimpleJSONRPCRequestHandler, logRequests=True, encoding=None, bind_and_activate=True, - address_family=socket.AF_INET): + address_family=socket.AF_INET, + config=jsonrpclib.config.DEFAULT): + """ + Sets up the server and the dispatcher + + :param addr: The server listening address + :param requestHandler: Custom request handler + :param logRequests: Flag to(de)activate requests logging + :param encoding: The dispatcher request encoding + :param bind_and_activate: If True, starts the server immediately + :param address_family: The server listening address family + :param config: A JSONRPClib Config instance + """ + # Set up the dispatcher fields + SimpleJSONRPCDispatcher.__init__(self, encoding, config) + + # Prepare the server configuration self.logRequests = logRequests - SimpleJSONRPCDispatcher.__init__(self, encoding) - # TCPServer.__init__ has an extra parameter on 2.6+, so - # check Python version and decide on how to call it - vi = sys.version_info self.address_family = address_family - if USE_UNIX_SOCKETS and address_family == socket.AF_UNIX: - # Unix sockets can't be bound if they already exist in the - # filesystem. The convention of e.g. X11 is to unlink - # before binding again. - if os.path.exists(addr): - try: - os.unlink(addr) - except OSError: - logging.warning("Could not unlink socket %s", addr) - # if python 2.5 and lower - if vi[0] < 3 and vi[1] < 6: - SocketServer.TCPServer.__init__(self, addr, requestHandler) - else: - SocketServer.TCPServer.__init__(self, addr, requestHandler, - bind_and_activate) + self.json_config = config + + # Work on the request handler + class RequestHandlerWrapper(requestHandler): + def __init__(self, *args, **kwargs): + """ + Constructs the wrapper after having stored the configuration + """ + self.config = config + super(RequestHandlerWrapper, self).__init__(*args, **kwargs) + + # Set up the server + socketserver.TCPServer.__init__(self, addr, requestHandler, + bind_and_activate) + + # Windows-specific if fcntl is not None and hasattr(fcntl, 'FD_CLOEXEC'): flags = fcntl.fcntl(self.fileno(), fcntl.F_GETFD) flags |= fcntl.FD_CLOEXEC fcntl.fcntl(self.fileno(), fcntl.F_SETFD, flags) -class CGIJSONRPCRequestHandler(SimpleJSONRPCDispatcher): +class CGIJSONRPCRequestHandler(SimpleJSONRPCDispatcher, xmlrpcserver.CGIXMLRPCRequestHandler): + """ + JSON-RPC CGI handler (and dispatcher) + """ + def __init__(self, encoding=None, config=jsonrpclib.config.DEFAULT): + """ + Sets up the dispatcher + + :param encoding: Dispatcher encoding + :param config: A JSONRPClib Config instance + """ + SimpleJSONRPCDispatcher.__init__(self, encoding, config) - def __init__(self, encoding=None): - SimpleJSONRPCDispatcher.__init__(self, encoding) def handle_jsonrpc(self, request_text): + """ + Handle a JSON-RPC request + """ response = self._marshaled_dispatch(request_text) - print 'Content-Type: application/json-rpc' - print 'Content-Length: %d' % len(response) - print + sys.stdout.write('Content-Type: {0}\r\n' \ + .format(self.json_config.content_type)) + sys.stdout.write('Content-Length: {0:d}\r\n'.format(len(response))) + sys.stdout.write('\r\n') sys.stdout.write(response) + # XML-RPC alias handle_xmlrpc = handle_jsonrpc + +# ------------------------------------------------------------------------------ + +class WSGIJSONRPCApp(SimpleJSONRPCDispatcher): + """ + WSGI compatible JSON-RPC dispatcher + """ + def __init__(self, encoding=None, config=jsonrpclib.config.DEFAULT): + """ + Sets up the dispatcher + + :param encoding: Dispatcher encoding + :param config: A JSONRPClib Config instance + """ + SimpleJSONRPCDispatcher.__init__(self, encoding, config) + + def __call__(self, environ, start_response): + """ + WSGI application callable. See the WSGI spec + """ + try: + msg_len = int(environ.get('CONTENT_LENGTH', 0)) + except ValueError: + msg_len = 0 + msg = environ['wsgi.input'].read(msg_len).decode(self.encoding) + response = self._marshaled_dispatch(msg).encode(self.encoding) + headers = [('Content-Type', self.json_config.content_type), + ('Content-Length', str(len(response)))] + start_response('200 OK', headers) + return (response,) diff --git a/jsonrpclib/__init__.py b/jsonrpclib/__init__.py index 6e884b8..9c66eb2 100644 --- a/jsonrpclib/__init__.py +++ b/jsonrpclib/__init__.py @@ -1,6 +1,14 @@ -from jsonrpclib.config import Config -config = Config.instance() -from jsonrpclib.history import History -history = History.instance() -from jsonrpclib.jsonrpc import Server, MultiCall, Fault -from jsonrpclib.jsonrpc import ProtocolError, loads, dumps +#!/usr/bin/python +# -- Content-Encoding: UTF-8 -- +""" +Aliases to ease access to jsonrpclib classes + +:license: Apache License 2.0 +""" + +# Easy access to utility methods and classes +from jsonrpclib.jsonrpc import Server, ServerProxy +from jsonrpclib.jsonrpc import MultiCall, Fault, ProtocolError, AppError +from jsonrpclib.jsonrpc import loads, dumps, load, dump +from jsonrpclib.jsonrpc import jloads, jdumps +import jsonrpclib.utils as utils diff --git a/jsonrpclib/config.py b/jsonrpclib/config.py index 4d28f1b..d095359 100644 --- a/jsonrpclib/config.py +++ b/jsonrpclib/config.py @@ -1,38 +1,108 @@ +#!/usr/bin/python +# -- Content-Encoding: UTF-8 -- +""" +The configuration module. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +:license: Apache License 2.0 +:version: 0.1.6 +""" + +# Module version +__version_info__ = (0, 1, 6) +__version__ = ".".join(str(x) for x in __version_info__) + +# Documentation strings format +__docformat__ = "restructuredtext en" + +# ------------------------------------------------------------------------------ + import sys +# ------------------------------------------------------------------------------ + class LocalClasses(dict): + """ + Associates local classes with their names (used in the jsonclass module) + """ def add(self, cls): + """ + Stores a local class + + :param cls: A class + """ self[cls.__name__] = cls +# ------------------------------------------------------------------------------ + class Config(object): """ - This is pretty much used exclusively for the 'jsonclass' + This is pretty much used exclusively for the 'jsonclass' functionality... set use_jsonclass to False to turn it off. You can change serialize_method and ignore_attribute, or use the local_classes.add(class) to include "local" classes. """ - use_jsonclass = True - # Change to False to keep __jsonclass__ entries raw. - serialize_method = '_serialize' - # The serialize_method should be a string that references the - # method on a custom class object which is responsible for - # returning a tuple of the constructor arguments and a dict of - # attributes. - ignore_attribute = '_ignore' - # The ignore attribute should be a string that references the - # attribute on a custom class object which holds strings and / or - # references of the attributes the class translator should ignore. - classes = LocalClasses() - # The list of classes to use for jsonclass translation. - version = 2.0 - # Version of the JSON-RPC spec to support - user_agent = 'jsonrpclib/0.1 (Python %s)' % \ - '.'.join([str(ver) for ver in sys.version_info[0:3]]) - # User agent to use for calls. - _instance = None - - @classmethod - def instance(cls): - if not cls._instance: - cls._instance = cls() - return cls._instance + def __init__(self, version=2.0, content_type="application/json-rpc", + user_agent=None, use_jsonclass=True, + serialize_method='_serialize', + ignore_attribute='_ignore'): + """ + Sets up a configuration of JSONRPClib + + :param version: JSON-RPC specification version + :param content_type: HTTP content type header value + :param user_agent: The HTTP request user agent + :param use_jsonclass: Allow bean marshalling + :param serialize_method: A string that references the method on a + custom class object which is responsible for + returning a tuple of the arguments and a dict + of attributes. + :param ignore_attribute: A string that references the attribute on a + custom class object which holds strings and/or + references of the attributes the class + translator should ignore. + """ + # JSON-RPC specification + self.version = version + + # Change to False to keep __jsonclass__ entries raw. + self.use_jsonclass = use_jsonclass + + # it SHOULD be 'application/json-rpc' + # but MAY be 'application/json' or 'application/jsonrequest' + self.content_type = content_type + + # Default user agent + if user_agent is None: + user_agent = 'jsonrpclib/{0} (Python {1})' \ + .format(__version__, + '.'.join(str(ver) for ver in sys.version_info[0:3])) + self.user_agent = user_agent + + # The list of classes to use for jsonclass translation. + self.classes = LocalClasses() + + # The serialize_method should be a string that references the + # method on a custom class object which is responsible for + # returning a tuple of the constructor arguments and a dict of + # attributes. + self.serialize_method = serialize_method + + # The ignore attribute should be a string that references the + # attribute on a custom class object which holds strings and / or + # references of the attributes the class translator should ignore. + self.ignore_attribute = ignore_attribute + +# Default configuration +DEFAULT = Config() diff --git a/jsonrpclib/history.py b/jsonrpclib/history.py index d11863d..3cfea05 100644 --- a/jsonrpclib/history.py +++ b/jsonrpclib/history.py @@ -1,40 +1,93 @@ +#!/usr/bin/python +# -- Content-Encoding: UTF-8 -- +""" +The history module. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +:license: Apache License 2.0 +:version: 0.1.5 +""" + +# Module version +__version_info__ = (0, 1, 5) +__version__ = ".".join(str(x) for x in __version_info__) + +# Documentation strings format +__docformat__ = "restructuredtext en" + +# ------------------------------------------------------------------------------ + class History(object): """ This holds all the response and request objects for a session. A server using this should call "clear" after - each request cycle in order to keep it from clogging + each request cycle in order to keep it from clogging memory. """ - requests = [] - responses = [] - _instance = None - - @classmethod - def instance(cls): - if not cls._instance: - cls._instance = cls() - return cls._instance + def __init__(self): + """ + Sets up members + """ + self.requests = [] + self.responses = [] def add_response(self, response_obj): + """ + Adds a response to the history + + :param response_obj: Response content + """ self.responses.append(response_obj) - + def add_request(self, request_obj): + """ + Adds a request to the history + + :param request_obj: A request object + """ self.requests.append(request_obj) @property def request(self): - if len(self.requests) == 0: - return None - else: + """ + Returns the latest stored request or None + + :return: The latest stored request + """ + try: return self.requests[-1] + except IndexError: + return None + @property def response(self): - if len(self.responses) == 0: - return None - else: + """ + Returns the latest stored response or None + + :return: The latest stored response + """ + try: return self.responses[-1] + except IndexError: + return None + + def clear(self): + """ + Clears the history lists + """ del self.requests[:] del self.responses[:] diff --git a/jsonrpclib/jsonclass.py b/jsonrpclib/jsonclass.py old mode 100644 new mode 100755 index 1d86d5f..134498a --- a/jsonrpclib/jsonclass.py +++ b/jsonrpclib/jsonclass.py @@ -1,70 +1,108 @@ -import types -import inspect -import re -import traceback +#!/usr/bin/python +# -- Content-Encoding: UTF-8 -- +""" +The serialization module + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +:license: Apache License 2.0 +:version: 0.1.6 +""" -from jsonrpclib import config +# Module version +__version_info__ = (0, 1, 6) +__version__ = ".".join(str(x) for x in __version_info__) -iter_types = [ - types.DictType, - types.ListType, - types.TupleType -] +# Documentation strings format +__docformat__ = "restructuredtext en" -string_types = [ - types.StringType, - types.UnicodeType -] +# ------------------------------------------------------------------------------ -numeric_types = [ - types.IntType, - types.LongType, - types.FloatType -] +# Local package +import jsonrpclib.config +import jsonrpclib.utils as utils -value_types = [ - types.BooleanType, - types.NoneType -] +# Standard library +import inspect +import re + +# ------------------------------------------------------------------------------ -supported_types = iter_types+string_types+numeric_types+value_types +# Supported transmitted code +supported_types = (utils.DictType,) + utils.iterable_types \ + + utils.primitive_types + +# Regex of invalid module characters invalid_module_chars = r'[^a-zA-Z0-9\_\.]' +# ------------------------------------------------------------------------------ + class TranslationError(Exception): + """ + Unmarshaling exception + """ pass -def dump(obj, serialize_method=None, ignore_attribute=None, ignore=[]): +# ------------------------------------------------------------------------------ + +def dump(obj, serialize_method=None, ignore_attribute=None, ignore=[], + config=jsonrpclib.config.DEFAULT): + """ + Transforms the given object into a JSON-RPC compliant form. + Converts beans into dictionaries with a __jsonclass__ entry. + Doesn't change primitive types. + + :param obj: An object to convert + :param serialize_method: Custom serialization method + :param ignore_attribute: Name of the object attribute containing the names + of members to ignore + :param ignore: A list of members to ignore + :param config: A JSONRPClib Config instance + :return: A JSON-RPC compliant object + """ if not serialize_method: serialize_method = config.serialize_method + if not ignore_attribute: ignore_attribute = config.ignore_attribute - obj_type = type(obj) + # Parse / return default "types"... - if obj_type in numeric_types+string_types+value_types: + # Primitive + if isinstance(obj, utils.primitive_types): return obj - if obj_type in iter_types: - if obj_type in (types.ListType, types.TupleType): - new_obj = [] - for item in obj: - new_obj.append(dump(item, serialize_method, - ignore_attribute, ignore)) - if obj_type is types.TupleType: - new_obj = tuple(new_obj) - return new_obj - # It's a dict... - else: - new_obj = {} - for key, value in obj.iteritems(): - new_obj[key] = dump(value, serialize_method, - ignore_attribute, ignore) - return new_obj + + # Iterative + elif isinstance(obj, utils.iterable_types): + # List, set or tuple + return [dump(item, serialize_method, ignore_attribute, ignore) + for item in obj] + + elif isinstance(obj, utils.DictType): + # Dictionary + return dict((key, dump(value, serialize_method, + ignore_attribute, ignore)) + for key, value in obj.items()) + # It's not a standard type, so it needs __jsonclass__ module_name = inspect.getmodule(obj).__name__ - class_name = obj.__class__.__name__ - json_class = class_name - if module_name not in ['', '__main__']: - json_class = '%s.%s' % (module_name, json_class) - return_obj = {"__jsonclass__":[json_class,]} + json_class = obj.__class__.__name__ + + if module_name not in ('', '__main__'): + json_class = '{0}.{1}'.format(module_name, json_class) + + # Keep the class name in the returned object + return_obj = {"__jsonclass__": [json_class, ]} + # If a serialization method is defined.. if serialize_method in dir(obj): # Params can be a dict (keyword) or list (positional) @@ -74,79 +112,117 @@ def dump(obj, serialize_method=None, ignore_attribute=None, ignore=[]): return_obj['__jsonclass__'].append(params) return_obj.update(attrs) return return_obj - # Otherwise, try to figure it out - # Obviously, we can't assume to know anything about the - # parameters passed to __init__ - return_obj['__jsonclass__'].append([]) - attrs = {} - ignore_list = getattr(obj, ignore_attribute, [])+ignore - for attr_name, attr_value in obj.__dict__.iteritems(): - if type(attr_value) in supported_types and \ - attr_name not in ignore_list and \ - attr_value not in ignore_list: - attrs[attr_name] = dump(attr_value, serialize_method, - ignore_attribute, ignore) - return_obj.update(attrs) - return return_obj - -def load(obj): - if type(obj) in string_types+numeric_types+value_types: + + else: + # Otherwise, try to figure it out + # Obviously, we can't assume to know anything about the + # parameters passed to __init__ + return_obj['__jsonclass__'].append([]) + attrs = {} + ignore_list = getattr(obj, ignore_attribute, []) + ignore + for attr_name, attr_value in obj.__dict__.items(): + if isinstance(attr_value, supported_types) and \ + attr_name not in ignore_list and \ + attr_value not in ignore_list: + attrs[attr_name] = dump(attr_value, serialize_method, + ignore_attribute, ignore) + return_obj.update(attrs) + return return_obj + + +def load(obj, classes=None): + """ + If 'obj' is a dictionary containing a __jsonclass__ entry, converts the + dictionary item into a bean of this class. + + :param obj: An object from a JSON-RPC dictionary + :param classes: A custom {name: class} dictionary + :return: The loaded object + """ + # Primitive + if isinstance(obj, utils.primitive_types): return obj - if type(obj) is types.ListType: - return_list = [] - for entry in obj: - return_list.append(load(entry)) - return return_list - # Othewise, it's a dict type - if '__jsonclass__' not in obj.keys(): - return_dict = {} - for key, value in obj.iteritems(): - new_value = load(value) - return_dict[key] = new_value - return return_dict - # It's a dict, and it's a __jsonclass__ + + # List, set or tuple + elif isinstance(obj, utils.iterable_types): + # This comes from a JSON parser, so it can only be a list... + return [load(entry) for entry in obj] + + # Otherwise, it's a dict type + elif '__jsonclass__' not in obj.keys(): + return dict((key, load(value)) for key, value in obj.items()) + + # It's a dictionary, and it has a __jsonclass__ orig_module_name = obj['__jsonclass__'][0] params = obj['__jsonclass__'][1] - if orig_module_name == '': + + # Validate the module name + if not orig_module_name: raise TranslationError('Module name empty.') + json_module_clean = re.sub(invalid_module_chars, '', orig_module_name) if json_module_clean != orig_module_name: - raise TranslationError('Module name %s has invalid characters.' % - orig_module_name) + raise TranslationError('Module name {0} has invalid characters.' \ + .format(orig_module_name)) + + # Load the class json_module_parts = json_module_clean.split('.') json_class = None - if len(json_module_parts) == 1: + if classes and len(json_module_parts) == 1: # Local class name -- probably means it won't work - if json_module_parts[0] not in config.classes.keys(): - raise TranslationError('Unknown class or module %s.' % - json_module_parts[0]) - json_class = config.classes[json_module_parts[0]] + try: + json_class = classes[json_module_parts[0]] + except KeyError: + raise TranslationError('Unknown class or module {0}.' \ + .format(json_module_parts[0])) + else: + # Module + class json_class_name = json_module_parts.pop() json_module_tree = '.'.join(json_module_parts) try: - temp_module = __import__(json_module_tree) + # Use fromlist to load the module itself, not the package + temp_module = __import__(json_module_tree, + fromlist=[json_class_name]) except ImportError: - raise TranslationError('Could not import %s from module %s.' % - (json_class_name, json_module_tree)) + raise TranslationError('Could not import {0} from module {1}.' \ + .format(json_class_name, json_module_tree)) - # The returned class is the top-level module, not the one we really - # want. (E.g., if we import a.b.c, we now have a.) Walk through other - # path components to get to b and c. - for i in json_module_parts[1:]: - temp_module = getattr(temp_module, i) + try: + json_class = getattr(temp_module, json_class_name) + + except AttributeError: + raise TranslationError("Unknown class {0}.{1}." \ + .format(json_module_tree, json_class_name)) - json_class = getattr(temp_module, json_class_name) - # Creating the object... + # Create the object new_obj = None - if type(params) is types.ListType: - new_obj = json_class(*params) - elif type(params) is types.DictType: - new_obj = json_class(**params) + if isinstance(params, utils.ListType): + try: + new_obj = json_class(*params) + + except TypeError as ex: + raise TranslationError("Error instantiating {0}: {1}"\ + .format(json_class.__name__, ex)) + + elif isinstance(params, utils.DictType): + try: + new_obj = json_class(**params) + + except TypeError as ex: + raise TranslationError("Error instantiating {0}: {1}"\ + .format(json_class.__name__, ex)) + else: - raise TranslationError('Constructor args must be a dict or list.') - for key, value in obj.iteritems(): - if key == '__jsonclass__': - continue - setattr(new_obj, key, value) + raise TranslationError("Constructor args must be a dict or a list, " + "not {0}".format(type(params).__name__)) + + # Remove the class information, as it must be ignored during the + # reconstruction of the object + del obj['__jsonclass__'] + + for key, value in obj.items(): + # Recursive loading + setattr(new_obj, key, load(value, classes)) + return new_obj diff --git a/jsonrpclib/jsonrpc.py b/jsonrpclib/jsonrpc.py old mode 100644 new mode 100755 index e11939a..2de93f5 --- a/jsonrpclib/jsonrpc.py +++ b/jsonrpclib/jsonrpc.py @@ -1,15 +1,17 @@ +#!/usr/bin/python +# -- Content-Encoding: UTF-8 -- """ -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. ============================ JSONRPC Library (jsonrpclib) @@ -29,7 +31,7 @@ and other things to tie the thing off nicely. :) For a quick-start, just open a console and type the following, -replacing the server address, method, and parameters +replacing the server address, method, and parameters appropriately. >>> import jsonrpclib >>> server = jsonrpclib.Server('http://localhost:8181') @@ -43,82 +45,203 @@ >>> batch() [53, 5] -See http://code.google.com/p/jsonrpclib/ for more info. +See https://github.com/tcalmant/jsonrpclib for more info. + +:license: Apache License 2.0 +:version: 0.1.6.1 """ -import types -import sys -from xmlrpclib import Transport as XMLTransport -from xmlrpclib import SafeTransport as XMLSafeTransport -from xmlrpclib import ServerProxy as XMLServerProxy -from xmlrpclib import _Method as XML_Method -import time -import string -import random +# Module version +__version_info__ = (0, 1, 6, 1) +__version__ = ".".join(str(x) for x in __version_info__) + +# Documentation strings format +__docformat__ = "restructuredtext en" + +# ------------------------------------------------------------------------------ # Library includes -import jsonrpclib -from jsonrpclib import config -from jsonrpclib import history +import jsonrpclib.config +import jsonrpclib.utils as utils + +# Standard library +import contextlib +import sys +import uuid + +try: + # Python 3 + from urllib.parse import splittype + from urllib.parse import splithost + from xmlrpc.client import Transport as XMLTransport + from xmlrpc.client import SafeTransport as XMLSafeTransport + from xmlrpc.client import ServerProxy as XMLServerProxy + from xmlrpc.client import _Method as XML_Method + +except ImportError: + # Python 2 + from urllib import splittype + from urllib import splithost + from xmlrpclib import Transport as XMLTransport + from xmlrpclib import SafeTransport as XMLSafeTransport + from xmlrpclib import ServerProxy as XMLServerProxy + from xmlrpclib import _Method as XML_Method + +# ------------------------------------------------------------------------------ +# JSON library import + +# JSON class serialization +from jsonrpclib import jsonclass -# JSON library importing -cjson = None -json = None try: + # Using cjson import cjson + + # Declare cjson methods + def jdumps(obj, encoding='utf-8'): + return cjson.encode(obj) + + def jloads(json_string): + return cjson.decode(json_string) + except ImportError: + # Use json or simplejson try: import json except ImportError: try: import simplejson as json except ImportError: - raise ImportError( - 'You must have the cjson, json, or simplejson ' + - 'module(s) available.' - ) - -IDCHARS = string.ascii_lowercase+string.digits - -class UnixSocketMissing(Exception): - """ - Just a properly named Exception if Unix Sockets usage is - attempted on a platform that doesn't support them (Windows) - """ - pass + raise ImportError('You must have the cjson, json, or simplejson ' \ + 'module(s) available.') -#JSON Abstractions + # Declare json methods + if sys.version_info[0] < 3: + def jdumps(obj, encoding='utf-8'): + # Python 2 (explicit encoding) + return json.dumps(obj, encoding=encoding) -def jdumps(obj, encoding='utf-8'): - # Do 'serialize' test at some point for other classes - global cjson - if cjson: - return cjson.encode(obj) else: - return json.dumps(obj, encoding=encoding) + # Python 3 + def jdumps(obj, encoding='utf-8'): + # Python 3 (the encoding parameter has been removed) + return json.dumps(obj) -def jloads(json_string): - global cjson - if cjson: - return cjson.decode(json_string) - else: + def jloads(json_string): return json.loads(json_string) - +# ------------------------------------------------------------------------------ # XMLRPClib re-implementations class ProtocolError(Exception): + """ + JSON-RPC error + + ProtocolError[0] can be: + * an error message (string) + * a (code, message) tuple + """ pass +class AppError(ProtocolError): + """ + Application error: the error code is not in the pre-defined ones + + AppError[0][0]: Error code + AppError[0][1]: Error message or trace + AppError[0][2]: Associated data + """ + def data(self): + """ + Retrieves the value found in the 'data' entry of the error, or None + + :return: The data associated to the error, or None + """ + return self[0][2] + + class TransportMixIn(object): """ Just extends the XMLRPC transport where necessary. """ - user_agent = config.user_agent # for Python 2.7 support _connection = None + # Additional headers: list of dictionaries + additional_headers = [] + + # List of non-overridable headers + # Use the configuration to change the content-type + readonly_headers = ('content-length', 'content-type') + + def __init__(self, config=jsonrpclib.config.DEFAULT): + """ + Sets up the transport + + :param config: A JSONRPClib Config instance + """ + # Store the configuration + self._config = config + + # Set up the user agent + self.user_agent = config.user_agent + + def push_headers(self, headers): + """ + Adds a dictionary of headers to the additional headers list + + :param headers: A dictionary + """ + self.additional_headers.append(headers) + + def pop_headers(self, headers): + """ + Removes the given dictionary from the additional headers list. + Also validates that given headers are on top of the stack + + :param headers: Headers to remove + :raise AssertionError: The given dictionary is not on the latest stored + in the additional headers list + """ + assert self.additional_headers[-1] == headers + self.additional_headers.pop() + + + def emit_additional_headers(self, connection): + """ + Puts headers as is in the request, filtered read only headers + + :param connection: The request connection + """ + additional_headers = {} + + # Prepare the merged dictionary + for headers in self.additional_headers: + additional_headers.update(headers) + + # Remove forbidden keys + for forbidden in self.readonly_headers: + additional_headers.pop(forbidden, None) + + # Reversed order: in the case of multiple headers value definition, + # the latest pushed has priority + for key, value in additional_headers.items(): + key = str(key) + if key.lower() not in self.readonly_headers: + # Only accept replaceable headers + connection.putheader(str(key), str(value)) + + def send_content(self, connection, request_body): - connection.putheader("Content-Type", "application/json-rpc") + connection.putheader("Accept", "application/json") + # Convert the body first + request_body = utils.to_bytes(request_body) + + # "static" headers + connection.putheader("Content-Type", self._config.content_type) connection.putheader("Content-Length", str(len(request_body))) + + # Emit additional headers here in order not to override content-length + self.emit_additional_headers(connection) + connection.endheaders() if request_body: connection.send(request_body) @@ -142,98 +265,102 @@ def __init__(self): self.data = [] def feed(self, data): + # Store raw data: it might not contain whole wide-character self.data.append(data) def close(self): - return ''.join(self.data) + if not self.data: + return '' + + else: + data = type(self.data[0])().join(self.data) + try: + # Convert the whole final string + data = utils.from_bytes(data) + except: + # Try a pass-through + pass + + return data class Transport(TransportMixIn, XMLTransport): pass class SafeTransport(TransportMixIn, XMLSafeTransport): pass -from httplib import HTTP, HTTPConnection -from socket import socket -USE_UNIX_SOCKETS = False +# ------------------------------------------------------------------------------ -try: - from socket import AF_UNIX, SOCK_STREAM - USE_UNIX_SOCKETS = True -except ImportError: - pass - -if (USE_UNIX_SOCKETS): - - class UnixHTTPConnection(HTTPConnection): - def connect(self): - self.sock = socket(AF_UNIX, SOCK_STREAM) - self.sock.connect(self.host) - - class UnixHTTP(HTTP): - _connection_class = UnixHTTPConnection - - class UnixTransport(TransportMixIn, XMLTransport): - def make_connection(self, host): - import httplib - host, extra_headers, x509 = self.get_host_info(host) - return UnixHTTP(host) - - class ServerProxy(XMLServerProxy): """ Unfortunately, much more of this class has to be copied since so much of it does the serialization. """ - def __init__(self, uri, transport=None, encoding=None, - verbose=0, version=None): - import urllib + def __init__(self, uri, transport=None, encoding=None, + verbose=0, version=None, headers=None, history=None, + config=jsonrpclib.config.DEFAULT): + """ + Sets up the server proxy + + :param uri: Request URI + :param transport: Custom transport handler + :param encoding: Specified encoding + :param verbose: Log verbosity level + :param version: JSON-RPC specification version + :param headers: Custom additional headers for each request + :param history: History object (for tests) + :param config: A JSONRPClib Config instance + """ + # Store the configuration + self._config = config + if not version: version = config.version self.__version = version - schema, uri = urllib.splittype(uri) - if schema not in ('http', 'https', 'unix'): + + schema, uri = splittype(uri) + if schema not in ('http', 'https'): raise IOError('Unsupported JSON-RPC protocol.') - if schema == 'unix': - if not USE_UNIX_SOCKETS: - # Don't like the "generic" Exception... - raise UnixSocketMissing("Unix sockets not available.") - self.__host = uri + + self.__host, self.__handler = splithost(uri) + if not self.__handler: + # Not sure if this is in the JSON spec? self.__handler = '/' - else: - self.__host, self.__handler = urllib.splithost(uri) - if not self.__handler: - # Not sure if this is in the JSON spec? - #self.__handler = '/' - self.__handler == '/' + if transport is None: - if schema == 'unix': - transport = UnixTransport() - elif schema == 'https': - transport = SafeTransport() + if schema == 'https': + transport = SafeTransport(config=config) else: - transport = Transport() + transport = Transport(config=config) self.__transport = transport + self.__encoding = encoding self.__verbose = verbose + self.__history = history + + # Global custom headers are injected into Transport + self.__transport.push_headers(headers or {}) def _request(self, methodname, params, rpcid=None): request = dumps(params, methodname, encoding=self.__encoding, - rpcid=rpcid, version=self.__version) + rpcid=rpcid, version=self.__version, + config=self._config) response = self._run_request(request) check_for_errors(response) return response['result'] def _request_notify(self, methodname, params, rpcid=None): request = dumps(params, methodname, encoding=self.__encoding, - rpcid=rpcid, version=self.__version, notify=True) + rpcid=rpcid, version=self.__version, notify=True, + config=self._config) response = self._run_request(request, notify=True) check_for_errors(response) return def _run_request(self, request, notify=None): - history.add_request(request) + if self.__history is not None: + self.__history.add_request(request) response = self.__transport.request( self.__host, @@ -241,31 +368,77 @@ def _run_request(self, request, notify=None): request, verbose=self.__verbose ) - + # Here, the XMLRPC library translates a single list # response to the single value -- should we do the # same, and require a tuple / list to be passed to - # the response object, or expect the Server to be + # the response object, or expect the Server to be # outputting the response appropriately? - - history.add_response(response) + + if self.__history is not None: + self.__history.add_response(response) + if not response: return None - return_obj = loads(response) + return_obj = loads(response, self._config) return return_obj def __getattr__(self, name): # Same as original, just with new _Method reference return _Method(self._request, name) + def __close(self): + """ + Closes the transport layer + """ + try: + self.__transport.close() + + except AttributeError: + # Not available in Python 2.6 + pass + + + def __call__(self, attr): + """ + A workaround to get special attributes on the ServerProxy + without interfering with the magic __getattr__ + + (code from xmlrpclib in Python 2.7) + """ + if attr == "close": + return self.__close + + elif attr == "transport": + return self.__transport + + raise AttributeError("Attribute {0} not found".format(attr)) + + @property def _notify(self): # Just like __getattr__, but with notify namespace. return _Notify(self._request_notify) + @contextlib.contextmanager + def _additional_headers(self, headers): + """ + Allow to specify additional headers, to be added inside the with + block. Example of usage: + + >>> with client._additional_headers({'X-Test' : 'Test'}) as new_client: + ... new_client.method() + ... + >>> # Here old headers are restored + """ + self.__transport.push_headers(headers) + yield self + self.__transport.pop_headers(headers) + +# ------------------------------------------------------------------------------ class _Method(XML_Method): - + def __call__(self, *args, **kwargs): if len(args) > 0 and len(kwargs) > 0: raise ProtocolError('Cannot use both positional ' + @@ -276,11 +449,14 @@ def __call__(self, *args, **kwargs): return self.__send(self.__name, kwargs) def __getattr__(self, name): + if name == "__name__": + return self.__name + self.__name = '%s.%s' % (self.__name, name) return self # The old method returned a new instance, but this seemed wasteful. # The only thing that changes is the name. - #return _Method(self.__send, "%s.%s" % (self.__name, name)) + # return _Method(self.__send, "%s.%s" % (self.__name, name)) class _Notify(object): def __init__(self, request): @@ -288,15 +464,17 @@ def __init__(self, request): def __getattr__(self, name): return _Method(self._request, name) - + +# ------------------------------------------------------------------------------ # Batch implementation class MultiCallMethod(object): - - def __init__(self, method, notify=False): + + def __init__(self, method, notify=False, config=jsonrpclib.config.DEFAULT): self.method = method self.params = [] self.notify = notify + self._config = config def __call__(self, *args, **kwargs): if len(kwargs) > 0 and len(args) > 0: @@ -309,28 +487,30 @@ def __call__(self, *args, **kwargs): def request(self, encoding=None, rpcid=None): return dumps(self.params, self.method, version=2.0, - encoding=encoding, rpcid=rpcid, notify=self.notify) + encoding=encoding, rpcid=rpcid, notify=self.notify, + config=self._config) def __repr__(self): return '%s' % self.request() - + def __getattr__(self, method): new_method = '%s.%s' % (self.method, method) self.method = new_method return self class MultiCallNotify(object): - - def __init__(self, multicall): + + def __init__(self, multicall, config=jsonrpclib.config.DEFAULT): self.multicall = multicall + self._config = config def __getattr__(self, name): - new_job = MultiCallMethod(name, notify=True) + new_job = MultiCallMethod(name, notify=True, config=self._config) self.multicall._job_list.append(new_job) return new_job class MultiCallIterator(object): - + def __init__(self, results): self.results = results @@ -348,10 +528,11 @@ def __len__(self): return len(self.results) class MultiCall(object): - - def __init__(self, server): + + def __init__(self, server, config=jsonrpclib.config.DEFAULT): self._server = server self._job_list = [] + self._config = config def _request(self): if len(self._job_list) < 1: @@ -367,83 +548,179 @@ def _request(self): @property def _notify(self): - return MultiCallNotify(self) + return MultiCallNotify(self, self._config) def __getattr__(self, name): - new_job = MultiCallMethod(name) + new_job = MultiCallMethod(name, config=self._config) self._job_list.append(new_job) return new_job __call__ = _request -# These lines conform to xmlrpclib's "compatibility" line. +# These lines conform to xmlrpclib's "compatibility" line. # Not really sure if we should include these, but oh well. Server = ServerProxy +# ------------------------------------------------------------------------------ + class Fault(object): - # JSON-RPC error class - def __init__(self, code=-32000, message='Server error', rpcid=None): + """ + JSON-RPC error class + """ + def __init__(self, code=-32000, message='Server error', rpcid=None, + config=jsonrpclib.config.DEFAULT): + """ + Sets up the error description + + :param code: Fault code + :param message: Associated message + :param rpcid: Request ID + :param config: A JSONRPClib Config instance + """ self.faultCode = code self.faultString = message self.rpcid = rpcid + self.config = config def error(self): + """ + Returns the error as a dictionary + + :returns: A {'code', 'message'} dictionary + """ return {'code':self.faultCode, 'message':self.faultString} def response(self, rpcid=None, version=None): + """ + Returns the error as a JSON-RPC response string + + :param rpcid: Forced request ID + :param version: JSON-RPC version + :return: A JSON-RPC response string + """ if not version: - version = config.version + version = self.config.version + if rpcid: self.rpcid = rpcid - return dumps( - self, methodresponse=True, rpcid=self.rpcid, version=version - ) + + return dumps(self, methodresponse=True, rpcid=self.rpcid, + version=version, config=self.config) + + def dump(self, rpcid=None, version=None): + """ + Returns the error as a JSON-RPC response dictionary + + :param rpcid: Forced request ID + :param version: JSON-RPC version + :return: A JSON-RPC response dictionary + """ + if not version: + version = self.config.version + + if rpcid: + self.rpcid = rpcid + + return dump(self, is_response=True, rpcid=self.rpcid, + version=version, config=self.config) def __repr__(self): - return '' % (self.faultCode, self.faultString) + """ + String representation + """ + return ''.format(self.faultCode, self.faultString) + -def random_id(length=8): - return_id = '' - for i in range(length): - return_id += random.choice(IDCHARS) - return return_id +class Payload(object): + """ + JSON-RPC content handler + """ + def __init__(self, rpcid=None, version=None, + config=jsonrpclib.config.DEFAULT): + """ + Sets up the JSON-RPC handler -class Payload(dict): - def __init__(self, rpcid=None, version=None): + :param rpcid: Request ID + :param version: JSON-RPC version + :param config: A JSONRPClib Config instance + """ if not version: version = config.version + self.id = rpcid self.version = float(version) - + + def request(self, method, params=[]): - if type(method) not in types.StringTypes: + """ + Prepares a method call request + + :param method: Method name + :param params: Method parameters + :return: A JSON-RPC request dictionary + """ + if type(method) not in utils.StringTypes: raise ValueError('Method name must be a string.') + if not self.id: - self.id = random_id() + # Generate a request ID + self.id = str(uuid.uuid4()) + request = { 'id':self.id, 'method':method } - if params: + if params or self.version < 1.1: request['params'] = params + if self.version >= 2: request['jsonrpc'] = str(self.version) + return request + def notify(self, method, params=[]): + """ + Prepares a notification request + + :param method: Notification name + :param params: Notification parameters + :return: A JSON-RPC notification dictionary + """ + # Prepare the request dictionary request = self.request(method, params) + + # Remove the request ID, as it's a notification if self.version >= 2: del request['id'] else: request['id'] = None + return request + def response(self, result=None): + """ + Prepares a response dictionary + + :param result: The result of method call + :return: A JSON-RPC response dictionary + """ response = {'result':result, 'id':self.id} + if self.version >= 2: response['jsonrpc'] = str(self.version) else: response['error'] = None + return response + def error(self, code=-32000, message='Server error.'): + """ + Prepares an error dictionary + + :param code: Error code + :param message: Error message + :return: A JSON-RPC error dictionary + """ error = self.response() if self.version >= 2: del error['result'] @@ -452,89 +729,208 @@ def error(self, code=-32000, message='Server error.'): error['error'] = {'code':code, 'message':message} return error -def dumps(params=[], methodname=None, methodresponse=None, - encoding=None, rpcid=None, version=None, notify=None): +# ------------------------------------------------------------------------------ + +def dump(params=[], methodname=None, rpcid=None, version=None, + is_response=None, is_notify=None, config=jsonrpclib.config.DEFAULT): """ - This differs from the Python implementation in that it implements - the rpcid argument since the 2.0 spec requires it for responses. + Prepares a JSON-RPC dictionary (request, notification, response or error) + + :param params: Method parameters (if a method name is given) or a Fault + :param methodname: Method name + :param rpcid: Request ID + :param version: JSON-RPC version + :param is_response: If True, this is a response dictionary + :param is_notify: If True, this is a notification request + :param config: A JSONRPClib Config instance + :return: A JSON-RPC dictionary """ + # Default version if not version: version = config.version - valid_params = (types.TupleType, types.ListType, types.DictType) - if methodname in types.StringTypes and \ - type(params) not in valid_params and \ - not isinstance(params, Fault): - """ + + # Validate method name and parameters + valid_params = (utils.TupleType, utils.ListType, utils.DictType, Fault) + if methodname in utils.StringTypes and \ + not isinstance(params, valid_params): + """ If a method, and params are not in a listish or a Fault, error out. """ - raise TypeError('Params must be a dict, list, tuple or Fault ' + - 'instance.') - # Begin parsing object + raise TypeError('Params must be a dict, list, tuple or Fault instance.') + + # Prepares the JSON-RPC content payload = Payload(rpcid=rpcid, version=version) - if not encoding: - encoding = 'utf-8' + if type(params) is Fault: - response = payload.error(params.faultCode, params.faultString) - return jdumps(response, encoding=encoding) - if type(methodname) not in types.StringTypes and methodresponse != True: - raise ValueError('Method name must be a string, or methodresponse '+ + # Prepare an error dictionary + return payload.error(params.faultCode, params.faultString) + + if type(methodname) not in utils.StringTypes and not is_response: + # Neither a request nor a response + raise ValueError('Method name must be a string, or is_response ' \ 'must be set to True.') - if config.use_jsonclass == True: - from jsonrpclib import jsonclass + + if config.use_jsonclass: + # Use jsonclass to convert the parameters params = jsonclass.dump(params) - if methodresponse is True: + + if is_response: + # Prepare a response dictionary if rpcid is None: + # A response must have a request ID raise ValueError('A method response must have an rpcid.') - response = payload.response(params) - return jdumps(response, encoding=encoding) - request = None - if notify == True: - request = payload.notify(methodname, params) + return payload.response(params) + + if is_notify: + # Prepare a notification dictionary + return payload.notify(methodname, params) + else: - request = payload.request(methodname, params) + # Prepare a method call dictionary + return payload.request(methodname, params) + + +def dumps(params=[], methodname=None, methodresponse=None, + encoding=None, rpcid=None, version=None, notify=None, + config=jsonrpclib.config.DEFAULT): + """ + Prepares a JSON-RPC request/response string + + :param params: Method parameters (if a method name is given) or a Fault + :param methodname: Method name + :param methodresponse: If True, this is a response dictionary + :param encoding: Result string encoding + :param rpcid: Request ID + :param version: JSON-RPC version + :param notify: If True, this is a notification request + :param config: A JSONRPClib Config instance + :return: A JSON-RPC dictionary + """ + # Prepare the dictionary + request = dump(params, methodname, rpcid, version, methodresponse, notify, + config) + + # Set the default encoding + if not encoding: + encoding = "UTF-8" + + # Returns it as a JSON string return jdumps(request, encoding=encoding) -def loads(data): + +def load(data, config=jsonrpclib.config.DEFAULT): """ - This differs from the Python implementation, in that it returns - the request structure in Dict format instead of the method, params. - It will return a list in the case of a batch request / response. + Loads a JSON-RPC request/response dictionary. Calls jsonclass to load beans + + :param data: A JSON-RPC dictionary + :param config: A JSONRPClib Config instance (or None for default values) + :return: A parsed dictionary or None """ - if data == '': - # notification + if data is None: + # Notification return None - result = jloads(data) - # if the above raises an error, the implementing server code + + # if the above raises an error, the implementing server code # should return something like the following: # { 'jsonrpc':'2.0', 'error': fault.error(), id: None } - if config.use_jsonclass == True: - from jsonrpclib import jsonclass - result = jsonclass.load(result) - return result + if config.use_jsonclass: + # Convert beans + data = jsonclass.load(data, config.classes) + + return data + + +def loads(data, config=jsonrpclib.config.DEFAULT): + """ + Loads a JSON-RPC request/response string. Calls jsonclass to load beans + + :param data: A JSON-RPC string + :param config: A JSONRPClib Config instance (or None for default values) + :return: A parsed dictionary or None + """ + if data == '': + # Notification + return None + + # Parse the JSON dictionary + result = jloads(data) + + # Load the beans + return load(result, config) + +# ------------------------------------------------------------------------------ def check_for_errors(result): + """ + Checks if a result dictionary signals an error + + :param result: A result dictionary + :raise TypeError: Invalid parameter + :raise NotImplementedError: Unknown JSON-RPC version + :raise ValueError: Invalid dictionary content + :raise ProtocolError: An error occurred on the server side + :return: The result parameter + """ if not result: # Notification return result - if type(result) is not types.DictType: + + if type(result) is not utils.DictType: + # Invalid argument raise TypeError('Response is not a dict.') - if 'jsonrpc' in result.keys() and float(result['jsonrpc']) > 2.0: + + if 'jsonrpc' in result and float(result['jsonrpc']) > 2.0: + # Unknown JSON-RPC version raise NotImplementedError('JSON-RPC version not yet supported.') - if 'result' not in result.keys() and 'error' not in result.keys(): + + if 'result' not in result and 'error' not in result: + # Invalid dictionary content raise ValueError('Response does not have a result or error key.') - if 'error' in result.keys() and result['error'] != None: - code = result['error']['code'] - message = result['error']['message'] - raise ProtocolError((code, message)) + + if 'error' in result and result['error']: + # Server-side error + if 'code' in result['error']: + # Code + Message + code = result['error']['code'] + try: + # Get the message (jsonrpclib) + message = result['error']['message'] + + except KeyError: + # Get the trace (jabsorb) + message = result['error'].get('trace', '') + + if -32700 <= code <= -32000: + # Pre-defined errors + # See http://www.jsonrpc.org/specification#error_object + raise ProtocolError((code, message)) + + else: + # Application error + data = result['error'].get('data', None) + raise AppError((code, message, data)) + + raise ProtocolError((code, message)) + + elif isinstance(result['error'], dict) and len(result['error']) == 1: + # Error with a single entry ('reason', ...): use its content + error_key = result['error'].keys()[0] + raise ProtocolError(result['error'][error_key]) + + else: + # Use the raw error content + raise ProtocolError(result['error']) + return result + def isbatch(result): - if type(result) not in (types.ListType, types.TupleType): + if type(result) not in (utils.ListType, utils.TupleType): return False if len(result) < 1: return False - if type(result[0]) is not types.DictType: + if type(result[0]) is not utils.DictType: return False if 'jsonrpc' not in result[0].keys(): return False @@ -546,11 +942,20 @@ def isbatch(result): return False return True + def isnotification(request): - if 'id' not in request.keys(): + """ + Tests if the given request is a notification + + :param request: A request dictionary + :return: True if the request is a notification + """ + if 'id' not in request: # 2.0 notification return True - if request['id'] == None: + + if request['id'] is None: # 1.0 notification return True + return False diff --git a/jsonrpclib/utils.py b/jsonrpclib/utils.py new file mode 100644 index 0000000..ef7c957 --- /dev/null +++ b/jsonrpclib/utils.py @@ -0,0 +1,124 @@ +#!/usr/bin/python +# -- Content-Encoding: UTF-8 -- +""" +Utility methods, for compatibility between Python version + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +:author: Thomas Calmant +:license: Apache License 2.0 +:version: 1.0.1 +""" + +# Module version +__version_info__ = (1, 0, 1) +__version__ = ".".join(str(x) for x in __version_info__) + +# Documentation strings format +__docformat__ = "restructuredtext en" + +# ------------------------------------------------------------------------------ + +import sys + +# ------------------------------------------------------------------------------ + +if sys.version_info[0] < 3: + # Python 2 + import types + StringTypes = types.StringTypes + + string_types = ( + types.StringType, + types.UnicodeType + ) + + numeric_types = ( + types.IntType, + types.LongType, + types.FloatType + ) + + def to_bytes(string): + """ + Converts the given string into bytes + """ + if type(string) is unicode: + return str(string) + + return string + + def from_bytes(data): + """ + Converts the given bytes into a string + """ + if type(data) is str: + return data + + return str(data) + +# ------------------------------------------------------------------------------ + +else: + # Python 3 + StringTypes = (str,) + + string_types = ( + bytes, + str + ) + + numeric_types = ( + int, + float + ) + + def to_bytes(string): + """ + Converts the given string into bytes + """ + if type(string) is bytes: + return string + + return bytes(string, "UTF-8") + + def from_bytes(data): + """ + Converts the given bytes into a string + """ + if type(data) is str: + return data + + return str(data, "UTF-8") + +# ------------------------------------------------------------------------------ +# Common + +DictType = dict + +ListType = list +SetTypes = (set, frozenset) +TupleType = tuple + +iterable_types = ( + list, + set, frozenset, + tuple +) + +value_types = ( + bool, + type(None) +) + +primitive_types = string_types + numeric_types + value_types diff --git a/setup.py b/setup.py index 72f8b35..8eb86fa 100755 --- a/setup.py +++ b/setup.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -- Content-Encoding: UTF-8 -- """ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,17 +14,46 @@ limitations under the License. """ -import distutils.core - -distutils.core.setup( - name = "jsonrpclib", - version = "0.1.3", - packages = ["jsonrpclib"], - author = "Josh Marshall", - author_email = "catchjosh@gmail.com", - url = "http://github.com/joshmarshall/jsonrpclib/", - license = "http://www.apache.org/licenses/LICENSE-2.0", - description = "This project is an implementation of the JSON-RPC v2.0 " + - "specification (backwards-compatible) as a client library.", - long_description = open("README.md").read() -) +# Module version +__version_info__ = (0, 1, 6, 1) +__version__ = ".".join(str(x) for x in __version_info__) + +# Documentation strings format +__docformat__ = "restructuredtext en" + +# ------------------------------------------------------------------------------ + +try: + from setuptools import setup + +except ImportError: + from distutils.core import setup + +# ------------------------------------------------------------------------------ + +setup(name="jsonrpclib-pelix", + version=__version__, + license="http://www.apache.org/licenses/LICENSE-2.0", + author="Thomas Calmant", + author_email="thomas.calmant+github@gmail.com", + url="http://github.com/tcalmant/jsonrpclib/", + download_url='https://github.com/tcalmant/jsonrpclib/archive/master.zip', + description="This project is an implementation of the JSON-RPC v2.0 " \ + "specification (backwards-compatible) as a client library. " \ + "This version is a fork of jsonrpclib by Josh Marshall, " \ + "usable with Pelix remote services.", + long_description=open("README.rst").read(), + packages=["jsonrpclib"], + classifiers=[ + 'Development Status :: 5 - Production/Stable', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: Apache Software License', + 'Operating System :: OS Independent', + 'Programming Language :: Python :: 2.6', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.0', + 'Programming Language :: Python :: 3.1', + 'Programming Language :: Python :: 3.2', + 'Programming Language :: Python :: 3.3', + ]) diff --git a/tests.py b/tests.py index 3ce1009..760a2d1 100644 --- a/tests.py +++ b/tests.py @@ -1,54 +1,221 @@ +#!/usr/bin/python +# -- Content-Encoding: UTF-8 -- """ The tests in this file compare the request and response objects to the JSON-RPC 2.0 specification document, as well as testing -several internal components of the jsonrpclib library. Run this +several internal components of the jsonrpclib library. Run this module without any parameters to run the tests. -Currently, this is not easily tested with a framework like +Currently, this is not easily tested with a framework like nosetests because we spin up a daemon thread running the the Server, and nosetests (at least in my tests) does not ever "kill" the thread. If you are testing jsonrpclib and the module doesn't return to -the command prompt after running the tests, you can hit +the command prompt after running the tests, you can hit "Ctrl-C" (or "Ctrl-Break" on Windows) and that should kill it. TODO: * Finish implementing JSON-RPC 2.0 Spec tests * Implement JSON-RPC 1.0 tests * Implement JSONClass, History, Config tests + + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +:license: Apache License 2.0 +:version: 1.0.0 """ -from jsonrpclib import Server, MultiCall, history, config, ProtocolError -from jsonrpclib import jsonrpc -from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer -from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCRequestHandler -import socket -import tempfile -import unittest -import os +# Module version +__version_info__ = (1, 0, 0) +__version__ = ".".join(str(x) for x in __version_info__) + +# Documentation strings format +__docformat__ = "restructuredtext en" + +# ------------------------------------------------------------------------------ + +# jsonrpclib +from jsonrpclib import Server, MultiCall, ProtocolError +from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer, WSGIJSONRPCApp +from jsonrpclib.utils import from_bytes +import jsonrpclib.history + +# Standard library +import contextlib +import re +import sys +import threading import time +import unittest +import wsgiref.validate +import wsgiref.simple_server + +try: + # Python 2 + from StringIO import StringIO + +except ImportError: + # Python 3 + from io import StringIO + try: import json + except ImportError: import simplejson as json -from threading import Thread -PORTS = range(8000, 8999) +# ------------------------------------------------------------------------------ + +PORTS = list(range(8000, 8999)) + +# ------------------------------------------------------------------------------ +# Test methods + +def subtract(minuend, subtrahend): + """ + Using the keywords from the JSON-RPC v2 doc + """ + return minuend - subtrahend + +def add(x, y): + return x + y + +def update(*args): + return args + +def summation(*args): + return sum(args) + +def notify_hello(*args): + return args + +def get_data(): + return ['hello', 5] + +def ping(): + return True + +@staticmethod +def TCPJSONRPCServer(addr, port): + return SimpleJSONRPCServer((addr, port), logRequests=False) + +@staticmethod +def WSGIJSONRPCServer(addr, port): + class NoLogHandler(wsgiref.simple_server.WSGIRequestHandler): + def log_message(self, format, *args): + pass + + return wsgiref.simple_server.make_server(addr, port, WSGIJSONRPCApp(), + handler_class=NoLogHandler) + +# ------------------------------------------------------------------------------ +# Server utility class + +class UtilityServer(object): + """ + Utility start/stop server + """ + def __init__(self): + """ + Sets up members + """ + self._server = None + self._thread = None + + + def start(self, server_cls, addr, port): + """ + Starts the server + + :param server_cls: Callable that returns a subclass of XMLRPCServer + :param addr: A binding address + :param port: A listening port + :return: This object (for in-line calls) + """ + # Create the server + self._server = server = server_cls(addr, port) + if hasattr(server, 'get_app'): + server = server.get_app() + + # Register test methods + server.register_function(summation, 'sum') + server.register_function(summation, 'notify_sum') + server.register_function(notify_hello) + server.register_function(subtract) + server.register_function(update) + server.register_function(get_data) + server.register_function(add) + server.register_function(ping) + server.register_function(summation, 'namespace.sum') + + # Serve in a thread + self._thread = threading.Thread(target=self._server.serve_forever) + self._thread.daemon = True + self._thread.start() + + # Allow an in-line instantiation + return self + + + def stop(self): + """ + Stops the server and waits for its thread to finish + """ + self._server.shutdown() + self._server.server_close() + self._thread.join() + + self._server = None + self._thread = None + +# ------------------------------------------------------------------------------ class TestCompatibility(unittest.TestCase): - + client = None port = None server = None - + server_cls = TCPJSONRPCServer + def setUp(self): + """ + Pre-test set up + """ + # Set up the server self.port = PORTS.pop() - self.server = server_set_up(addr=('', self.port)) - self.client = Server('http://localhost:%d' % self.port) - + self.server = UtilityServer().start(self.server_cls, '', self.port) + + # Set up the client + self.history = jsonrpclib.history.History() + self.client = Server('http://localhost:{0}'.format(self.port), + history=self.history) + + + def tearDown(self): + """ + Post-test clean up + """ + # Close the client + self.client("close")() + + # Stop the server + self.server.stop() + + # v1 tests forthcoming - + # Version 2.0 Tests def test_positional(self): """ Positional arguments in a single call """ @@ -56,10 +223,10 @@ def test_positional(self): self.assertTrue(result == -19) result = self.client.subtract(42, 23) self.assertTrue(result == 19) - request = json.loads(history.request) - response = json.loads(history.response) + request = json.loads(self.history.request) + response = json.loads(self.history.response) verify_request = { - "jsonrpc": "2.0", "method": "subtract", + "jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": request['id'] } verify_response = { @@ -67,18 +234,18 @@ def test_positional(self): } self.assertTrue(request == verify_request) self.assertTrue(response == verify_response) - + def test_named(self): """ Named arguments in a single call """ result = self.client.subtract(subtrahend=23, minuend=42) self.assertTrue(result == 19) result = self.client.subtract(minuend=42, subtrahend=23) self.assertTrue(result == 19) - request = json.loads(history.request) - response = json.loads(history.response) + request = json.loads(self.history.request) + response = json.loads(self.history.response) verify_request = { - "jsonrpc": "2.0", "method": "subtract", - "params": {"subtrahend": 23, "minuend": 42}, + "jsonrpc": "2.0", "method": "subtract", + "params": {"subtrahend": 23, "minuend": 42}, "id": request['id'] } verify_response = { @@ -86,101 +253,101 @@ def test_named(self): } self.assertTrue(request == verify_request) self.assertTrue(response == verify_response) - + def test_notification(self): """ Testing a notification (response should be null) """ result = self.client._notify.update(1, 2, 3, 4, 5) self.assertTrue(result == None) - request = json.loads(history.request) - response = history.response + request = json.loads(self.history.request) + response = self.history.response verify_request = { - "jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5] + "jsonrpc": "2.0", "method": "update", "params": [1, 2, 3, 4, 5] } verify_response = '' self.assertTrue(request == verify_request) self.assertTrue(response == verify_response) - + def test_non_existent_method(self): self.assertRaises(ProtocolError, self.client.foobar) - request = json.loads(history.request) - response = json.loads(history.response) + request = json.loads(self.history.request) + response = json.loads(self.history.response) verify_request = { "jsonrpc": "2.0", "method": "foobar", "id": request['id'] } verify_response = { - "jsonrpc": "2.0", - "error": - {"code": -32601, "message": response['error']['message']}, + "jsonrpc": "2.0", + "error": + {"code":-32601, "message": response['error']['message']}, "id": request['id'] } self.assertTrue(request == verify_request) self.assertTrue(response == verify_response) - + def test_invalid_json(self): - invalid_json = '{"jsonrpc": "2.0", "method": "foobar, '+ \ + invalid_json = '{"jsonrpc": "2.0", "method": "foobar, ' + \ '"params": "bar", "baz]' - response = self.client._run_request(invalid_json) - response = json.loads(history.response) + self.client._run_request(invalid_json) + response = json.loads(self.history.response) verify_response = json.loads( - '{"jsonrpc": "2.0", "error": {"code": -32700,'+ + '{"jsonrpc": "2.0", "error": {"code": -32700,' + ' "message": "Parse error."}, "id": null}' ) verify_response['error']['message'] = response['error']['message'] self.assertTrue(response == verify_response) - + def test_invalid_request(self): invalid_request = '{"jsonrpc": "2.0", "method": 1, "params": "bar"}' - response = self.client._run_request(invalid_request) - response = json.loads(history.response) + self.client._run_request(invalid_request) + response = json.loads(self.history.response) verify_response = json.loads( - '{"jsonrpc": "2.0", "error": {"code": -32600, '+ + '{"jsonrpc": "2.0", "error": {"code": -32600, ' + '"message": "Invalid Request."}, "id": null}' ) verify_response['error']['message'] = response['error']['message'] self.assertTrue(response == verify_response) - + def test_batch_invalid_json(self): - invalid_request = '[ {"jsonrpc": "2.0", "method": "sum", '+ \ + invalid_request = '[ {"jsonrpc": "2.0", "method": "sum", ' + \ '"params": [1,2,4], "id": "1"},{"jsonrpc": "2.0", "method" ]' - response = self.client._run_request(invalid_request) - response = json.loads(history.response) + self.client._run_request(invalid_request) + response = json.loads(self.history.response) verify_response = json.loads( - '{"jsonrpc": "2.0", "error": {"code": -32700,'+ + '{"jsonrpc": "2.0", "error": {"code": -32700,' + '"message": "Parse error."}, "id": null}' ) verify_response['error']['message'] = response['error']['message'] self.assertTrue(response == verify_response) - + def test_empty_array(self): invalid_request = '[]' - response = self.client._run_request(invalid_request) - response = json.loads(history.response) + self.client._run_request(invalid_request) + response = json.loads(self.history.response) verify_response = json.loads( - '{"jsonrpc": "2.0", "error": {"code": -32600, '+ + '{"jsonrpc": "2.0", "error": {"code": -32600, ' + '"message": "Invalid Request."}, "id": null}' ) verify_response['error']['message'] = response['error']['message'] self.assertTrue(response == verify_response) - + def test_nonempty_array(self): invalid_request = '[1,2]' request_obj = json.loads(invalid_request) - response = self.client._run_request(invalid_request) - response = json.loads(history.response) + self.client._run_request(invalid_request) + response = json.loads(self.history.response) self.assertTrue(len(response) == len(request_obj)) for resp in response: verify_resp = json.loads( - '{"jsonrpc": "2.0", "error": {"code": -32600, '+ + '{"jsonrpc": "2.0", "error": {"code": -32600, ' + '"message": "Invalid Request."}, "id": null}' ) verify_resp['error']['message'] = resp['error']['message'] self.assertTrue(resp == verify_resp) - + def test_batch(self): multicall = MultiCall(self.client) - multicall.sum(1,2,4) + multicall.sum(1, 2, 4) multicall._notify.notify_hello(7) - multicall.subtract(42,23) + multicall.subtract(42, 23) multicall.foo.get(name='myself') multicall.get_data() job_requests = [j.request() for j in multicall._job_list] @@ -188,16 +355,16 @@ def test_batch(self): json_requests = '[%s]' % ','.join(job_requests) requests = json.loads(json_requests) responses = self.client._run_request(json_requests) - + verify_requests = json.loads("""[ {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"}, {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}, {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"}, {"foo": "boo"}, {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"}, - {"jsonrpc": "2.0", "method": "get_data", "id": "9"} + {"jsonrpc": "2.0", "method": "get_data", "id": "9"} ]""") - + # Thankfully, these are in order so testing is pretty simple. verify_responses = json.loads("""[ {"jsonrpc": "2.0", "result": 7, "id": "1"}, @@ -206,20 +373,20 @@ def test_batch(self): {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found."}, "id": "5"}, {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"} ]""") - + self.assertTrue(len(requests) == len(verify_requests)) self.assertTrue(len(responses) == len(verify_responses)) - + responses_by_id = {} response_i = 0 - + for i in range(len(requests)): verify_request = verify_requests[i] request = requests[i] response = None if request.get('method') != 'notify_hello': req_id = request.get('id') - if verify_request.has_key('id'): + if 'id' in verify_request: verify_request['id'] = req_id verify_response = verify_responses[response_i] verify_response['id'] = req_id @@ -227,49 +394,70 @@ def test_batch(self): response_i += 1 response = verify_response self.assertTrue(request == verify_request) - + for response in responses: verify_response = responses_by_id.get(response.get('id')) - if verify_response.has_key('error'): + if 'error' in verify_response: verify_response['error']['message'] = \ response['error']['message'] self.assertTrue(response == verify_response) - - def test_batch_notifications(self): + + def test_batch_notifications(self): multicall = MultiCall(self.client) multicall._notify.notify_sum(1, 2, 4) multicall._notify.notify_hello(7) result = multicall() self.assertTrue(len(result) == 0) valid_request = json.loads( - '[{"jsonrpc": "2.0", "method": "notify_sum", '+ - '"params": [1,2,4]},{"jsonrpc": "2.0", '+ + '[{"jsonrpc": "2.0", "method": "notify_sum", ' + + '"params": [1,2,4]},{"jsonrpc": "2.0", ' + '"method": "notify_hello", "params": [7]}]' ) - request = json.loads(history.request) + request = json.loads(self.history.request) self.assertTrue(len(request) == len(valid_request)) for i in range(len(request)): req = request[i] valid_req = valid_request[i] self.assertTrue(req == valid_req) - self.assertTrue(history.response == '') - + self.assertTrue(self.history.response == '') + +# ------------------------------------------------------------------------------ + +class WSGITestCompatibility(TestCompatibility): + server_cls = WSGIJSONRPCServer + +# ------------------------------------------------------------------------------ + class InternalTests(unittest.TestCase): - """ - These tests verify that the client and server portions of + """ + These tests verify that the client and server portions of jsonrpclib talk to each other properly. - """ - client = None + """ server = None port = None - + server_cls = TCPJSONRPCServer + def setUp(self): + # Set up the server self.port = PORTS.pop() - self.server = server_set_up(addr=('', self.port)) - + self.server = UtilityServer().start(self.server_cls, '', self.port) + + # Prepare the client + self.history = jsonrpclib.history.History() + + + def tearDown(self): + """ + Post-test clean up + """ + # Stop the server + self.server.stop() + + def get_client(self): - return Server('http://localhost:%d' % self.port) - + return Server('http://localhost:{0}'.format(self.port), + history=self.history) + def get_multicall_client(self): server = self.get_client() return MultiCall(server) @@ -278,33 +466,33 @@ def test_connect(self): client = self.get_client() result = client.ping() self.assertTrue(result) - + def test_single_args(self): client = self.get_client() result = client.add(5, 10) self.assertTrue(result == 15) - + def test_single_kwargs(self): client = self.get_client() result = client.add(x=5, y=10) self.assertTrue(result == 15) - + def test_single_kwargs_and_args(self): client = self.get_client() self.assertRaises(ProtocolError, client.add, (5,), {'y':10}) - + def test_single_notify(self): client = self.get_client() result = client._notify.add(5, 10) self.assertTrue(result == None) - + def test_single_namespace(self): client = self.get_client() - response = client.namespace.sum(1,2,4) - request = json.loads(history.request) - response = json.loads(history.response) + client.namespace.sum(1, 2, 4) + request = json.loads(self.history.request) + response = json.loads(self.history.response) verify_request = { - "jsonrpc": "2.0", "params": [1, 2, 4], + "jsonrpc": "2.0", "params": [1, 2, 4], "id": "5", "method": "namespace.sum" } verify_response = { @@ -314,25 +502,24 @@ def test_single_namespace(self): verify_response['id'] = request['id'] self.assertTrue(verify_request == request) self.assertTrue(verify_response == response) - + def test_multicall_success(self): multicall = self.get_multicall_client() multicall.ping() multicall.add(5, 10) - multicall.namespace.sum([5, 10, 15]) + multicall.namespace.sum(5, 10, 15) correct = [True, 15, 30] - i = 0 - for result in multicall(): + + for i, result in enumerate(multicall()): self.assertTrue(result == correct[i]) - i += 1 - - def test_multicall_success(self): + + def test_multicall_success_2(self): multicall = self.get_multicall_client() for i in range(3): multicall.add(5, i) result = multicall() self.assertTrue(result[2] == 7) - + def test_multicall_failure(self): multicall = self.get_multicall_client() multicall.ping() @@ -346,111 +533,248 @@ def test_multicall_failure(self): def func(): return result[i] self.assertRaises(raises[i], func) - - -if jsonrpc.USE_UNIX_SOCKETS: - # We won't do these tests unless Unix Sockets are supported - - class UnixSocketInternalTests(InternalTests): - """ - These tests run the same internal communication tests, - but over a Unix socket instead of a TCP socket. - """ - def setUp(self): - suffix = "%d.sock" % PORTS.pop() - - # Open to safer, alternative processes - # for getting a temp file name... - temp = tempfile.NamedTemporaryFile( - suffix=suffix - ) - self.port = temp.name - temp.close() - - self.server = server_set_up( - addr=self.port, - address_family=socket.AF_UNIX - ) - def get_client(self): - return Server('unix:/%s' % self.port) - - def tearDown(self): - """ Removes the tempory socket file """ - os.unlink(self.port) - -class UnixSocketErrorTests(unittest.TestCase): - """ - Simply tests that the proper exceptions fire if - Unix sockets are attempted to be used on a platform - that doesn't support them. +# ------------------------------------------------------------------------------ + +class WSGIInternalTests(InternalTests): + server_cls = WSGIJSONRPCServer + +# ------------------------------------------------------------------------------ + +class HeadersTests(unittest.TestCase): + """ + These tests verify functionality of additional headers. """ - + server = None + port = None + server_cls = TCPJSONRPCServer + + REQUEST_LINE = "^send: POST" + def setUp(self): - self.original_value = jsonrpc.USE_UNIX_SOCKETS - if (jsonrpc.USE_UNIX_SOCKETS): - jsonrpc.USE_UNIX_SOCKETS = False - - def test_client(self): - address = "unix://shouldnt/work.sock" - self.assertRaises( - jsonrpc.UnixSocketMissing, - Server, - address - ) - + """ + Sets up the test + """ + # Set up the server + self.port = PORTS.pop() + self.server = UtilityServer().start(self.server_cls, '', self.port) + + def tearDown(self): - jsonrpc.USE_UNIX_SOCKETS = self.original_value - + """ + Post-test clean up + """ + # Stop the server + self.server.stop() -""" Test Methods """ -def subtract(minuend, subtrahend): - """ Using the keywords from the JSON-RPC v2 doc """ - return minuend-subtrahend - -def add(x, y): - return x + y - -def update(*args): - return args - -def summation(*args): - return sum(args) - -def notify_hello(*args): - return args - -def get_data(): - return ['hello', 5] - -def ping(): - return True - -def server_set_up(addr, address_family=socket.AF_INET): - # Not sure this is a good idea to spin up a new server thread - # for each test... but it seems to work fine. - def log_request(self, *args, **kwargs): - """ Making the server output 'quiet' """ - pass - SimpleJSONRPCRequestHandler.log_request = log_request - server = SimpleJSONRPCServer(addr, address_family=address_family) - server.register_function(summation, 'sum') - server.register_function(summation, 'notify_sum') - server.register_function(notify_hello) - server.register_function(subtract) - server.register_function(update) - server.register_function(get_data) - server.register_function(add) - server.register_function(ping) - server.register_function(summation, 'namespace.sum') - server_proc = Thread(target=server.serve_forever) - server_proc.daemon = True - server_proc.start() - return server_proc + + @contextlib.contextmanager + def captured_headers(self): + """ + Captures the request headers. Yields the {header : value} dictionary, + where keys are in lower case. + """ + # Redirect the standard output, to catch jsonrpclib verbose messages + stdout = sys.stdout + sys.stdout = f = StringIO() + headers = {} + yield headers + sys.stdout = stdout + + # Extract the sent request content + request_lines = f.getvalue().splitlines() + request_lines = list(filter(lambda l: l.startswith("send:"), + request_lines)) + request_line = request_lines[0].split("send: ") [-1] + + # Convert it to a string + try: + # Use eval to convert the representation into a string + request_line = from_bytes(eval(request_line)) + except: + # Keep the received version + pass + + # Extract headers + raw_headers = request_line.splitlines()[1:-1] + raw_headers = map(lambda h: re.split(":\s?", h, 1), raw_headers) + for header, value in raw_headers: + headers[header.lower()] = value + + + def test_should_extract_headers(self): + # given + client = Server('http://localhost:{0}'.format(self.port), verbose=1) + + # when + with self.captured_headers() as headers: + response = client.ping() + self.assertTrue(response) + + # then + self.assertTrue(len(headers) > 0) + self.assertTrue('content-type' in headers) + self.assertEqual(headers['content-type'], 'application/json-rpc') + + def test_should_add_additional_headers(self): + # given + client = Server('http://localhost:{0}'.format(self.port), verbose=1, + headers={'X-My-Header' : 'Test'}) + + # when + with self.captured_headers() as headers: + response = client.ping() + self.assertTrue(response) + + # then + self.assertTrue('x-my-header' in headers) + self.assertEqual(headers['x-my-header'], 'Test') + + def test_should_add_additional_headers_to_notifications(self): + # given + client = Server('http://localhost:{0}'.format(self.port), verbose=1, + headers={'X-My-Header' : 'Test'}) + + # when + with self.captured_headers() as headers: + client._notify.ping() + + # then + self.assertTrue('x-my-header' in headers) + self.assertEqual(headers['x-my-header'], 'Test') + + def test_should_override_headers(self): + # given + client = Server('http://localhost:{0}'.format(self.port), verbose=1, + headers={ + 'User-Agent' : 'jsonrpclib test', + 'Host' : 'example.com' + }) + + # when + with self.captured_headers() as headers: + response = client.ping() + self.assertTrue(response) + + # then + self.assertEqual(headers['user-agent'], 'jsonrpclib test') + self.assertEqual(headers['host'], 'example.com') + + def test_should_not_override_content_length(self): + # given + client = Server('http://localhost:{0}'.format(self.port), verbose=1, + headers={'Content-Length' : 'invalid value'}) + + # when + with self.captured_headers() as headers: + response = client.ping() + self.assertTrue(response) + + # then + self.assertTrue('content-length' in headers) + self.assertNotEqual(headers['content-length'], 'invalid value') + + def test_should_convert_header_values_to_basestring(self): + # given + client = Server('http://localhost:{0}'.format(self.port), verbose=1, + headers={'X-Test' : 123}) + + # when + with self.captured_headers() as headers: + response = client.ping() + self.assertTrue(response) + + # then + self.assertTrue('x-test' in headers) + self.assertEqual(headers['x-test'], '123') + + def test_should_add_custom_headers_to_methods(self): + # given + client = Server('http://localhost:{0}'.format(self.port), verbose=1) + + # when + with self.captured_headers() as headers: + with client._additional_headers({'X-Method' : 'Method'}) as cl: + response = cl.ping() + + self.assertTrue(response) + + # then + self.assertTrue('x-method' in headers) + self.assertEqual(headers['x-method'], 'Method') + + def test_should_override_global_headers(self): + # given + client = Server('http://localhost:{0}'.format(self.port), verbose=1, + headers={'X-Test' : 'Global'}) + + # when + with self.captured_headers() as headers: + with client._additional_headers({'X-Test' : 'Method'}) as cl: + response = cl.ping() + self.assertTrue(response) + + # then + self.assertTrue('x-test' in headers) + self.assertEqual(headers['x-test'], 'Method') + + def test_should_restore_global_headers(self): + # given + client = Server('http://localhost:{0}'.format(self.port), verbose=1, + headers={'X-Test' : 'Global'}) + + # when + with self.captured_headers() as headers: + with client._additional_headers({'X-Test' : 'Method'}) as cl: + response = cl.ping() + self.assertTrue(response) + + self.assertTrue('x-test' in headers) + self.assertEqual(headers['x-test'], 'Method') + + with self.captured_headers() as headers: + response = cl.ping() + self.assertTrue(response) + + # then + self.assertTrue('x-test' in headers) + self.assertEqual(headers['x-test'], 'Global') + + + def test_should_allow_to_nest_additional_header_blocks(self): + # given + client = Server('http://localhost:%d' % self.port, verbose=1) + + # when + with client._additional_headers({'X-Level-1' : '1'}) as cl_level1: + with self.captured_headers() as headers1: + response = cl_level1.ping() + self.assertTrue(response) + + with cl_level1._additional_headers({'X-Level-2' : '2'}) as cl: + with self.captured_headers() as headers2: + response = cl.ping() + self.assertTrue(response) + + # then + self.assertTrue('x-level-1' in headers1) + self.assertEqual(headers1['x-level-1'], '1') + + self.assertTrue('x-level-1' in headers2) + self.assertEqual(headers1['x-level-1'], '1') + self.assertTrue('x-level-2' in headers2) + self.assertEqual(headers2['x-level-2'], '2') + +# ------------------------------------------------------------------------------ + +class WSGIHeadersTests(HeadersTests): + server_cls = WSGIJSONRPCServer + +# ------------------------------------------------------------------------------ if __name__ == '__main__': - print "===============================================================" - print " NOTE: There may be threading exceptions after tests finish. " - print "===============================================================" - time.sleep(2) + print("===============================================================") + print(" NOTE: There may be threading exceptions after tests finish. ") + print("===============================================================") + time.sleep(.5) unittest.main() diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..6fa376d --- /dev/null +++ b/tox.ini @@ -0,0 +1,7 @@ +[tox] +envlist = py26,py27,py32,py33,pypy + +[testenv] +commands = nosetests +deps = + nose