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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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:
  54. WebUiClient.restore_session(session_token)
  55. #next line is for testing purpose
  56. print("ACCESS DATAS : ", WebUiClient['last_request'])
  57. session_token = None
  58. #next line is for testing purpose
  59. WebUiClient['last_request'] = time.time()
  60. try:
  61. controller = get_controller(request)
  62. logger.debug(controller)
  63. response = controller(request)
  64. except HttpException as e:
  65. try:
  66. response = e.render(request)
  67. except Exception as eb:
  68. raise eb
  69. res = Response()
  70. res.status_code = 500
  71. return res
  72. session_token = WebUiClient.session_token()
  73. if session_token is not None:
  74. save_cookie(response, session_token)
  75. session_token = None
  76. except (ClientError, ClientAuthenticationError):
  77. response = HttpException(200).render(request)
  78. empty_cookie(response)
  79. except ClientAuthenticationFailure:
  80. response = HttpException(200).render(request)
  81. empty_cookie(response)
  82. except Exception as e:
  83. raise e
  84. res = response(env, start_response)
  85. WebUiClient.clean()
  86. return res