def auth(self, callback): Log.d(TAG, "auth") if "Authorization" in self.request.headers: login, passw = get_loginpass(self.request.headers["Authorization"]) if login == self.login and passw == self.passw: callback() return
def connect(self): if self._client is not None: while not (yield self._client.connect()): Log.e(TAG, "client connect error. retry after 1 sec") yield tornado.gen.sleep(1) raise tornado.gen.Return() raise AttributeError("tornadis client is None!")
def on_close(self, *args, **kwargs): Log.d(TAG, "on_open %r" % id(self)) if not self.is_closed and self.is_static: self.is_closed = True if self._static_connection is not None: self._static_connection.abort() self._static_connection = None super(StaticSocketHandler, self)._break_cycles()
def _execute_callbacks(queue, *args, **kwargs): # type: (list, tuple, dict) -> None for i in range(len(queue)): callback = queue.pop() try: callback(*args, **kwargs) except Exception as e: Log.e(TAG, b"callback execute error", e) Log.d(TAG, b"callbacks execution done")
def _handle_request(self, data): try: start_line, headers = self.parse_headers(data) request = httputil.HTTPServerRequest( headers=headers, start_line=httputil.parse_request_start_line(start_line)) deli = self._handler.application.find_handler(request) getattr(self._handler, request.method.lower(), self._handler.r404)(*deli.path_args, **deli.path_kwargs) except httputil.HTTPInputError as e: Log.e(TAG, b"_handle_request error", e)
def authenticate(self, headers): # type: (dict) -> Account or None for header_name in ["Authorization", "WWW-Authorization"]: auth_data = headers.get(header_name, None) if auth_data is not None: try: auth_type, token = auth_data.split(" ") auth_method = { "otp": self.auth_otp, "basic": self.auth_basic }.get(auth_type.lower(), None) if auth_method is not None: raise tornado.gen.Return((yield auth_method( Base64.decode(token).decode("utf-8")))) except Exception as e: Log.e(TAG, "authenticate error", e)
def main(): psql = models.Model.set_parameters(**s.POSTGRES) redis = RedisClient(**s.REDIS) initializer = dict(relay=object(), push=object(), session=object(), auth=AuthManager(redis)) application = tornado.web.Application([ (r'^/session/(?P<device_id>\d+)/(?P<number>[0-9\+]{3,15})$', MainHandler, initializer) ]) server = tornado.httpserver.HTTPServer(application) server.listen(s.SERVER_PORT, s.SERVER_HOST) server.start() Log.d(TAG, "OpenRedPhoneServer is run")
def ring(self, device_id, number): Log.d(TAG, "ring") return self.handle_ring(device_id, number)
def handle_create(self, device_id, number): Log.d(TAG, "handle_create: %r, %r" % (device_id, number)) self.set_status(200) self.finish()
def busy(self, device_id, number): Log.d(TAG, "busy") return self.handle_busy(device_id, number)
def on_open(self, *args, **kwargs): Log.d(TAG, "on_open %r" % id(self))
def delete(self, device_id, number): Log.d(TAG, "delete") return self.handle_delete(device_id, number)
def __del__(self): # Will not be invoke if there is a memory leak somewhere # TODO(s1z): Remove it when everything is done Log.d(TAG, "__del__ %s" % StaticSocketHandler.__name__)
def __del__(self): # Will not be invoke if there is a memory leak somewhere # TODO(s1z): Remove it when everything is done Log.d(TAG, b"__del__ %r" % MainHandler.__name__)
def _handle_response(self, data): try: response = httputil.parse_response_start_line(data) Log.d(TAG, b"_handle_response %r" % response) except httputil.HTTPInputError as e: Log.e(TAG, b"_handle_response error", e)
def get(self, device_id, number): Log.d(TAG, "get") return self.handle_create(device_id, number)
def on_close_callback(self): Log.d(TAG, "on_connected_callback") if self.is_running is True: self._client.disconnect() yield self.connect()
def on_connected_callback(self): Log.d(TAG, "on_connected_callback")