Пример #1
0
 def render_get(self, request):
     """
     Render HTTP GET request
     :param request: request
     """
     log = self._log.getChild("render_get")
     url = request.url
     try:
         self.getUrlList[url](request)
         return
     except KeyError:
         self.get_from_static(request)
     except (error.BaseException, BaseSessionException) as bErr:
         response = Response()
         response.responseCode = 400
         response.content = validator.create_json_response(
             errorCode=bErr.errno, reason=bErr.errorMsg)
         response.headers['Content-Type'] = 'application/json'
         self.write(response)
     except Exception as err:
         log.exception("Exception in rended: {}".format(err))
         response = Response()
         response.responseCode = 500
         response.content = validator.create_json_response(
             errorCode=500, reason="Internal error.")
         response.headers['Content-Type'] = 'application/json'
         self.write(response)
Пример #2
0
 def render_post(self, request):
     """
     Render HTTP POST request
     :param request: http request
     """
     log = self._log.getChild("render_post")
     try:
         self.postUrlList[request.url](request)
     except KeyError:
         response = Response()
         response.responseCode = 404
         self.write(response)
     except (error.BaseException, BaseSessionException) as bErr:
         response = Response()
         response.responseCode = 400
         response.content = validator.create_json_response(errorCode=bErr.errno, reason=bErr.errorMsg)
         response.headers['Content-Type'] = 'application/json'
         self.write(response)
     except Exception as err:
         log.exception("Exception in rended: {}".format(err))
         response = Response()
         response.responseCode = 500
         response.content = validator.create_json_response(errorCode=500, reason="Internal error.")
         response.headers['Content-Type'] = 'application/json'
         self.write(response)
Пример #3
0
 def get_redirect_response(self, path='/'):
     """
     Send redirect
     :param path: redirect path
     :return:
     """
     response = Response()
     response.responseCode = 302
     response.headers['Location'] = path
     return response
Пример #4
0
 def get_redirect_response(self, path='/'):
     """
     Send redirect
     :param path: redirect path
     :return:
     """
     response = Response()
     response.responseCode = 302
     response.headers['Location'] = path
     return response
Пример #5
0
 def render(self, request):
     log = self._log.getChild("render")
     method = request.method.lower()
     if method == self.GET_METHOD_LOWER:
         gevent.spawn(self.render_get, request).join()
     elif method == self.POST_METHOD_LOWER:
         gevent.spawn(self.render_post, request).join()
     else:
         response = Response()
         response.responseCode = 501
         self.write(response)
Пример #6
0
 def render(self, request):
     log = self._log.getChild("render")
     method = request.method.lower()
     if method == self.GET_METHOD_LOWER:
         gevent.spawn(self.render_get, request).join()
     elif method == self.POST_METHOD_LOWER:
         gevent.spawn(self.render_post, request).join()
     else:
         response = Response()
         response.responseCode = 501
         self.write(response)
Пример #7
0
 def render(self, request):
     """
     Render response
     :param request: http request
     """
     response = Response()
     self.write(response)
Пример #8
0
 def auth(self, request):
     """
     Authorize user
     :param request:
     :return:
     """
     log = self._log.getChild("auth")
     login = request.get_param('login')
     password = request.get_param('password')
     user = self.dbHandler.get_user(login, password)
     cookie = self.dbHandler.set_session(user.id)
     response = Response()
     response.set_cookie(self.cookieName, cookie)
     response.content = validator.create_json_response(data={})
     response.headers['Content-Type'] = '; '.join(['application/json', 'charset=utf-8'])
     log.info("Set cookie: {}, login: {}, userid: {}".format(cookie, login, user.id))
     self.write(response)
Пример #9
0
 def get_from_static(self, request):
     """
     Get files from static
     :param url: HTTP url
     :return:
     """
     log = self._log.getChild("get_from_static")
     url = request.url
     if url == '/':
         url = '/index.html'
     cookie = request.get_cookie(self.cookieName)
     response = Response()
     try:
         path = ''.join([self.staticPath, url])
         with open(path, 'r') as file:
             content = file.read()
             response.content = content
             contentType = self.get_file_content_type(path)
             response.set_cookie(self.cookieName, cookie)
             response.headers['Content-Type'] = '; '.join(
                 [contentType, 'charset=utf-8'])
     except Exception as err:
         log.warning("Client: {}, url: {} error: {}".format(
             self.addr, url, err))
         response.responseCode = 404
     finally:
         self.write(response)
Пример #10
0
 def registration(self, request):
     """
     Registrate new user
     :param request: http request
     :return:
     """
     log = self._log.getChild("registration")
     login = request.get_param('login')
     password = request.get_param('password')
     validator.is_valid_login(login)
     validator.is_valid_password(password)
     userId = self.dbHandler.register_user(login, password)
     cookie = self.dbHandler.set_session(userId)
     response = Response()
     response.set_cookie(self.cookieName, cookie)
     response.content = validator.create_json_response(data={})
     response.headers['Content-Type'] = '; '.join(['application/json', 'charset=utf-8'])
     log.info("Set cookie: {}, login: {}, userid: {}".format(cookie, login, userId))
     self.write(response)
Пример #11
0
 def get_from_static(self, request):
     """
     Get files from static
     :param url: HTTP url
     :return:
     """
     log = self._log.getChild("get_from_static")
     url = request.url
     if url == '/':
         url = '/index.html'
     cookie = request.get_cookie(self.cookieName)
     response = Response()
     try:
         path = ''.join([self.staticPath, url])
         with open(path, 'r') as file:
             content = file.read()
             response.content = content
             contentType = self.get_file_content_type(path)
             response.set_cookie(self.cookieName, cookie)
             response.headers['Content-Type'] = '; '.join([contentType, 'charset=utf-8'])
     except Exception as err:
         log.warning("Client: {}, url: {} error: {}".format(self.addr, url, err))
         response.responseCode = 404
     finally:
         self.write(response)
Пример #12
0
 def auth(self, request):
     """
     Authorize user
     :param request:
     :return:
     """
     log = self._log.getChild("auth")
     login = request.get_param('login')
     password = request.get_param('password')
     user = self.dbHandler.get_user(login, password)
     cookie = self.dbHandler.set_session(user.id)
     response = Response()
     response.set_cookie(self.cookieName, cookie)
     response.content = validator.create_json_response(data={})
     response.headers['Content-Type'] = '; '.join(
         ['application/json', 'charset=utf-8'])
     log.info("Set cookie: {}, login: {}, userid: {}".format(
         cookie, login, user.id))
     self.write(response)
Пример #13
0
 def registration(self, request):
     """
     Registrate new user
     :param request: http request
     :return:
     """
     log = self._log.getChild("registration")
     login = request.get_param('login')
     password = request.get_param('password')
     validator.is_valid_login(login)
     validator.is_valid_password(password)
     userId = self.dbHandler.register_user(login, password)
     cookie = self.dbHandler.set_session(userId)
     response = Response()
     response.set_cookie(self.cookieName, cookie)
     response.content = validator.create_json_response(data={})
     response.headers['Content-Type'] = '; '.join(
         ['application/json', 'charset=utf-8'])
     log.info("Set cookie: {}, login: {}, userid: {}".format(
         cookie, login, userId))
     self.write(response)