def register(self, url, key, value): exceptions.raise_parameter_error('url', str, url) exceptions.raise_parameter_error('key', str, key) if url in self._route_tree: self._route_tree[url][key] = value else: self._route_tree[url] = dict({key: value})
def register_controller(self, namespace, controller_name): exceptions.raise_parameter_error('namespace', str, namespace) if not issubclass(controller_name, base_controller.BaseController): raise exceptions.ParameterError( 'handlers must be derived with WebSocketHandlerProtocol') self._router.register(namespace, 'controller', controller_name) logger.info("Controller: {namespace} => {controller}".format( namespace=namespace, controller=controller_name))
def unregister(self, url, key=None): exceptions.raise_parameter_error('url', str, url) exceptions.raise_parameter_error('key', str, key) if url in self._route_tree: if key is None: self._route_tree[url].clear() else: self._route_tree[url].pop(key) else: raise exceptions.ParameterError('url not found in router')
def solution(self, url, key=None): exceptions.raise_parameter_error('url', str, url) exceptions.raise_parameter_error('key', str, key) if url in self._route_tree: if key in self._route_tree[url]: return self._route_tree[url][key] elif key in self._default_dict: return self._default_dict[key] else: if key in self._default_dict: return self._default_dict[key] raise exceptions.ParameterError('url or key not found in router')
def verify_request(client_name, request): if _enable_http_verifier is False: return True if not isinstance(request, http_message.HttpRequest): raise exceptions.raise_parameter_error( 'request', http_message.HttpRequest, request) with HttpHandshakeVerifier(client_name, request) as verifier: # A |Host| header field containing the server's authority. if not verifier.verify_host(): raise exceptions.HttpVerifierError( 'Client({}) `Host` invalid'.format(client_name)) # The |Origin| header field in the client's handshake indicates # the origin of the script establishing the connection. If the # server does not validate the origin, it will accept connections # from anywhere. If the server does not wish to accept this # connection, it MUST return an appropriate HTTP error code # (e.g., 403 Forbidden) and abort the WebSocket handshake # described in this section. if not verifier.verify_origin(): raise exceptions.HttpVerifierError( 'Client({}) `Origin` invalid'.format(client_name)) # Optionally, a |Sec-WebSocket-Extensions| header field, with a # list of values indicating which extensions the client would like # to speak. return verifier.verify_sec_websocket_extensions()
def register_handler(self, namespace): exceptions.raise_parameter_error('namespace', str, namespace) def _decorator_wrapper(class_object): if not issubclass(class_object, handler.WebSocketHandlerProtocol): raise exceptions.ParameterError( 'handlers must be derived with WebSocketHandlerProtocol') self._router.register(namespace, 'handler', class_object) class_object.__namespace__ = namespace logger.info("Handler: '{namespace}' => {handler}".format( namespace=namespace, handler=class_object)) @functools.wraps(class_object) def _handler_wrapper(*args, **kwargs): return class_object(*args, **kwargs) return _handler_wrapper return _decorator_wrapper
def unregister_default(self, key): exceptions.raise_parameter_error('key', str, key) self._default_dict.pop(key)
def register_default(self, key, value): exceptions.raise_parameter_error('key', str, key) self._default_dict[key] = value