2 # Copyright(C) 2012 thomasv@gitorious
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as
6 # published by the Free Software Foundation, either version 3 of the
7 # License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public
15 # License along with this program. If not, see
16 # <http://www.gnu.org/licenses/agpl.html>.
18 sessions are identified with cookies
19 - each session has a buffer of responses to requests
22 from the processor point of view:
23 - the user only defines process() ; the rest is session management. thus sessions should not belong to processor
30 import SimpleXMLRPCServer
40 from jsonrpclib import Fault
41 from jsonrpclib.jsonrpc import USE_UNIX_SOCKETS
42 from OpenSSL import SSL
51 from processor import Session
52 from utils import random_string, print_log
55 def get_version(request):
57 if 'jsonrpc' in request.keys():
59 if 'id' in request.keys():
64 def validate_request(request):
65 if not isinstance(request, types.DictType):
66 return Fault(-32600, 'Request must be {}, not %s.' % type(request))
67 rpcid = request.get('id', None)
68 version = get_version(request)
70 return Fault(-32600, 'Request %s invalid.' % request, rpcid=rpcid)
71 request.setdefault('params', [])
72 method = request.get('method', None)
73 params = request.get('params')
74 param_types = (types.ListType, types.DictType, types.TupleType)
75 if not method or type(method) not in types.StringTypes or type(params) not in param_types:
76 return Fault(-32600, 'Invalid request parameters or method.', rpcid=rpcid)
80 class StratumJSONRPCDispatcher(SimpleXMLRPCServer.SimpleXMLRPCDispatcher):
82 def __init__(self, encoding=None):
84 SimpleXMLRPCServer.SimpleXMLRPCDispatcher.__init__(self, allow_none=True, encoding=encoding)
86 def _marshaled_dispatch(self, session_id, data, dispatch_method=None):
89 request = jsonrpclib.loads(data)
91 fault = Fault(-32700, 'Request %s invalid. (%s)' % (data, e))
92 response = fault.response()
95 session = self.dispatcher.get_session_by_address(session_id)
97 return 'Error: session not found'
98 session.time = time.time()
101 if not isinstance(request, types.ListType):
104 for req_entry in request:
105 result = validate_request(req_entry)
106 if type(result) is Fault:
107 responses.append(result.response())
110 self.dispatcher.do_dispatch(session, req_entry)
112 if req_entry['method'] == 'server.stop':
113 return json.dumps({'result': 'ok'})
115 r = self.poll_session(session)
117 responses.append(json.dumps(item))
119 if len(responses) > 1:
120 response = '[%s]' % ','.join(responses)
121 elif len(responses) == 1:
122 response = responses[0]
128 def create_session(self):
129 session_id = random_string(10)
130 session = HttpSession(session_id)
131 self.dispatcher.add_session(session)
134 def poll_session(self, session):
135 q = session.pending_responses
140 #print "poll: %d responses"%len(responses)
144 class StratumJSONRPCRequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler):
146 def do_OPTIONS(self):
147 self.send_response(200)
148 self.send_header('Allow', 'GET, POST, OPTIONS')
149 self.send_header('Access-Control-Allow-Origin', '*')
150 self.send_header('Access-Control-Allow-Headers', '*')
151 self.send_header('Content-Length', '0')
155 if not self.is_rpc_path_valid():
160 c = self.headers.get('cookie')
162 if c[0:8] == 'SESSION=':
163 #print "found cookie", c[8:]
166 if session_id is None:
167 session_id = self.server.create_session()
168 #print "setting cookie", session_id
170 data = json.dumps([])
171 response = self.server._marshaled_dispatch(session_id, data)
172 self.send_response(200)
174 self.send_response(500)
175 err_lines = traceback.format_exc().splitlines()
176 trace_string = '%s | %s' % (err_lines[-3], err_lines[-1])
177 fault = jsonrpclib.Fault(-32603, 'Server error: %s' % trace_string)
178 response = fault.response()
179 print "500", trace_string
184 self.send_header("Set-Cookie", "SESSION=%s" % session_id)
186 self.send_header("Content-type", "application/json-rpc")
187 self.send_header("Access-Control-Allow-Origin", "*")
188 self.send_header("Content-length", str(len(response)))
190 self.wfile.write(response)
192 self.shutdown_connection()
195 if not self.is_rpc_path_valid():
199 max_chunk_size = 10*1024*1024
200 size_remaining = int(self.headers["content-length"])
202 while size_remaining:
203 chunk_size = min(size_remaining, max_chunk_size)
204 L.append(self.rfile.read(chunk_size))
205 size_remaining -= len(L[-1])
209 c = self.headers.get('cookie')
211 if c[0:8] == 'SESSION=':
212 #print "found cookie", c[8:]
215 if session_id is None:
216 session_id = self.server.create_session()
217 #print "setting cookie", session_id
219 response = self.server._marshaled_dispatch(session_id, data)
220 self.send_response(200)
222 self.send_response(500)
223 err_lines = traceback.format_exc().splitlines()
224 trace_string = '%s | %s' % (err_lines[-3], err_lines[-1])
225 fault = jsonrpclib.Fault(-32603, 'Server error: %s' % trace_string)
226 response = fault.response()
227 print "500", trace_string
232 self.send_header("Set-Cookie", "SESSION=%s" % session_id)
234 self.send_header("Content-type", "application/json-rpc")
235 self.send_header("Access-Control-Allow-Origin", "*")
236 self.send_header("Content-length", str(len(response)))
238 self.wfile.write(response)
240 self.shutdown_connection()
242 def shutdown_connection(self):
243 self.connection.shutdown(1)
246 class SSLRequestHandler(StratumJSONRPCRequestHandler):
248 self.connection = self.request
249 self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
250 self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
252 def shutdown_connection(self):
253 self.connection.shutdown()
256 class SSLTCPServer(SocketServer.TCPServer):
257 def __init__(self, server_address, certfile, keyfile, RequestHandlerClass, bind_and_activate=True):
258 SocketServer.BaseServer.__init__(self, server_address, RequestHandlerClass)
259 ctx = SSL.Context(SSL.SSLv23_METHOD)
260 ctx.use_privatekey_file(keyfile)
261 ctx.use_certificate_file(certfile)
262 self.socket = SSL.Connection(ctx, socket.socket(self.address_family, self.socket_type))
263 if bind_and_activate:
265 self.server_activate()
267 def shutdown_request(self, request):
272 class StratumHTTPServer(SocketServer.TCPServer, StratumJSONRPCDispatcher):
274 allow_reuse_address = True
276 def __init__(self, addr, requestHandler=StratumJSONRPCRequestHandler,
277 logRequests=False, encoding=None, bind_and_activate=True,
278 address_family=socket.AF_INET):
279 self.logRequests = logRequests
280 StratumJSONRPCDispatcher.__init__(self, encoding)
281 # TCPServer.__init__ has an extra parameter on 2.6+, so
282 # check Python version and decide on how to call it
283 vi = sys.version_info
284 self.address_family = address_family
285 if USE_UNIX_SOCKETS and address_family == socket.AF_UNIX:
286 # Unix sockets can't be bound if they already exist in the
287 # filesystem. The convention of e.g. X11 is to unlink
288 # before binding again.
289 if os.path.exists(addr):
293 logging.warning("Could not unlink socket %s", addr)
295 SocketServer.TCPServer.__init__(self, addr, requestHandler, bind_and_activate)
297 if fcntl is not None and hasattr(fcntl, 'FD_CLOEXEC'):
298 flags = fcntl.fcntl(self.fileno(), fcntl.F_GETFD)
299 flags |= fcntl.FD_CLOEXEC
300 fcntl.fcntl(self.fileno(), fcntl.F_SETFD, flags)
303 class StratumHTTPSSLServer(SSLTCPServer, StratumJSONRPCDispatcher):
305 allow_reuse_address = True
307 def __init__(self, addr, certfile, keyfile,
308 requestHandler=SSLRequestHandler,
309 logRequests=False, encoding=None, bind_and_activate=True,
310 address_family=socket.AF_INET):
312 self.logRequests = logRequests
313 StratumJSONRPCDispatcher.__init__(self, encoding)
314 # TCPServer.__init__ has an extra parameter on 2.6+, so
315 # check Python version and decide on how to call it
316 vi = sys.version_info
317 self.address_family = address_family
318 if USE_UNIX_SOCKETS and address_family == socket.AF_UNIX:
319 # Unix sockets can't be bound if they already exist in the
320 # filesystem. The convention of e.g. X11 is to unlink
321 # before binding again.
322 if os.path.exists(addr):
326 logging.warning("Could not unlink socket %s", addr)
328 SSLTCPServer.__init__(self, addr, certfile, keyfile, requestHandler, bind_and_activate)
330 if fcntl is not None and hasattr(fcntl, 'FD_CLOEXEC'):
331 flags = fcntl.fcntl(self.fileno(), fcntl.F_GETFD)
332 flags |= fcntl.FD_CLOEXEC
333 fcntl.fcntl(self.fileno(), fcntl.F_SETFD, flags)
336 class HttpSession(Session):
338 def __init__(self, session_id):
339 Session.__init__(self)
340 self.pending_responses = Queue.Queue()
341 self.address = session_id
344 def send_response(self, response):
345 raw_response = json.dumps(response)
346 self.pending_responses.put(response)
350 if time.time() - self.time > 60:
355 class HttpServer(threading.Thread):
356 def __init__(self, dispatcher, host, port, use_ssl, certfile, keyfile):
357 self.shared = dispatcher.shared
358 self.dispatcher = dispatcher.request_dispatcher
359 threading.Thread.__init__(self)
363 self.use_ssl = use_ssl
364 self.certfile = certfile
365 self.keyfile = keyfile
366 self.lock = threading.Lock()
369 # see http://code.google.com/p/jsonrpclib/
370 from SocketServer import ThreadingMixIn
372 class StratumThreadedServer(ThreadingMixIn, StratumHTTPSSLServer):
374 self.server = StratumThreadedServer((self.host, self.port), self.certfile, self.keyfile)
375 print_log("HTTPS server started.")
377 class StratumThreadedServer(ThreadingMixIn, StratumHTTPServer):
379 self.server = StratumThreadedServer((self.host, self.port))
380 print_log("HTTP server started.")
382 self.server.dispatcher = self.dispatcher
383 self.server.register_function(None, 'server.stop')
384 self.server.register_function(None, 'server.info')
386 self.server.serve_forever()