No Description
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.2KB

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