def _source(source_str): """Source a handler definition string.""" global_dic = {} try: exec source_str in global_dic except Exception: raise DispatchError('Error in sourcing handler:' + util.get_stack_trace()) return (_extract_handler(global_dic, _DO_EXTRA_HANDSHAKE_HANDLER_NAME), _extract_handler(global_dic, _TRANSFER_DATA_HANDLER_NAME))
class WebSocketRequestHandler(CGIHTTPServer.CGIHTTPRequestHandler): """CGIHTTPRequestHandler specialized for Web Socket.""" def setup(self): """Override SocketServer.StreamRequestHandler.setup.""" self.connection = self.request self.rfile = memorizingfile.MemorizingFile( socket._fileobject(self.request, 'rb', self.rbufsize), max_memorized_lines=_MAX_MEMORIZED_LINES) self.wfile = socket._fileobject(self.request, 'wb', self.wbufsize) def __init__(self, *args, **keywords): self._request = _StandaloneRequest( self, WebSocketRequestHandler.options.use_tls) self._dispatcher = WebSocketRequestHandler.options.dispatcher self._print_warnings_if_any() self._handshaker = handshake.Handshaker( self._request, self._dispatcher, WebSocketRequestHandler.options.strict) CGIHTTPServer.CGIHTTPRequestHandler.__init__( self, *args, **keywords) def _print_warnings_if_any(self): warnings = self._dispatcher.source_warnings() if warnings: for warning in warnings: logging.warning('mod_pywebsocket: %s' % warning) def parse_request(self): """Override BaseHTTPServer.BaseHTTPRequestHandler.parse_request. Return True to continue processing for HTTP(S), False otherwise. """ result = CGIHTTPServer.CGIHTTPRequestHandler.parse_request(self) if result: try: self._handshaker.do_handshake() self._dispatcher.transfer_data(self._request) return False except handshake.HandshakeError, e: # Handshake for ws(s) failed. Assume http(s). logging.info('mod_pywebsocket: %s' % e) return True except dispatch.DispatchError, e: logging.warning('mod_pywebsocket: %s' % e) return False except Exception, e: logging.warning('mod_pywebsocket: %s' % e) logging.info('mod_pywebsocket: %s' % util.get_stack_trace()) return False
def transfer_data(self, request): """Let a handler transfer_data with a Web Socket client. Select a handler based on request.ws_resource and call its web_socket_transfer_data function. Args: request: mod_python request. """ unused_do_extra_handshake, transfer_data_ = self._handler(request) try: transfer_data_(request) except Exception: raise DispatchError('%s raised exception: %s' % (_TRANSFER_DATA_HANDLER_NAME, util.get_stack_trace()))
def do_extra_handshake(self, request): """Do extra checking in Web Socket handshake. Select a handler based on request.uri and call its web_socket_do_extra_handshake function. Args: request: mod_python request. """ do_extra_handshake_, unused_transfer_data = self._handler(request) try: do_extra_handshake_(request) except Exception: raise DispatchError('%s raised exception: %s' % (_DO_EXTRA_HANDSHAKE_HANDLER_NAME, util.get_stack_trace()))
def handle_error(self, rquest, client_address): """Override SocketServer.handle_error.""" logging.error( ('Exception in processing request from: %r' % (client_address,)) + '\n' + util.get_stack_trace())
def handle_error(self, rquest, client_address): """Override SocketServer.handle_error.""" logging.error(('Exception in processing request from: %r' % (client_address, )) + '\n' + util.get_stack_trace())