home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / python / cgihandler.py < prev    next >
Encoding:
Python Source  |  2004-02-16  |  3.3 KB  |  110 lines

  1.  #
  2.  # Copyright 2004 Apache Software Foundation 
  3.  # 
  4.  # Licensed under the Apache License, Version 2.0 (the "License"); you
  5.  # may not use this file except in compliance with the License.  You
  6.  # may obtain a copy of the License at
  7.  #
  8.  #      http://www.apache.org/licenses/LICENSE-2.0
  9.  #
  10.  # Unless required by applicable law or agreed to in writing, software
  11.  # distributed under the License is distributed on an "AS IS" BASIS,
  12.  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  13.  # implied.  See the License for the specific language governing
  14.  # permissions and limitations under the License.
  15.  #
  16.  # Originally developed by Gregory Trubetskoy.
  17.  #
  18.  # $Id: cgihandler.py,v 1.13 2004/02/16 19:47:27 grisha Exp $
  19.  
  20. import apache
  21. import imp
  22. import os
  23. import sys
  24.  
  25. # if threads are not available
  26. # create a functionless lock object
  27. try:
  28.     import threading
  29.     _lock = threading.Lock()
  30. except (ImportError, AttributeError):
  31.     class DummyLock:
  32.         def acquire(self):
  33.             pass
  34.         def release(self):
  35.             pass
  36.     _lock = DummyLock()
  37.  
  38. # the next statement  deserves some explaining.
  39. # it seems that the standard os.environ object looses
  40. # memory if the environment is manipulated frequently. Since for
  41. # CGI you have to rebuild it for every request, your httpd will
  42. # grow rather fast. I am not exactly sure why it happens and if there
  43. # is a more sensible remedy, but this seems to work OK.
  44. os.environ = {}
  45.  
  46. original = sys.modules.keys()
  47.  
  48. # find out the standard library location
  49. stdlib, x = os.path.split(os.__file__)
  50.  
  51. def handler(req):
  52.  
  53.     ### if you don't need indirect modules reloaded, comment out
  54.     ### code unitl ### end
  55.  
  56.     # if there are any new modules since the import of this module,
  57.     # delete them.
  58.     for m in sys.modules.keys():
  59.         if m not in original:
  60.             # unless they are part of standard library
  61.             mod = sys.modules[m]
  62.             if hasattr(mod, "__file__"):
  63.                 path, x = os.path.split(mod.__file__)
  64.                 if path != stdlib:
  65.                     del sys.modules[m]
  66.     ### end
  67.  
  68.     # get the filename of the script
  69.     if req.subprocess_env.has_key("script_filename"):
  70.         dir, file = os.path.split(req.subprocess_env["script_filename"])
  71.     else:
  72.         dir, file = os.path.split(req.filename)
  73.     module_name, ext = os.path.splitext(file)
  74.  
  75.     _lock.acquire()
  76.     try:
  77.  
  78.         try:
  79.  
  80.             # The CGI spec requires us to set current working
  81.             # directory to that of the script. This is not
  82.             # thread safe, this is why we must obtain the lock.
  83.             cwd = os.getcwd()
  84.             os.chdir(dir)
  85.  
  86.             # simulate cgi environment
  87.             env, si, so = apache.setup_cgi(req)
  88.  
  89.             try:
  90.                 # we do not search the pythonpath (security reasons)
  91.                 fd, path, desc = imp.find_module(module_name, [dir])
  92.             except ImportError:
  93.                 raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
  94.  
  95.             # this executes the module
  96.             imp.load_module(module_name, fd, path, desc)
  97.  
  98.             return apache.OK
  99.  
  100.         finally:
  101.             # unsimulate the cgi environment
  102.             apache.restore_nocgi(env, si, so)
  103.             try:
  104.                 fd.close()
  105.             except: pass
  106.             os.chdir(cwd)
  107.     finally:
  108.         _lock.release()
  109.  
  110.