From 5072f503c467fbaf1bb4610025b430bd514eb6e4 Mon Sep 17 00:00:00 2001 From: Jack Date: Sun, 20 Mar 2016 22:53:01 -0400 Subject: [PATCH] fix cross scripting problem add access-control-allow-origin: http://localhost:5279 --- lbrynet/lbrynet_daemon/LBRYDaemon.py | 32 +++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index e3b9a437b..1d647a917 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -11,6 +11,7 @@ import requests from twisted.web import server, resource, static from twisted.internet import defer, threads, error, reactor +from txjsonrpc import jsonrpclib from txjsonrpc.web import jsonrpc from jsonrpc.proxy import JSONRPCProxy @@ -64,6 +65,35 @@ class LBRYDaemon(jsonrpc.JSONRPC): """ isLeaf = True + def render(self, request): + request.content.seek(0, 0) + # Unmarshal the JSON-RPC data. + content = request.content.read() + parsed = jsonrpclib.loads(content) + functionPath = parsed.get("method") + args = parsed.get('params') + id = parsed.get('id') + version = parsed.get('jsonrpc') + if version: + version = int(float(version)) + elif id and not version: + version = jsonrpclib.VERSION_1 + else: + version = jsonrpclib.VERSION_PRE1 + # XXX this all needs to be re-worked to support logic for multiple + # versions... + try: + function = self._getFunction(functionPath) + except jsonrpclib.Fault, f: + self._cbRender(f, request, id, version) + else: + request.setHeader('Access-Control-Allow-Origin', ('http://localhost' + ':' + str(API_PORT))) + request.setHeader("content-type", "text/json") + d = defer.maybeDeferred(function, *args) + d.addErrback(self._ebRender, id) + d.addCallback(self._cbRender, request, id, version) + return server.NOT_DONE_YET + def setup(self, wallet_type, check_for_updates): def _set_vars(wallet_type, check_for_updates): reactor.addSystemEventTrigger('before', 'shutdown', self._shutdown) @@ -1206,4 +1236,4 @@ class LBRYDaemonWeb(resource.Resource): d.addCallbacks(lambda results: self._delayed_render(request, results), lambda err: self._delayed_render(request, json.dumps({'message': err.getTraceback(), 'code': BAD_REQUEST}))) - return server.NOT_DONE_YET + return server.NOT_DONE_YET \ No newline at end of file