oubiwann / txjsonrpc

MIT License
48 stars 44 forks source link

Please provide a way for cross-origin requests to web.jsonrps #5

Closed qwiglydee closed 8 years ago

qwiglydee commented 11 years ago

According to http://www.w3.org/TR/cors/ this requires adding some Acess-Control headers in both POST and OPTIONS requests.

My workaround:

--- a/txjsonrpc/web/jsonrpc.py
+++ b/txjsonrpc/web/jsonrpc.py
@@ -85,10 +85,19 @@ class JSONRPC(resource.Resource, BaseSubhandler):
     def __init__(self):
         resource.Resource.__init__(self)
         BaseSubhandler.__init__(self)
+    
+    # additional headers to add
+    aux_headers = []
+    def add_aux_headers(self, request):
+        for h,v in self.aux_headers:
+            request.setHeader(h,v)

     def render(self, request):
+        self.add_aux_headers(request)
+        if request.method == 'OPTIONS':
+            return ""
         request.content.seek(0, 0)
-        # Unmarshal the JSON-RPC data.
+        # Unmarshal the JSON-RPC data.        
         content = request.content.read()
         if not content and request.method=='GET' and request.args.has_key('request'):
             content=request.args['request'][0]
qwiglydee commented 8 years ago

forgotten