index.wsgi 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/python
  2. #
  3. # CMS WSGI wrapper
  4. #
  5. # Copyright (C) 2011-2012 Michael Buesch <m@bues.ch>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. import sys
  20. import atexit
  21. try:
  22. from urlparse import parse_qs
  23. except ImportError:
  24. from cgi import parse_qs
  25. try:
  26. sys.path.append("/var/cms") # workaround for old WSGI
  27. from cms import *
  28. except ImportError:
  29. raise Exception("Failed to import cms.py. Wrong WSGIPythonPath?")
  30. cms = None
  31. def __initCMS(environ):
  32. global cms
  33. if cms:
  34. return # Already initialized
  35. try:
  36. domain = environ["cms.domain"]
  37. cmsBase = environ["cms.cmsBase"]
  38. wwwBase = environ["cms.wwwBase"]
  39. except (KeyError), e:
  40. raise Exception("WSGI environment %s not set" % str(e))
  41. debug = False
  42. try:
  43. debug = stringBool(environ["cms.debug"])
  44. except (KeyError), e:
  45. pass
  46. # Initialize the CMS module
  47. cms = CMS(dbPath = cmsBase + "/db",
  48. wwwPath = wwwBase,
  49. domain = domain,
  50. debug = debug)
  51. atexit.register(cms.shutdown)
  52. def application(environ, start_response):
  53. __initCMS(environ)
  54. if cms.debug:
  55. startStamp = datetime.now()
  56. status = "200 OK"
  57. additional_headers = []
  58. try:
  59. method = environ["REQUEST_METHOD"].upper()
  60. path = environ["PATH_INFO"]
  61. query = parse_qs(environ["QUERY_STRING"])
  62. if method == "GET":
  63. response_body, response_mime = cms.get(path, query)
  64. elif method == "POST":
  65. response_body, response_mime = cms.post(path, query)
  66. else:
  67. response_body, response_mime =\
  68. "INVALID REQUEST_METHOD", "text/plain"
  69. except (CMSException), e:
  70. status = e.httpStatus
  71. response_body, response_mime, additional_headers = cms.getErrorPage(e)
  72. if cms.debug and response_mime.lower() == "text/html":
  73. delta = datetime.now() - startStamp
  74. sec = float(delta.seconds) + float(delta.microseconds) / 1000000
  75. response_body += "\n<!-- generated in %.3f seconds -->" % sec
  76. response_headers = [ ('Content-Type', response_mime),
  77. ('Content-Length', str(len(response_body))) ]
  78. response_headers.extend(additional_headers)
  79. start_response(status, response_headers)
  80. return (response_body,)