Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

run.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # -*- coding: utf-8 -*-
  2. import loader # Lodel2 loader
  3. import os
  4. import hashlib
  5. import time
  6. from werkzeug.wrappers import Response
  7. from lodel.settings import Settings
  8. from .interface.router import get_controller
  9. from .interface.lodelrequest import LodelRequest
  10. from .exceptions import *
  11. from .client import WebUiClient
  12. from lodel.auth.exceptions import *
  13. try:
  14. SESSION_FILES_BASE_DIR = Settings.webui.sessions.directory
  15. SESSION_FILES_TEMPLATE = Settings.webui.sessions.file_template
  16. SESSION_EXPIRATION_LIMIT = Settings.webui.sessions.expiration
  17. COOKIE_SECRET_KEY = bytes(Settings.webui.cookie_secret_key, 'utf-8')
  18. COOKIE_SESSION_ID = Settings.webui.cookie_session_id
  19. except Exception as e:
  20. print("Fails to start : ", e, file=sys.stderr)
  21. exit(1)
  22. from werkzeug.contrib.securecookie import SecureCookie
  23. def load_cookie(request):
  24. datas = request.cookies.get(COOKIE_SESSION_ID)
  25. if not datas:
  26. return None
  27. cookie_content = SecureCookie.unserialize(datas, COOKIE_SECRET_KEY)
  28. if 'token' not in cookie_content:
  29. return None
  30. token = cookie_content['token']
  31. if token is None or len(token) == 0:
  32. return None
  33. return token
  34. def save_cookie(response, token):
  35. response.set_cookie(COOKIE_SESSION_ID, SecureCookie({'token': token}, COOKIE_SECRET_KEY).serialize())
  36. def empty_cookie(response):
  37. response.set_cookie(COOKIE_SESSION_ID, '')
  38. #Starting instance
  39. loader.start()
  40. #providing access to dyncode
  41. import lodel
  42. import leapi_dyncode as dyncode
  43. lodel.dyncode = dyncode
  44. # WSGI Application
  45. def application(env, start_response):
  46. request = LodelRequest(env)
  47. session_token = None
  48. try:
  49. #We have to create the client before restoring cookie in order to be able
  50. #to log messages with client infos
  51. client = WebUiClient(env['REMOTE_ADDR'], env['HTTP_USER_AGENT'], None)
  52. session_token = load_cookie(request)
  53. if session_token is not None and len(session_token) > 0:
  54. WebUiClient.restore_session(session_token)
  55. session_token = None
  56. try:
  57. controller = get_controller(request)
  58. logger.debug(controller)
  59. response = controller(request)
  60. except HttpException as e:
  61. try:
  62. response = e.render(request)
  63. except Exception as eb:
  64. raise eb
  65. res = Response()
  66. res.status_code = 500
  67. return res
  68. session_token = WebUiClient.session_token()
  69. if session_token is not None:
  70. save_cookie(response, session_token)
  71. session_token = None
  72. except (ClientError, ClientAuthenticationError):
  73. response = HttpException(200).render(request)
  74. empty_cookie(response)
  75. except ClientAuthenticationFailure:
  76. response = HttpException(200).render(request)
  77. empty_cookie(response)
  78. except Exception as e:
  79. raise e
  80. res = response(env, start_response)
  81. WebUiClient.clean()
  82. return res