2
# Copyright 2004 Apache Software Foundation
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
8
# http://www.apache.org/licenses/LICENSE-2.0
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.
16
# Originally developed by Gregory Trubetskoy.
18
# $Id: cgihandler.py,v 1.13 2004/02/16 19:47:27 grisha Exp $
25
# if threads are not available
26
# create a functionless lock object
29
_lock = threading.Lock()
30
except (ImportError, AttributeError):
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.
46
original = sys.modules.keys()
48
# find out the standard library location
49
stdlib, x = os.path.split(os.__file__)
53
### if you don't need indirect modules reloaded, comment out
54
### code unitl ### end
56
# if there are any new modules since the import of this module,
58
for m in sys.modules.keys():
60
# unless they are part of standard library
62
if hasattr(mod, "__file__"):
63
path, x = os.path.split(mod.__file__)
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"])
72
dir, file = os.path.split(req.filename)
73
module_name, ext = os.path.splitext(file)
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.
86
# simulate cgi environment
87
env, si, so = apache.setup_cgi(req)
90
# we do not search the pythonpath (security reasons)
91
fd, path, desc = imp.find_module(module_name, [dir])
93
raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
95
# this executes the module
96
imp.load_module(module_name, fd, path, desc)
101
# unsimulate the cgi environment
102
apache.restore_nocgi(env, si, so)